-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebservice_serial.py
47 lines (33 loc) · 1.73 KB
/
webservice_serial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from application.component.component import Component
from webservice_serial.webservice_serial_client import WebServiceSerialClient
from webservice_serial.request_message_processor import RequestMessageProcessor
class WebServiceSerial(Component):
port = 8888
def __init__(self, application, target, ws_port=3000):
"""
:param Application application: that AndroidController will be executed
:param Target target: Device protocol expected to communication
:param int ws_port: WebService port
"""
super(WebServiceSerial, self).__init__(application)
self.target = target
self._client = WebServiceSerialClient('localhost', WebServiceSerial.port)
self.request_message_processor = RequestMessageProcessor(ws_port)
def init(self):
self._client.connected_listener = self._on_connected
self._client.message_listener = self._process_message
self.request_message_processor.processed_listener = self._on_processed
self.target.init(self.application, WebServiceSerial.port)
self._client.connect()
def close(self):
self.request_message_processor.close()
self.target.close()
def _on_connected(self):
self.application.log('AndroidController - DisplayView connected')
def _process_message(self, message):
self.application.log('AndroidController - Message received: {}', message)
self.request_message_processor.process(message)
def _on_processed(self, request_message, response_message):
self.application.log('AndroidController - Message sent: {}', response_message)
response_message = self.target.process(request_message, response_message)
self._client.send(response_message)