140 svghmi_watchdog.feed() |
140 svghmi_watchdog.feed() |
141 |
141 |
142 class HMIWebSocketServerFactory(WebSocketServerFactory): |
142 class HMIWebSocketServerFactory(WebSocketServerFactory): |
143 protocol = HMIProtocol |
143 protocol = HMIProtocol |
144 |
144 |
145 svghmi_root = None |
145 svghmi_servers = {} |
146 svghmi_listener = None |
|
147 svghmi_send_thread = None |
146 svghmi_send_thread = None |
148 |
147 |
149 def SendThreadProc(): |
148 def SendThreadProc(): |
150 global svghmi_session |
149 global svghmi_session |
151 size = ctypes.c_uint32() |
150 size = ctypes.c_uint32() |
163 pass |
162 pass |
164 else: |
163 else: |
165 # this happens when finishing |
164 # this happens when finishing |
166 break |
165 break |
167 |
166 |
168 |
167 def AddPathToSVGHMIServers(path, factory): |
169 def watchdog_trigger(): |
168 for k,v in svghmi_servers.iteritems(): |
170 print("SVGHMI watchdog trigger") |
169 svghmi_root, svghmi_listener, path_list = v |
171 |
170 svghmi_root.putChild(path, factory()) |
172 |
171 |
173 # Called by PLCObject at start |
172 # Called by PLCObject at start |
174 def _runtime_00_svghmi_start(): |
173 def _runtime_00_svghmi_start(): |
175 global svghmi_listener, svghmi_root, svghmi_send_thread |
174 global svghmi_send_thread |
176 |
|
177 svghmi_root = Resource() |
|
178 svghmi_root.putChild("ws", WebSocketResource(HMIWebSocketServerFactory())) |
|
179 |
|
180 svghmi_listener = reactor.listenTCP(8008, Site(svghmi_root), interface='localhost') |
|
181 |
175 |
182 # start a thread that call the C part of SVGHMI |
176 # start a thread that call the C part of SVGHMI |
183 svghmi_send_thread = Thread(target=SendThreadProc, name="SVGHMI Send") |
177 svghmi_send_thread = Thread(target=SendThreadProc, name="SVGHMI Send") |
184 svghmi_send_thread.start() |
178 svghmi_send_thread.start() |
185 |
179 |
186 |
180 |
187 # Called by PLCObject at stop |
181 # Called by PLCObject at stop |
188 def _runtime_00_svghmi_stop(): |
182 def _runtime_00_svghmi_stop(): |
189 global svghmi_listener, svghmi_root, svghmi_send_thread, svghmi_session |
183 global svghmi_send_thread, svghmi_session |
190 |
184 |
191 if svghmi_session is not None: |
185 if svghmi_session is not None: |
192 svghmi_session.close() |
186 svghmi_session.close() |
193 svghmi_root.delEntity("ws") |
|
194 svghmi_root = None |
|
195 svghmi_listener.stopListening() |
|
196 svghmi_listener = None |
|
197 # plc cleanup calls svghmi_(locstring)_cleanup and unlocks send thread |
187 # plc cleanup calls svghmi_(locstring)_cleanup and unlocks send thread |
198 svghmi_send_thread.join() |
188 svghmi_send_thread.join() |
199 svghmi_send_thread = None |
189 svghmi_send_thread = None |
200 |
190 |
201 |
191 |