author | Edouard Tisserant |
Thu, 12 Sep 2019 12:56:47 +0200 | |
branch | svghmi |
changeset 2774 | 9857b4b0d979 |
parent 2773 | 6e5e752372c8 |
child 2775 | 3b93409ba22c |
permissions | -rw-r--r-- |
2771
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
1 |
#!/usr/bin/env python |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
2 |
# -*- coding: utf-8 -*- |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
3 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
4 |
# This file is part of Beremiz |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
5 |
# Copyright (C) 2019: Edouard TISSERANT |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
6 |
# See COPYING file for copyrights details. |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
7 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
8 |
from __future__ import absolute_import |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
9 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
10 |
from twisted.web.server import Site |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
11 |
from twisted.web.resource import Resource |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
12 |
from twisted.internet import reactor |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
13 |
from twisted.web.static import File |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
14 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
15 |
from autobahn.twisted.websocket import WebSocketServerFactory, WebSocketServerProtocol |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
16 |
from autobahn.twisted.resource import WebSocketResource |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
17 |
|
2773 | 18 |
# TODO multiclient : |
19 |
# session list lock |
|
20 |
# svghmi_sessions = [] |
|
21 |
||
22 |
svghmi_session = None |
|
2771
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
23 |
|
2774
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
24 |
svghmi_send_collect = PLCBinary.svghmi_send_collect |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
25 |
svghmi_send_collect.restype = ctypes.c_int # error or 0 |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
26 |
svghmi_send_collect.argtypes = [ |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
27 |
ctypes.POINTER(ctypes.c_uint32), # size |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
28 |
ctypes.POINTER(ctypes.c_void_p)] # data ptr |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
29 |
# TODO multiclient : switch to arrays |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
30 |
|
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
31 |
svghmi_recv_dispatch = PLCBinary.svghmi_recv_dispatch |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
32 |
svghmi_recv_dispatch.restype = ctypes.c_int # error or 0 |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
33 |
svghmi_recv_dispatch.argtypes = [ |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
34 |
ctypes.POINTER(ctypes.c_uint32), # size |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
35 |
ctypes.POINTER(ctypes.c_void_p)] # data ptr |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
36 |
# TODO multiclient : switch to arrays |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
37 |
|
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
38 |
def SendThreadProc(): |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
39 |
assert(svghmi_session) |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
40 |
size = ctypes.c_uint32() |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
41 |
ptr = ctypes.c_void_p() |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
42 |
while res == 0: |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
43 |
res = svghmi_send_collect(ctypes.byref(size), ctypes.byref(ptr)) |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
44 |
|
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
45 |
# TODO multiclient : dispatch to sessions |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
46 |
svghmi_session.sendMessage(ctypes.string_at(ptr,size)) |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
47 |
|
2771
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
48 |
class HMISession(object): |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
49 |
def __init__(self, protocol_instance): |
2773 | 50 |
global svghmi_session |
51 |
||
52 |
# TODO: kill existing session for robustness |
|
53 |
assert(svghmi_session is None) |
|
54 |
||
55 |
svghmi_session = self |
|
56 |
self.protocol_instance = protocol_instance |
|
2771
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
57 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
58 |
# TODO multiclient : |
2773 | 59 |
# svghmi_sessions.append(self) |
2771
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
60 |
# get a unique bit index amont other svghmi_sessions, |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
61 |
# so that we can match flags passed by C->python callback |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
62 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
63 |
def __del__(self): |
2773 | 64 |
global svghmi_session |
65 |
assert(svghmi_session) |
|
66 |
svghmi_session = None |
|
67 |
||
68 |
# TODO multiclient : |
|
69 |
# svghmi_sessions.remove(self) |
|
2771
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
70 |
|
2772 | 71 |
def onMessage(self, msg): |
2771
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
72 |
# TODO : pass it to the C side recieve_message() |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
73 |
# update HMITree |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
74 |
# - values |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
75 |
# - refresh rates / subsriptions |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
76 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
77 |
# TODO multiclient : pass client index as well |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
78 |
pass |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
79 |
|
2774
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
80 |
def sendMessage(self, msg): |
9857b4b0d979
SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents:
2773
diff
changeset
|
81 |
self.sendMessage(msg, True) |
2771
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
82 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
83 |
class HMIProtocol(WebSocketServerProtocol): |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
84 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
85 |
def __init__(self, *args, **kwargs): |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
86 |
self._hmi_session = None |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
87 |
WebSocketServerProtocol.__init__(self, *args, **kwargs) |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
88 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
89 |
def onOpen(self): |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
90 |
self._hmi_session = HMISession(self) |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
91 |
print "open" |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
92 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
93 |
def onClose(self, wasClean, code, reason): |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
94 |
del self._hmi_session |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
95 |
self._hmi_session = None |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
96 |
print "close" |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
97 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
98 |
def onMessage(self, msg, isBinary): |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
99 |
self._hmi_session.onMessage(msg) |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
100 |
print msg |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
101 |
#self.sendMessage(msg, binary) |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
102 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
103 |
svghmi_root = None |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
104 |
svghmi_listener = None |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
105 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
106 |
# Called by PLCObject at start |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
107 |
def _runtime_svghmi0_start(): |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
108 |
global svghmi_listener, svghmi_root |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
109 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
110 |
svghmi_root = Resource() |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
111 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
112 |
wsfactory = WebSocketServerFactory() |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
113 |
wsfactory.protocol = HMIProtocol |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
114 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
115 |
# svghmi_root.putChild("",File(".svg")) |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
116 |
svghmi_root.putChild("ws",WebSocketResource(wsfactory)) |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
117 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
118 |
sitefactory = Site(svghmi_root) |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
119 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
120 |
svghmi_listener = reactor.listenTCP(8008, sitefactory) |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
121 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
122 |
# TODO |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
123 |
# start a thread that call the C part of SVGHMI |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
124 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
125 |
|
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
126 |
# Called by PLCObject at stop |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
127 |
def _runtime_svghmi0_stop(): |
361366b891ca
WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff
changeset
|
128 |
global svghmi_listener |
2772 | 129 |
svghmi_listener.stopListening() |