-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadam.py
170 lines (153 loc) · 5.68 KB
/
adam.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import re
__author__ = 'ruggero'
class Adam(object):
def __init__(self, model, address='00'):
path = './model/%s.dat' % model
self.__model = model
self.__id = address
try:
load = open(path, 'r')
except IOError as e:
print ("I/O error({0}): {1}".format(e.errno, e.strerror))
raise ValueError('model not defined', 0)
except:
raise ValueError('No I/O error here?!?, you are fucked')
self.commands = eval(load.read())
def send_command(self, command, **kwargs):
"""
this method given a command and the list of parameters needed return a bytearray and a reciver class
"""
# primary check
if command in self.commands.keys():
command = self.command_parsing(command)
else:
raise ValueError('command not defined')
# build the command
pkg_send = bytearray()
for c in command:
if c == 'AA':
pkg_send += bytearray(self.__id, encoding='utf-8')
# elif c in kwargs.keys() or c == 'N':
elif c in kwargs.keys():
pkg_send += bytearray(kwargs.pop(c), encoding='utf-8')
elif len(c) == 1:
pkg_send.append(ord(c))
else:
raise ValueError('error in the parameters')
# add the CR character
pkg_send.append(13)
# creation of the receiver function
def rec(received):
debug = False
nonlocal command
path = './model/receive.dat'
try:
load = open(path, 'r')
except IOError as e:
print("I/O error({0}): {1}".format(e.errno, e.strerror))
raise ValueError('receive database not found')
except:
raise ValueError('No I/O error here?!?, you are fucked')
# answer database parsing here
commands = eval(load.read())
# if there is a standard answer, do the parsing
if command in commands.keys():
parse = commands[command]
rec_command = []
supp = []
flag = False
for i in range(len(parse)):
supp.append(parse[i])
if parse[i] == '(' or flag:
flag = True
if command[i] == ')':
flag = False
rec.append(''.join(supp))
supp = []
continue
else:
continue
if i < len(parse)-1:
if parse[i+1] == parse[i]:
continue
else:
rec_command.append(''.join(supp))
supp = []
else:
rec_command.append(''.join(supp))
rec_command = tuple(rec_command)
else:
rec_command = None
# Decoding starts here
if len(received) == 0:
return [('data', None),('error', '0 data received')]
received = received.decode('utf-8')
if received[0] == '?':
# wrong command has been sent
return [('AA', received[1:-1]), ('error', 'wrong command has been sent')]
elif received[0] == '!':
# commando with info has been sent
if rec_command is None:
return [('info', received[1:])]
else:
supp = []
pointer = 0
for i in rec_command:
supp.append(received[pointer:pointer+len(i)])
pointer += len(i)
xmap = zip(rec_command, supp)
if debug: print(list(xmap))
return list(xmap)
elif received[0] == '>':
# command with data has been sent
l1=[]
l = re.findall(r'([-+]\d+.\d+)', received[1:])
for i in range(len(l)):
s = 'IN' + str(i)
l1.append((s, l[i]))
if debug: print(l1)
return l1
else:
return [('data', None), ('error', 'not standard pack')]
return pkg_send, rec
def command_parsing(self, command):
cmd = self.commands[command][0]
parse = []
supp = []
for i in range(len(cmd)-1):
supp.append(cmd[i])
if cmd[i+1] == cmd[i]:
continue
else:
parse.append(''.join(supp))
supp = []
else:
i += 1
supp.append(cmd[i])
parse.append(''.join(supp))
return tuple(parse)
def get_id(self):
return self.__id
def cmd(self):
s = ''
for k in self.commands.keys():
s = s + k + ':' + str(self.commands[k][0]) + '\n'
return s
def __str__(self):
s = 'initialized module = ' + str(self.__model) + '\n'
s = s + 'initialized address = ' + str(self.__id) + '\n\n'
for k in self.commands.keys():
s = s + k + ':' + str(self.commands[k][0]) + '\n' + str(self.commands[k][1]) + '\n\n'
return s
if __name__ == '__main__':
sens1 = Adam('4017')
a, b = sens1.send_command('ConfB')
# print(a)
# print(b.command)
print('-----')
print(b(b'!04090680\r'))
print('----')
print(b(b'?04\r'))
print('----')
a, b = sens1.send_command('ReadAll')
print(b(b'>+0.5-0.4+9.2-7.5\r'))