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