-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarStateMachine.py
executable file
·105 lines (82 loc) · 2.79 KB
/
carStateMachine.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
#!/usr/bin/env python
# dependencies:
# pip install transitions
# or from https://github.com/tyarkoni/transitions
from transitions import Machine
import os
import time
import datetime
# Statemachine:
class CarDetector(object):
states=['waitingForCar', 'getIgnitionTime', 'departTasks', 'arrivalTasks', 'waitForNoCar']
transitions = [
{ 'trigger': 'detected', 'source': 'waitingForCar', 'dest': 'getIgnitionTime' },
{ 'trigger': 'error', 'source': 'getIgnitionTime', 'dest': 'waitingForCar' },
{ 'trigger': 'shorttime', 'source': 'getIgnitionTime', 'dest': 'departTasks' },
{ 'trigger': 'longtime', 'source': 'getIgnitionTime', 'dest': 'arrivalTasks' },
{ 'trigger': 'notdetected', 'source': 'waitForNoCar', 'dest': 'waitingForCar' },
{ 'trigger': 'advance', 'source': 'departTasks', 'dest': 'waitForNoCar' },
{ 'trigger': 'advance', 'source': 'arrivalTasks', 'dest': 'waitForNoCar' }
]
def __init__(self, name, IPAddress):
self.name = name
self.IPAddress = IPAddress
self.machine = Machine(self, states=CarDetector.states, transitions=CarDetector.transitions)
def on_enter_waitingForCar(self):
self.printMessage()
while(not self.CarDetected()):
time.sleep(5)
self.detected()
def on_enter_getIgnitionTime(self):
self.printMessage()
try:
if int(self.ignitionTime()) < 30:
self.shorttime()
else:
self.longtime()
except:
self.error()
def on_enter_departTasks(self):
self.printMessage()
self.departTasks()
self.advance()
def on_enter_arrivalTasks(self):
self.printMessage()
self.arrivalTasks()
self.advance()
def on_enter_waitForNoCar(self):
self.printMessage()
while(not self.CarNotDetected()):
time.sleep(5)
self.notdetected()
def printMessage(self):
print(self.name),
print("entered state %s" %self.state),
print(datetime.datetime.now().strftime('%H:%M:%S %d/%m/%Y'))
def CarDetected(self):
response = os.system("ping -c 1 %s > /dev/null 2>&1" %self.IPAddress)
if response == 0:
return True
return False
def ignitionTime(self):
os.system("wget %s -O %s_ignitionTime.txt --quiet" %(self.IPAddress, self.name))
with open("%s_ignitionTime.txt" %self.name, 'r') as file:
ignitionTime = file.read()
file.close()
print(self.name),
print("ignition time is %s seconds" %ignitionTime)
return ignitionTime
def departTasks(self):
self.beep(2);
def arrivalTasks(self):
self.beep(3);
def beep(self, beeps):
os.system ("wget %s/beep%d -O %s_ignitionTime.txt --quiet" %(self.IPAddress, beeps, self.name))
def CarNotDetected(self):
response = os.system("ping -c 3 %s > /dev/null 2>&1" %self.IPAddress)
if response == 0:
return False
return True
# Create an object and kick the state machine into life
stevesCar = CarDetector("stevesCar", "192.168.0.1")
stevesCar.to_waitingForCar() # forces calling on_enter_waitingForCar