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
|
|
26 |
|
|
27 |
def usage():
|
|
28 |
print """
|
|
29 |
Usage of Beremiz PLC execution service :\n
|
|
30 |
%s {[-n servicename] [-i ip] [-p port] [-x enabletaskbar] [-a autostart]|-h|--help} working_dir
|
|
31 |
-n - zeroconf service name (default:disabled)
|
|
32 |
-i - ip of interface to bind to (default:localhost)
|
|
33 |
-p - port number default:3000
|
|
34 |
-h - print this help text and quit
|
|
35 |
-a - autostart PLC (0:disable 1:enable) (default:0)
|
|
36 |
-x - enable/disable wxTaskbarIcon (0:disable 1:enable) (default:1)
|
|
37 |
-t - enable/disable Twisted web interface (0:disable 1:enable) (default:1)
|
|
38 |
|
|
39 |
working_dir - directory where are stored PLC files
|
|
40 |
"""%sys.argv[0]
|
|
41 |
|
|
42 |
try:
|
|
43 |
opts, argv = getopt.getopt(sys.argv[1:], "i:p:n:x:t:a:h")
|
|
44 |
except getopt.GetoptError, err:
|
|
45 |
# print help information and exit:
|
|
46 |
print str(err) # will print something like "option -a not recognized"
|
|
47 |
usage()
|
|
48 |
sys.exit(2)
|
|
49 |
|
|
50 |
# default values
|
|
51 |
ip = ""
|
|
52 |
port = 3000
|
|
53 |
servicename = None
|
|
54 |
autostart = False
|
|
55 |
enablewx = True
|
|
56 |
havewx = False
|
|
57 |
enabletwisted = True
|
|
58 |
havetwisted = False
|
|
59 |
|
|
60 |
for o, a in opts:
|
|
61 |
if o == "-h":
|
|
62 |
usage()
|
|
63 |
sys.exit()
|
|
64 |
elif o == "-i":
|
|
65 |
if len(a.split(".")) == 4 or a == "localhost":
|
|
66 |
ip = a
|
|
67 |
elif o == "-p":
|
|
68 |
# port: port that the service runs on
|
|
69 |
port = int(a)
|
|
70 |
elif o == "-n":
|
|
71 |
servicename = a
|
|
72 |
elif o == "-x":
|
|
73 |
enablewx = int(a)
|
|
74 |
elif o == "-t":
|
|
75 |
enabletwisted = int(a)
|
|
76 |
elif o == "-a":
|
|
77 |
autostart = int(a)
|
|
78 |
else:
|
|
79 |
usage()
|
|
80 |
sys.exit()
|
|
81 |
|
|
82 |
if len(argv) > 1:
|
|
83 |
usage()
|
|
84 |
sys.exit()
|
|
85 |
elif len(argv) == 1:
|
|
86 |
WorkingDir = argv[0]
|
|
87 |
os.chdir(WorkingDir)
|
|
88 |
elif len(argv) == 0:
|
|
89 |
WorkingDir = os.getcwd()
|
|
90 |
argv=[WorkingDir]
|
|
91 |
|
|
92 |
import __builtin__
|
|
93 |
if __name__ == '__main__':
|
|
94 |
__builtin__.__dict__['_'] = lambda x: x
|
|
95 |
|
|
96 |
if enablewx:
|
|
97 |
try:
|
|
98 |
import wx, re
|
|
99 |
from threading import Thread, currentThread
|
|
100 |
from types import *
|
|
101 |
havewx = True
|
|
102 |
except:
|
|
103 |
print "Wx unavailable !"
|
|
104 |
havewx = False
|
|
105 |
|
|
106 |
if havewx:
|
|
107 |
app=wx.App(redirect=False)
|
|
108 |
|
|
109 |
# Import module for internationalization
|
|
110 |
import gettext
|
|
111 |
|
|
112 |
CWD = os.path.split(os.path.realpath(__file__))[0]
|
|
113 |
|
|
114 |
# Get folder containing translation files
|
|
115 |
localedir = os.path.join(CWD,"locale")
|
|
116 |
# Get the default language
|
|
117 |
langid = wx.LANGUAGE_DEFAULT
|
|
118 |
# Define translation domain (name of translation files)
|
|
119 |
domain = "Beremiz"
|
|
120 |
|
|
121 |
# Define locale for wx
|
|
122 |
loc = __builtin__.__dict__.get('loc', None)
|
|
123 |
if loc is None:
|
|
124 |
loc = wx.Locale(langid)
|
|
125 |
__builtin__.__dict__['loc'] = loc
|
|
126 |
# Define location for searching translation files
|
|
127 |
loc.AddCatalogLookupPathPrefix(localedir)
|
|
128 |
# Define locale domain
|
|
129 |
loc.AddCatalog(domain)
|
|
130 |
|
|
131 |
def unicode_translation(message):
|
|
132 |
return wx.GetTranslation(message).encode("utf-8")
|
|
133 |
|
|
134 |
if __name__ == '__main__':
|
|
135 |
__builtin__.__dict__['_'] = wx.GetTranslation#unicode_translation
|
|
136 |
|
|
137 |
try:
|
|
138 |
from wx.lib.embeddedimage import PyEmbeddedImage
|
|
139 |
except:
|
|
140 |
import cStringIO
|
|
141 |
import base64
|
|
142 |
|
|
143 |
class PyEmbeddedImage:
|
|
144 |
def __init__(self, image_string):
|
|
145 |
stream = cStringIO.StringIO(base64.b64decode(image_string))
|
|
146 |
self.Image = wx.ImageFromStream(stream)
|
|
147 |
def GetImage(self):
|
|
148 |
return self.Image
|
|
149 |
|
|
150 |
defaulticon = PyEmbeddedImage(
|
|
151 |
"iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAABc5J"
|
|
152 |
"REFUSIl9lW1MW9cZx3/n2vf6BQO2MZiXGBISILCVUEUlitYpjaKpXZJ1XZZ2kzJVY9r6IeLD"
|
|
153 |
"pGTaNG3KtGmNNGlb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zWU+xWPTkcjmXaZpxwzDCsVhsbqNggP8BMJOU3UUUf+0AAAAASUVORK5CYII=")
|
|
180 |
|
|
181 |
#----------------------------------------------------------------------
|
|
182 |
starticon = PyEmbeddedImage(
|
|
183 |
"iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAABbpJ"
|
|
184 |
"REFUSIl9lltsFNcdxn9nZnbHs15fd23j9TXYC0UCKzEhMQ+oIS2g1kQ1pbFStX0opFWsovSh"
|
|
185 |
"rUqp2pS2ioTUolaKFOGHqGkiJcKRuDhOaZRiZCsCXyBgCBBfMfbu+oa9s17wzuzl9MH24mDD"
|
|
186 |
"XzoPc/6fft+c72jOGSGlZEVlAU8D9cB20zQ9HR0duRcvXszq7e01EomEUlFREa+srLR8Pl+g"
|
|
187 |
"sbHx3zk5ORcAFfACA8Bt4CFUSomUUkgpS6SUB6SUH5umOXLgwIEHqqrKJfGao7S0VB49ejRo"
|
|
188 |
"2/YnUsrT+/fvb66pqSldYiKlRCytoBB4Gfjx6dOnq5qamjwTExOKqqqU+QrYUJFN7QY32Qbc"
|
|
189 |
"vSeYCGtcux1i5M5dAPx+P1VVVQvnzp0ziouLfx8MBt9cXoAGZABbgZ1HjhwpO378eEEymaSi"
|
|
190 |
"tIBjPy9lU5nKoyWExF2yjy+mN3HsH+/Q3d3NwMCAsZTI9pVaDXgK2Hr27Nn85ubmEpdh8IMX"
|
|
191 |
"ffxirwshVrGXHBQSC/dIRvoZGuz/WkvTtHIhhCGlXABQgI2Tk5P5hw8f3uZwOGj8VjGHXnoC"
|
|
192 |
"HJCpJFbkLtr8FXbX+XC79HRPVVW/qqre9LtIKX/S0NDwy76+vq1lhTr/fM2NAmTk+fHv/dea"
|
|
193 |
"BlZkDHP0PHODH2NHg1gykw8/X7Dfb7vjTNgJqqurT3R1db0GoF2/fl0fGhqqdWca/K7RhZLO"
|
|
194 |
"WSBU55oGGXlVZORVkeV7nsFPDqKL+9TWJCI3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gFlsfieK+WpJ5e/3z7pcuXzmia1rJSs3xlOg8dOvTD"
|
|
208 |
"8fHx7wQCgb4tW7bMm6b55/Pnz+eGw+FFGJDT5iT1XRWlfxHMZ06+/Vz9dCAQeG9kZKR1x44d"
|
|
209 |
"nSdPnkyuZSAArbq6eqOiKAP9/f3xlpaWgra2tlei0eiryWSyKGKa2TcaL+muwcxU5aDf9Gi+"
|
|
210 |
"L0Oh0BehUOiaZVlnAoHAzFr7Ih75bVnVb2pqcvf09Phi0ei6+/rUC6lw1k0p5bSUctThcIwP"
|
|
211 |
"Dw/HnwT4P6CDl+TMvD0JAAAAAElFTkSuQmCC")
|
|
212 |
|
|
213 |
#----------------------------------------------------------------------
|
|
214 |
stopicon = PyEmbeddedImage(
|
|
215 |
"iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAABPRJ"
|
|
216 |
"REFUSImdlllsVGUUx3/f/e4sd5iZLjNt6XSFdtgkjWFRePABDaCBGgjamIg81CU0aoxbRHww"
|
|
217 |
"+EDkhWjEB5rYGEMUxQTCJg8EoQ2BbgrFCNJWltplgC63naEzd+bO50NLLVAq4STfwz3nfP/f"
|
|
218 |
"PSf3O98VSikmmQ94HFgDLDdNM1BfX5955swZX0tLi5FKpbSSkpJkaWlpIhQKdVdVVX2XkZFx"
|
|
219 |
"EpBAEGgHLgH/iSqlUEoJpVSBUqpaKXXYNM0r1dXVt6WUajx5ylVYWKi2bdvWY1nWUaXUgQ0b"
|
|
220 |
"NtRWVFQUjmuilEKMV5ALvAhsPHDgQFlNTU2gr69Pk1JSFMphTomfRXO8+A243i/oG9I5f6mX"
|
|
221 |
"K1evAxAOhykrKxs9duyYkZ+f/0lPT8/2OwXogBtYDKzYunVr0c6dO3Ns26akMIcdbxQyv0hy"
|
|
222 |
"rwmh8Bas5/eb89nxRR1NTU20t7cb4x1ZPjlXB2YBiw8ePJhdW1tb4DEMXng6xJtrPQhxn/Y4"
|
|
223 |
"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|
236 |
"8LszMRweXLr7kWB35oMdCAT+1jRt0cqVK6Otra2+hvoGGuobWPLEEsoXzkbPkLhvR4CBRwJY"
|
|
237 |
"Xq/3SGVlZbq7u7utsrJyxDTNz06cOJHZ0tRCS1MLAKuRwNQT9v8AyV27dn1fXl7eqmlae11d"
|
|
238 |
"XXLfvn0/+Xy+l6LR6Gu2befFYjFfzrk2FzeHp7mK7jdxz2/LffGamhpvc3NzyLKsbFd3z1PG"
|
|
239 |
"aHyBTKdjum0POGzbFAp7qo0xVOtJZdf/C/wRDnL5FYGSAAAAAElFTkSuQmCC")
|
|
240 |
|
|
241 |
class ParamsEntryDialog(wx.TextEntryDialog):
|
|
242 |
if wx.VERSION < (2, 6, 0):
|
|
243 |
def Bind(self, event, function, id = None):
|
|
244 |
if id is not None:
|
|
245 |
event(self, id, function)
|
|
246 |
else:
|
|
247 |
event(self, function)
|
|
248 |
|
|
249 |
|
|
250 |
def __init__(self, parent, message, caption = "Please enter text", defaultValue = "",
|
|
251 |
style = wx.OK|wx.CANCEL|wx.CENTRE, pos = wx.DefaultPosition):
|
|
252 |
wx.TextEntryDialog.__init__(self, parent, message, caption, defaultValue, style, pos)
|
|
253 |
|
|
254 |
self.Tests = []
|
|
255 |
if wx.VERSION >= (2, 8, 0):
|
|
256 |
self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetAffirmativeId())
|
|
257 |
elif wx.VERSION >= (2, 6, 0):
|
|
258 |
self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetAffirmativeButton().GetId())
|
|
259 |
else:
|
|
260 |
self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId())
|
|
261 |
|
|
262 |
def OnOK(self, event):
|
|
263 |
value = self.GetValue()
|
|
264 |
texts = {"value" : value}
|
|
265 |
for function, message in self.Tests:
|
|
266 |
if not function(value):
|
|
267 |
message = wx.MessageDialog(self, message%texts, _("Error"), wx.OK|wx.ICON_ERROR)
|
|
268 |
message.ShowModal()
|
|
269 |
message.Destroy()
|
|
270 |
return
|
|
271 |
self.EndModal(wx.ID_OK)
|
|
272 |
event.Skip()
|
|
273 |
|
|
274 |
def GetValue(self):
|
|
275 |
return self.GetSizer().GetItem(1).GetWindow().GetValue()
|
|
276 |
|
|
277 |
def SetTests(self, tests):
|
|
278 |
self.Tests = tests
|
|
279 |
|
|
280 |
class BeremizTaskBarIcon(wx.TaskBarIcon):
|
|
281 |
TBMENU_START = wx.NewId()
|
|
282 |
TBMENU_STOP = wx.NewId()
|
|
283 |
TBMENU_CHANGE_NAME = wx.NewId()
|
|
284 |
TBMENU_CHANGE_PORT = wx.NewId()
|
|
285 |
TBMENU_CHANGE_INTERFACE = wx.NewId()
|
|
286 |
TBMENU_LIVE_SHELL = wx.NewId()
|
|
287 |
TBMENU_WXINSPECTOR = wx.NewId()
|
|
288 |
TBMENU_CHANGE_WD = wx.NewId()
|
|
289 |
TBMENU_QUIT = wx.NewId()
|
|
290 |
|
|
291 |
def __init__(self, pyroserver):
|
|
292 |
wx.TaskBarIcon.__init__(self)
|
|
293 |
self.pyroserver = pyroserver
|
|
294 |
# Set the image
|
|
295 |
self.UpdateIcon(None)
|
|
296 |
|
|
297 |
# bind some events
|
|
298 |
self.Bind(wx.EVT_MENU, self.OnTaskBarStartPLC, id=self.TBMENU_START)
|
|
299 |
self.Bind(wx.EVT_MENU, self.OnTaskBarStopPLC, id=self.TBMENU_STOP)
|
|
300 |
self.Bind(wx.EVT_MENU, self.OnTaskBarChangeName, id=self.TBMENU_CHANGE_NAME)
|
|
301 |
self.Bind(wx.EVT_MENU, self.OnTaskBarChangeInterface, id=self.TBMENU_CHANGE_INTERFACE)
|
|
302 |
self.Bind(wx.EVT_MENU, self.OnTaskBarLiveShell, id=self.TBMENU_LIVE_SHELL)
|
|
303 |
self.Bind(wx.EVT_MENU, self.OnTaskBarWXInspector, id=self.TBMENU_WXINSPECTOR)
|
|
304 |
self.Bind(wx.EVT_MENU, self.OnTaskBarChangePort, id=self.TBMENU_CHANGE_PORT)
|
|
305 |
self.Bind(wx.EVT_MENU, self.OnTaskBarChangeWorkingDir, id=self.TBMENU_CHANGE_WD)
|
|
306 |
self.Bind(wx.EVT_MENU, self.OnTaskBarQuit, id=self.TBMENU_QUIT)
|
|
307 |
|
|
308 |
def CreatePopupMenu(self):
|
|
309 |
"""
|
|
310 |
This method is called by the base class when it needs to popup
|
|
311 |
the menu for the default EVT_RIGHT_DOWN event. Just create
|
|
312 |
the menu how you want it and return it from this function,
|
|
313 |
the base class takes care of the rest.
|
|
314 |
"""
|
|
315 |
menu = wx.Menu()
|
|
316 |
menu.Append(self.TBMENU_START, _("Start PLC"))
|
|
317 |
menu.Append(self.TBMENU_STOP, _("Stop PLC"))
|
|
318 |
menu.Append(self.TBMENU_CHANGE_NAME, _("Change Name"))
|
|
319 |
menu.Append(self.TBMENU_CHANGE_INTERFACE, _("Change IP of interface to bind"))
|
|
320 |
menu.Append(self.TBMENU_LIVE_SHELL, _("Launch a live Python shell"))
|
|
321 |
menu.Append(self.TBMENU_WXINSPECTOR, _("Launch WX GUI inspector"))
|
|
322 |
menu.Append(self.TBMENU_CHANGE_PORT, _("Change Port Number"))
|
|
323 |
menu.AppendSeparator()
|
|
324 |
menu.Append(self.TBMENU_CHANGE_WD, _("Change working directory"))
|
|
325 |
menu.Append(self.TBMENU_QUIT, _("Quit"))
|
|
326 |
return menu
|
|
327 |
|
|
328 |
def MakeIcon(self, img):
|
|
329 |
"""
|
|
330 |
The various platforms have different requirements for the
|
|
331 |
icon size...
|
|
332 |
"""
|
|
333 |
if "wxMSW" in wx.PlatformInfo:
|
|
334 |
img = img.Scale(16, 16)
|
|
335 |
elif "wxGTK" in wx.PlatformInfo:
|
|
336 |
img = img.Scale(22, 22)
|
|
337 |
# wxMac can be any size upto 128x128, so leave the source img alone....
|
|
338 |
icon = wx.IconFromBitmap(img.ConvertToBitmap() )
|
|
339 |
return icon
|
|
340 |
|
|
341 |
def OnTaskBarStartPLC(self, evt):
|
|
342 |
if self.pyroserver.plcobj is not None:
|
|
343 |
self.pyroserver.plcobj.StartPLC()
|
|
344 |
evt.Skip()
|
|
345 |
|
|
346 |
def OnTaskBarStopPLC(self, evt):
|
|
347 |
if self.pyroserver.plcobj is not None:
|
|
348 |
self.pyroserver.plcobj.StopPLC()
|
|
349 |
evt.Skip()
|
|
350 |
|
|
351 |
def OnTaskBarChangeInterface(self, evt):
|
|
352 |
dlg = ParamsEntryDialog(None, _("Enter the IP of the interface to bind"), defaultValue=self.pyroserver.ip)
|
|
353 |
dlg.SetTests([(re.compile('\d{1,3}(?:\.\d{1,3}){3}$').match, _("IP is not valid!")),
|
|
354 |
( lambda ip :len([x for x in ip.split(".") if 0 <= int(x) <= 255]) == 4, _("IP is not valid!"))
|
|
355 |
])
|
|
356 |
if dlg.ShowModal() == wx.ID_OK:
|
|
357 |
self.pyroserver.ip = dlg.GetValue()
|
|
358 |
self.pyroserver.Stop()
|
|
359 |
evt.Skip()
|
|
360 |
|
|
361 |
def OnTaskBarChangePort(self, evt):
|
|
362 |
dlg = ParamsEntryDialog(None, _("Enter a port number "), defaultValue=str(self.pyroserver.port))
|
|
363 |
dlg.SetTests([(UnicodeType.isdigit, _("Port number must be an integer!")), (lambda port : 0 <= int(port) <= 65535 , _("Port number must be 0 <= port <= 65535!"))])
|
|
364 |
if dlg.ShowModal() == wx.ID_OK:
|
|
365 |
self.pyroserver.port = int(dlg.GetValue())
|
|
366 |
self.pyroserver.Stop()
|
|
367 |
evt.Skip()
|
|
368 |
|
|
369 |
def OnTaskBarChangeWorkingDir(self, evt):
|
|
370 |
dlg = wx.DirDialog(None, _("Choose a working directory "), self.pyroserver.workdir, wx.DD_NEW_DIR_BUTTON)
|
|
371 |
if dlg.ShowModal() == wx.ID_OK:
|
|
372 |
self.pyroserver.workdir = dlg.GetPath()
|
|
373 |
self.pyroserver.Stop()
|
|
374 |
evt.Skip()
|
|
375 |
|
|
376 |
def OnTaskBarChangeName(self, evt):
|
|
377 |
dlg = ParamsEntryDialog(None, _("Enter a name "), defaultValue=self.pyroserver.name)
|
|
378 |
dlg.SetTests([(lambda name : len(name) is not 0 , _("Name must not be null!"))])
|
|
379 |
if dlg.ShowModal() == wx.ID_OK:
|
|
380 |
self.pyroserver.name = dlg.GetValue()
|
|
381 |
self.pyroserver.Restart()
|
|
382 |
evt.Skip()
|
|
383 |
|
|
384 |
def OnTaskBarLiveShell(self, evt):
|
|
385 |
if self.pyroserver.plcobj is not None and self.pyroserver.plcobj.python_threads_vars is not None:
|
|
386 |
from wx import py
|
|
387 |
#frame = py.shell.ShellFrame(locals=self.pyroserver.plcobj.python_threads_vars)
|
|
388 |
frame = py.crust.CrustFrame(locals=self.pyroserver.plcobj.python_threads_vars)
|
|
389 |
frame.Show()
|
|
390 |
else:
|
|
391 |
wx.MessageBox(_("No running PLC"), _("Error"))
|
|
392 |
evt.Skip()
|
|
393 |
|
|
394 |
def OnTaskBarWXInspector(self, evt):
|
|
395 |
# Activate the widget inspection tool
|
|
396 |
from wx.lib.inspection import InspectionTool
|
|
397 |
if not InspectionTool().initialized:
|
|
398 |
InspectionTool().Init(locals=self.pyroserver.plcobj.python_threads_vars)
|
|
399 |
|
|
400 |
# Find a widget to be selected in the tree. Use either the
|
|
401 |
# one under the cursor, if any, or this frame.
|
|
402 |
wnd = wx.FindWindowAtPointer()
|
|
403 |
if not wnd:
|
|
404 |
wnd = wx.GetApp()
|
|
405 |
InspectionTool().Show(wnd, True)
|
|
406 |
evt.Skip()
|
|
407 |
|
|
408 |
def OnTaskBarQuit(self, evt):
|
|
409 |
self.pyroserver.Quit()
|
|
410 |
self.RemoveIcon()
|
|
411 |
wx.CallAfter(wx.GetApp().Exit)
|
|
412 |
evt.Skip()
|
|
413 |
|
|
414 |
def UpdateIcon(self, plcstatus):
|
|
415 |
if plcstatus is "Started" :
|
|
416 |
currenticon = self.MakeIcon(starticon.GetImage())
|
|
417 |
elif plcstatus is "Stopped":
|
|
418 |
currenticon = self.MakeIcon(stopicon.GetImage())
|
|
419 |
else:
|
|
420 |
currenticon = self.MakeIcon(defaulticon.GetImage())
|
|
421 |
self.SetIcon(currenticon, "Beremiz Service")
|
|
422 |
|
|
423 |
from runtime import PLCObject, PLCprint, ServicePublisher
|
|
424 |
import Pyro.core as pyro
|
|
425 |
|
|
426 |
if not os.path.isdir(WorkingDir):
|
|
427 |
os.mkdir(WorkingDir)
|
|
428 |
|
|
429 |
def default_evaluator(callable, *args, **kwargs):
|
|
430 |
return callable(*args,**kwargs)
|
|
431 |
|
|
432 |
class Server():
|
|
433 |
def __init__(self, servicename, ip, port, workdir, argv, autostart=False, statuschange=None, evaluator=default_evaluator, website=None):
|
|
434 |
self.continueloop = True
|
|
435 |
self.daemon = None
|
|
436 |
self.servicename = servicename
|
|
437 |
self.ip = ip
|
|
438 |
self.port = port
|
|
439 |
self.workdir = workdir
|
|
440 |
self.argv = argv
|
|
441 |
self.plcobj = None
|
|
442 |
self.servicepublisher = None
|
|
443 |
self.autostart = autostart
|
|
444 |
self.statuschange = statuschange
|
|
445 |
self.evaluator = evaluator
|
|
446 |
self.website = website
|
|
447 |
|
|
448 |
def Loop(self):
|
|
449 |
while self.continueloop:
|
|
450 |
self.Start()
|
|
451 |
|
|
452 |
def Restart(self):
|
|
453 |
self.Stop()
|
|
454 |
|
|
455 |
def Quit(self):
|
|
456 |
self.continueloop = False
|
|
457 |
self.Stop()
|
|
458 |
|
|
459 |
def Start(self):
|
|
460 |
pyro.initServer()
|
|
461 |
self.daemon=pyro.Daemon(host=self.ip, port=self.port)
|
|
462 |
self.plcobj = PLCObject(self.workdir, self.daemon, self.argv, self.statuschange, self.evaluator, self.website)
|
|
463 |
uri = self.daemon.connect(self.plcobj,"PLCObject")
|
|
464 |
|
|
465 |
print "Pyro port :",self.port
|
|
466 |
print "Pyro object's uri :",uri
|
|
467 |
print "Current working directory :",self.workdir
|
|
468 |
|
|
469 |
# Configure and publish service
|
|
470 |
# Not publish service if localhost in address params
|
|
471 |
if self.servicename is not None and self.ip != "localhost" and self.ip != "127.0.0.1":
|
|
472 |
print "Publishing service on local network"
|
|
473 |
self.servicepublisher = ServicePublisher.ServicePublisher()
|
|
474 |
self.servicepublisher.RegisterService(self.servicename, self.ip, self.port)
|
|
475 |
|
|
476 |
if self.autostart:
|
|
477 |
self.plcobj.StartPLC()
|
|
478 |
|
|
479 |
sys.stdout.flush()
|
|
480 |
|
|
481 |
self.daemon.requestLoop()
|
|
482 |
|
|
483 |
def Stop(self):
|
|
484 |
self.plcobj.StopPLC()
|
|
485 |
if self.servicepublisher is not None:
|
|
486 |
self.servicepublisher.UnRegisterService()
|
|
487 |
self.servicepublisher = None
|
|
488 |
self.daemon.shutdown(True)
|
|
489 |
|
|
490 |
if enabletwisted:
|
|
491 |
import warnings
|
|
492 |
with warnings.catch_warnings():
|
|
493 |
warnings.simplefilter("ignore")
|
|
494 |
try:
|
|
495 |
from threading import Thread, currentThread
|
|
496 |
if havewx:
|
|
497 |
from twisted.internet import wxreactor
|
|
498 |
wxreactor.install()
|
|
499 |
from twisted.internet import reactor, task
|
|
500 |
from twisted.python import log, util
|
|
501 |
from nevow import rend, appserver, inevow, tags, loaders, athena
|
|
502 |
from nevow.page import renderer
|
|
503 |
|
|
504 |
havetwisted = True
|
|
505 |
except:
|
|
506 |
print "Twisted unavailable !"
|
|
507 |
havetwisted = False
|
|
508 |
|
|
509 |
if havetwisted:
|
|
510 |
|
|
511 |
xhtml_header = '''<?xml version="1.0" encoding="utf-8"?>
|
|
512 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
|
|
513 |
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
|
514 |
'''
|
|
515 |
|
|
516 |
class PLCHMI(athena.LiveElement):
|
|
517 |
|
|
518 |
initialised = False
|
|
519 |
|
|
520 |
def HMIinitialised(self, result):
|
|
521 |
self.initialised = True
|
|
522 |
|
|
523 |
def HMIinitialisation(self):
|
|
524 |
self.HMIinitialised(None)
|
|
525 |
|
|
526 |
class DefaultPLCStartedHMI(PLCHMI):
|
|
527 |
docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[
|
|
528 |
tags.h1["PLC IS NOW STARTED"],
|
|
529 |
])
|
|
530 |
|
|
531 |
class PLCStoppedHMI(PLCHMI):
|
|
532 |
docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[
|
|
533 |
tags.h1["PLC IS STOPPED"],
|
|
534 |
])
|
|
535 |
|
|
536 |
class MainPage(athena.LiveElement):
|
|
537 |
jsClass = u"WebInterface.PLC"
|
|
538 |
docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[
|
|
539 |
tags.div(id='content')[
|
|
540 |
tags.div(render = tags.directive('PLCElement')),
|
|
541 |
]])
|
|
542 |
|
|
543 |
def __init__(self, *a, **kw):
|
|
544 |
athena.LiveElement.__init__(self, *a, **kw)
|
|
545 |
self.pcl_state = False
|
|
546 |
self.HMI = None
|
|
547 |
self.resetPLCStartedHMI()
|
|
548 |
|
|
549 |
def setPLCState(self, state):
|
|
550 |
self.pcl_state = state
|
|
551 |
if self.HMI is not None:
|
|
552 |
self.callRemote('updateHMI')
|
|
553 |
|
|
554 |
def setPLCStartedHMI(self, hmi):
|
|
555 |
self.PLCStartedHMIClass = hmi
|
|
556 |
|
|
557 |
def resetPLCStartedHMI(self):
|
|
558 |
self.PLCStartedHMIClass = DefaultPLCStartedHMI
|
|
559 |
|
|
560 |
def getHMI(self):
|
|
561 |
return self.HMI
|
|
562 |
|
|
563 |
def HMIexec(self, function, *args, **kwargs):
|
|
564 |
if self.HMI is not None:
|
|
565 |
getattr(self.HMI, function, lambda:None)(*args, **kwargs)
|
|
566 |
athena.expose(HMIexec)
|
|
567 |
|
|
568 |
def resetHMI(self):
|
|
569 |
self.HMI = None
|
|
570 |
|
|
571 |
def PLCElement(self, ctx, data):
|
|
572 |
return self.getPLCElement()
|
|
573 |
renderer(PLCElement)
|
|
574 |
|
|
575 |
def getPLCElement(self):
|
|
576 |
self.detachFragmentChildren()
|
|
577 |
if self.pcl_state:
|
|
578 |
f = self.PLCStartedHMIClass()
|
|
579 |
else:
|
|
580 |
f = PLCStoppedHMI()
|
|
581 |
f.setFragmentParent(self)
|
|
582 |
self.HMI = f
|
|
583 |
return f
|
|
584 |
athena.expose(getPLCElement)
|
|
585 |
|
|
586 |
def detachFragmentChildren(self):
|
|
587 |
for child in self.liveFragmentChildren[:]:
|
|
588 |
child.detach()
|
|
589 |
|
|
590 |
class WebInterface(athena.LivePage):
|
|
591 |
|
|
592 |
docFactory = loaders.stan([tags.raw(xhtml_header),
|
|
593 |
tags.html(xmlns="http://www.w3.org/1999/xhtml")[
|
|
594 |
tags.head(render=tags.directive('liveglue')),
|
|
595 |
tags.body[
|
|
596 |
tags.div[
|
|
597 |
tags.div( render = tags.directive( "MainPage" ))
|
|
598 |
]]]])
|
|
599 |
MainPage = MainPage()
|
|
600 |
PLCHMI = PLCHMI
|
|
601 |
|
|
602 |
def __init__(self, plcState=False, *a, **kw):
|
|
603 |
super(WebInterface, self).__init__(*a, **kw)
|
|
604 |
self.jsModules.mapping[u'WebInterface'] = util.sibpath(__file__, 'webinterface.js')
|
|
605 |
self.plcState = plcState
|
|
606 |
self.MainPage.setPLCState(plcState)
|
|
607 |
|
|
608 |
def getHMI(self):
|
|
609 |
return self.MainPage.getHMI()
|
|
610 |
|
|
611 |
def LoadHMI(self, hmi, jsmodules):
|
|
612 |
for name, path in jsmodules.iteritems():
|
|
613 |
self.jsModules.mapping[name] = os.path.join(WorkingDir, path)
|
|
614 |
self.MainPage.setPLCStartedHMI(hmi)
|
|
615 |
|
|
616 |
def UnLoadHMI(self):
|
|
617 |
self.MainPage.resetPLCStartedHMI()
|
|
618 |
|
|
619 |
def PLCStarted(self):
|
|
620 |
self.plcState = True
|
|
621 |
self.MainPage.setPLCState(True)
|
|
622 |
|
|
623 |
def PLCStopped(self):
|
|
624 |
self.plcState = False
|
|
625 |
self.MainPage.setPLCState(False)
|
|
626 |
|
|
627 |
def renderHTTP(self, ctx):
|
|
628 |
"""
|
|
629 |
Force content type to fit with SVG
|
|
630 |
"""
|
|
631 |
req = inevow.IRequest(ctx)
|
|
632 |
req.setHeader('Content-type', 'application/xhtml+xml')
|
|
633 |
return super(WebInterface, self).renderHTTP(ctx)
|
|
634 |
|
|
635 |
def render_MainPage(self, ctx, data):
|
|
636 |
f = self.MainPage
|
|
637 |
f.setFragmentParent(self)
|
|
638 |
return ctx.tag[f]
|
|
639 |
|
|
640 |
def child_(self, ctx):
|
|
641 |
self.MainPage.detachFragmentChildren()
|
|
642 |
return WebInterface(plcState=self.plcState)
|
|
643 |
|
|
644 |
def beforeRender(self, ctx):
|
|
645 |
d = self.notifyOnDisconnect()
|
|
646 |
d.addErrback(self.disconnected)
|
|
647 |
|
|
648 |
def disconnected(self, reason):
|
|
649 |
self.MainPage.resetHMI()
|
|
650 |
#print reason
|
|
651 |
#print "We will be called back when the client disconnects"
|
|
652 |
|
|
653 |
if havewx:
|
|
654 |
reactor.registerWxApp(app)
|
|
655 |
website = WebInterface()
|
|
656 |
site = appserver.NevowSite(website)
|
|
657 |
|
|
658 |
website_port = 8009
|
|
659 |
listening = False
|
|
660 |
while not listening:
|
|
661 |
try:
|
|
662 |
reactor.listenTCP(website_port, site)
|
|
663 |
listening = True
|
|
664 |
except:
|
|
665 |
website_port += 1
|
|
666 |
print "Http interface port :",website_port
|
|
667 |
else:
|
|
668 |
website = None
|
|
669 |
|
|
670 |
if havewx:
|
|
671 |
from threading import Semaphore
|
|
672 |
wx_eval_lock = Semaphore(0)
|
|
673 |
mythread = currentThread()
|
|
674 |
|
|
675 |
def statuschange(status):
|
|
676 |
wx.CallAfter(taskbar_instance.UpdateIcon,status)
|
|
677 |
|
|
678 |
eval_res = None
|
|
679 |
def wx_evaluator(callable, *args, **kwargs):
|
|
680 |
global eval_res
|
|
681 |
try:
|
|
682 |
eval_res=callable(*args,**kwargs)
|
|
683 |
except Exception,e:
|
|
684 |
PLCprint("#EXCEPTION : "+str(e))
|
|
685 |
finally:
|
|
686 |
wx_eval_lock.release()
|
|
687 |
|
|
688 |
def evaluator(callable, *args, **kwargs):
|
|
689 |
# call directly the callable function if call from the wx mainloop (avoid dead lock)
|
|
690 |
if(mythread == currentThread()):
|
|
691 |
callable(*args,**kwargs)
|
|
692 |
else:
|
|
693 |
wx.CallAfter(wx_evaluator,callable,*args,**kwargs)
|
|
694 |
wx_eval_lock.acquire()
|
|
695 |
return eval_res
|
|
696 |
|
|
697 |
pyroserver = Server(servicename, ip, port, WorkingDir, argv, autostart, statuschange, evaluator, website)
|
|
698 |
taskbar_instance = BeremizTaskBarIcon(pyroserver)
|
|
699 |
else:
|
|
700 |
pyroserver = Server(servicename, ip, port, WorkingDir, argv, autostart, website=website)
|
|
701 |
|
|
702 |
if havetwisted or havewx:
|
|
703 |
pyro_thread=Thread(target=pyroserver.Loop)
|
|
704 |
pyro_thread.start()
|
|
705 |
|
|
706 |
if havetwisted:
|
|
707 |
reactor.run()
|
|
708 |
elif havewx:
|
|
709 |
app.MainLoop()
|
|
710 |
else:
|
|
711 |
try :
|
|
712 |
pyroserver.Loop()
|
|
713 |
except KeyboardInterrupt,e:
|
|
714 |
pass
|
|
715 |
pyroserver.Quit()
|
|
716 |
sys.exit(0)
|