author | Laurent Bessard |
Wed, 02 Oct 2013 01:21:35 +0200 | |
changeset 1338 | c1e6c712cc35 |
parent 1270 | aa9bc3e6181d |
child 1434 | 6e0cd0ceabb7 |
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 |
|
1067
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
26 |
from threading import Thread |
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] |
|
1067
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
117 |
def Bpath(*args): |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
118 |
return os.path.join(CWD,*args) |
641 | 119 |
|
120 |
# Get folder containing translation files |
|
121 |
localedir = os.path.join(CWD,"locale") |
|
122 |
# Get the default language |
|
123 |
langid = wx.LANGUAGE_DEFAULT |
|
124 |
# Define translation domain (name of translation files) |
|
125 |
domain = "Beremiz" |
|
126 |
||
127 |
# Define locale for wx |
|
128 |
loc = __builtin__.__dict__.get('loc', None) |
|
129 |
if loc is None: |
|
130 |
loc = wx.Locale(langid) |
|
131 |
__builtin__.__dict__['loc'] = loc |
|
132 |
# Define location for searching translation files |
|
133 |
loc.AddCatalogLookupPathPrefix(localedir) |
|
134 |
# Define locale domain |
|
135 |
loc.AddCatalog(domain) |
|
136 |
||
137 |
def unicode_translation(message): |
|
138 |
return wx.GetTranslation(message).encode("utf-8") |
|
139 |
||
140 |
if __name__ == '__main__': |
|
141 |
__builtin__.__dict__['_'] = wx.GetTranslation#unicode_translation |
|
142 |
||
1067
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
143 |
defaulticon = wx.Image(Bpath("images", "brz.png")) |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
144 |
starticon = wx.Image(Bpath("images", "icoplay24.png")) |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
145 |
stopicon = wx.Image(Bpath("images", "icostop24.png")) |
641 | 146 |
|
147 |
class ParamsEntryDialog(wx.TextEntryDialog): |
|
148 |
if wx.VERSION < (2, 6, 0): |
|
149 |
def Bind(self, event, function, id = None): |
|
150 |
if id is not None: |
|
151 |
event(self, id, function) |
|
152 |
else: |
|
153 |
event(self, function) |
|
154 |
||
155 |
||
156 |
def __init__(self, parent, message, caption = "Please enter text", defaultValue = "", |
|
157 |
style = wx.OK|wx.CANCEL|wx.CENTRE, pos = wx.DefaultPosition): |
|
158 |
wx.TextEntryDialog.__init__(self, parent, message, caption, defaultValue, style, pos) |
|
159 |
||
160 |
self.Tests = [] |
|
161 |
if wx.VERSION >= (2, 8, 0): |
|
162 |
self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetAffirmativeId()) |
|
163 |
elif wx.VERSION >= (2, 6, 0): |
|
164 |
self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetAffirmativeButton().GetId()) |
|
165 |
else: |
|
166 |
self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId()) |
|
167 |
||
168 |
def OnOK(self, event): |
|
169 |
value = self.GetValue() |
|
170 |
texts = {"value" : value} |
|
171 |
for function, message in self.Tests: |
|
172 |
if not function(value): |
|
173 |
message = wx.MessageDialog(self, message%texts, _("Error"), wx.OK|wx.ICON_ERROR) |
|
174 |
message.ShowModal() |
|
175 |
message.Destroy() |
|
176 |
return |
|
177 |
self.EndModal(wx.ID_OK) |
|
178 |
event.Skip() |
|
179 |
||
180 |
def GetValue(self): |
|
181 |
return self.GetSizer().GetItem(1).GetWindow().GetValue() |
|
182 |
||
183 |
def SetTests(self, tests): |
|
184 |
self.Tests = tests |
|
185 |
||
186 |
class BeremizTaskBarIcon(wx.TaskBarIcon): |
|
187 |
TBMENU_START = wx.NewId() |
|
188 |
TBMENU_STOP = wx.NewId() |
|
189 |
TBMENU_CHANGE_NAME = wx.NewId() |
|
190 |
TBMENU_CHANGE_PORT = wx.NewId() |
|
191 |
TBMENU_CHANGE_INTERFACE = wx.NewId() |
|
192 |
TBMENU_LIVE_SHELL = wx.NewId() |
|
193 |
TBMENU_WXINSPECTOR = wx.NewId() |
|
194 |
TBMENU_CHANGE_WD = wx.NewId() |
|
195 |
TBMENU_QUIT = wx.NewId() |
|
196 |
||
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
|
197 |
def __init__(self, pyroserver, level): |
641 | 198 |
wx.TaskBarIcon.__init__(self) |
199 |
self.pyroserver = pyroserver |
|
200 |
# Set the image |
|
201 |
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
|
202 |
self.level = level |
641 | 203 |
|
204 |
# bind some events |
|
205 |
self.Bind(wx.EVT_MENU, self.OnTaskBarStartPLC, id=self.TBMENU_START) |
|
206 |
self.Bind(wx.EVT_MENU, self.OnTaskBarStopPLC, id=self.TBMENU_STOP) |
|
207 |
self.Bind(wx.EVT_MENU, self.OnTaskBarChangeName, id=self.TBMENU_CHANGE_NAME) |
|
208 |
self.Bind(wx.EVT_MENU, self.OnTaskBarChangeInterface, id=self.TBMENU_CHANGE_INTERFACE) |
|
209 |
self.Bind(wx.EVT_MENU, self.OnTaskBarLiveShell, id=self.TBMENU_LIVE_SHELL) |
|
210 |
self.Bind(wx.EVT_MENU, self.OnTaskBarWXInspector, id=self.TBMENU_WXINSPECTOR) |
|
211 |
self.Bind(wx.EVT_MENU, self.OnTaskBarChangePort, id=self.TBMENU_CHANGE_PORT) |
|
212 |
self.Bind(wx.EVT_MENU, self.OnTaskBarChangeWorkingDir, id=self.TBMENU_CHANGE_WD) |
|
213 |
self.Bind(wx.EVT_MENU, self.OnTaskBarQuit, id=self.TBMENU_QUIT) |
|
214 |
||
215 |
def CreatePopupMenu(self): |
|
216 |
""" |
|
217 |
This method is called by the base class when it needs to popup |
|
218 |
the menu for the default EVT_RIGHT_DOWN event. Just create |
|
219 |
the menu how you want it and return it from this function, |
|
220 |
the base class takes care of the rest. |
|
221 |
""" |
|
222 |
menu = wx.Menu() |
|
223 |
menu.Append(self.TBMENU_START, _("Start PLC")) |
|
224 |
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
|
225 |
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
|
226 |
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
|
227 |
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
|
228 |
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
|
229 |
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
|
230 |
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
|
231 |
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
|
232 |
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
|
233 |
menu.Append(self.TBMENU_WXINSPECTOR, _("Launch WX GUI inspector")) |
641 | 234 |
menu.AppendSeparator() |
235 |
menu.Append(self.TBMENU_QUIT, _("Quit")) |
|
236 |
return menu |
|
237 |
||
238 |
def MakeIcon(self, img): |
|
239 |
""" |
|
240 |
The various platforms have different requirements for the |
|
241 |
icon size... |
|
242 |
""" |
|
243 |
if "wxMSW" in wx.PlatformInfo: |
|
244 |
img = img.Scale(16, 16) |
|
245 |
elif "wxGTK" in wx.PlatformInfo: |
|
246 |
img = img.Scale(22, 22) |
|
247 |
# wxMac can be any size upto 128x128, so leave the source img alone.... |
|
248 |
icon = wx.IconFromBitmap(img.ConvertToBitmap() ) |
|
249 |
return icon |
|
250 |
||
251 |
def OnTaskBarStartPLC(self, evt): |
|
252 |
if self.pyroserver.plcobj is not None: |
|
253 |
self.pyroserver.plcobj.StartPLC() |
|
254 |
||
255 |
def OnTaskBarStopPLC(self, evt): |
|
256 |
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
|
257 |
Thread(target=self.pyroserver.plcobj.StopPLC).start() |
641 | 258 |
|
259 |
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
|
260 |
dlg = ParamsEntryDialog(None, _("Enter the IP of the interface to bind"), defaultValue=self.pyroserver.ip_addr) |
641 | 261 |
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
|
262 |
( lambda x :len([x for x in x.split(".") if 0 <= int(x) <= 255]) == 4, _("IP is not valid!")) |
641 | 263 |
]) |
264 |
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
|
265 |
self.pyroserver.ip_addr = dlg.GetValue() |
641 | 266 |
self.pyroserver.Stop() |
267 |
||
268 |
def OnTaskBarChangePort(self, evt): |
|
269 |
dlg = ParamsEntryDialog(None, _("Enter a port number "), defaultValue=str(self.pyroserver.port)) |
|
270 |
dlg.SetTests([(UnicodeType.isdigit, _("Port number must be an integer!")), (lambda port : 0 <= int(port) <= 65535 , _("Port number must be 0 <= port <= 65535!"))]) |
|
271 |
if dlg.ShowModal() == wx.ID_OK: |
|
272 |
self.pyroserver.port = int(dlg.GetValue()) |
|
273 |
self.pyroserver.Stop() |
|
274 |
||
275 |
def OnTaskBarChangeWorkingDir(self, evt): |
|
276 |
dlg = wx.DirDialog(None, _("Choose a working directory "), self.pyroserver.workdir, wx.DD_NEW_DIR_BUTTON) |
|
277 |
if dlg.ShowModal() == wx.ID_OK: |
|
278 |
self.pyroserver.workdir = dlg.GetPath() |
|
279 |
self.pyroserver.Stop() |
|
280 |
||
281 |
def OnTaskBarChangeName(self, evt): |
|
282 |
dlg = ParamsEntryDialog(None, _("Enter a name "), defaultValue=self.pyroserver.name) |
|
283 |
dlg.SetTests([(lambda name : len(name) is not 0 , _("Name must not be null!"))]) |
|
284 |
if dlg.ShowModal() == wx.ID_OK: |
|
285 |
self.pyroserver.name = dlg.GetValue() |
|
286 |
self.pyroserver.Restart() |
|
287 |
||
959
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
288 |
def _LiveShellLocals(self): |
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
289 |
if self.pyroserver.plcobj is not None: |
1049 | 290 |
return {"locals":self.pyroserver.plcobj.python_runtime_vars} |
959
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
291 |
else: |
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
292 |
return {} |
1046
103467ccb22f
Fixed bug Beremiz_service process not stopped when quitting
Laurent Bessard
parents:
1045
diff
changeset
|
293 |
|
641 | 294 |
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
|
295 |
from wx import py |
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
296 |
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
|
297 |
frame.Show() |
641 | 298 |
|
299 |
def OnTaskBarWXInspector(self, evt): |
|
300 |
# Activate the widget inspection tool |
|
301 |
from wx.lib.inspection import InspectionTool |
|
302 |
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
|
303 |
InspectionTool().Init(**self._LiveShellLocals()) |
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
304 |
|
046aeae0d71c
Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents:
867
diff
changeset
|
305 |
wnd = wx.GetApp() |
641 | 306 |
InspectionTool().Show(wnd, True) |
307 |
||
308 |
def OnTaskBarQuit(self, evt): |
|
1121
d3838e8f1b90
Fixed Beremiz_service not closing on Windows
Laurent Bessard
parents:
1067
diff
changeset
|
309 |
if wx.Platform == '__WXMSW__': |
d3838e8f1b90
Fixed Beremiz_service not closing on Windows
Laurent Bessard
parents:
1067
diff
changeset
|
310 |
Thread(target=self.pyroserver.Quit).start() |
641 | 311 |
self.RemoveIcon() |
1121
d3838e8f1b90
Fixed Beremiz_service not closing on Windows
Laurent Bessard
parents:
1067
diff
changeset
|
312 |
wx.CallAfter(wx.GetApp().ExitMainLoop) |
641 | 313 |
|
314 |
def UpdateIcon(self, plcstatus): |
|
315 |
if plcstatus is "Started" : |
|
1067
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
316 |
currenticon = self.MakeIcon(starticon) |
641 | 317 |
elif plcstatus is "Stopped": |
1067
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
318 |
currenticon = self.MakeIcon(stopicon) |
641 | 319 |
else: |
1067
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
320 |
currenticon = self.MakeIcon(defaulticon) |
641 | 321 |
self.SetIcon(currenticon, "Beremiz Service") |
322 |
||
323 |
from runtime import PLCObject, PLCprint, ServicePublisher |
|
324 |
import Pyro.core as pyro |
|
325 |
||
326 |
if not os.path.isdir(WorkingDir): |
|
327 |
os.mkdir(WorkingDir) |
|
328 |
||
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
|
329 |
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
|
330 |
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
|
331 |
res=(tocall(*args,**kwargs), None) |
1051
847d68c3e7ff
Extended exception info from evaluator. Problems in python runtime init/cleanup code now more readable
Edouard Tisserant
parents:
1049
diff
changeset
|
332 |
except Exception: |
847d68c3e7ff
Extended exception info from evaluator. Problems in python runtime init/cleanup code now more readable
Edouard Tisserant
parents:
1049
diff
changeset
|
333 |
res=(None, sys.exc_info()) |
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
|
334 |
return res |
641 | 335 |
|
336 |
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
|
337 |
def __init__(self, servicename, ip_addr, port, workdir, argv, autostart=False, statuschange=None, evaluator=default_evaluator, website=None): |
641 | 338 |
self.continueloop = True |
339 |
self.daemon = None |
|
340 |
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
|
341 |
self.ip_addr = ip_addr |
641 | 342 |
self.port = port |
343 |
self.workdir = workdir |
|
344 |
self.argv = argv |
|
345 |
self.plcobj = None |
|
346 |
self.servicepublisher = None |
|
347 |
self.autostart = autostart |
|
348 |
self.statuschange = statuschange |
|
349 |
self.evaluator = evaluator |
|
350 |
self.website = website |
|
351 |
||
352 |
def Loop(self): |
|
353 |
while self.continueloop: |
|
354 |
self.Start() |
|
355 |
||
356 |
def Restart(self): |
|
357 |
self.Stop() |
|
358 |
||
359 |
def Quit(self): |
|
360 |
self.continueloop = False |
|
1045
a220a27defe5
Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents:
1034
diff
changeset
|
361 |
if self.plcobj is not None: |
a220a27defe5
Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents:
1034
diff
changeset
|
362 |
self.plcobj.UnLoadPLC() |
641 | 363 |
self.Stop() |
364 |
||
365 |
def Start(self): |
|
366 |
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
|
367 |
self.daemon=pyro.Daemon(host=self.ip_addr, port=self.port) |
641 | 368 |
self.plcobj = PLCObject(self.workdir, self.daemon, self.argv, self.statuschange, self.evaluator, self.website) |
369 |
uri = self.daemon.connect(self.plcobj,"PLCObject") |
|
370 |
||
371 |
print "Pyro port :",self.port |
|
372 |
print "Pyro object's uri :",uri |
|
373 |
print "Current working directory :",self.workdir |
|
374 |
||
375 |
# Configure and publish service |
|
376 |
# 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
|
377 |
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
|
378 |
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
|
379 |
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
|
380 |
self.ip_addr != "127.0.0.1"): |
641 | 381 |
print "Publishing service on local network" |
382 |
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
|
383 |
self.servicepublisher.RegisterService(self.servicename, self.ip_addr, self.port) |
641 | 384 |
|
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
|
385 |
if self.autostart and self.plcobj.GetPLCstatus()[0] != "Empty": |
641 | 386 |
self.plcobj.StartPLC() |
387 |
||
388 |
sys.stdout.flush() |
|
389 |
||
390 |
self.daemon.requestLoop() |
|
391 |
||
392 |
def Stop(self): |
|
1045
a220a27defe5
Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents:
1034
diff
changeset
|
393 |
if self.plcobj is not None: |
a220a27defe5
Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents:
1034
diff
changeset
|
394 |
self.plcobj.StopPLC() |
641 | 395 |
if self.servicepublisher is not None: |
396 |
self.servicepublisher.UnRegisterService() |
|
397 |
self.servicepublisher = None |
|
398 |
self.daemon.shutdown(True) |
|
399 |
||
400 |
if enabletwisted: |
|
401 |
import warnings |
|
402 |
with warnings.catch_warnings(): |
|
403 |
warnings.simplefilter("ignore") |
|
404 |
try: |
|
405 |
from threading import Thread, currentThread |
|
406 |
if havewx: |
|
407 |
from twisted.internet import wxreactor |
|
408 |
wxreactor.install() |
|
409 |
from twisted.internet import reactor, task |
|
410 |
from twisted.python import log, util |
|
411 |
from nevow import rend, appserver, inevow, tags, loaders, athena |
|
412 |
from nevow.page import renderer |
|
413 |
||
414 |
havetwisted = True |
|
415 |
except: |
|
416 |
print "Twisted unavailable !" |
|
417 |
havetwisted = False |
|
418 |
||
419 |
if havetwisted: |
|
420 |
||
421 |
xhtml_header = '''<?xml version="1.0" encoding="utf-8"?> |
|
422 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" |
|
423 |
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> |
|
424 |
''' |
|
425 |
||
426 |
class PLCHMI(athena.LiveElement): |
|
427 |
||
428 |
initialised = False |
|
429 |
||
430 |
def HMIinitialised(self, result): |
|
431 |
self.initialised = True |
|
432 |
||
433 |
def HMIinitialisation(self): |
|
434 |
self.HMIinitialised(None) |
|
435 |
||
436 |
class DefaultPLCStartedHMI(PLCHMI): |
|
437 |
docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[ |
|
438 |
tags.h1["PLC IS NOW STARTED"], |
|
439 |
]) |
|
440 |
||
441 |
class PLCStoppedHMI(PLCHMI): |
|
442 |
docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[ |
|
443 |
tags.h1["PLC IS STOPPED"], |
|
444 |
]) |
|
445 |
||
446 |
class MainPage(athena.LiveElement): |
|
447 |
jsClass = u"WebInterface.PLC" |
|
448 |
docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[ |
|
449 |
tags.div(id='content')[ |
|
450 |
tags.div(render = tags.directive('PLCElement')), |
|
451 |
]]) |
|
452 |
||
453 |
def __init__(self, *a, **kw): |
|
454 |
athena.LiveElement.__init__(self, *a, **kw) |
|
455 |
self.pcl_state = False |
|
456 |
self.HMI = None |
|
457 |
self.resetPLCStartedHMI() |
|
458 |
||
459 |
def setPLCState(self, state): |
|
460 |
self.pcl_state = state |
|
461 |
if self.HMI is not None: |
|
462 |
self.callRemote('updateHMI') |
|
463 |
||
464 |
def setPLCStartedHMI(self, hmi): |
|
465 |
self.PLCStartedHMIClass = hmi |
|
466 |
||
467 |
def resetPLCStartedHMI(self): |
|
468 |
self.PLCStartedHMIClass = DefaultPLCStartedHMI |
|
469 |
||
470 |
def getHMI(self): |
|
471 |
return self.HMI |
|
472 |
||
473 |
def HMIexec(self, function, *args, **kwargs): |
|
474 |
if self.HMI is not None: |
|
475 |
getattr(self.HMI, function, lambda:None)(*args, **kwargs) |
|
476 |
athena.expose(HMIexec) |
|
477 |
||
478 |
def resetHMI(self): |
|
479 |
self.HMI = None |
|
480 |
||
481 |
def PLCElement(self, ctx, data): |
|
482 |
return self.getPLCElement() |
|
483 |
renderer(PLCElement) |
|
484 |
||
485 |
def getPLCElement(self): |
|
486 |
self.detachFragmentChildren() |
|
487 |
if self.pcl_state: |
|
488 |
f = self.PLCStartedHMIClass() |
|
489 |
else: |
|
490 |
f = PLCStoppedHMI() |
|
491 |
f.setFragmentParent(self) |
|
492 |
self.HMI = f |
|
493 |
return f |
|
494 |
athena.expose(getPLCElement) |
|
495 |
||
496 |
def detachFragmentChildren(self): |
|
497 |
for child in self.liveFragmentChildren[:]: |
|
498 |
child.detach() |
|
499 |
||
500 |
class WebInterface(athena.LivePage): |
|
501 |
||
502 |
docFactory = loaders.stan([tags.raw(xhtml_header), |
|
503 |
tags.html(xmlns="http://www.w3.org/1999/xhtml")[ |
|
504 |
tags.head(render=tags.directive('liveglue')), |
|
505 |
tags.body[ |
|
506 |
tags.div[ |
|
507 |
tags.div( render = tags.directive( "MainPage" )) |
|
508 |
]]]]) |
|
509 |
MainPage = MainPage() |
|
510 |
PLCHMI = PLCHMI |
|
511 |
||
512 |
def __init__(self, plcState=False, *a, **kw): |
|
513 |
super(WebInterface, self).__init__(*a, **kw) |
|
719 | 514 |
self.jsModules.mapping[u'WebInterface'] = util.sibpath(__file__, os.path.join('runtime', 'webinterface.js')) |
641 | 515 |
self.plcState = plcState |
516 |
self.MainPage.setPLCState(plcState) |
|
517 |
||
518 |
def getHMI(self): |
|
519 |
return self.MainPage.getHMI() |
|
520 |
||
521 |
def LoadHMI(self, hmi, jsmodules): |
|
522 |
for name, path in jsmodules.iteritems(): |
|
523 |
self.jsModules.mapping[name] = os.path.join(WorkingDir, path) |
|
524 |
self.MainPage.setPLCStartedHMI(hmi) |
|
525 |
||
526 |
def UnLoadHMI(self): |
|
527 |
self.MainPage.resetPLCStartedHMI() |
|
528 |
||
529 |
def PLCStarted(self): |
|
530 |
self.plcState = True |
|
531 |
self.MainPage.setPLCState(True) |
|
532 |
||
533 |
def PLCStopped(self): |
|
534 |
self.plcState = False |
|
535 |
self.MainPage.setPLCState(False) |
|
536 |
||
537 |
def renderHTTP(self, ctx): |
|
538 |
""" |
|
539 |
Force content type to fit with SVG |
|
540 |
""" |
|
541 |
req = inevow.IRequest(ctx) |
|
542 |
req.setHeader('Content-type', 'application/xhtml+xml') |
|
543 |
return super(WebInterface, self).renderHTTP(ctx) |
|
544 |
||
545 |
def render_MainPage(self, ctx, data): |
|
546 |
f = self.MainPage |
|
547 |
f.setFragmentParent(self) |
|
548 |
return ctx.tag[f] |
|
549 |
||
550 |
def child_(self, ctx): |
|
551 |
self.MainPage.detachFragmentChildren() |
|
552 |
return WebInterface(plcState=self.plcState) |
|
553 |
||
554 |
def beforeRender(self, ctx): |
|
555 |
d = self.notifyOnDisconnect() |
|
556 |
d.addErrback(self.disconnected) |
|
557 |
||
558 |
def disconnected(self, reason): |
|
559 |
self.MainPage.resetHMI() |
|
560 |
#print reason |
|
561 |
#print "We will be called back when the client disconnects" |
|
562 |
||
563 |
if havewx: |
|
564 |
reactor.registerWxApp(app) |
|
565 |
website = WebInterface() |
|
566 |
site = appserver.NevowSite(website) |
|
567 |
||
568 |
website_port = 8009 |
|
569 |
listening = False |
|
570 |
while not listening: |
|
571 |
try: |
|
572 |
reactor.listenTCP(website_port, site) |
|
573 |
listening = True |
|
574 |
except: |
|
575 |
website_port += 1 |
|
576 |
print "Http interface port :",website_port |
|
577 |
else: |
|
578 |
website = None |
|
579 |
||
580 |
if havewx: |
|
581 |
from threading import Semaphore |
|
582 |
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
|
583 |
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
|
584 |
|
641 | 585 |
def statuschange(status): |
586 |
wx.CallAfter(taskbar_instance.UpdateIcon,status) |
|
587 |
||
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
|
588 |
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
|
589 |
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
|
590 |
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
|
591 |
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
|
592 |
|
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
|
593 |
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
|
594 |
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
|
595 |
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
|
596 |
# 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
|
597 |
return default_evaluator(tocall, *args, **kwargs) |
641 | 598 |
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
|
599 |
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
|
600 |
wx.CallAfter(wx_evaluator,o) |
641 | 601 |
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
|
602 |
return o.res |
641 | 603 |
|
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
|
604 |
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
|
605 |
taskbar_instance = BeremizTaskBarIcon(pyroserver, enablewx) |
641 | 606 |
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
|
607 |
pyroserver = Server(servicename, given_ip, port, WorkingDir, argv, autostart, website=website) |
641 | 608 |
|
1067
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
609 |
# Exception hooks s |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
610 |
import threading, traceback |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
611 |
def LogException(*exp): |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
612 |
if pyroserver.plcobj is not None: |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
613 |
pyroserver.plcobj.LogMessage(0,'\n'.join(traceback.format_exception(*exp))) |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
614 |
else: |
1270 | 615 |
traceback.print_exception(*exp) |
1067
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
616 |
|
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
617 |
sys.excepthook = LogException |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
618 |
def installThreadExcepthook(): |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
619 |
init_old = threading.Thread.__init__ |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
620 |
def init(self, *args, **kwargs): |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
621 |
init_old(self, *args, **kwargs) |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
622 |
run_old = self.run |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
623 |
def run_with_except_hook(*args, **kw): |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
624 |
try: |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
625 |
run_old(*args, **kw) |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
626 |
except (KeyboardInterrupt, SystemExit): |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
627 |
raise |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
628 |
except: |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
629 |
sys.excepthook(*sys.exc_info()) |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
630 |
self.run = run_with_except_hook |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
631 |
threading.Thread.__init__ = init |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
632 |
installThreadExcepthook() |
4f460c1dffb5
Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents:
1051
diff
changeset
|
633 |
|
641 | 634 |
if havetwisted or havewx: |
635 |
pyro_thread=Thread(target=pyroserver.Loop) |
|
636 |
pyro_thread.start() |
|
637 |
||
638 |
if havetwisted: |
|
639 |
reactor.run() |
|
640 |
elif havewx: |
|
641 |
app.MainLoop() |
|
642 |
else: |
|
643 |
try : |
|
644 |
pyroserver.Loop() |
|
645 |
except KeyboardInterrupt,e: |
|
646 |
pass |
|
647 |
pyroserver.Quit() |
|
648 |
sys.exit(0) |