36 from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS |
36 from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS |
37 from autobahn.wamp import types |
37 from autobahn.wamp import types |
38 from autobahn.wamp.exception import TransportLost |
38 from autobahn.wamp.exception import TransportLost |
39 from autobahn.wamp.serializer import MsgPackSerializer |
39 from autobahn.wamp.serializer import MsgPackSerializer |
40 |
40 |
41 from runtime import PlcStatus |
|
42 |
41 |
43 _WampSession = None |
42 _WampSession = None |
44 _WampConnection = None |
43 _WampConnection = None |
45 _WampSessionEvent = Event() |
44 _WampSessionEvent = Event() |
46 |
45 |
55 def onLeave(self, details): |
54 def onLeave(self, details): |
56 global _WampSession |
55 global _WampSession |
57 _WampSessionEvent.clear() |
56 _WampSessionEvent.clear() |
58 _WampSession = None |
57 _WampSession = None |
59 print('WAMP session left') |
58 print('WAMP session left') |
60 |
|
61 |
|
62 PLCObjDefaults = { |
|
63 "StartPLC": False, |
|
64 "GetTraceVariables": ("Broken", None), |
|
65 "GetPLCstatus": (PlcStatus.Broken, None), |
|
66 "RemoteExec": (-1, "RemoteExec script failed!") |
|
67 } |
|
68 |
59 |
69 |
60 |
70 def _WAMP_connector_factory(cls, uri, confnodesroot): |
61 def _WAMP_connector_factory(cls, uri, confnodesroot): |
71 """ |
62 """ |
72 WAMP://127.0.0.1:12345/path#realm#ID |
63 WAMP://127.0.0.1:12345/path#realm#ID |
108 global _WampConnection |
99 global _WampConnection |
109 _WampConnection = RegisterWampClient() |
100 _WampConnection = RegisterWampClient() |
110 AddToDoBeforeQuit(reactor.stop) |
101 AddToDoBeforeQuit(reactor.stop) |
111 reactor.run(installSignalHandlers=False) |
102 reactor.run(installSignalHandlers=False) |
112 |
103 |
113 def WampSessionProcMapper(funcname): |
|
114 wampfuncname = text('.'.join((ID, funcname))) |
|
115 |
|
116 def catcher_func(*args, **kwargs): |
|
117 if _WampSession is not None: |
|
118 try: |
|
119 return threads.blockingCallFromThread( |
|
120 reactor, _WampSession.call, wampfuncname, |
|
121 *args, **kwargs) |
|
122 except TransportLost: |
|
123 confnodesroot.logger.write_error(_("Connection lost!\n")) |
|
124 confnodesroot._SetConnector(None) |
|
125 except Exception: |
|
126 errmess = traceback.format_exc() |
|
127 confnodesroot.logger.write_error(errmess+"\n") |
|
128 print(errmess) |
|
129 # confnodesroot._SetConnector(None) |
|
130 return PLCObjDefaults.get(funcname) |
|
131 return catcher_func |
|
132 |
|
133 class WampPLCObjectProxy(object): |
104 class WampPLCObjectProxy(object): |
134 def __init__(self): |
105 def __init__(self): |
135 global _WampConnection |
106 global _WampConnection |
136 if not reactor.running: |
107 if not reactor.running: |
137 Thread(target=ThreadProc).start() |
108 Thread(target=ThreadProc).start() |
145 def __del__(self): |
116 def __del__(self): |
146 _WampConnection.disconnect() |
117 _WampConnection.disconnect() |
147 # |
118 # |
148 # reactor.stop() |
119 # reactor.stop() |
149 |
120 |
|
121 def WampSessionProcMapper(self, funcname): |
|
122 wampfuncname = text('.'.join((ID, funcname))) |
|
123 |
|
124 def catcher_func(*args, **kwargs): |
|
125 if _WampSession is not None: |
|
126 try: |
|
127 return threads.blockingCallFromThread( |
|
128 reactor, _WampSession.call, wampfuncname, |
|
129 *args, **kwargs) |
|
130 except TransportLost: |
|
131 confnodesroot.logger.write_error(_("Connection lost!\n")) |
|
132 confnodesroot._SetConnector(None) |
|
133 except Exception: |
|
134 errmess = traceback.format_exc() |
|
135 confnodesroot.logger.write_error(errmess+"\n") |
|
136 print(errmess) |
|
137 # confnodesroot._SetConnector(None) |
|
138 return self.PLCObjDefaults.get(funcname) |
|
139 return catcher_func |
|
140 |
150 def __getattr__(self, attrName): |
141 def __getattr__(self, attrName): |
151 member = self.__dict__.get(attrName, None) |
142 member = self.__dict__.get(attrName, None) |
152 if member is None: |
143 if member is None: |
153 member = WampSessionProcMapper(attrName) |
144 member = self.WampSessionProcMapper(attrName) |
154 self.__dict__[attrName] = member |
145 self.__dict__[attrName] = member |
155 return member |
146 return member |
156 |
147 |
157 # TODO : GetPLCID() |
148 # TODO : GetPLCID() |
158 # TODO : PSK.UpdateID() |
149 # TODO : PSK.UpdateID() |