-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpfeifferLAN.py
executable file
·113 lines (88 loc) · 3.87 KB
/
pfeifferLAN.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
import os, sys
import numpy as np
from daemon import SimpleFactory, SimpleProtocol
from command import Command
from daemon import catch
class DaemonProtocol(SimpleProtocol):
_debug = False # Display all traffic for debug purposes
_simulator = False
@catch
def processMessage(self, string):
# It will handle some generic messages and return pre-parsed Command object
cmd = SimpleProtocol.processMessage(self, string)
if cmd is None:
return
obj = self.object # Object holding the state
hw = obj['hw'] # HW factory
if cmd.name == 'get_status':
if self._simulator:
self.message('status hw_connected=1 status=0 pressure=%g simulator=1' % (np.random.uniform(1.0, 10.0)))
else:
self.message('status hw_connected=%s status=%d pressure=%g' % (self.object['hw_connected'], self.object['status'], self.object['pressure']))
else:
if obj['hw_connected']:
# Pass all other commands directly to hardware
hw.messageAll(string, name='hw', type='hw')
class HWProtocol(SimpleProtocol):
_debug = False # Display all traffic for debug purposes
@catch
def connectionMade(self):
self.object['hw_connected'] = 1
self.name = 'hw'
self.type = 'hw'
SimpleProtocol.connectionMade(self)
@catch
def connectionLost(self, reason):
self.object['hw_connected'] = 0
SimpleProtocol.connectionLost(self, reason)
@catch
def processMessage(self, string):
# Process the device reply
if self._debug:
print ("hw > %s" % string)
if len(string) and string[0] >= '0' and string[0] <= '6' and 'E' in string:
# b,sx.xxxxEsxx
self.object['status'] = int(string[0])
self.object['pressure'] = float(string[2:])
@catch
def message(self, string):
"""Sending outgoing message"""
if self._debug:
print (">> serial >>", string)
self.transport.write(string.encode('ascii'))
self.transport.write("\r\n".encode('ascii'))
@catch
def update(self):
# Request the hardware state from the device
self.message('COM')
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser(usage="usage: %prog [options] arg")
parser.add_option('-H', '--hw-host', help='Hardware host to connect', action='store', dest='hw_host', default='192.168.1.11')
parser.add_option('-P', '--hw-port', help='Hardware port to connect', action='store', dest='hw_port', type='int', default=8000)
parser.add_option('-p', '--port', help='Daemon port', action='store', dest='port', type='int', default=7023)
parser.add_option('-n', '--name', help='Daemon name', action='store', dest='name', default='pfeiffer')
parser.add_option("-D", '--debug', help='Debug mode', action="store_true", dest="debug")
parser.add_option("-S", '--simulator', help='Simulator mode', action="store_true", dest="simulator")
(options,args) = parser.parse_args()
# Object holding actual state and work logic.
# May be anything that will be passed by reference - list, dict, object etc
obj = {'hw_connected':0, 'status':-1, 'pressure':0}
# Factories for daemon and hardware connections
# We need two different factories as the protocols are different
daemon = SimpleFactory(DaemonProtocol, obj)
hw = SimpleFactory(HWProtocol, obj)
daemon.name = options.name
obj['daemon'] = daemon
obj['hw'] = hw
if options.debug:
daemon._protocol._debug = True
hw._protocol._debug = True
if options.simulator:
daemon._protocol._simulator = True
# Incoming connections
daemon.listen(options.port)
# Outgoing connection
hw.connect(options.hw_host, options.hw_port)
daemon._reactor.run()