author | laurent |
Tue, 15 Dec 2009 15:39:39 +0100 | |
changeset 519 | 3a5faff52e5c |
parent 517 | 6da1617a7645 |
child 521 | 02cb9e5fb6f6 |
permissions | -rwxr-xr-x |
442 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
503 | 3 |
import shutil |
442 | 4 |
|
5 |
__version__ = "$Revision$" |
|
6 |
||
7 |
import os, sys, getopt, wx, tempfile |
|
8 |
from types import TupleType, StringType, UnicodeType |
|
9 |
||
10 |
CWD = os.path.split(os.path.realpath(__file__))[0] |
|
11 |
||
12 |
def Bpath(*args): |
|
13 |
return os.path.join(CWD,*args) |
|
14 |
||
15 |
if __name__ == '__main__': |
|
16 |
def usage(): |
|
17 |
print "\nUsage of LPCBeremiz.py :" |
|
18 |
print "\n %s [Projectpath] [Buildpath]\n"%sys.argv[0] |
|
19 |
||
20 |
try: |
|
21 |
opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) |
|
22 |
except getopt.GetoptError: |
|
23 |
# print help information and exit: |
|
24 |
usage() |
|
25 |
sys.exit(2) |
|
26 |
||
27 |
for o, a in opts: |
|
28 |
if o in ("-h", "--help"): |
|
29 |
usage() |
|
30 |
sys.exit() |
|
31 |
||
32 |
if len(args) > 2: |
|
33 |
usage() |
|
34 |
sys.exit() |
|
35 |
elif len(args) == 1: |
|
36 |
projectOpen = args[0] |
|
37 |
buildpath = None |
|
38 |
elif len(args) == 2: |
|
39 |
projectOpen = args[0] |
|
40 |
buildpath = args[1] |
|
41 |
else: |
|
42 |
projectOpen = None |
|
43 |
buildpath = None |
|
44 |
||
45 |
class PseudoLocale: |
|
46 |
LocaleDirs = [] |
|
47 |
Domains = [] |
|
48 |
||
49 |
def AddCatalogLookupPathPrefix(self, localedir): |
|
50 |
self.LocaleDirs.append(localedir) |
|
51 |
||
52 |
def AddCatalog(self, domain): |
|
53 |
self.Domains.append(domain) |
|
54 |
||
55 |
# Import module for internationalization |
|
56 |
import gettext |
|
57 |
import __builtin__ |
|
58 |
||
59 |
# Define locale for wx |
|
60 |
__builtin__.__dict__['loc'] = PseudoLocale() |
|
61 |
||
62 |
if __name__ == '__main__': |
|
63 |
__builtin__.__dict__['_'] = wx.GetTranslation#unicode_translation |
|
64 |
||
65 |
from Beremiz import * |
|
492 | 66 |
from plugger import PluginsRoot, PlugTemplate, opjimg, connectors |
442 | 67 |
from plcopen.structures import LOCATIONDATATYPES |
68 |
from PLCControler import LOCATION_PLUGIN, LOCATION_MODULE, LOCATION_GROUP,\ |
|
69 |
LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY |
|
500
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
70 |
from PLCOpenEditor import IDEFrame, ProjectDialog |
442 | 71 |
|
72 |
#------------------------------------------------------------------------------- |
|
512
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
510
diff
changeset
|
73 |
# CANFESTIVAL PLUGIN HACK |
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
510
diff
changeset
|
74 |
#------------------------------------------------------------------------------- |
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
510
diff
changeset
|
75 |
from plugins.canfestival import canfestival |
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
510
diff
changeset
|
76 |
class LPC_canfestival_config: |
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
510
diff
changeset
|
77 |
def getCFLAGS(self, *args): |
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
510
diff
changeset
|
78 |
return "" |
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
510
diff
changeset
|
79 |
|
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
510
diff
changeset
|
80 |
def getLDFLAGS(self, *args): |
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
510
diff
changeset
|
81 |
return "" |
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
510
diff
changeset
|
82 |
|
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
510
diff
changeset
|
83 |
canfestival.local_canfestival_config = LPC_canfestival_config() |
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
510
diff
changeset
|
84 |
#------------------------------------------------------------------------------- |
442 | 85 |
# LPCModule Class |
86 |
#------------------------------------------------------------------------------- |
|
87 |
||
472
80eb3bde98e3
Adding support for defining input and output with the same location in LPCBeremiz
laurent
parents:
445
diff
changeset
|
88 |
LOCATION_TYPES = {"I": LOCATION_VAR_INPUT, |
80eb3bde98e3
Adding support for defining input and output with the same location in LPCBeremiz
laurent
parents:
445
diff
changeset
|
89 |
"Q": LOCATION_VAR_OUTPUT, |
80eb3bde98e3
Adding support for defining input and output with the same location in LPCBeremiz
laurent
parents:
445
diff
changeset
|
90 |
"M": LOCATION_VAR_MEMORY} |
80eb3bde98e3
Adding support for defining input and output with the same location in LPCBeremiz
laurent
parents:
445
diff
changeset
|
91 |
|
80eb3bde98e3
Adding support for defining input and output with the same location in LPCBeremiz
laurent
parents:
445
diff
changeset
|
92 |
LOCATION_DIRS = dict([(dir, size) for size, dir in LOCATION_TYPES.iteritems()]) |
80eb3bde98e3
Adding support for defining input and output with the same location in LPCBeremiz
laurent
parents:
445
diff
changeset
|
93 |
|
80eb3bde98e3
Adding support for defining input and output with the same location in LPCBeremiz
laurent
parents:
445
diff
changeset
|
94 |
LOCATION_SIZES = {} |
80eb3bde98e3
Adding support for defining input and output with the same location in LPCBeremiz
laurent
parents:
445
diff
changeset
|
95 |
for size, types in LOCATIONDATATYPES.iteritems(): |
80eb3bde98e3
Adding support for defining input and output with the same location in LPCBeremiz
laurent
parents:
445
diff
changeset
|
96 |
for type in types: |
80eb3bde98e3
Adding support for defining input and output with the same location in LPCBeremiz
laurent
parents:
445
diff
changeset
|
97 |
LOCATION_SIZES[type] = size |
80eb3bde98e3
Adding support for defining input and output with the same location in LPCBeremiz
laurent
parents:
445
diff
changeset
|
98 |
|
442 | 99 |
def _GetModuleChildren(module): |
100 |
children = [] |
|
101 |
for child in module["children"]: |
|
102 |
if child["type"] == LOCATION_GROUP: |
|
103 |
children.extend(child["children"]) |
|
104 |
else: |
|
105 |
children.append(child) |
|
106 |
return children |
|
107 |
||
108 |
def _GetLastModuleGroup(module): |
|
109 |
group = module |
|
110 |
for child in module["children"]: |
|
111 |
if child["type"] == LOCATION_GROUP: |
|
112 |
group = child |
|
113 |
return group["children"] |
|
114 |
||
115 |
def _GetModuleBySomething(module, something, toks): |
|
116 |
for child in _GetModuleChildren(module): |
|
117 |
if child.get(something) == toks[0]: |
|
118 |
if len(toks) > 1: |
|
119 |
return _GetModuleBySomething(child, something, toks[1:]) |
|
120 |
return child |
|
121 |
return None |
|
122 |
||
472
80eb3bde98e3
Adding support for defining input and output with the same location in LPCBeremiz
laurent
parents:
445
diff
changeset
|
123 |
def _GetModuleVariable(module, location, direction): |
442 | 124 |
for child in _GetModuleChildren(module): |
472
80eb3bde98e3
Adding support for defining input and output with the same location in LPCBeremiz
laurent
parents:
445
diff
changeset
|
125 |
if child["location"] == location and child["type"] == LOCATION_TYPES[direction]: |
442 | 126 |
return child |
127 |
return None |
|
128 |
||
129 |
def _RemoveModuleChild(module, child): |
|
130 |
if child in module["children"]: |
|
131 |
module["children"].remove(child) |
|
132 |
else: |
|
133 |
for group in module["children"]: |
|
134 |
if group["type"] == LOCATION_GROUP and child in group["children"]: |
|
135 |
group["children"].remove(child) |
|
136 |
||
137 |
BUS_TEXT = """/* Code generated by LPCBus plugin */ |
|
138 |
||
139 |
/* LPCBus plugin includes */ |
|
140 |
#include "app_glue.h" |
|
141 |
#ifdef _WINDOWS_H |
|
142 |
#include "iec_types.h" |
|
143 |
#else |
|
144 |
#include "iec_std_lib.h" |
|
145 |
#endif |
|
146 |
||
147 |
%(declare_code)s |
|
148 |
||
149 |
/* LPCBus plugin user variables definition */ |
|
150 |
%(var_decl)s |
|
151 |
||
152 |
/* LPCBus plugin functions */ |
|
153 |
int __init_%(location_str)s(int argc,char **argv) |
|
154 |
{ |
|
155 |
return 0; |
|
156 |
} |
|
157 |
||
158 |
void __cleanup_%(location_str)s(void) |
|
159 |
{ |
|
160 |
} |
|
161 |
||
162 |
void __retrieve_%(location_str)s(void) |
|
163 |
{ |
|
164 |
%(retrieve_code)s |
|
165 |
} |
|
166 |
||
167 |
void __publish_%(location_str)s(void) |
|
168 |
{ |
|
169 |
%(publish_code)s |
|
170 |
} |
|
171 |
""" |
|
172 |
||
173 |
class LPCBus(object): |
|
174 |
||
175 |
def __init__(self): |
|
176 |
self.VariableLocationTree = [] |
|
177 |
self.ResetUsedLocations() |
|
178 |
self.Icon = None |
|
179 |
||
180 |
def __getitem__(self, key): |
|
181 |
if key == "children": |
|
182 |
return self.VariableLocationTree |
|
183 |
raise KeyError, "Only 'children' key is available" |
|
184 |
||
185 |
def SetIcon(self, icon): |
|
186 |
self.Icon = icon |
|
187 |
||
188 |
def _GetChildBySomething(self, something, toks): |
|
189 |
return _GetModuleBySomething({"children" : self.VariableLocationTree}, something, toks) |
|
190 |
||
191 |
def GetBaseTypes(self): |
|
192 |
return self.GetPlugRoot().GetBaseTypes() |
|
193 |
||
194 |
def GetSizeOfType(self, type): |
|
195 |
return LOCATION_SIZES[self.GetPlugRoot().GetBaseType(type)] |
|
196 |
||
197 |
def _GetVariableLocationTree(self, current_location, infos): |
|
198 |
if infos["type"] == LOCATION_MODULE: |
|
199 |
location = current_location + (infos["IEC_Channel"],) |
|
200 |
return {"name": infos["name"], |
|
201 |
"type": infos["type"], |
|
202 |
"location": ".".join(map(str, location + ("x",))), |
|
203 |
"icon": infos["icon"], |
|
204 |
"children": [self._GetVariableLocationTree(location, child) for child in infos["children"]]} |
|
205 |
elif infos["type"] == LOCATION_GROUP: |
|
206 |
return {"name": infos["name"], |
|
207 |
"type": infos["type"], |
|
208 |
"location": "", |
|
209 |
"icon": infos["icon"], |
|
210 |
"children": [self._GetVariableLocationTree(current_location, child) for child in infos["children"]]} |
|
211 |
else: |
|
212 |
size = self.GetSizeOfType(infos["IEC_type"]) |
|
213 |
location = "%" + LOCATION_DIRS[infos["type"]] + size + ".".join(map(str, current_location + infos["location"])) |
|
214 |
return {"name": infos["name"], |
|
215 |
"type": infos["type"], |
|
216 |
"size": size, |
|
217 |
"IEC_type": infos["IEC_type"], |
|
218 |
"location": location, |
|
219 |
"description": infos["description"], |
|
220 |
"children": []} |
|
221 |
||
222 |
def GetVariableLocationTree(self): |
|
223 |
return {"name": self.BaseParams.getName(), |
|
224 |
"type": LOCATION_PLUGIN, |
|
225 |
"location": self.GetFullIEC_Channel(), |
|
226 |
"icon": self.Icon, |
|
227 |
"children": [self._GetVariableLocationTree(self.GetCurrentLocation(), child) |
|
228 |
for child in self.VariableLocationTree]} |
|
229 |
||
230 |
def PlugTestModified(self): |
|
231 |
return False |
|
232 |
||
233 |
def PlugMakeDir(self): |
|
234 |
pass |
|
235 |
||
236 |
def PlugRequestSave(self): |
|
237 |
return None |
|
238 |
||
239 |
def ResetUsedLocations(self): |
|
240 |
self.UsedLocations = {} |
|
241 |
||
242 |
def _AddUsedLocation(self, parent, location): |
|
243 |
num = location.pop(0) |
|
244 |
if not parent.has_key(num): |
|
245 |
parent[num] = {"used": False, "children": {}} |
|
246 |
if len(location) > 0: |
|
247 |
self._AddUsedLocation(parent[num]["children"], location) |
|
248 |
else: |
|
249 |
parent[num]["used"] = True |
|
250 |
||
251 |
def AddUsedLocation(self, location): |
|
252 |
if len(location) > 0: |
|
253 |
self._AddUsedLocation(self.UsedLocations, list(location)) |
|
254 |
||
255 |
def _CheckLocationConflicts(self, parent, location): |
|
256 |
num = location.pop(0) |
|
257 |
if not parent.has_key(num): |
|
258 |
return False |
|
259 |
if len(location) > 0: |
|
260 |
if parent[num]["used"]: |
|
261 |
return True |
|
262 |
return self._CheckLocationConflicts(parent[num]["children"], location) |
|
263 |
elif len(parent[num]["children"]) > 0: |
|
264 |
return True |
|
265 |
return False |
|
266 |
||
267 |
def CheckLocationConflicts(self, location): |
|
268 |
if len(location) > 0: |
|
269 |
return self._CheckLocationConflicts(self.UsedLocations, list(location)) |
|
270 |
return False |
|
271 |
||
272 |
def PlugGenerate_C(self, buildpath, locations): |
|
273 |
""" |
|
274 |
Generate C code |
|
275 |
@param current_location: Tupple containing plugin IEC location : %I0.0.4.5 => (0,0,4,5) |
|
276 |
@param locations: List of complete variables locations \ |
|
277 |
[{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...) |
|
278 |
"NAME" : name of the variable (generally "__IW0_1_2" style) |
|
279 |
"DIR" : direction "Q","I" or "M" |
|
280 |
"SIZE" : size "X", "B", "W", "D", "L" |
|
281 |
"LOC" : tuple of interger for IEC location (0,1,2,...) |
|
282 |
}, ...] |
|
283 |
@return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND |
|
284 |
""" |
|
285 |
current_location = self.GetCurrentLocation() |
|
286 |
# define a unique name for the generated C file |
|
287 |
location_str = "_".join(map(str, current_location)) |
|
288 |
||
289 |
code_str = {"location_str": location_str, |
|
290 |
"var_decl": "", |
|
291 |
"declare_code": "", |
|
292 |
"retrieve_code": "", |
|
293 |
"publish_code": "", |
|
294 |
} |
|
295 |
||
296 |
# Adding variables |
|
297 |
vars = [] |
|
298 |
self.ResetUsedLocations() |
|
299 |
for location in locations: |
|
300 |
loc = location["LOC"][len(current_location):] |
|
301 |
group = next = self |
|
302 |
i = 0 |
|
303 |
while next is not None and i < len(loc): |
|
304 |
next = self._GetChildBySomething("IEC_Channel", loc[:i + 1]) |
|
305 |
if next is not None: |
|
306 |
i += 1 |
|
307 |
group = next |
|
308 |
var_loc = loc[i:] |
|
309 |
for variable in group["children"]: |
|
310 |
if variable["location"] == var_loc: |
|
311 |
if location["DIR"] != LOCATION_DIRS[variable["type"]]: |
|
312 |
raise Exception, "Direction conflict in variable definition" |
|
313 |
if location["IEC_TYPE"] != variable["IEC_type"]: |
|
314 |
raise Exception, "Type conflict in variable definition" |
|
315 |
if location["DIR"] == "Q": |
|
316 |
if self.CheckLocationConflicts(location["LOC"]): |
|
317 |
raise Exception, "BYTE and BIT from the same BYTE can't be used together" |
|
318 |
self.AddUsedLocation(location["LOC"]) |
|
319 |
vars.append({"location": location["NAME"], |
|
320 |
"Type": variable["IEC_type"], |
|
321 |
"Declare": variable["declare"], |
|
322 |
"Retrieve": variable["retrieve"], |
|
323 |
"Publish": variable["publish"], |
|
324 |
}) |
|
325 |
break |
|
326 |
base_types = self.GetPlugRoot().GetBaseTypes() |
|
327 |
for var in vars: |
|
328 |
prefix = "" |
|
329 |
if var["Type"] in base_types: |
|
330 |
prefix = "IEC_" |
|
331 |
code_str["var_decl"] += "%s%s beremiz%s;\n"%(prefix, var["Type"], var["location"]) |
|
332 |
code_str["var_decl"] += "%s%s *%s = &beremiz%s;\n"%(prefix, var["Type"], var["location"], var["location"]) |
|
333 |
if var["Declare"] != "": |
|
334 |
code_str["declare_code"] += var["Declare"] + "\n" |
|
335 |
if var["Retrieve"] != "": |
|
336 |
code_str["retrieve_code"] += var["Retrieve"] % ("*" + var["location"]) + "\n" |
|
337 |
if var["Publish"] != "": |
|
338 |
code_str["publish_code"] += var["Publish"] % ("*" + var["location"]) + "\n" |
|
339 |
||
340 |
Gen_Module_path = os.path.join(buildpath, "Module_%s.c"%location_str) |
|
341 |
module = open(Gen_Module_path,'w') |
|
342 |
module.write(BUS_TEXT % code_str) |
|
343 |
module.close() |
|
344 |
||
345 |
matiec_flags = '"-I%s"'%os.path.abspath(self.GetPlugRoot().GetIECLibPath()) |
|
346 |
return [(Gen_Module_path, matiec_flags)],"",True |
|
347 |
||
348 |
#------------------------------------------------------------------------------- |
|
349 |
# LPCPluginsRoot Class |
|
350 |
#------------------------------------------------------------------------------- |
|
351 |
||
352 |
def mycopytree(src, dst): |
|
353 |
""" |
|
354 |
Copy content of a directory to an other, omit hidden files |
|
355 |
@param src: source directory |
|
356 |
@param dst: destination directory |
|
357 |
""" |
|
358 |
for i in os.listdir(src): |
|
359 |
if not i.startswith('.'): |
|
360 |
srcpath = os.path.join(src,i) |
|
361 |
dstpath = os.path.join(dst,i) |
|
362 |
if os.path.isdir(srcpath): |
|
503 | 363 |
if os.path.exists(dstpath): |
364 |
shutil.rmtree(dstpath) |
|
365 |
os.makedirs(dstpath) |
|
442 | 366 |
mycopytree(srcpath, dstpath) |
367 |
elif os.path.isfile(srcpath): |
|
368 |
shutil.copy2(srcpath, dstpath) |
|
369 |
||
370 |
class LPCPluginsRoot(PluginsRoot): |
|
371 |
||
372 |
PluginMethods = [ |
|
492 | 373 |
{"bitmap" : opjimg("Debug"), |
374 |
"name" : _("Simulate"), |
|
375 |
"tooltip" : _("Simulate PLC"), |
|
376 |
"method" : "_Simulate"}, |
|
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
377 |
{"bitmap" : opjimg("Run"), |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
378 |
"name" : _("Run"), |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
379 |
"shown" : False, |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
380 |
"tooltip" : _("Start PLC"), |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
381 |
"method" : "_Run"}, |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
382 |
{"bitmap" : opjimg("Stop"), |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
383 |
"name" : _("Stop"), |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
384 |
"shown" : False, |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
385 |
"tooltip" : _("Stop Running PLC"), |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
386 |
"method" : "_Stop"}, |
492 | 387 |
{"bitmap" : opjimg("Build"), |
388 |
"name" : _("Build"), |
|
389 |
"tooltip" : _("Build project into build folder"), |
|
390 |
"method" : "_build"}, |
|
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
391 |
{"bitmap" : opjimg("Transfer"), |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
392 |
"name" : _("Transfer"), |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
393 |
"shown" : False, |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
394 |
"tooltip" : _("Transfer PLC"), |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
395 |
"method" : "_Transfer"}, |
442 | 396 |
] |
397 |
||
478 | 398 |
def __init__(self, frame, logger): |
399 |
PluginsRoot.__init__(self, frame, logger) |
|
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
400 |
|
478 | 401 |
self.PlugChildsTypes += [("LPCBus", LPCBus, "LPC bus")] |
402 |
||
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
403 |
self.OnlineMode = 0 |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
404 |
self.OnlinePath = None |
492 | 405 |
|
406 |
self.BuildSimulation = False |
|
407 |
self.SimulationBuildPath = None |
|
408 |
||
409 |
self.previous_simulating = False |
|
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
410 |
|
442 | 411 |
def GetProjectName(self): |
412 |
return self.Project.getname() |
|
413 |
||
510
8038c08b9874
Getting default target when no target defined fixed
laurent
parents:
503
diff
changeset
|
414 |
def GetDefaultTargetName(self): |
492 | 415 |
if self.BuildSimulation: |
510
8038c08b9874
Getting default target when no target defined fixed
laurent
parents:
503
diff
changeset
|
416 |
return PluginsRoot.GetDefaultTargetName(self) |
492 | 417 |
else: |
510
8038c08b9874
Getting default target when no target defined fixed
laurent
parents:
503
diff
changeset
|
418 |
return "LPC" |
8038c08b9874
Getting default target when no target defined fixed
laurent
parents:
503
diff
changeset
|
419 |
|
8038c08b9874
Getting default target when no target defined fixed
laurent
parents:
503
diff
changeset
|
420 |
def GetTarget(self): |
8038c08b9874
Getting default target when no target defined fixed
laurent
parents:
503
diff
changeset
|
421 |
target = PluginsRoot.GetTarget(self) |
8038c08b9874
Getting default target when no target defined fixed
laurent
parents:
503
diff
changeset
|
422 |
if not self.BuildSimulation: |
8038c08b9874
Getting default target when no target defined fixed
laurent
parents:
503
diff
changeset
|
423 |
target.getcontent()["value"].setBuildPath(self.BuildPath) |
442 | 424 |
return target |
492 | 425 |
|
426 |
def _getBuildPath(self): |
|
427 |
if self.BuildSimulation: |
|
428 |
if self.SimulationBuildPath is None: |
|
429 |
self.SimulationBuildPath = os.path.join(tempfile.mkdtemp(), os.path.basename(self.ProjectPath), "build") |
|
430 |
return self.SimulationBuildPath |
|
431 |
else: |
|
494
9e4263099427
Bug when opening with not empty buildpath parameter fixed
laurent
parents:
492
diff
changeset
|
432 |
return PluginsRoot._getBuildPath(self) |
503 | 433 |
|
434 |
def _build(self): |
|
435 |
if self.BuildPath is not None: |
|
436 |
mycopytree(self.OrigBuildPath, self.BuildPath) |
|
437 |
PluginsRoot._build(self) |
|
492 | 438 |
|
442 | 439 |
def SetProjectName(self, name): |
440 |
return self.Project.setname(name) |
|
441 |
||
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
442 |
def SetOnlineMode(self, mode, path=None): |
492 | 443 |
if self.OnlineMode != mode: |
444 |
self.OnlineMode = mode |
|
510
8038c08b9874
Getting default target when no target defined fixed
laurent
parents:
503
diff
changeset
|
445 |
self.KillDebugThread() |
8038c08b9874
Getting default target when no target defined fixed
laurent
parents:
503
diff
changeset
|
446 |
|
517 | 447 |
if self.OnlineMode != 0: |
492 | 448 |
if self._connector is None: |
449 |
uri = "LPC://%s" % path |
|
450 |
try: |
|
451 |
self._connector = connectors.ConnectorFactory(uri, self) |
|
452 |
except Exception, msg: |
|
453 |
self.logger.write_error(_("Exception while connecting %s!\n")%uri) |
|
454 |
self.logger.write_error(traceback.format_exc()) |
|
455 |
||
456 |
# Did connection success ? |
|
457 |
if self._connector is None: |
|
458 |
# Oups. |
|
459 |
self.logger.write_error(_("Connection failed to %s!\n")%uri) |
|
460 |
||
461 |
if self._connector is not None: |
|
462 |
||
517 | 463 |
if self.OnlineMode == 1: |
492 | 464 |
self.CompareLocalAndRemotePLC() |
465 |
||
466 |
# Init with actual PLC status and print it |
|
467 |
self.UpdateMethodsFromPLCStatus() |
|
468 |
if self.previous_plcstate is not None: |
|
469 |
status = _(self.previous_plcstate) |
|
470 |
else: |
|
471 |
status = "" |
|
472 |
self.logger.write(_("PLC is %s\n")%status) |
|
473 |
||
474 |
# Start the status Timer |
|
475 |
self.StatusTimer.Start(milliseconds=500, oneShot=False) |
|
476 |
||
477 |
if self.previous_plcstate=="Started": |
|
478 |
if self.DebugAvailable() and self.GetIECProgramsAndVariables(): |
|
479 |
self.logger.write(_("Debug connect matching running PLC\n")) |
|
480 |
self._connect_debug() |
|
481 |
else: |
|
482 |
self.logger.write_warning(_("Debug do not match PLC - stop/transfert/start to re-enable\n")) |
|
483 |
||
484 |
else: |
|
485 |
self._connector = None |
|
486 |
self.StatusTimer.Stop() |
|
487 |
||
488 |
self.OnlinePath = path |
|
489 |
self.UpdateMethodsFromPLCStatus() |
|
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
490 |
|
444
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
491 |
# Update a PLCOpenEditor Pou variable name |
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
492 |
def UpdateProjectVariableName(self, old_name, new_name): |
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
493 |
self.Project.updateElementName(old_name, new_name) |
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
494 |
self.BufferProject() |
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
495 |
|
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
496 |
def RemoveProjectVariableByAddress(self, address): |
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
497 |
self.Project.removeVariableByAddress(address) |
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
498 |
self.BufferProject() |
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
499 |
|
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
500 |
def RemoveProjectVariableByFilter(self, leading): |
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
501 |
self.Project.removeVariableByFilter(leading) |
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
502 |
self.BufferProject() |
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
503 |
|
442 | 504 |
def LoadProject(self, ProjectPath, BuildPath=None): |
505 |
""" |
|
478 | 506 |
Load a project contained in a folder |
507 |
@param ProjectPath: path of the project folder |
|
442 | 508 |
""" |
478 | 509 |
if os.path.basename(ProjectPath) == "": |
510 |
ProjectPath = os.path.dirname(ProjectPath) |
|
500
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
511 |
|
478 | 512 |
# Verify that project contains a PLCOpen program |
513 |
plc_file = os.path.join(ProjectPath, "plc.xml") |
|
500
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
514 |
if os.path.isfile(plc_file): |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
515 |
# Load PLCOpen file |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
516 |
result = self.OpenXMLFile(plc_file) |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
517 |
if result: |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
518 |
return result |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
519 |
else: |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
520 |
self.CreateNewProject({"companyName": "", |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
521 |
"productName": "", |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
522 |
"productVersion": "", |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
523 |
"projectName": "", |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
524 |
"pageSize": (0, 0), |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
525 |
"scaling": {}}) |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
526 |
|
442 | 527 |
# Change XSD into class members |
528 |
self._AddParamsMembers() |
|
529 |
self.PluggedChilds = {} |
|
500
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
530 |
|
442 | 531 |
# Keep track of the root plugin (i.e. project path) |
532 |
self.ProjectPath = ProjectPath |
|
500
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
533 |
|
478 | 534 |
self.BuildPath = self._getBuildPath() |
503 | 535 |
self.OrigBuildPath = BuildPath |
442 | 536 |
if BuildPath is not None: |
537 |
mycopytree(BuildPath, self.BuildPath) |
|
538 |
||
478 | 539 |
# If dir have already be made, and file exist |
540 |
if os.path.isdir(self.PlugPath()) and os.path.isfile(self.PluginXmlFilePath()): |
|
541 |
#Load the plugin.xml file into parameters members |
|
542 |
result = self.LoadXMLParams() |
|
543 |
if result: |
|
544 |
return result |
|
545 |
#Load and init all the childs |
|
546 |
self.LoadChilds() |
|
500
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
547 |
|
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
548 |
if self.PlugTestModified(): |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
549 |
self.SaveProject() |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
550 |
|
442 | 551 |
self.RefreshPluginsBlockLists() |
478 | 552 |
|
553 |
return None |
|
442 | 554 |
|
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
555 |
############# Real PLC object access ############# |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
556 |
def UpdateMethodsFromPLCStatus(self): |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
557 |
# Get PLC state : Running or Stopped |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
558 |
# TODO : use explicit status instead of boolean |
492 | 559 |
simulating = False |
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
560 |
if self.OnlineMode == 0: |
492 | 561 |
if self._connector is not None: |
562 |
simulating = self._connector.GetPLCstatus() == "Started" |
|
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
563 |
status = "Disconnected" |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
564 |
elif self.OnlineMode == 1: |
492 | 565 |
if self._connector is not None: |
566 |
simulating = self._connector.GetPLCstatus() == "Started" |
|
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
567 |
status = "Connected" |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
568 |
else: |
492 | 569 |
if self._connector is not None: |
570 |
status = self._connector.GetPLCstatus() |
|
571 |
else: |
|
572 |
status = "Disconnected" |
|
573 |
if self.previous_plcstate != status or self.previous_simulating != simulating: |
|
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
574 |
for args in { |
492 | 575 |
"Started" : [("_Simulate", False), |
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
576 |
("_Run", False), |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
577 |
("_Stop", True), |
492 | 578 |
("_build", False), |
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
579 |
("_Transfer", False)], |
492 | 580 |
"Stopped" : [("_Simulate", False), |
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
581 |
("_Run", True), |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
582 |
("_Stop", False), |
492 | 583 |
("_build", False), |
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
584 |
("_Transfer", False)], |
492 | 585 |
"Connected" : [("_Simulate", not simulating), |
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
586 |
("_Run", False), |
492 | 587 |
("_Stop", simulating), |
588 |
("_build", False), |
|
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
589 |
("_Transfer", True)], |
492 | 590 |
"Disconnected" :[("_Simulate", not simulating), |
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
591 |
("_Run", False), |
492 | 592 |
("_Stop", simulating), |
593 |
("_build", True), |
|
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
594 |
("_Transfer", False)], |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
595 |
}.get(status,[]): |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
596 |
self.ShowMethod(*args) |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
597 |
self.previous_plcstate = status |
492 | 598 |
self.previous_simulating = simulating |
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
599 |
return True |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
600 |
return False |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
601 |
|
492 | 602 |
def Generate_plc_declare_locations(self): |
603 |
""" |
|
604 |
Declare used locations in order to simulatePLC in a black box |
|
605 |
""" |
|
606 |
return """#include "iec_types_all.h" |
|
607 |
||
608 |
#define __LOCATED_VAR(type, name, ...) \ |
|
609 |
type beremiz_##name;\ |
|
610 |
type *name = &beremiz_##name; |
|
611 |
||
612 |
#include "LOCATED_VARIABLES.h" |
|
613 |
||
614 |
#undef __LOCATED_VAR |
|
615 |
||
616 |
""" |
|
617 |
||
618 |
def _Simulate(self): |
|
619 |
""" |
|
620 |
Method called by user to Simulate PLC |
|
621 |
""" |
|
622 |
uri = "LOCAL://" |
|
623 |
try: |
|
624 |
self._connector = connectors.ConnectorFactory(uri, self) |
|
625 |
except Exception, msg: |
|
626 |
self.logger.write_error(_("Exception while connecting %s!\n")%uri) |
|
627 |
self.logger.write_error(traceback.format_exc()) |
|
628 |
||
629 |
# Did connection success ? |
|
630 |
if self._connector is None: |
|
631 |
# Oups. |
|
632 |
self.logger.write_error(_("Connection failed to %s!\n")%uri) |
|
633 |
return False |
|
634 |
||
635 |
self.BuildSimulation = True |
|
636 |
||
637 |
||
638 |
buildpath = self._getBuildPath() |
|
639 |
||
640 |
# Eventually create build dir |
|
641 |
if not os.path.exists(buildpath): |
|
642 |
os.makedirs(buildpath) |
|
643 |
||
644 |
# Generate SoftPLC IEC code |
|
645 |
IECGenRes = self._Generate_SoftPLC() |
|
646 |
||
647 |
# If IEC code gen fail, bail out. |
|
648 |
if not IECGenRes: |
|
649 |
self.logger.write_error(_("IEC-61131-3 code generation failed !\n")) |
|
650 |
self.BuildSimulation = False |
|
651 |
return False |
|
652 |
||
653 |
# Reset variable and program list that are parsed from |
|
654 |
# CSV file generated by IEC2C compiler. |
|
655 |
self.ResetIECProgramsAndVariables() |
|
656 |
||
657 |
gen_result = self.PlugGenerate_C(buildpath, self.PLCGeneratedLocatedVars) |
|
658 |
PlugCFilesAndCFLAGS, PlugLDFLAGS, DoCalls = gen_result[:3] |
|
659 |
# if some files have been generated put them in the list with their location |
|
660 |
if PlugCFilesAndCFLAGS: |
|
661 |
self.LocationCFilesAndCFLAGS = [(self.GetCurrentLocation(), PlugCFilesAndCFLAGS, DoCalls)] |
|
662 |
else: |
|
663 |
self.LocationCFilesAndCFLAGS = [] |
|
664 |
||
665 |
# plugin asks for some LDFLAGS |
|
666 |
if PlugLDFLAGS: |
|
667 |
# LDFLAGS can be either string |
|
668 |
if type(PlugLDFLAGS)==type(str()): |
|
669 |
self.LDFLAGS=[PlugLDFLAGS] |
|
670 |
#or list of strings |
|
671 |
elif type(PlugLDFLAGS)==type(list()): |
|
672 |
self.LDFLAGS=PlugLDFLAGS[:] |
|
673 |
else: |
|
674 |
self.LDFLAGS=[] |
|
675 |
||
676 |
# Template based part of C code generation |
|
677 |
# files are stacked at the beginning, as files of plugin tree root |
|
678 |
for generator, filename, name in [ |
|
679 |
# debugger code |
|
680 |
(self.Generate_plc_debugger, "plc_debugger.c", "Debugger"), |
|
681 |
# init/cleanup/retrieve/publish, run and align code |
|
682 |
(self.Generate_plc_common_main,"plc_common_main.c","Common runtime"), |
|
683 |
# declare located variables for simulate in a black box |
|
684 |
(self.Generate_plc_declare_locations,"plc_declare_locations.c","Declare Locations")]: |
|
685 |
try: |
|
686 |
# Do generate |
|
687 |
code = generator() |
|
688 |
if code is None: |
|
689 |
raise |
|
690 |
code_path = os.path.join(buildpath,filename) |
|
691 |
open(code_path, "w").write(code) |
|
692 |
# Insert this file as first file to be compiled at root plugin |
|
693 |
self.LocationCFilesAndCFLAGS[0][1].insert(0,(code_path, self.plcCFLAGS)) |
|
694 |
except Exception, exc: |
|
695 |
self.logger.write_error(name+_(" generation failed !\n")) |
|
696 |
self.logger.write_error(traceback.format_exc()) |
|
697 |
self.BuildSimulation = False |
|
698 |
return False |
|
699 |
||
700 |
# Get simulation builder |
|
701 |
builder = self.GetBuilder() |
|
702 |
if builder is None: |
|
703 |
self.logger.write_error(_("Fatal : cannot get builder.\n")) |
|
704 |
self.BuildSimulation = False |
|
705 |
return False |
|
706 |
||
707 |
# Build |
|
708 |
try: |
|
709 |
if not builder.build() : |
|
710 |
self.logger.write_error(_("C Build failed.\n")) |
|
711 |
self.BuildSimulation = False |
|
712 |
return False |
|
713 |
except Exception, exc: |
|
714 |
self.logger.write_error(_("C Build crashed !\n")) |
|
715 |
self.logger.write_error(traceback.format_exc()) |
|
716 |
self.BuildSimulation = False |
|
717 |
return False |
|
718 |
||
719 |
data = builder.GetBinaryCode() |
|
720 |
if data is not None : |
|
721 |
if self._connector.NewPLC(builder.GetBinaryCodeMD5(), data, []): |
|
722 |
if self.AppFrame is not None: |
|
723 |
self.AppFrame.CloseDebugTabs() |
|
724 |
self.AppFrame.RefreshInstancesTree() |
|
725 |
self.UnsubscribeAllDebugIECVariable() |
|
726 |
self.ProgramTransferred() |
|
727 |
self.logger.write(_("Transfer completed successfully.\n")) |
|
728 |
else: |
|
729 |
self.logger.write_error(_("Transfer failed\n")) |
|
730 |
self.BuildSimulation = False |
|
731 |
return False |
|
732 |
||
733 |
self._Run() |
|
734 |
||
735 |
self.BuildSimulation = False |
|
736 |
||
737 |
# Start the status Timer |
|
738 |
self.StatusTimer.Start(milliseconds=500, oneShot=False) |
|
739 |
||
442 | 740 |
#------------------------------------------------------------------------------- |
741 |
# LPCBeremiz Class |
|
742 |
#------------------------------------------------------------------------------- |
|
743 |
||
744 |
class LPCBeremiz(Beremiz): |
|
745 |
||
746 |
def _init_coll_FileMenu_Items(self, parent): |
|
747 |
AppendMenu(parent, help='', id=wx.ID_SAVE, |
|
748 |
kind=wx.ITEM_NORMAL, text=_(u'Save\tCTRL+S')) |
|
749 |
AppendMenu(parent, help='', id=wx.ID_CLOSE, |
|
750 |
kind=wx.ITEM_NORMAL, text=_(u'Close Tab\tCTRL+W')) |
|
751 |
parent.AppendSeparator() |
|
752 |
AppendMenu(parent, help='', id=wx.ID_PAGE_SETUP, |
|
753 |
kind=wx.ITEM_NORMAL, text=_(u'Page Setup')) |
|
754 |
AppendMenu(parent, help='', id=wx.ID_PREVIEW, |
|
755 |
kind=wx.ITEM_NORMAL, text=_(u'Preview')) |
|
756 |
AppendMenu(parent, help='', id=wx.ID_PRINT, |
|
757 |
kind=wx.ITEM_NORMAL, text=_(u'Print')) |
|
758 |
parent.AppendSeparator() |
|
759 |
AppendMenu(parent, help='', id=wx.ID_PROPERTIES, |
|
760 |
kind=wx.ITEM_NORMAL, text=_(u'Properties')) |
|
761 |
parent.AppendSeparator() |
|
762 |
AppendMenu(parent, help='', id=wx.ID_EXIT, |
|
763 |
kind=wx.ITEM_NORMAL, text=_(u'Quit\tCTRL+Q')) |
|
764 |
||
765 |
self.Bind(wx.EVT_MENU, self.OnSaveProjectMenu, id=wx.ID_SAVE) |
|
766 |
self.Bind(wx.EVT_MENU, self.OnCloseTabMenu, id=wx.ID_CLOSE) |
|
767 |
self.Bind(wx.EVT_MENU, self.OnPageSetupMenu, id=wx.ID_PAGE_SETUP) |
|
768 |
self.Bind(wx.EVT_MENU, self.OnPreviewMenu, id=wx.ID_PREVIEW) |
|
769 |
self.Bind(wx.EVT_MENU, self.OnPrintMenu, id=wx.ID_PRINT) |
|
770 |
self.Bind(wx.EVT_MENU, self.OnPropertiesMenu, id=wx.ID_PROPERTIES) |
|
771 |
self.Bind(wx.EVT_MENU, self.OnQuitMenu, id=wx.ID_EXIT) |
|
772 |
||
773 |
def _init_ctrls(self, prnt): |
|
774 |
IDEFrame._init_ctrls(self, prnt) |
|
775 |
||
776 |
self.Bind(wx.EVT_MENU, self.OnOpenWidgetInspector, id=ID_BEREMIZINSPECTOR) |
|
777 |
accel = wx.AcceleratorTable([wx.AcceleratorEntry(wx.ACCEL_CTRL|wx.ACCEL_ALT, ord('I'), ID_BEREMIZINSPECTOR)]) |
|
778 |
self.SetAcceleratorTable(accel) |
|
779 |
||
780 |
self.PLCConfig = wx.ScrolledWindow(id=ID_BEREMIZPLCCONFIG, |
|
781 |
name='PLCConfig', parent=self.LeftNoteBook, pos=wx.Point(0, 0), |
|
782 |
size=wx.Size(-1, -1), style=wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER|wx.HSCROLL|wx.VSCROLL) |
|
783 |
self.PLCConfig.SetBackgroundColour(wx.WHITE) |
|
784 |
self.PLCConfig.Bind(wx.EVT_LEFT_DOWN, self.OnPanelLeftDown) |
|
785 |
self.PLCConfig.Bind(wx.EVT_SIZE, self.OnMoveWindow) |
|
786 |
self.LeftNoteBook.InsertPage(0, self.PLCConfig, _("Topology"), True) |
|
787 |
||
788 |
self.LogConsole = wx.TextCtrl(id=ID_BEREMIZLOGCONSOLE, value='', |
|
789 |
name='LogConsole', parent=self.BottomNoteBook, pos=wx.Point(0, 0), |
|
790 |
size=wx.Size(0, 0), style=wx.TE_MULTILINE|wx.TE_RICH2) |
|
791 |
self.LogConsole.Bind(wx.EVT_LEFT_DCLICK, self.OnLogConsoleDClick) |
|
792 |
self.BottomNoteBook.AddPage(self.LogConsole, _("Log Console")) |
|
793 |
||
794 |
self._init_beremiz_sizers() |
|
795 |
||
796 |
def OnCloseFrame(self, event): |
|
519
3a5faff52e5c
Adding message to standard output to signal when LPCBeremiz is closed
laurent
parents:
517
diff
changeset
|
797 |
global frame |
442 | 798 |
frame = None |
799 |
self.PluginRoot.ResetAppFrame(lpcberemiz_cmd.Log) |
|
492 | 800 |
if self.PluginRoot.OnlineMode == 0: |
801 |
self.PluginRoot._connector = None |
|
802 |
||
803 |
self.PluginRoot.KillDebugThread() |
|
442 | 804 |
self.KillLocalRuntime() |
805 |
||
519
3a5faff52e5c
Adding message to standard output to signal when LPCBeremiz is closed
laurent
parents:
517
diff
changeset
|
806 |
print "Closed" |
3a5faff52e5c
Adding message to standard output to signal when LPCBeremiz is closed
laurent
parents:
517
diff
changeset
|
807 |
|
442 | 808 |
event.Skip() |
809 |
||
500
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
810 |
def ShowProperties(self): |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
811 |
old_values = self.Controler.GetProjectProperties() |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
812 |
dialog = ProjectDialog(self ,False) |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
813 |
dialog.SetValues(old_values) |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
814 |
if dialog.ShowModal() == wx.ID_OK: |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
815 |
new_values = dialog.GetValues() |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
816 |
new_values["creationDateTime"] = old_values["creationDateTime"] |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
817 |
if new_values != old_values: |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
818 |
self.Controler.SetProjectProperties(None, new_values) |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
819 |
self._Refresh(TITLE, TOOLBAR, FILEMENU, EDITMENU, DISPLAYMENU, |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
820 |
TYPESTREE, INSTANCESTREE, SCALING) |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
821 |
dialog.Destroy() |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
822 |
|
442 | 823 |
def RefreshFileMenu(self): |
824 |
if self.PluginRoot is not None: |
|
825 |
selected = self.TabsOpened.GetSelection() |
|
826 |
if selected >= 0: |
|
827 |
graphic_viewer = isinstance(self.TabsOpened.GetPage(selected), Viewer) |
|
828 |
else: |
|
829 |
graphic_viewer = False |
|
830 |
if self.TabsOpened.GetPageCount() > 0: |
|
831 |
self.FileMenu.Enable(wx.ID_CLOSE, True) |
|
832 |
if graphic_viewer: |
|
833 |
self.FileMenu.Enable(wx.ID_PREVIEW, True) |
|
834 |
self.FileMenu.Enable(wx.ID_PRINT, True) |
|
835 |
else: |
|
836 |
self.FileMenu.Enable(wx.ID_PREVIEW, False) |
|
837 |
self.FileMenu.Enable(wx.ID_PRINT, False) |
|
838 |
else: |
|
839 |
self.FileMenu.Enable(wx.ID_CLOSE, False) |
|
840 |
self.FileMenu.Enable(wx.ID_PREVIEW, False) |
|
841 |
self.FileMenu.Enable(wx.ID_PRINT, False) |
|
842 |
self.FileMenu.Enable(wx.ID_PAGE_SETUP, True) |
|
500
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
843 |
self.FileMenu.Enable(wx.ID_SAVE, self.PluginRoot.PlugTestModified()) |
442 | 844 |
self.FileMenu.Enable(wx.ID_PROPERTIES, True) |
845 |
else: |
|
846 |
self.FileMenu.Enable(wx.ID_CLOSE, False) |
|
847 |
self.FileMenu.Enable(wx.ID_PAGE_SETUP, False) |
|
848 |
self.FileMenu.Enable(wx.ID_PREVIEW, False) |
|
849 |
self.FileMenu.Enable(wx.ID_PRINT, False) |
|
850 |
self.FileMenu.Enable(wx.ID_SAVE, False) |
|
851 |
self.FileMenu.Enable(wx.ID_PROPERTIES, False) |
|
852 |
||
853 |
def RefreshPLCParams(self): |
|
854 |
self.Freeze() |
|
855 |
self.ClearSizer(self.PLCParamsSizer) |
|
856 |
||
857 |
if self.PluginRoot is not None: |
|
858 |
plcwindow = wx.Panel(self.PLCConfig, -1, size=wx.Size(-1, -1)) |
|
859 |
if self.PluginRoot.PlugTestModified(): |
|
860 |
bkgdclr = CHANGED_TITLE_COLOUR |
|
861 |
else: |
|
862 |
bkgdclr = TITLE_COLOUR |
|
863 |
||
864 |
if self.PluginRoot not in self.PluginInfos: |
|
865 |
self.PluginInfos[self.PluginRoot] = {"right_visible" : False} |
|
866 |
||
867 |
plcwindow.SetBackgroundColour(TITLE_COLOUR) |
|
868 |
plcwindow.Bind(wx.EVT_LEFT_DOWN, self.OnPanelLeftDown) |
|
869 |
self.PLCParamsSizer.AddWindow(plcwindow, 0, border=0, flag=wx.GROW) |
|
870 |
||
871 |
plcwindowsizer = wx.BoxSizer(wx.HORIZONTAL) |
|
872 |
plcwindow.SetSizer(plcwindowsizer) |
|
873 |
||
874 |
st = wx.StaticText(plcwindow, -1) |
|
875 |
st.SetFont(wx.Font(faces["size"], wx.DEFAULT, wx.NORMAL, wx.BOLD, faceName = faces["helv"])) |
|
876 |
st.SetLabel(self.PluginRoot.GetProjectName()) |
|
877 |
plcwindowsizer.AddWindow(st, 0, border=5, flag=wx.ALL|wx.ALIGN_CENTER) |
|
878 |
||
879 |
plcwindowmainsizer = wx.BoxSizer(wx.VERTICAL) |
|
880 |
plcwindowsizer.AddSizer(plcwindowmainsizer, 0, border=5, flag=wx.ALL) |
|
881 |
||
882 |
plcwindowbuttonsizer = wx.BoxSizer(wx.HORIZONTAL) |
|
883 |
plcwindowmainsizer.AddSizer(plcwindowbuttonsizer, 0, border=0, flag=wx.ALIGN_CENTER) |
|
884 |
||
885 |
msizer = self.GenerateMethodButtonSizer(self.PluginRoot, plcwindow, not self.PluginInfos[self.PluginRoot]["right_visible"]) |
|
886 |
plcwindowbuttonsizer.AddSizer(msizer, 0, border=0, flag=wx.GROW) |
|
887 |
||
888 |
self.PLCConfigMainSizer.Layout() |
|
889 |
self.RefreshScrollBars() |
|
890 |
self.Thaw() |
|
891 |
||
892 |
def GenerateTreeBranch(self, plugin): |
|
893 |
leftwindow = wx.Panel(self.PLCConfig, -1, size=wx.Size(-1, -1)) |
|
894 |
if plugin.PlugTestModified(): |
|
895 |
bkgdclr=CHANGED_WINDOW_COLOUR |
|
896 |
else: |
|
897 |
bkgdclr=WINDOW_COLOUR |
|
898 |
||
899 |
leftwindow.SetBackgroundColour(bkgdclr) |
|
900 |
||
901 |
if plugin not in self.PluginInfos: |
|
902 |
self.PluginInfos[plugin] = {"expanded" : False, "left_visible" : False, "right_visible" : False} |
|
903 |
||
904 |
self.PluginInfos[plugin]["children"] = plugin.IECSortedChilds() |
|
905 |
plugin_infos = plugin.GetVariableLocationTree() |
|
906 |
plugin_locations = [] |
|
907 |
if len(self.PluginInfos[plugin]["children"]) == 0: |
|
908 |
plugin_locations = plugin_infos["children"] |
|
909 |
if not self.PluginInfos[plugin].has_key("locations_infos"): |
|
910 |
self.PluginInfos[plugin]["locations_infos"] = {"root": {"expanded" : False}} |
|
911 |
||
912 |
self.PluginInfos[plugin]["locations_infos"]["root"]["children"] = [] |
|
913 |
||
914 |
self.PluginTreeSizer.AddWindow(leftwindow, 0, border=0, flag=wx.GROW) |
|
915 |
||
916 |
leftwindowsizer = wx.BoxSizer(wx.HORIZONTAL) |
|
917 |
leftwindow.SetSizer(leftwindowsizer) |
|
918 |
||
919 |
st = wx.StaticText(leftwindow, -1) |
|
920 |
st.SetFont(wx.Font(faces["size"], wx.DEFAULT, wx.NORMAL, wx.BOLD, faceName = faces["helv"])) |
|
921 |
st.SetLabel(plugin.GetFullIEC_Channel()) |
|
922 |
leftwindowsizer.AddWindow(st, 0, border=5, flag=wx.RIGHT) |
|
923 |
||
924 |
expandbutton_id = wx.NewId() |
|
925 |
expandbutton = wx.lib.buttons.GenBitmapToggleButton(id=expandbutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'plus.png')), |
|
926 |
name='ExpandButton', parent=leftwindow, pos=wx.Point(0, 0), |
|
927 |
size=wx.Size(13, 13), style=wx.NO_BORDER) |
|
928 |
expandbutton.labelDelta = 0 |
|
929 |
expandbutton.SetBezelWidth(0) |
|
930 |
expandbutton.SetUseFocusIndicator(False) |
|
931 |
expandbutton.SetBitmapSelected(wx.Bitmap(Bpath( 'images', 'minus.png'))) |
|
932 |
||
933 |
if len(self.PluginInfos[plugin]["children"]) > 0: |
|
934 |
expandbutton.SetToggle(self.PluginInfos[plugin]["expanded"]) |
|
935 |
def togglebutton(event): |
|
936 |
if expandbutton.GetToggle(): |
|
937 |
self.ExpandPlugin(plugin) |
|
938 |
else: |
|
939 |
self.CollapsePlugin(plugin) |
|
940 |
self.PluginInfos[plugin]["expanded"] = expandbutton.GetToggle() |
|
941 |
self.PLCConfigMainSizer.Layout() |
|
942 |
self.RefreshScrollBars() |
|
943 |
event.Skip() |
|
944 |
expandbutton.Bind(wx.EVT_BUTTON, togglebutton, id=expandbutton_id) |
|
945 |
elif len(plugin_locations) > 0: |
|
946 |
locations_infos = self.PluginInfos[plugin]["locations_infos"] |
|
947 |
expandbutton.SetToggle(locations_infos["root"]["expanded"]) |
|
948 |
def togglebutton(event): |
|
949 |
if expandbutton.GetToggle(): |
|
950 |
self.ExpandLocation(locations_infos, "root") |
|
951 |
else: |
|
952 |
self.CollapseLocation(locations_infos, "root") |
|
953 |
self.PluginInfos[plugin]["expanded"] = expandbutton.GetToggle() |
|
954 |
locations_infos["root"]["expanded"] = expandbutton.GetToggle() |
|
955 |
self.PLCConfigMainSizer.Layout() |
|
956 |
self.RefreshScrollBars() |
|
957 |
event.Skip() |
|
958 |
expandbutton.Bind(wx.EVT_BUTTON, togglebutton, id=expandbutton_id) |
|
959 |
else: |
|
960 |
expandbutton.Enable(False) |
|
961 |
leftwindowsizer.AddWindow(expandbutton, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL) |
|
962 |
||
963 |
sb = wx.StaticBitmap(leftwindow, -1) |
|
964 |
icon = plugin_infos.get("icon", None) |
|
965 |
if icon is None: |
|
966 |
icon = os.path.join(base_folder, "plcopeneditor", 'Images', '%s.png' % self.LOCATION_BITMAP[plugin_infos["type"]]) |
|
967 |
sb.SetBitmap(wx.Bitmap(icon)) |
|
968 |
leftwindowsizer.AddWindow(sb, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL) |
|
969 |
||
970 |
st_id = wx.NewId() |
|
971 |
st = wx.StaticText(leftwindow, st_id, size=wx.DefaultSize, style=wx.NO_BORDER) |
|
972 |
st.SetFont(wx.Font(faces["size"] * 0.75, wx.DEFAULT, wx.NORMAL, wx.BOLD, faceName = faces["helv"])) |
|
973 |
st.SetLabel(plugin.MandatoryParams[1].getName()) |
|
974 |
leftwindowsizer.AddWindow(st, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL) |
|
975 |
||
976 |
rightwindow = wx.Panel(self.PLCConfig, -1, size=wx.Size(-1, -1)) |
|
977 |
rightwindow.SetBackgroundColour(bkgdclr) |
|
978 |
||
979 |
self.PluginTreeSizer.AddWindow(rightwindow, 0, border=0, flag=wx.GROW) |
|
980 |
||
981 |
self.PluginInfos[plugin]["left"] = leftwindow |
|
982 |
self.PluginInfos[plugin]["right"] = rightwindow |
|
983 |
for child in self.PluginInfos[plugin]["children"]: |
|
984 |
self.GenerateTreeBranch(child) |
|
985 |
if not self.PluginInfos[child]["expanded"]: |
|
986 |
self.CollapsePlugin(child) |
|
987 |
if len(plugin_locations) > 0: |
|
988 |
locations_infos = self.PluginInfos[plugin]["locations_infos"] |
|
989 |
for location in plugin_locations: |
|
990 |
locations_infos["root"]["children"].append("root.%s" % location["name"]) |
|
991 |
self.GenerateLocationTreeBranch(locations_infos, "root", location) |
|
992 |
if not locations_infos["root"]["expanded"]: |
|
993 |
self.CollapseLocation(locations_infos, "root") |
|
994 |
||
995 |
frame = None |
|
514
d36a89dcd21c
Bug on LPCBeremiz when wxPopen asking for Yield after reopening of a Beremiz window fixed
laurent
parents:
510
diff
changeset
|
996 |
app = None |
d36a89dcd21c
Bug on LPCBeremiz when wxPopen asking for Yield after reopening of a Beremiz window fixed
laurent
parents:
510
diff
changeset
|
997 |
|
d36a89dcd21c
Bug on LPCBeremiz when wxPopen asking for Yield after reopening of a Beremiz window fixed
laurent
parents:
510
diff
changeset
|
998 |
def GetApp(): |
d36a89dcd21c
Bug on LPCBeremiz when wxPopen asking for Yield after reopening of a Beremiz window fixed
laurent
parents:
510
diff
changeset
|
999 |
global app |
d36a89dcd21c
Bug on LPCBeremiz when wxPopen asking for Yield after reopening of a Beremiz window fixed
laurent
parents:
510
diff
changeset
|
1000 |
return app |
d36a89dcd21c
Bug on LPCBeremiz when wxPopen asking for Yield after reopening of a Beremiz window fixed
laurent
parents:
510
diff
changeset
|
1001 |
wx.GetApp = GetApp |
442 | 1002 |
|
1003 |
def BeremizStartProc(plugin_root): |
|
514
d36a89dcd21c
Bug on LPCBeremiz when wxPopen asking for Yield after reopening of a Beremiz window fixed
laurent
parents:
510
diff
changeset
|
1004 |
global frame, app |
442 | 1005 |
|
1006 |
app = wx.PySimpleApp() |
|
1007 |
app.SetAppName('beremiz') |
|
1008 |
wx.InitAllImageHandlers() |
|
1009 |
||
1010 |
# Get the english language |
|
1011 |
langid = wx.LANGUAGE_ENGLISH |
|
1012 |
# Import module for internationalization |
|
1013 |
import gettext |
|
1014 |
import __builtin__ |
|
1015 |
||
1016 |
# Define locale for wx |
|
1017 |
loc = wx.Locale(langid) |
|
1018 |
for localedir in PseudoLocale.LocaleDirs: |
|
1019 |
loc.AddCatalogLookupPathPrefix(localedir) |
|
1020 |
for domain in PseudoLocale.Domains: |
|
1021 |
loc.AddCatalog(domain) |
|
1022 |
||
1023 |
__builtin__.__dict__['_'] = wx.GetTranslation#unicode_translation |
|
1024 |
||
1025 |
# Install a exception handle for bug reports |
|
1026 |
AddExceptHook(os.getcwd(),__version__) |
|
1027 |
||
492 | 1028 |
frame = LPCBeremiz(None, plugin_root=plugin_root, debug=True) |
442 | 1029 |
plugin_root.SetAppFrame(frame, frame.Log) |
1030 |
frame.Show() |
|
445 | 1031 |
frame.Raise() |
442 | 1032 |
|
1033 |
app.MainLoop() |
|
1034 |
||
445 | 1035 |
frame = None |
514
d36a89dcd21c
Bug on LPCBeremiz when wxPopen asking for Yield after reopening of a Beremiz window fixed
laurent
parents:
510
diff
changeset
|
1036 |
app = None |
445 | 1037 |
|
442 | 1038 |
class StdoutPseudoFile: |
1039 |
""" Base class for file like objects to facilitate StdOut for the Shell.""" |
|
1040 |
def write(self, s, style = None): |
|
1041 |
if s != '': |
|
1042 |
print s |
|
1043 |
||
1044 |
def write_warning(self, s): |
|
1045 |
self.write(s) |
|
1046 |
||
1047 |
def write_error(self, s): |
|
1048 |
self.write(s) |
|
1049 |
||
1050 |
def flush(self): |
|
1051 |
pass |
|
1052 |
||
1053 |
def isatty(self): |
|
1054 |
return false |
|
1055 |
||
1056 |
if __name__ == '__main__': |
|
1057 |
||
1058 |
from threading import Thread, Timer |
|
1059 |
import cmd |
|
1060 |
||
1061 |
class LPCBeremiz_Cmd(cmd.Cmd): |
|
1062 |
||
1063 |
prompt = "" |
|
1064 |
Log = StdoutPseudoFile() |
|
1065 |
RefreshTimer = None |
|
1066 |
||
1067 |
def __init__(self, projectOpen, buildpath): |
|
1068 |
cmd.Cmd.__init__(self) |
|
1069 |
self.PluginRoot = LPCPluginsRoot(None, self.Log) |
|
478 | 1070 |
if projectOpen is not None and os.path.isdir(projectOpen): |
442 | 1071 |
result = self.PluginRoot.LoadProject(projectOpen, buildpath) |
1072 |
if result: |
|
1073 |
print "Error: Invalid project directory", result |
|
1074 |
else: |
|
1075 |
print "Error: No such file or directory" |
|
1076 |
||
1077 |
def RestartTimer(self): |
|
1078 |
if self.RefreshTimer is not None: |
|
1079 |
self.RefreshTimer.cancel() |
|
1080 |
self.RefreshTimer = Timer(0.1, self.Refresh) |
|
1081 |
self.RefreshTimer.start() |
|
1082 |
||
1083 |
def Exit(self): |
|
1084 |
self.Close() |
|
1085 |
return True |
|
1086 |
||
1087 |
def do_EOF(self, line): |
|
1088 |
return self.Exit() |
|
1089 |
||
1090 |
def Show(self): |
|
445 | 1091 |
global frame |
1092 |
if frame is None: |
|
1093 |
beremiz_thread=Thread(target=BeremizStartProc, args=[self.PluginRoot]) |
|
1094 |
beremiz_thread.start() |
|
442 | 1095 |
|
1096 |
def Refresh(self): |
|
1097 |
global frame |
|
1098 |
if frame is not None: |
|
444
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
1099 |
wx.CallAfter(frame._Refresh, TITLE, INSTANCESTREE, FILEMENU, EDITMENU) |
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
1100 |
wx.CallAfter(frame.RefreshEditor) |
442 | 1101 |
wx.CallAfter(frame.RefreshAll) |
1102 |
||
1103 |
def Close(self): |
|
1104 |
global frame |
|
1105 |
||
1106 |
self.PluginRoot.ResetAppFrame(self.Log) |
|
1107 |
if frame is not None: |
|
1108 |
wx.CallAfter(frame.Close) |
|
1109 |
||
1110 |
def Compile(self): |
|
1111 |
if wx.GetApp() is None: |
|
1112 |
self.PluginRoot._build() |
|
1113 |
else: |
|
1114 |
wx.CallAfter(self.PluginRoot._build) |
|
1115 |
||
500
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
1116 |
def SetProjectProperties(self, projectname, productname, productversion, companyname): |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
1117 |
properties = self.PluginRoot.GetProjectProperties() |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
1118 |
properties["projectName"] = projectname |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
1119 |
properties["productName"] = productname |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
1120 |
properties["productVersion"] = productversion |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
1121 |
properties["companyName"] = companyname |
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
1122 |
self.PluginRoot.SetProjectProperties(properties=properties) |
442 | 1123 |
self.RestartTimer() |
1124 |
||
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
1125 |
def SetOnlineMode(self, mode, path=None): |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
1126 |
self.PluginRoot.SetOnlineMode(mode, path) |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
1127 |
self.RestartTimer() |
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
1128 |
|
442 | 1129 |
def AddBus(self, iec_channel, name, icon=None): |
1130 |
for child in self.PluginRoot.IterChilds(): |
|
1131 |
if child.BaseParams.getName() == name: |
|
1132 |
return "Error: A bus named %s already exists" % name |
|
1133 |
elif child.BaseParams.getIEC_Channel() == iec_channel: |
|
1134 |
return "Error: A bus with IEC_channel %d already exists" % iec_channel |
|
1135 |
bus = self.PluginRoot.PlugAddChild(name, "LPCBus", iec_channel) |
|
1136 |
if bus is None: |
|
1137 |
return "Error: Unable to create bus" |
|
1138 |
bus.SetIcon(icon) |
|
1139 |
self.RestartTimer() |
|
1140 |
||
1141 |
def RenameBus(self, iec_channel, name): |
|
1142 |
bus = self.PluginRoot.GetChildByIECLocation((iec_channel,)) |
|
1143 |
if bus is None: |
|
1144 |
return "Error: No bus found" |
|
1145 |
for child in self.PluginRoot.IterChilds(): |
|
1146 |
if child != bus and child.BaseParams.getName() == name: |
|
1147 |
return "Error: A bus named %s already exists" % name |
|
1148 |
bus.BaseParams.setName(name) |
|
1149 |
self.RestartTimer() |
|
1150 |
||
1151 |
def ChangeBusIECChannel(self, old_iec_channel, new_iec_channel): |
|
1152 |
bus = self.PluginRoot.GetChildByIECLocation((old_iec_channel,)) |
|
1153 |
if bus is None: |
|
1154 |
return "Error: No bus found" |
|
1155 |
for child in self.PluginRoot.IterChilds(): |
|
1156 |
if child != bus and child.BaseParams.getIEC_Channel() == new_iec_channel: |
|
1157 |
return "Error: A bus with IEC_channel %d already exists" % new_iec_channel |
|
444
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
1158 |
self.PluginRoot.UpdateProjectVariableLocation(str(old_iec_channel), str(new_iec_channel)) |
442 | 1159 |
bus.BaseParams.setIEC_Channel(new_iec_channel) |
1160 |
self.RestartTimer() |
|
1161 |
||
1162 |
def RemoveBus(self, iec_channel): |
|
1163 |
bus = self.PluginRoot.GetChildByIECLocation((iec_channel,)) |
|
1164 |
if bus is None: |
|
1165 |
return "Error: No bus found" |
|
444
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
1166 |
self.PluginRoot.RemoveProjectVariableByFilter(str(iec_channel)) |
442 | 1167 |
self.PluginRoot.PluggedChilds["LPCBus"].remove(bus) |
1168 |
self.RestartTimer() |
|
1169 |
||
1170 |
def AddModule(self, parent, iec_channel, name, icon=None): |
|
1171 |
module = self.PluginRoot.GetChildByIECLocation(parent) |
|
1172 |
if module is None: |
|
1173 |
return "Error: No parent found" |
|
1174 |
for child in _GetModuleChildren(module): |
|
1175 |
if child["name"] == name: |
|
1176 |
return "Error: A module named %s already exists" % name |
|
1177 |
elif child["IEC_Channel"] == iec_channel: |
|
1178 |
return "Error: A module with IEC_channel %d already exists" % iec_channel |
|
1179 |
_GetLastModuleGroup(module).append({"name": name, |
|
1180 |
"type": LOCATION_MODULE, |
|
1181 |
"IEC_Channel": iec_channel, |
|
1182 |
"icon": icon, |
|
1183 |
"children": []}) |
|
1184 |
self.RestartTimer() |
|
1185 |
||
1186 |
def RenameModule(self, iec_location, name): |
|
1187 |
module = self.PluginRoot.GetChildByIECLocation(iec_location) |
|
1188 |
if module is None: |
|
1189 |
return "Error: No module found" |
|
1190 |
parent = self.PluginRoot.GetChildByIECLocation(iec_location[:-1]) |
|
1191 |
if parent is self.PluginRoot: |
|
1192 |
return "Error: No module found" |
|
1193 |
if module["name"] != name: |
|
1194 |
for child in _GetModuleChildren(parent): |
|
1195 |
if child["name"] == name: |
|
1196 |
return "Error: A module named %s already exists" % name |
|
1197 |
module["name"] = name |
|
1198 |
self.RestartTimer() |
|
1199 |
||
1200 |
def ChangeModuleIECChannel(self, old_iec_location, new_iec_channel): |
|
1201 |
module = self.PluginRoot.GetChildByIECLocation(old_iec_location) |
|
1202 |
if module is None: |
|
1203 |
return "Error: No module found" |
|
1204 |
parent = self.PluginRoot.GetChildByIECLocation(old_iec_location[:-1]) |
|
1205 |
if parent is self.PluginRoot: |
|
1206 |
return "Error: No module found" |
|
1207 |
if module["IEC_Channel"] != new_iec_channel: |
|
1208 |
for child in _GetModuleChildren(parent): |
|
1209 |
if child["IEC_Channel"] == new_iec_channel: |
|
1210 |
return "Error: A module with IEC_channel %d already exists" % new_iec_channel |
|
444
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
1211 |
self.PluginRoot.UpdateProjectVariableLocation(".".join(map(str, old_iec_location)), ".".join(map(str, old_iec_location[:1] + (new_iec_channel,)))) |
442 | 1212 |
module["IEC_Channel"] = new_iec_channel |
1213 |
self.RestartTimer() |
|
1214 |
||
1215 |
def RemoveModule(self, parent, iec_channel): |
|
1216 |
module = self.PluginRoot.GetChildByIECLocation(parent) |
|
1217 |
if module is None: |
|
1218 |
return "Error: No parent found" |
|
1219 |
child = _GetModuleBySomething(module, "IEC_Channel", (iec_channel,)) |
|
1220 |
if child is None: |
|
1221 |
return "Error: No module found" |
|
444
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
1222 |
self.PluginRoot.RemoveProjectVariableByFilter(".".join(map(str, parent + (iec_channel,)))) |
442 | 1223 |
_RemoveModuleChild(module, child) |
1224 |
self.RestartTimer() |
|
1225 |
||
1226 |
def StartGroup(self, parent, name, icon=None): |
|
1227 |
module = self.PluginRoot.GetChildByIECLocation(parent) |
|
1228 |
if module is None: |
|
1229 |
return "Error: No parent found" |
|
1230 |
for child in module["children"]: |
|
1231 |
if child["type"] == LOCATION_GROUP and child["name"] == name: |
|
1232 |
return "Error: A group named %s already exists" % name |
|
1233 |
module["children"].append({"name": name, |
|
1234 |
"type": LOCATION_GROUP, |
|
1235 |
"icon": icon, |
|
1236 |
"children": []}) |
|
1237 |
self.RestartTimer() |
|
1238 |
||
1239 |
def AddVariable(self, parent, location, name, direction, type, dcode, rcode, pcode, description=""): |
|
1240 |
module = self.PluginRoot.GetChildByIECLocation(parent) |
|
1241 |
if module is None: |
|
1242 |
return "Error: No parent found" |
|
1243 |
for child in _GetModuleChildren(module): |
|
1244 |
if child["name"] == name: |
|
1245 |
return "Error: A variable named %s already exists" % name |
|
472
80eb3bde98e3
Adding support for defining input and output with the same location in LPCBeremiz
laurent
parents:
445
diff
changeset
|
1246 |
if child["location"] == location and child["type"] == LOCATION_TYPES[direction]: |
442 | 1247 |
return "Error: A variable with location %s already exists" % ".".join(map(str, location)) |
1248 |
_GetLastModuleGroup(module).append({"name": name, |
|
1249 |
"location": location, |
|
1250 |
"type": LOCATION_TYPES[direction], |
|
1251 |
"IEC_type": type, |
|
1252 |
"description": description, |
|
1253 |
"declare": dcode, |
|
1254 |
"retrieve": rcode, |
|
1255 |
"publish": pcode}) |
|
1256 |
self.RestartTimer() |
|
1257 |
||
1258 |
def ChangeVariableParams(self, parent, location, new_name, new_direction, new_type, new_dcode, new_rcode, new_pcode, new_description=None): |
|
1259 |
module = self.PluginRoot.GetChildByIECLocation(parent) |
|
1260 |
if module is None: |
|
1261 |
return "Error: No parent found" |
|
1262 |
variable = None |
|
1263 |
for child in _GetModuleChildren(module): |
|
472
80eb3bde98e3
Adding support for defining input and output with the same location in LPCBeremiz
laurent
parents:
445
diff
changeset
|
1264 |
if child["location"] == location and child["type"] == LOCATION_TYPES[new_direction]: |
442 | 1265 |
variable = child |
1266 |
elif child["name"] == new_name: |
|
1267 |
return "Error: A variable named %s already exists" % new_name |
|
1268 |
if variable is None: |
|
1269 |
return "Error: No variable found" |
|
444
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
1270 |
if variable["name"] != new_name: |
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
1271 |
self.PluginRoot.UpdateProjectVariableName(variable["name"], new_name) |
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
1272 |
variable["name"] = new_name |
442 | 1273 |
variable["type"] = LOCATION_TYPES[new_direction] |
1274 |
variable["IEC_type"] = new_type |
|
1275 |
variable["declare"] = new_dcode |
|
1276 |
variable["retrieve"] = new_rcode |
|
1277 |
variable["publish"] = new_pcode |
|
1278 |
if new_description is not None: |
|
1279 |
variable["description"] = new_description |
|
1280 |
self.RestartTimer() |
|
1281 |
||
472
80eb3bde98e3
Adding support for defining input and output with the same location in LPCBeremiz
laurent
parents:
445
diff
changeset
|
1282 |
def RemoveVariable(self, parent, location, direction): |
442 | 1283 |
module = self.PluginRoot.GetChildByIECLocation(parent) |
1284 |
if module is None: |
|
1285 |
return "Error: No parent found" |
|
472
80eb3bde98e3
Adding support for defining input and output with the same location in LPCBeremiz
laurent
parents:
445
diff
changeset
|
1286 |
child = _GetModuleVariable(module, location, direction) |
442 | 1287 |
if child is None: |
1288 |
return "Error: No variable found" |
|
444
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
1289 |
size = LOCATION_SIZES[self.PluginRoot.GetBaseType(child["IEC_type"])] |
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
1290 |
address = "%" + LOCATION_DIRS[child["type"]] + size + ".".join(map(str, parent + location)) |
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
1291 |
self.PluginRoot.RemoveProjectVariableByAddress(address) |
442 | 1292 |
_RemoveModuleChild(module, child) |
1293 |
self.RestartTimer() |
|
1294 |
||
1295 |
def location(loc): |
|
1296 |
return tuple(map(int, loc.split("."))) |
|
1297 |
||
1298 |
def GetCmdFunction(function, arg_types, opt=0): |
|
1299 |
arg_number = len(arg_types) |
|
1300 |
def CmdFunction(self, line): |
|
1301 |
args_toks = line.split('"') |
|
1302 |
if len(args_toks) % 2 == 0: |
|
1303 |
print "Error: Invalid command" |
|
1304 |
return |
|
1305 |
args = [] |
|
1306 |
for num, arg in enumerate(args_toks): |
|
1307 |
if num % 2 == 0: |
|
1308 |
stripped = arg.strip() |
|
1309 |
if stripped: |
|
1310 |
args.extend(stripped.split(" ")) |
|
1311 |
else: |
|
1312 |
args.append(arg) |
|
1313 |
number = None |
|
1314 |
extra = "" |
|
1315 |
if opt == 0 and len(args) != arg_number: |
|
1316 |
number = arg_number |
|
1317 |
elif len(args) > arg_number: |
|
1318 |
number = arg_number |
|
1319 |
extra = " at most" |
|
1320 |
elif len(args) < arg_number - opt: |
|
1321 |
number = arg_number - opt |
|
1322 |
extra = " at least" |
|
1323 |
if number is not None: |
|
1324 |
if number == 0: |
|
1325 |
print "Error: No argument%s expected" % extra |
|
1326 |
elif number == 1: |
|
1327 |
print "Error: 1 argument%s expected" % extra |
|
1328 |
else: |
|
1329 |
print "Error: %d arguments%s expected" % (number, extra) |
|
1330 |
return |
|
1331 |
for num, arg in enumerate(args): |
|
1332 |
try: |
|
1333 |
args[num] = arg_types[num](arg) |
|
1334 |
except: |
|
1335 |
print "Error: Invalid value for argument %d" % (num + 1) |
|
1336 |
return |
|
1337 |
res = getattr(self, function)(*args) |
|
1338 |
if isinstance(res, (StringType, UnicodeType)): |
|
1339 |
print res |
|
1340 |
return False |
|
1341 |
else: |
|
1342 |
return res |
|
1343 |
return CmdFunction |
|
1344 |
||
1345 |
for function, (arg_types, opt) in {"Exit": ([], 0), |
|
1346 |
"Show": ([], 0), |
|
1347 |
"Refresh": ([], 0), |
|
1348 |
"Close": ([], 0), |
|
1349 |
"Compile": ([], 0), |
|
500
af7c28de4cc7
Adding support for defining mandatory parameters and creating blank xml file when there is nothing in the folder in LPCBeremiz
laurent
parents:
494
diff
changeset
|
1350 |
"SetProjectProperties": ([str, str, str, str], 0), |
487
1df4a28d3586
Adding support for receiving PLC state from LPCComposer
laurent
parents:
478
diff
changeset
|
1351 |
"SetOnlineMode": ([int, str], 1), |
442 | 1352 |
"AddBus": ([int, str, str], 1), |
1353 |
"RenameBus": ([int, str], 0), |
|
1354 |
"ChangeBusIECChannel": ([int, int], 0), |
|
1355 |
"RemoveBus": ([int], 0), |
|
1356 |
"AddModule": ([location, int, str, str], 1), |
|
1357 |
"RenameModule": ([location, str], 0), |
|
1358 |
"ChangeModuleIECChannel": ([location, int], 0), |
|
1359 |
"RemoveModule": ([location, int], 0), |
|
1360 |
"StartGroup": ([location, str, str], 1), |
|
444
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
1361 |
"AddVariable": ([location, location, str, str, str, str, str, str, str], 1), |
8eb1186fc9cf
Adding support for updating or removing located variables by their address or leading address numbers when changing or removing VariableTree element
laurent
parents:
442
diff
changeset
|
1362 |
"ChangeVariableParams": ([location, location, str, str, str, str, str, str, str], 1), |
442 | 1363 |
"RemoveVariable": ([location, location], 0)}.iteritems(): |
1364 |
||
1365 |
setattr(LPCBeremiz_Cmd, "do_%s" % function, GetCmdFunction(function, arg_types, opt)) |
|
1366 |
||
1367 |
lpcberemiz_cmd = LPCBeremiz_Cmd(projectOpen, buildpath) |
|
1368 |
lpcberemiz_cmd.cmdloop() |
|
1369 |