This repository has been archived by the owner on Jun 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.py
119 lines (94 loc) · 3.15 KB
/
state.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
import time
import json
import random
import threading
import re
import os.path
#m- motion
#s- status
#o- output
class STATE_test():
def __init__(self, USB, FILE):
self.cmd_list = ["W", "T", "F"]
self.USB = USB
self.FILE = FILE
def __del__(self):
pass
def load_test(self):
cmd = random.choice(self.cmd_list)
self.random_test('test_state', cmd, 1)
self.tests_list = self.data["CMDS"]
def random_test(self, group, cmd, y):
while 1==1:
x = random.randint(1,y)
name = ('tests/{}_{}_{}.json'.format(group, cmd, x))
if os.path.exists(name) == 1:
break
with open(name) as json_file:
self.data = json.load(json_file)
def start(self):
while 1==1:
# print(threading.currentThread())
is_passed = 1
self.load_test()
for self.test in self.tests_list:
if self.test["DIR"]=='R':
if self.recive()==0:
is_passed = 0
break
elif self.test["DIR"]=='T':
if self.transmite()==0:
is_passed = 0
break
else:
FILE.write_f("Bledna komenda okreslajaca kierunek transferu danych")
is_passed = 0
break
if is_passed==0:
self.failed()
else:
self.passed()
time.sleep(0.05)
self.USB.buf_o.clear()
def failed(self):
self.FILE.write_f("{}==> FAILED\n".format(self.data['DESC']))
def passed(self):
self.FILE.write_p("{}==> PASSED\n".format(self.data['DESC']))
def recive(self):
t = time.time()
while time.time() < t+(self.test["TIMEOUT"]/1000):
time.sleep(0.05)
if len(self.USB.buf_o) != 0:
self.answ = self.USB.buf_o.pop().decode('utf-8')
if self.is_correct_answ()==1:
return 1
else:
return 0
return 0
def is_correct_answ(self):
regex = "^{}\s".format(self.test["CMD"])
if re.search(regex, self.answ):
if self.test["CMD"]=='F':
regex = "^{}\su\s\w\sd\s\w$".format(self.test["CMD"])
if re.search(regex, self.answ):
return 1
else:
return 0
elif self.test["CMD"]=='T':
regex = "^{}\sc\s\w\sd\s\wm\s\w$".format(self.test["CMD"])
if re.search(regex, self.answ):
return 1
else:
return 0
elif self.test["CMD"]=='W':
regex = "^{}\s{}$".format(self.test["CMD"], (self.test["VAL"]))
if re.search(regex, self.answ):
return 1
else:
return 0
else:
return 0
def transmite(self):
cmd_to_send = self.test["CMD_TO_SEND"].encode('utf-8')
self.USB.send(cmd_to_send)
return 1