23 |
23 |
24 svghmi_send_collect = PLCBinary.svghmi_send_collect |
24 svghmi_send_collect = PLCBinary.svghmi_send_collect |
25 svghmi_send_collect.restype = ctypes.c_int # error or 0 |
25 svghmi_send_collect.restype = ctypes.c_int # error or 0 |
26 svghmi_send_collect.argtypes = [ |
26 svghmi_send_collect.argtypes = [ |
27 ctypes.POINTER(ctypes.c_uint32), # size |
27 ctypes.POINTER(ctypes.c_uint32), # size |
28 ctypes.POINTER(ctypes.c_void_p)] # data ptr |
28 ctypes.POINTER(ctypes.c_char_p)] # data ptr |
29 # TODO multiclient : switch to arrays |
29 # TODO multiclient : switch to arrays |
30 |
30 |
31 svghmi_recv_dispatch = PLCBinary.svghmi_recv_dispatch |
31 svghmi_recv_dispatch = PLCBinary.svghmi_recv_dispatch |
32 svghmi_recv_dispatch.restype = ctypes.c_int # error or 0 |
32 svghmi_recv_dispatch.restype = ctypes.c_int # error or 0 |
33 svghmi_recv_dispatch.argtypes = [ |
33 svghmi_recv_dispatch.argtypes = [ |
34 ctypes.c_uint32, # size |
34 ctypes.c_uint32, # size |
35 ctypes.POINTER(ctypes.c_void_p)] # data ptr |
35 ctypes.c_char_p] # data ptr |
36 # TODO multiclient : switch to arrays |
36 # TODO multiclient : switch to arrays |
37 |
37 |
38 class HMISession(object): |
38 class HMISession(object): |
39 def __init__(self, protocol_instance): |
39 def __init__(self, protocol_instance): |
40 global svghmi_session |
40 global svghmi_session |
57 |
57 |
58 # TODO multiclient : |
58 # TODO multiclient : |
59 # svghmi_sessions.remove(self) |
59 # svghmi_sessions.remove(self) |
60 |
60 |
61 def onMessage(self, msg): |
61 def onMessage(self, msg): |
62 # TODO : pass it to the C side recieve_message() |
62 # pass message to the C side recieve_message() |
63 # update HMITree |
63 c_string = ctypes.c_char_p(msg) |
64 # - values |
64 c_string_pointer = ctypes.c_void_p(ctypes.addressof(c_string)) |
65 # - refresh rates / subsriptions |
65 svghmi_recv_dispatch(len(msg), msg) |
66 |
66 |
67 # TODO multiclient : pass client index as well |
67 # TODO multiclient : pass client index as well |
68 |
68 |
69 # |
|
70 svghmi_recv_dispatch(len(msg), ctypes.c_void_p.from_buffer_copy(msg)) |
|
71 |
69 |
72 def sendMessage(self, msg): |
70 def sendMessage(self, msg): |
73 self.sendMessage(msg, True) |
71 self.sendMessage(msg, True) |
74 |
72 |
75 class HMIProtocol(WebSocketServerProtocol): |
73 class HMIProtocol(WebSocketServerProtocol): |
90 def onMessage(self, msg, isBinary): |
88 def onMessage(self, msg, isBinary): |
91 self._hmi_session.onMessage(msg) |
89 self._hmi_session.onMessage(msg) |
92 print msg |
90 print msg |
93 #self.sendMessage(msg, binary) |
91 #self.sendMessage(msg, binary) |
94 |
92 |
|
93 class HMIWebSocketServerFactory(WebSocketServerFactory): |
|
94 protocol = HMIProtocol |
|
95 |
95 svghmi_root = None |
96 svghmi_root = None |
96 svghmi_listener = None |
97 svghmi_listener = None |
97 svghmi_send_thread = None |
98 svghmi_send_thread = None |
98 |
99 |
99 def SendThreadProc(): |
100 def SendThreadProc(): |
100 global svghmi_session |
101 global svghmi_session |
101 size = ctypes.c_uint32() |
102 size = ctypes.c_uint32() |
102 ptr = ctypes.c_void_p() |
103 ptr = ctypes.c_char_p() |
103 res = 0 |
104 res = 0 |
104 while svghmi_send_collect(ctypes.byref(size), ctypes.byref(ptr)) == 0 and \ |
105 while svghmi_send_collect(ctypes.byref(size), ctypes.byref(ptr)) == 0 and \ |
105 svghmi_session is not None and \ |
106 svghmi_session is not None and \ |
106 svghmi_session.sendMessage(ctypes.string_at(ptr,size)) == 0: |
107 svghmi_session.sendMessage(ctypes.string_at(ptr,size)) == 0: |
107 pass |
108 pass |
108 |
109 |
109 # TODO multiclient : dispatch to sessions |
110 # TODO multiclient : dispatch to sessions |
110 |
111 |
111 |
112 |
|
113 |
112 # Called by PLCObject at start |
114 # Called by PLCObject at start |
113 def _runtime_svghmi0_start(): |
115 def _runtime_svghmi0_start(): |
114 global svghmi_listener, svghmi_root, svghmi_send_thread |
116 global svghmi_listener, svghmi_root, svghmi_send_thread |
115 |
117 |
116 svghmi_root = Resource() |
118 svghmi_root = Resource() |
|
119 svghmi_root.putChild("ws", WebSocketResource(HMIWebSocketServerFactory())) |
117 |
120 |
118 wsfactory = WebSocketServerFactory() |
121 svghmi_listener = reactor.listenTCP(8008, Site(svghmi_root)) |
119 wsfactory.protocol = HMIProtocol |
|
120 |
|
121 svghmi_root.putChild("ws", WebSocketResource(wsfactory)) |
|
122 |
|
123 sitefactory = Site(svghmi_root) |
|
124 |
|
125 svghmi_listener = reactor.listenTCP(8008, sitefactory) |
|
126 |
122 |
127 # start a thread that call the C part of SVGHMI |
123 # start a thread that call the C part of SVGHMI |
128 svghmi_send_thread = Thread(target=SendThreadProc, name="SVGHMI Send") |
124 svghmi_send_thread = Thread(target=SendThreadProc, name="SVGHMI Send") |
129 svghmi_send_thread.start() |
125 svghmi_send_thread.start() |
130 |
126 |