author | Laurent Bessard |
Thu, 21 Mar 2013 17:33:50 +0100 | |
changeset 992 | 72ee7f3e3cf3 |
parent 959 | 046aeae0d71c |
child 1027 | 4e44c2c3e081 |
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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CMp9kk7uep+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+oIS2g1kQ1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ThcIwP" |
|
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/pdIp5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
244 |
||
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 |
|
490 |
if self.autostart: |
|
491 |
self.plcobj.StartPLC() |
|
492 |
||
493 |
sys.stdout.flush() |
|
494 |
||
495 |
self.daemon.requestLoop() |
|
496 |
||
497 |
def Stop(self): |
|
498 |
self.plcobj.StopPLC() |
|
499 |
if self.servicepublisher is not None: |
|
500 |
self.servicepublisher.UnRegisterService() |
|
501 |
self.servicepublisher = None |
|
502 |
self.daemon.shutdown(True) |
|
503 |
||
504 |
if enabletwisted: |
|
505 |
import warnings |
|
506 |
with warnings.catch_warnings(): |
|
507 |
warnings.simplefilter("ignore") |
|
508 |
try: |
|
509 |
from threading import Thread, currentThread |
|
510 |
if havewx: |
|
511 |
from twisted.internet import wxreactor |
|
512 |
wxreactor.install() |
|
513 |
from twisted.internet import reactor, task |
|
514 |
from twisted.python import log, util |
|
515 |
from nevow import rend, appserver, inevow, tags, loaders, athena |
|
516 |
from nevow.page import renderer |
|
517 |
||
518 |
havetwisted = True |
|
519 |
except: |
|
520 |
print "Twisted unavailable !" |
|
521 |
havetwisted = False |
|
522 |
||
523 |
if havetwisted: |
|
524 |
||
525 |
xhtml_header = '''<?xml version="1.0" encoding="utf-8"?> |
|
526 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" |
|
527 |
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> |
|
528 |
''' |
|
529 |
||
530 |
class PLCHMI(athena.LiveElement): |
|
531 |
||
532 |
initialised = False |
|
533 |
||
534 |
def HMIinitialised(self, result): |
|
535 |
self.initialised = True |
|
536 |
||
537 |
def HMIinitialisation(self): |
|
538 |
self.HMIinitialised(None) |
|
539 |
||
540 |
class DefaultPLCStartedHMI(PLCHMI): |
|
541 |
docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[ |
|
542 |
tags.h1["PLC IS NOW STARTED"], |
|
543 |
]) |
|
544 |
||
545 |
class PLCStoppedHMI(PLCHMI): |
|
546 |
docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[ |
|
547 |
tags.h1["PLC IS STOPPED"], |
|
548 |
]) |
|
549 |
||
550 |
class MainPage(athena.LiveElement): |
|
551 |
jsClass = u"WebInterface.PLC" |
|
552 |
docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[ |
|
553 |
tags.div(id='content')[ |
|
554 |
tags.div(render = tags.directive('PLCElement')), |
|
555 |
]]) |
|
556 |
||
557 |
def __init__(self, *a, **kw): |
|
558 |
athena.LiveElement.__init__(self, *a, **kw) |
|
559 |
self.pcl_state = False |
|
560 |
self.HMI = None |
|
561 |
self.resetPLCStartedHMI() |
|
562 |
||
563 |
def setPLCState(self, state): |
|
564 |
self.pcl_state = state |
|
565 |
if self.HMI is not None: |
|
566 |
self.callRemote('updateHMI') |
|
567 |
||
568 |
def setPLCStartedHMI(self, hmi): |
|
569 |
self.PLCStartedHMIClass = hmi |
|
570 |
||
571 |
def resetPLCStartedHMI(self): |
|
572 |
self.PLCStartedHMIClass = DefaultPLCStartedHMI |
|
573 |
||
574 |
def getHMI(self): |
|
575 |
return self.HMI |
|
576 |
||
577 |
def HMIexec(self, function, *args, **kwargs): |
|
578 |
if self.HMI is not None: |
|
579 |
getattr(self.HMI, function, lambda:None)(*args, **kwargs) |
|
580 |
athena.expose(HMIexec) |
|
581 |
||
582 |
def resetHMI(self): |
|
583 |
self.HMI = None |
|
584 |
||
585 |
def PLCElement(self, ctx, data): |
|
586 |
return self.getPLCElement() |
|
587 |
renderer(PLCElement) |
|
588 |
||
589 |
def getPLCElement(self): |
|
590 |
self.detachFragmentChildren() |
|
591 |
if self.pcl_state: |
|
592 |
f = self.PLCStartedHMIClass() |
|
593 |
else: |
|
594 |
f = PLCStoppedHMI() |
|
595 |
f.setFragmentParent(self) |
|
596 |
self.HMI = f |
|
597 |
return f |
|
598 |
athena.expose(getPLCElement) |
|
599 |
||
600 |
def detachFragmentChildren(self): |
|
601 |
for child in self.liveFragmentChildren[:]: |
|
602 |
child.detach() |
|
603 |
||
604 |
class WebInterface(athena.LivePage): |
|
605 |
||
606 |
docFactory = loaders.stan([tags.raw(xhtml_header), |
|
607 |
tags.html(xmlns="http://www.w3.org/1999/xhtml")[ |
|
608 |
tags.head(render=tags.directive('liveglue')), |
|
609 |
tags.body[ |
|
610 |
tags.div[ |
|
611 |
tags.div( render = tags.directive( "MainPage" )) |
|
612 |
]]]]) |
|
613 |
MainPage = MainPage() |
|
614 |
PLCHMI = PLCHMI |
|
615 |
||
616 |
def __init__(self, plcState=False, *a, **kw): |
|
617 |
super(WebInterface, self).__init__(*a, **kw) |
|
719 | 618 |
self.jsModules.mapping[u'WebInterface'] = util.sibpath(__file__, os.path.join('runtime', 'webinterface.js')) |
641 | 619 |
self.plcState = plcState |
620 |
self.MainPage.setPLCState(plcState) |
|
621 |
||
622 |
def getHMI(self): |
|
623 |
return self.MainPage.getHMI() |
|
624 |
||
625 |
def LoadHMI(self, hmi, jsmodules): |
|
626 |
for name, path in jsmodules.iteritems(): |
|
627 |
self.jsModules.mapping[name] = os.path.join(WorkingDir, path) |
|
628 |
self.MainPage.setPLCStartedHMI(hmi) |
|
629 |
||
630 |
def UnLoadHMI(self): |
|
631 |
self.MainPage.resetPLCStartedHMI() |
|
632 |
||
633 |
def PLCStarted(self): |
|
634 |
self.plcState = True |
|
635 |
self.MainPage.setPLCState(True) |
|
636 |
||
637 |
def PLCStopped(self): |
|
638 |
self.plcState = False |
|
639 |
self.MainPage.setPLCState(False) |
|
640 |
||
641 |
def renderHTTP(self, ctx): |
|
642 |
""" |
|
643 |
Force content type to fit with SVG |
|
644 |
""" |
|
645 |
req = inevow.IRequest(ctx) |
|
646 |
req.setHeader('Content-type', 'application/xhtml+xml') |
|
647 |
return super(WebInterface, self).renderHTTP(ctx) |
|
648 |
||
649 |
def render_MainPage(self, ctx, data): |
|
650 |
f = self.MainPage |
|
651 |
f.setFragmentParent(self) |
|
652 |
return ctx.tag[f] |
|
653 |
||
654 |
def child_(self, ctx): |
|
655 |
self.MainPage.detachFragmentChildren() |
|
656 |
return WebInterface(plcState=self.plcState) |
|
657 |
||
658 |
def beforeRender(self, ctx): |
|
659 |
d = self.notifyOnDisconnect() |
|
660 |
d.addErrback(self.disconnected) |
|
661 |
||
662 |
def disconnected(self, reason): |
|
663 |
self.MainPage.resetHMI() |
|
664 |
#print reason |
|
665 |
#print "We will be called back when the client disconnects" |
|
666 |
||
667 |
if havewx: |
|
668 |
reactor.registerWxApp(app) |
|
669 |
website = WebInterface() |
|
670 |
site = appserver.NevowSite(website) |
|
671 |
||
672 |
website_port = 8009 |
|
673 |
listening = False |
|
674 |
while not listening: |
|
675 |
try: |
|
676 |
reactor.listenTCP(website_port, site) |
|
677 |
listening = True |
|
678 |
except: |
|
679 |
website_port += 1 |
|
680 |
print "Http interface port :",website_port |
|
681 |
else: |
|
682 |
website = None |
|
683 |
||
684 |
if havewx: |
|
685 |
from threading import Semaphore |
|
686 |
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
|
687 |
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
|
688 |
|
641 | 689 |
def statuschange(status): |
690 |
wx.CallAfter(taskbar_instance.UpdateIcon,status) |
|
691 |
||
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
|
692 |
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
|
693 |
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
|
694 |
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
|
695 |
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
|
696 |
|
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 |
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
|
698 |
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
|
699 |
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
|
700 |
# 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
|
701 |
return default_evaluator(tocall, *args, **kwargs) |
641 | 702 |
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
|
703 |
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
|
704 |
wx.CallAfter(wx_evaluator,o) |
641 | 705 |
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
|
706 |
return o.res |
641 | 707 |
|
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
|
708 |
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
|
709 |
taskbar_instance = BeremizTaskBarIcon(pyroserver, enablewx) |
641 | 710 |
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
|
711 |
pyroserver = Server(servicename, given_ip, port, WorkingDir, argv, autostart, website=website) |
641 | 712 |
|
713 |
if havetwisted or havewx: |
|
714 |
pyro_thread=Thread(target=pyroserver.Loop) |
|
715 |
pyro_thread.start() |
|
716 |
||
717 |
if havetwisted: |
|
718 |
reactor.run() |
|
719 |
elif havewx: |
|
720 |
app.MainLoop() |
|
721 |
else: |
|
722 |
try : |
|
723 |
pyroserver.Loop() |
|
724 |
except KeyboardInterrupt,e: |
|
725 |
pass |
|
726 |
pyroserver.Quit() |
|
727 |
sys.exit(0) |