author | Laurent Bessard |
Thu, 18 Apr 2013 21:30:20 +0200 | |
changeset 1042 | 6dbdc6844eb9 |
parent 1034 | 078bf153fb8c |
child 1045 | a220a27defe5 |
permissions | -rwxr-xr-x |
641 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of Beremiz, a Integrated Development Environment for |
|
5 |
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
|
6 |
# |
|
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
import os, sys, getopt |
|
837
fb0b66e9b4dd
Runtime Quit should not crash when plc noyt stopped
Edouard Tisserant
parents:
836
diff
changeset
|
26 |
from threading import Thread,Timer |
641 | 27 |
|
28 |
def usage(): |
|
29 |
print """ |
|
30 |
Usage of Beremiz PLC execution service :\n |
|
644
b511cab580eb
Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents:
641
diff
changeset
|
31 |
%s {[-n servicename] [-i IP] [-p port] [-x enabletaskbar] [-a autostart]|-h|--help} working_dir |
641 | 32 |
-n - zeroconf service name (default:disabled) |
644
b511cab580eb
Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents:
641
diff
changeset
|
33 |
-i - IP address of interface to bind to (default:localhost) |
641 | 34 |
-p - port number default:3000 |
35 |
-h - print this help text and quit |
|
36 |
-a - autostart PLC (0:disable 1:enable) (default:0) |
|
37 |
-x - enable/disable wxTaskbarIcon (0:disable 1:enable) (default:1) |
|
38 |
-t - enable/disable Twisted web interface (0:disable 1:enable) (default:1) |
|
39 |
||
40 |
working_dir - directory where are stored PLC files |
|
41 |
"""%sys.argv[0] |
|
42 |
||
43 |
try: |
|
44 |
opts, argv = getopt.getopt(sys.argv[1:], "i:p:n:x:t:a:h") |
|
45 |
except getopt.GetoptError, err: |
|
46 |
# print help information and exit: |
|
47 |
print str(err) # will print something like "option -a not recognized" |
|
48 |
usage() |
|
49 |
sys.exit(2) |
|
50 |
||
51 |
# default values |
|
644
b511cab580eb
Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents:
641
diff
changeset
|
52 |
given_ip = None |
641 | 53 |
port = 3000 |
54 |
servicename = None |
|
55 |
autostart = False |
|
56 |
enablewx = True |
|
57 |
havewx = False |
|
58 |
enabletwisted = True |
|
59 |
havetwisted = False |
|
60 |
||
61 |
for o, a in opts: |
|
62 |
if o == "-h": |
|
63 |
usage() |
|
64 |
sys.exit() |
|
65 |
elif o == "-i": |
|
66 |
if len(a.split(".")) == 4 or a == "localhost": |
|
644
b511cab580eb
Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents:
641
diff
changeset
|
67 |
given_ip = a |
b511cab580eb
Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents:
641
diff
changeset
|
68 |
else: |
b511cab580eb
Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents:
641
diff
changeset
|
69 |
usage() |
b511cab580eb
Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents:
641
diff
changeset
|
70 |
sys.exit() |
641 | 71 |
elif o == "-p": |
72 |
# port: port that the service runs on |
|
73 |
port = int(a) |
|
74 |
elif o == "-n": |
|
75 |
servicename = a |
|
76 |
elif o == "-x": |
|
77 |
enablewx = int(a) |
|
78 |
elif o == "-t": |
|
79 |
enabletwisted = int(a) |
|
80 |
elif o == "-a": |
|
81 |
autostart = int(a) |
|
82 |
else: |
|
83 |
usage() |
|
84 |
sys.exit() |
|
85 |
||
86 |
if len(argv) > 1: |
|
87 |
usage() |
|
88 |
sys.exit() |
|
89 |
elif len(argv) == 1: |
|
90 |
WorkingDir = argv[0] |
|
91 |
os.chdir(WorkingDir) |
|
92 |
elif len(argv) == 0: |
|
93 |
WorkingDir = os.getcwd() |
|
94 |
argv=[WorkingDir] |
|
95 |
||
96 |
import __builtin__ |
|
97 |
if __name__ == '__main__': |
|
98 |
__builtin__.__dict__['_'] = lambda x: x |
|
99 |
||
100 |
if enablewx: |
|
101 |
try: |
|
102 |
import wx, re |
|
103 |
from threading import Thread, currentThread |
|
104 |
from types import * |
|
105 |
havewx = True |
|
106 |
except: |
|
107 |
print "Wx unavailable !" |
|
108 |
havewx = False |
|
109 |
||
110 |
if havewx: |
|
111 |
app=wx.App(redirect=False) |
|
112 |
||
113 |
# Import module for internationalization |
|
114 |
import gettext |
|
115 |
||
116 |
CWD = os.path.split(os.path.realpath(__file__))[0] |
|
117 |
||
118 |
# Get folder containing translation files |
|
119 |
localedir = os.path.join(CWD,"locale") |
|
120 |
# Get the default language |
|
121 |
langid = wx.LANGUAGE_DEFAULT |
|
122 |
# Define translation domain (name of translation files) |
|
123 |
domain = "Beremiz" |
|
124 |
||
125 |
# Define locale for wx |
|
126 |
loc = __builtin__.__dict__.get('loc', None) |
|
127 |
if loc is None: |
|
128 |
loc = wx.Locale(langid) |
|
129 |
__builtin__.__dict__['loc'] = loc |
|
130 |
# Define location for searching translation files |
|
131 |
loc.AddCatalogLookupPathPrefix(localedir) |
|
132 |
# Define locale domain |
|
133 |
loc.AddCatalog(domain) |
|
134 |
||
135 |
def unicode_translation(message): |
|
136 |
return wx.GetTranslation(message).encode("utf-8") |
|
137 |
||
138 |
if __name__ == '__main__': |
|
139 |
__builtin__.__dict__['_'] = wx.GetTranslation#unicode_translation |
|
140 |
||
141 |
try: |
|
142 |
from wx.lib.embeddedimage import PyEmbeddedImage |
|
143 |
except: |
|
144 |
import cStringIO |
|
145 |
import base64 |
|
146 |
||
147 |
class PyEmbeddedImage: |
|
148 |
def __init__(self, image_string): |
|
149 |
stream = cStringIO.StringIO(base64.b64decode(image_string)) |
|
150 |
self.Image = wx.ImageFromStream(stream) |
|
151 |
def GetImage(self): |
|
152 |
return self.Image |
|
153 |
||
154 |
defaulticon = PyEmbeddedImage( |
|
155 |
"iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAABc5J" |
|
156 |
"REFUSIl9lW1MW9cZx3/n2vf6BQO2MZiXGBISILCVUEUlitYpjaKpXZJ1XZZ2kzJVY9r6IeLD" |
|
157 |
"pGTaNG3KtGmNNGlbpW3VFhRp0l6aZCllpVUqtVNJtBFKE5QXLxCjpCYEY7DBr9hcm3vPPgQY" |
|
158 |
"IQmPdKR7/vd5/v/n5dxzhZSSNeYBOoGDQGcoFPINDAyUDQ0NOUdGRmyGYSiBQGCpoaGhuGnT" |
|
159 |
"psShQ4f6WltbewEBVAK3gCBgrjJKKZFSKlLKeillt5Ty40gkMnnw4MFFQG60ysrKZHd3dyoe" |
|
160 |
"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p9kk7uep+ueWcWj6f8Xqioq8ck0xcIS6XT6vpRy3gqMqKpqRBfKLLNF1ZRV6YBiPDrw" |
|
174 |
"vduefwTL6hl6b74FgFVR0T4rJTU3jcvlymcymal8Ph+z9vf3p7u6uv5y/vz5bw994ld2fmUH" |
|
175 |
"7nYFRVG4Gb3Guv8FpmmQzCcIJ+5w8c5HRFL3UYRC+ZKX633j6LpObW3tDcMwrsODq4Jbt27V" |
|
176 |
"HT58+N7o6KgCYHfY2f2lXfi+6CJbnsAwjUeyXzFFKLgdHqb+mmL8xh22bduWmJycfHN2dvbX" |
|
177 |
"uVwuoQC0tbXlKisrYytBi/lFZsKzOErtTyQWCOxWO36ljvl/FLk+dJOSkhJTUZR35+fn+3K5" |
|
178 |
"XAIeXNcASz6fbxzwrxDYVQdqpARvs498IYchDUxpogiBVVFxqE7U/5Zx4c8fEo/FKS0tlR0d" |
|
179 |
"HZ8ODg6+l06nr6zwrAp4PJ6Qpmlf2L9/fywYDFaOXB0RI1dHaGpuoq29Fa1Uxe62YeZMInei" |
|
180 |
"jAY/IRqNAtDZ2blUV1fXPzg4+F5VVdU/H6p0eYjqsWPHvnz37t0XwuHw7d27d4eTyeTvLl26" |
|
181 |
"FJiamnpim6qrq9mzZ094fHz875FI5J3p6ekr631WBARgaWlpCezYsePeuXPnzFAo5Dp58uS+" |
|
182 |
"dDp91GKxNBYKBW82m3Vomqa7XK7pbDYbnJmZuR2LxYL5fP79WCyWeeys1h/D9e97enqsp06d" |
|
183 |
"8mWzWU+xWPTkcjmXaZpxwzDCsVhsbqNggP8BMJOU3UUUf+0AAAAASUVORK5CYII=") |
|
184 |
||
185 |
#---------------------------------------------------------------------- |
|
186 |
starticon = PyEmbeddedImage( |
|
187 |
"iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAABbpJ" |
|
188 |
"REFUSIl9lltsFNcdxn9nZnbHs15fd23j9TXYC0UCKzEhMQ+oIS2g1kQ1pb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IwP" |
|
215 |
"Dw/HnwT4P6CDl+TMvD0JAAAAAElFTkSuQmCC") |
|
216 |
||
217 |
#---------------------------------------------------------------------- |
|
218 |
stopicon = PyEmbeddedImage( |
|
219 |
"iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAABPRJ" |
|
220 |
"REFUSImdlllsVGUUx3/f/e4sd5iZLjNt6XSFdtgkjWFRePABDaCBGgjamIg81CU0aoxbRHww" |
|
221 |
"+EDkhWjEB5rYGEMUxQTCJg8EoQ2BbgrFCNJWltplgC63naEzd+bO50NLLVAq4STfwz3nfP/f" |
|
222 |
"PSf3O98VSikmmQ94HFgDLDdNM1BfX5955swZX0tLi5FKpbSSkpJkaWlpIhQKdVdVVX2XkZFx" |
|
223 |
"EpBAEGgHLgH/iSqlUEoJpVSBUqpaKXXYNM0r1dXVt6WUajx5ylVYWKi2bdvWY1nWUaXUgQ0b" |
|
224 |
"NtRWVFQUjmuilEKMV5ALvAhsPHDgQFlNTU2gr69Pk1JSFMphTomfRXO8+A243i/oG9I5f6mX" |
|
225 |
"K1evAxAOhykrKxs9duyYkZ+f/0lPT8/2OwXogBtYDKzYunVr0c6dO3Ns26akMIcdbxQyv0hy" |
|
226 |
"rwmh8Bas5/eb89nxRR1NTU20t7cb4x1ZPjlXB2YBiw8ePJhdW1tb4DEMXng6xJtrPQhxn/Y4" |
|
227 |
"QSM12o89fJnOjst3hXRdLxZCGEqpUQANmBuJRLK3bNmy1OFwUPVMPm9VTiMOqLRNYvg6+shv" |
|
228 |
"rFoWwutxTcSklGEpZXDiXZRSr6xbt+6dtra2xUW5Lr7c7EUD3Flhwmu/nRKQGO7CvHaCwY7D" |
|
229 |
"WNEeEmoGe0+PWnuOXHWmrBTl5eW7GxsbNwPoFy5ccHV2di7yzjD4uMqDNtFngZDOKQHurDLc" |
|
230 |
"WWX4Qk/ScfRVXCLGoorU8J+z5gbjxyWGYbwshPhQKTWi7d+/P9/pdIp5xR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 |
||
245 |
class ParamsEntryDialog(wx.TextEntryDialog): |
|
246 |
if wx.VERSION < (2, 6, 0): |
|
247 |
def Bind(self, event, function, id = None): |
|
248 |
if id is not None: |
|
249 |
event(self, id, function) |
|
250 |
else: |
|
251 |
event(self, function) |
|
252 |
||
253 |
||
254 |
def __init__(self, parent, message, caption = "Please enter text", defaultValue = "", |
|
255 |
style = wx.OK|wx.CANCEL|wx.CENTRE, pos = wx.DefaultPosition): |
|
256 |
wx.TextEntryDialog.__init__(self, parent, message, caption, defaultValue, style, pos) |
|
257 |
||
258 |
self.Tests = [] |
|
259 |
if wx.VERSION >= (2, 8, 0): |
|
260 |
self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetAffirmativeId()) |
|
261 |
elif wx.VERSION >= (2, 6, 0): |
|
262 |
self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetAffirmativeButton().GetId()) |
|
263 |
else: |
|
264 |
self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId()) |
|
265 |
||
266 |
def OnOK(self, event): |
|
267 |
value = self.GetValue() |
|
268 |
texts = {"value" : value} |
|
269 |
for function, message in self.Tests: |
|
270 |
if not function(value): |
|
271 |
message = wx.MessageDialog(self, message%texts, _("Error"), wx.OK|wx.ICON_ERROR) |
|
272 |
message.ShowModal() |
|
273 |
message.Destroy() |
|
274 |
return |
|
275 |
self.EndModal(wx.ID_OK) |
|
276 |
event.Skip() |
|
277 |
||
278 |
def GetValue(self): |
|
279 |
return self.GetSizer().GetItem(1).GetWindow().GetValue() |
|
280 |
||
281 |
def SetTests(self, tests): |
|
282 |
self.Tests = tests |
|
283 |
||
284 |
class BeremizTaskBarIcon(wx.TaskBarIcon): |
|
285 |
TBMENU_START = wx.NewId() |
|
286 |
TBMENU_STOP = wx.NewId() |
|
287 |
TBMENU_CHANGE_NAME = wx.NewId() |
|
288 |
TBMENU_CHANGE_PORT = wx.NewId() |
|
289 |
TBMENU_CHANGE_INTERFACE = wx.NewId() |
|
290 |
TBMENU_LIVE_SHELL = wx.NewId() |
|
291 |
TBMENU_WXINSPECTOR = wx.NewId() |
|
292 |
TBMENU_CHANGE_WD = wx.NewId() |
|
293 |
TBMENU_QUIT = wx.NewId() |
|
294 |
||
835
8145be14a2ae
Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents:
719
diff
changeset
|
295 |
def __init__(self, pyroserver, level): |
641 | 296 |
wx.TaskBarIcon.__init__(self) |
297 |
self.pyroserver = pyroserver |
|
298 |
# Set the image |
|
299 |
self.UpdateIcon(None) |
|
835
8145be14a2ae
Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents:
719
diff
changeset
|
300 |
self.level = level |
641 | 301 |
|
302 |
# bind some events |
|
303 |
self.Bind(wx.EVT_MENU, self.OnTaskBarStartPLC, id=self.TBMENU_START) |
|
304 |
self.Bind(wx.EVT_MENU, self.OnTaskBarStopPLC, id=self.TBMENU_STOP) |
|
305 |
self.Bind(wx.EVT_MENU, self.OnTaskBarChangeName, id=self.TBMENU_CHANGE_NAME) |
|
306 |
self.Bind(wx.EVT_MENU, self.OnTaskBarChangeInterface, id=self.TBMENU_CHANGE_INTERFACE) |
|
307 |
self.Bind(wx.EVT_MENU, self.OnTaskBarLiveShell, id=self.TBMENU_LIVE_SHELL) |
|
308 |
self.Bind(wx.EVT_MENU, self.OnTaskBarWXInspector, id=self.TBMENU_WXINSPECTOR) |
|
309 |
self.Bind(wx.EVT_MENU, self.OnTaskBarChangePort, id=self.TBMENU_CHANGE_PORT) |
|
310 |
self.Bind(wx.EVT_MENU, self.OnTaskBarChangeWorkingDir, id=self.TBMENU_CHANGE_WD) |
|
311 |
self.Bind(wx.EVT_MENU, self.OnTaskBarQuit, id=self.TBMENU_QUIT) |
|
312 |
||
313 |
def CreatePopupMenu(self): |
|
314 |
""" |
|
315 |
This method is called by the base class when it needs to popup |
|
316 |
the menu for the default EVT_RIGHT_DOWN event. Just create |
|
317 |
the menu how you want it and return it from this function, |
|
318 |
the base class takes care of the rest. |
|
319 |
""" |
|
320 |
menu = wx.Menu() |
|
321 |
menu.Append(self.TBMENU_START, _("Start PLC")) |
|
322 |
menu.Append(self.TBMENU_STOP, _("Stop PLC")) |
|
835
8145be14a2ae
Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents:
719
diff
changeset
|
323 |
if self.level==1: |
8145be14a2ae
Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents:
719
diff
changeset
|
324 |
menu.AppendSeparator() |
8145be14a2ae
Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents:
719
diff
changeset
|
325 |
menu.Append(self.TBMENU_CHANGE_NAME, _("Change Name")) |
8145be14a2ae
Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents:
719
diff
changeset
|
326 |
menu.Append(self.TBMENU_CHANGE_INTERFACE, _("Change IP of interface to bind")) |
8145be14a2ae
Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents:
719
diff
changeset
|
327 |
menu.Append(self.TBMENU_CHANGE_PORT, _("Change Port Number")) |
8145be14a2ae
Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents:
719
diff
changeset
|
328 |
menu.Append(self.TBMENU_CHANGE_WD, _("Change working directory")) |
8145be14a2ae
Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents:
719
diff
changeset
|
329 |
menu.AppendSeparator() |
8145be14a2ae
Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents:
719
diff
changeset
|
330 |
menu.Append(self.TBMENU_LIVE_SHELL, _("Launch a live Python shell")) |
8145be14a2ae
Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents:
719
diff
changeset
|
331 |
menu.Append(self.TBMENU_WXINSPECTOR, _("Launch WX GUI inspector")) |
641 | 332 |
menu.AppendSeparator() |
333 |
menu.Append(self.TBMENU_QUIT, _("Quit")) |
|
334 |
return menu |
|
335 |
||
336 |
def MakeIcon(self, img): |
|
337 |
""" |
|
338 |
The various platforms have different requirements for the |
|
339 |
icon size... |
|
340 |
""" |
|
341 |
if "wxMSW" in wx.PlatformInfo: |
|
342 |
img = img.Scale(16, 16) |
|
343 |
elif "wxGTK" in wx.PlatformInfo: |
|
344 |
img = img.Scale(22, 22) |
|
345 |
# wxMac can be any size upto 128x128, so leave the source img alone.... |
|
346 |
icon = wx.IconFromBitmap(img.ConvertToBitmap() ) |
|
347 |
return icon |
|
348 |
||
349 |
def OnTaskBarStartPLC(self, evt): |
|
350 |
if self.pyroserver.plcobj is not None: |
|
351 |
self.pyroserver.plcobj.StartPLC() |
|
352 |
evt.Skip() |
|
353 |
||
354 |
def OnTaskBarStopPLC(self, evt): |
|
355 |
if self.pyroserver.plcobj is not None: |
|
835
8145be14a2ae
Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents:
719
diff
changeset
|
356 |
Thread(target=self.pyroserver.plcobj.StopPLC).start() |
641 | 357 |
evt.Skip() |
358 |
||
359 |
def OnTaskBarChangeInterface(self, evt): |
|
644
b511cab580eb
Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents:
641
diff
changeset
|
360 |
dlg = ParamsEntryDialog(None, _("Enter the IP of the interface to bind"), defaultValue=self.pyroserver.ip_addr) |
641 | 361 |
dlg.SetTests([(re.compile('\d{1,3}(?:\.\d{1,3}){3}$').match, _("IP is not valid!")), |
644
b511cab580eb
Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents:
641
diff
changeset
|
362 |
( lambda x :len([x for x in x.split(".") if 0 <= int(x) <= 255]) == 4, _("IP is not valid!")) |
641 | 363 |
]) |
364 |
if dlg.ShowModal() == wx.ID_OK: |
|
644
b511cab580eb
Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents:
641
diff
changeset
|
365 |
self.pyroserver.ip_addr = dlg.GetValue() |
641 | 366 |
self.pyroserver.Stop() |
367 |
evt.Skip() |
|
368 |
||
369 |
def OnTaskBarChangePort(self, evt): |
|
370 |
dlg = ParamsEntryDialog(None, _("Enter a port number "), defaultValue=str(self.pyroserver.port)) |
|
371 |
dlg.SetTests([(UnicodeType.isdigit, _("Port number must be an integer!")), (lambda port : 0 <= int(port) <= 65535 , _("Port number must be 0 <= port <= 65535!"))]) |
|
372 |
if dlg.ShowModal() == wx.ID_OK: |
|
373 |
self.pyroserver.port = int(dlg.GetValue()) |
|
374 |
self.pyroserver.Stop() |
|
375 |
evt.Skip() |
|
376 |
||
377 |
def OnTaskBarChangeWorkingDir(self, evt): |
|
378 |
dlg = wx.DirDialog(None, _("Choose a working directory "), self.pyroserver.workdir, wx.DD_NEW_DIR_BUTTON) |
|
379 |
if dlg.ShowModal() == wx.ID_OK: |
|
380 |
self.pyroserver.workdir = dlg.GetPath() |
|
381 |
self.pyroserver.Stop() |
|
382 |
evt.Skip() |
|
383 |
||
384 |
def OnTaskBarChangeName(self, evt): |
|
385 |
dlg = ParamsEntryDialog(None, _("Enter a name "), defaultValue=self.pyroserver.name) |
|
386 |
dlg.SetTests([(lambda name : len(name) is not 0 , _("Name must not be null!"))]) |
|
387 |
if dlg.ShowModal() == wx.ID_OK: |
|
388 |
self.pyroserver.name = dlg.GetValue() |
|
389 |
self.pyroserver.Restart() |
|
390 |
evt.Skip() |
|
391 |
||
959
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
392 |
def _LiveShellLocals(self): |
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
393 |
if self.pyroserver.plcobj is not None: |
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
394 |
return {"locals":self.pyroserver.plcobj.python_threads_vars} |
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
395 |
else: |
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
396 |
return {} |
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
397 |
|
641 | 398 |
def OnTaskBarLiveShell(self, evt): |
959
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
399 |
from wx import py |
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
400 |
frame = py.crust.CrustFrame(**self._LiveShellLocals()) |
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
401 |
frame.Show() |
641 | 402 |
evt.Skip() |
403 |
||
404 |
def OnTaskBarWXInspector(self, evt): |
|
405 |
# Activate the widget inspection tool |
|
406 |
from wx.lib.inspection import InspectionTool |
|
407 |
if not InspectionTool().initialized: |
|
959
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
408 |
InspectionTool().Init(**self._LiveShellLocals()) |
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
409 |
|
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
410 |
wnd = wx.GetApp() |
641 | 411 |
InspectionTool().Show(wnd, True) |
959
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
412 |
|
641 | 413 |
evt.Skip() |
414 |
||
415 |
def OnTaskBarQuit(self, evt): |
|
836
b59e563bcada
Quit runtime with SysTray icon menu should not crash anymore
Edouard Tisserant
parents:
835
diff
changeset
|
416 |
Thread(target=self.pyroserver.Quit).start() |
641 | 417 |
self.RemoveIcon() |
850
072188d4d812
Fixed error message occuring sometime when quitting runtime from tray menu
Edouard Tisserant
parents:
837
diff
changeset
|
418 |
wx.CallAfter(wx.GetApp().Exit) |
641 | 419 |
evt.Skip() |
420 |
||
421 |
def UpdateIcon(self, plcstatus): |
|
422 |
if plcstatus is "Started" : |
|
423 |
currenticon = self.MakeIcon(starticon.GetImage()) |
|
424 |
elif plcstatus is "Stopped": |
|
425 |
currenticon = self.MakeIcon(stopicon.GetImage()) |
|
426 |
else: |
|
427 |
currenticon = self.MakeIcon(defaulticon.GetImage()) |
|
428 |
self.SetIcon(currenticon, "Beremiz Service") |
|
429 |
||
430 |
from runtime import PLCObject, PLCprint, ServicePublisher |
|
431 |
import Pyro.core as pyro |
|
432 |
||
433 |
if not os.path.isdir(WorkingDir): |
|
434 |
os.mkdir(WorkingDir) |
|
435 |
||
867
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
436 |
def default_evaluator(tocall, *args, **kwargs): |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
437 |
try: |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
438 |
res=(tocall(*args,**kwargs), None) |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
439 |
except Exception,exp: |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
440 |
res=(None, exp) |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
441 |
return res |
641 | 442 |
|
443 |
class Server(): |
|
644
b511cab580eb
Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents:
641
diff
changeset
|
444 |
def __init__(self, servicename, ip_addr, port, workdir, argv, autostart=False, statuschange=None, evaluator=default_evaluator, website=None): |
641 | 445 |
self.continueloop = True |
446 |
self.daemon = None |
|
447 |
self.servicename = servicename |
|
644
b511cab580eb
Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents:
641
diff
changeset
|
448 |
self.ip_addr = ip_addr |
641 | 449 |
self.port = port |
450 |
self.workdir = workdir |
|
451 |
self.argv = argv |
|
452 |
self.plcobj = None |
|
453 |
self.servicepublisher = None |
|
454 |
self.autostart = autostart |
|
455 |
self.statuschange = statuschange |
|
456 |
self.evaluator = evaluator |
|
457 |
self.website = website |
|
458 |
||
459 |
def Loop(self): |
|
460 |
while self.continueloop: |
|
461 |
self.Start() |
|
462 |
||
463 |
def Restart(self): |
|
464 |
self.Stop() |
|
465 |
||
466 |
def Quit(self): |
|
467 |
self.continueloop = False |
|
468 |
self.Stop() |
|
469 |
||
470 |
def Start(self): |
|
471 |
pyro.initServer() |
|
644
b511cab580eb
Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents:
641
diff
changeset
|
472 |
self.daemon=pyro.Daemon(host=self.ip_addr, port=self.port) |
641 | 473 |
self.plcobj = PLCObject(self.workdir, self.daemon, self.argv, self.statuschange, self.evaluator, self.website) |
474 |
uri = self.daemon.connect(self.plcobj,"PLCObject") |
|
475 |
||
476 |
print "Pyro port :",self.port |
|
477 |
print "Pyro object's uri :",uri |
|
478 |
print "Current working directory :",self.workdir |
|
479 |
||
480 |
# Configure and publish service |
|
481 |
# Not publish service if localhost in address params |
|
648
73295e742da2
Avoid starting Zeroconf if ip unspecified or set to localhost. Pick one interface address when given IP is 0.0.0.0
Edouard Tisserant
parents:
644
diff
changeset
|
482 |
if (self.servicename is not None and |
73295e742da2
Avoid starting Zeroconf if ip unspecified or set to localhost. Pick one interface address when given IP is 0.0.0.0
Edouard Tisserant
parents:
644
diff
changeset
|
483 |
self.ip_addr is not None and |
73295e742da2
Avoid starting Zeroconf if ip unspecified or set to localhost. Pick one interface address when given IP is 0.0.0.0
Edouard Tisserant
parents:
644
diff
changeset
|
484 |
self.ip_addr != "localhost" and |
73295e742da2
Avoid starting Zeroconf if ip unspecified or set to localhost. Pick one interface address when given IP is 0.0.0.0
Edouard Tisserant
parents:
644
diff
changeset
|
485 |
self.ip_addr != "127.0.0.1"): |
641 | 486 |
print "Publishing service on local network" |
487 |
self.servicepublisher = ServicePublisher.ServicePublisher() |
|
644
b511cab580eb
Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents:
641
diff
changeset
|
488 |
self.servicepublisher.RegisterService(self.servicename, self.ip_addr, self.port) |
641 | 489 |
|
1034
078bf153fb8c
Fix bug when launching Beremiz_service with autostart option and working dir doesn't contain any program
Laurent Bessard
parents:
1027
diff
changeset
|
490 |
if self.autostart and self.plcobj.GetPLCstatus()[0] != "Empty": |
1027
4e44c2c3e081
Fixed bug when starting Beremiz_runtime.py non empty (-a)
Edouard Tisserant
parents:
959
diff
changeset
|
491 |
self.plcobj.LoadPLC() |
641 | 492 |
self.plcobj.StartPLC() |
493 |
||
494 |
sys.stdout.flush() |
|
495 |
||
496 |
self.daemon.requestLoop() |
|
497 |
||
498 |
def Stop(self): |
|
499 |
self.plcobj.StopPLC() |
|
500 |
if self.servicepublisher is not None: |
|
501 |
self.servicepublisher.UnRegisterService() |
|
502 |
self.servicepublisher = None |
|
503 |
self.daemon.shutdown(True) |
|
504 |
||
505 |
if enabletwisted: |
|
506 |
import warnings |
|
507 |
with warnings.catch_warnings(): |
|
508 |
warnings.simplefilter("ignore") |
|
509 |
try: |
|
510 |
from threading import Thread, currentThread |
|
511 |
if havewx: |
|
512 |
from twisted.internet import wxreactor |
|
513 |
wxreactor.install() |
|
514 |
from twisted.internet import reactor, task |
|
515 |
from twisted.python import log, util |
|
516 |
from nevow import rend, appserver, inevow, tags, loaders, athena |
|
517 |
from nevow.page import renderer |
|
518 |
||
519 |
havetwisted = True |
|
520 |
except: |
|
521 |
print "Twisted unavailable !" |
|
522 |
havetwisted = False |
|
523 |
||
524 |
if havetwisted: |
|
525 |
||
526 |
xhtml_header = '''<?xml version="1.0" encoding="utf-8"?> |
|
527 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" |
|
528 |
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> |
|
529 |
''' |
|
530 |
||
531 |
class PLCHMI(athena.LiveElement): |
|
532 |
||
533 |
initialised = False |
|
534 |
||
535 |
def HMIinitialised(self, result): |
|
536 |
self.initialised = True |
|
537 |
||
538 |
def HMIinitialisation(self): |
|
539 |
self.HMIinitialised(None) |
|
540 |
||
541 |
class DefaultPLCStartedHMI(PLCHMI): |
|
542 |
docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[ |
|
543 |
tags.h1["PLC IS NOW STARTED"], |
|
544 |
]) |
|
545 |
||
546 |
class PLCStoppedHMI(PLCHMI): |
|
547 |
docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[ |
|
548 |
tags.h1["PLC IS STOPPED"], |
|
549 |
]) |
|
550 |
||
551 |
class MainPage(athena.LiveElement): |
|
552 |
jsClass = u"WebInterface.PLC" |
|
553 |
docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[ |
|
554 |
tags.div(id='content')[ |
|
555 |
tags.div(render = tags.directive('PLCElement')), |
|
556 |
]]) |
|
557 |
||
558 |
def __init__(self, *a, **kw): |
|
559 |
athena.LiveElement.__init__(self, *a, **kw) |
|
560 |
self.pcl_state = False |
|
561 |
self.HMI = None |
|
562 |
self.resetPLCStartedHMI() |
|
563 |
||
564 |
def setPLCState(self, state): |
|
565 |
self.pcl_state = state |
|
566 |
if self.HMI is not None: |
|
567 |
self.callRemote('updateHMI') |
|
568 |
||
569 |
def setPLCStartedHMI(self, hmi): |
|
570 |
self.PLCStartedHMIClass = hmi |
|
571 |
||
572 |
def resetPLCStartedHMI(self): |
|
573 |
self.PLCStartedHMIClass = DefaultPLCStartedHMI |
|
574 |
||
575 |
def getHMI(self): |
|
576 |
return self.HMI |
|
577 |
||
578 |
def HMIexec(self, function, *args, **kwargs): |
|
579 |
if self.HMI is not None: |
|
580 |
getattr(self.HMI, function, lambda:None)(*args, **kwargs) |
|
581 |
athena.expose(HMIexec) |
|
582 |
||
583 |
def resetHMI(self): |
|
584 |
self.HMI = None |
|
585 |
||
586 |
def PLCElement(self, ctx, data): |
|
587 |
return self.getPLCElement() |
|
588 |
renderer(PLCElement) |
|
589 |
||
590 |
def getPLCElement(self): |
|
591 |
self.detachFragmentChildren() |
|
592 |
if self.pcl_state: |
|
593 |
f = self.PLCStartedHMIClass() |
|
594 |
else: |
|
595 |
f = PLCStoppedHMI() |
|
596 |
f.setFragmentParent(self) |
|
597 |
self.HMI = f |
|
598 |
return f |
|
599 |
athena.expose(getPLCElement) |
|
600 |
||
601 |
def detachFragmentChildren(self): |
|
602 |
for child in self.liveFragmentChildren[:]: |
|
603 |
child.detach() |
|
604 |
||
605 |
class WebInterface(athena.LivePage): |
|
606 |
||
607 |
docFactory = loaders.stan([tags.raw(xhtml_header), |
|
608 |
tags.html(xmlns="http://www.w3.org/1999/xhtml")[ |
|
609 |
tags.head(render=tags.directive('liveglue')), |
|
610 |
tags.body[ |
|
611 |
tags.div[ |
|
612 |
tags.div( render = tags.directive( "MainPage" )) |
|
613 |
]]]]) |
|
614 |
MainPage = MainPage() |
|
615 |
PLCHMI = PLCHMI |
|
616 |
||
617 |
def __init__(self, plcState=False, *a, **kw): |
|
618 |
super(WebInterface, self).__init__(*a, **kw) |
|
719 | 619 |
self.jsModules.mapping[u'WebInterface'] = util.sibpath(__file__, os.path.join('runtime', 'webinterface.js')) |
641 | 620 |
self.plcState = plcState |
621 |
self.MainPage.setPLCState(plcState) |
|
622 |
||
623 |
def getHMI(self): |
|
624 |
return self.MainPage.getHMI() |
|
625 |
||
626 |
def LoadHMI(self, hmi, jsmodules): |
|
627 |
for name, path in jsmodules.iteritems(): |
|
628 |
self.jsModules.mapping[name] = os.path.join(WorkingDir, path) |
|
629 |
self.MainPage.setPLCStartedHMI(hmi) |
|
630 |
||
631 |
def UnLoadHMI(self): |
|
632 |
self.MainPage.resetPLCStartedHMI() |
|
633 |
||
634 |
def PLCStarted(self): |
|
635 |
self.plcState = True |
|
636 |
self.MainPage.setPLCState(True) |
|
637 |
||
638 |
def PLCStopped(self): |
|
639 |
self.plcState = False |
|
640 |
self.MainPage.setPLCState(False) |
|
641 |
||
642 |
def renderHTTP(self, ctx): |
|
643 |
""" |
|
644 |
Force content type to fit with SVG |
|
645 |
""" |
|
646 |
req = inevow.IRequest(ctx) |
|
647 |
req.setHeader('Content-type', 'application/xhtml+xml') |
|
648 |
return super(WebInterface, self).renderHTTP(ctx) |
|
649 |
||
650 |
def render_MainPage(self, ctx, data): |
|
651 |
f = self.MainPage |
|
652 |
f.setFragmentParent(self) |
|
653 |
return ctx.tag[f] |
|
654 |
||
655 |
def child_(self, ctx): |
|
656 |
self.MainPage.detachFragmentChildren() |
|
657 |
return WebInterface(plcState=self.plcState) |
|
658 |
||
659 |
def beforeRender(self, ctx): |
|
660 |
d = self.notifyOnDisconnect() |
|
661 |
d.addErrback(self.disconnected) |
|
662 |
||
663 |
def disconnected(self, reason): |
|
664 |
self.MainPage.resetHMI() |
|
665 |
#print reason |
|
666 |
#print "We will be called back when the client disconnects" |
|
667 |
||
668 |
if havewx: |
|
669 |
reactor.registerWxApp(app) |
|
670 |
website = WebInterface() |
|
671 |
site = appserver.NevowSite(website) |
|
672 |
||
673 |
website_port = 8009 |
|
674 |
listening = False |
|
675 |
while not listening: |
|
676 |
try: |
|
677 |
reactor.listenTCP(website_port, site) |
|
678 |
listening = True |
|
679 |
except: |
|
680 |
website_port += 1 |
|
681 |
print "Http interface port :",website_port |
|
682 |
else: |
|
683 |
website = None |
|
684 |
||
685 |
if havewx: |
|
686 |
from threading import Semaphore |
|
687 |
wx_eval_lock = Semaphore(0) |
|
867
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
688 |
main_thread = currentThread() |
835
8145be14a2ae
Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents:
719
diff
changeset
|
689 |
|
641 | 690 |
def statuschange(status): |
691 |
wx.CallAfter(taskbar_instance.UpdateIcon,status) |
|
692 |
||
867
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
693 |
def wx_evaluator(obj, *args, **kwargs): |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
694 |
tocall,args,kwargs = obj.call |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
695 |
obj.res = default_evaluator(tocall, *args, **kwargs) |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
696 |
wx_eval_lock.release() |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
697 |
|
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
698 |
def evaluator(tocall, *args, **kwargs): |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
699 |
global main_thread |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
700 |
if(main_thread == currentThread()): |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
701 |
# avoid dead lock if called from the wx mainloop |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
702 |
return default_evaluator(tocall, *args, **kwargs) |
641 | 703 |
else: |
867
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
704 |
o=type('',(object,),dict(call=(tocall, args, kwargs), res=None)) |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
705 |
wx.CallAfter(wx_evaluator,o) |
641 | 706 |
wx_eval_lock.acquire() |
867
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
850
diff
changeset
|
707 |
return o.res |
641 | 708 |
|
644
b511cab580eb
Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents:
641
diff
changeset
|
709 |
pyroserver = Server(servicename, given_ip, port, WorkingDir, argv, autostart, statuschange, evaluator, website) |
835
8145be14a2ae
Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents:
719
diff
changeset
|
710 |
taskbar_instance = BeremizTaskBarIcon(pyroserver, enablewx) |
641 | 711 |
else: |
644
b511cab580eb
Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents:
641
diff
changeset
|
712 |
pyroserver = Server(servicename, given_ip, port, WorkingDir, argv, autostart, website=website) |
641 | 713 |
|
714 |
if havetwisted or havewx: |
|
715 |
pyro_thread=Thread(target=pyroserver.Loop) |
|
716 |
pyro_thread.start() |
|
717 |
||
718 |
if havetwisted: |
|
719 |
reactor.run() |
|
720 |
elif havewx: |
|
721 |
app.MainLoop() |
|
722 |
else: |
|
723 |
try : |
|
724 |
pyroserver.Loop() |
|
725 |
except KeyboardInterrupt,e: |
|
726 |
pass |
|
727 |
pyroserver.Quit() |
|
728 |
sys.exit(0) |