forked from xiaokaizh/SmartDoorControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIRStateMachine.py
More file actions
157 lines (125 loc) · 3.54 KB
/
IRStateMachine.py
File metadata and controls
157 lines (125 loc) · 3.54 KB
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
# coding=utf-8
__author__ = 'xzhao'
from threading import Timer
import MessageHandle
class State(object):
@property
def nomal(self):
return "nomal"
@property
def preWarn(self):
return "preWarn"
@property
def shortStay(self):
return "shortStay"
@property
def longStay(self):
return "longStay"
@property
def wait(self):
return "wait"
@property
def wander(self):
return "wander"
class CtState(object):
def __init__(self):
self._currentState = "nomal"
@property
def currentState(self):
return self._currentState
@currentState.setter
def currentState(self, value):
if value == st.preWarn:
global timer1
timerSwitch(timer1)
timer1 = Timer(5, convert, [st.shortStay])
timer1.start()
elif value == st.shortStay:
global timer2
timerSwitch(timer2)
timer2 = Timer(5, convert, [st.longStay])
timer2.start()
elif value == st.longStay:
timerSwitch("")
print "you ren chang zhu,tong zhi yonghu"
url = "192.168.1.101"
port = 9001
MessageHandle.CallAlarm(url, port, 3)
global timer3
timerSwitch(timer3)
timer3 = Timer(5, convert, [st.nomal])
timer3.start()
# elif value == st.wander:
# timerSwitch("")
# print "you ren pai huai,tong zhi yonghu"
# url = "192.168.1.101"
# port = 9001
# MessageHandle.CallAlarm(url, port, 3)
# global timer3
# timerSwitch(timer3)
# timer3 = Timer(5, convert, [st.nomal])
# timer3.start()
elif value == st.nomal:
timerSwitch("")
self._currentState = value
# 当前状态
ctState = CtState()
# 所有状态类
st = State()
wanderNum = 0
# 可供外部调用直接改变状态机状态
def convert(a):
ctState.currentState = a
if ctState.currentState != st.nomal:
print("CurrentState %s" % a)
# if ctState._currentState == st.preWarn:
# global wanderNum
# wanderNum += 1
# if wanderNum >= 2:
# ctState.currentState = st.wander
# print("CurrentState %s" % ctState.currentState)
# if ctState._currentState != st.preWarn:
# wanderNum = 0
def timerinit():
print("Timer is PrePare")
timer1 = Timer(0, timerinit, )
timer2 = Timer(0, timerinit, )
timer3 = Timer(0, timerinit, )
timer4 = Timer(0, timerinit, )
# Timer切换
def timerSwitch(timer):
if timer == timer1:
timer2.cancel()
timer3.cancel()
timer4.cancel()
elif timer == timer2:
timer1.cancel()
timer3.cancel()
timer4.cancel()
elif timer == timer3:
timer1.cancel()
timer2.cancel()
timer4.cancel()
elif timer == timer4:
timer1.cancel()
timer2.cancel()
timer3.cancel()
else:
timer1.cancel()
timer2.cancel()
timer3.cancel()
timer4.cancel()
# 状态判断函数
def stateJudge(ir):
if ir == 1:
if ctState.currentState == st.nomal:
convert(st.preWarn)
else:
if ctState.currentState == st.nomal:
convert(st.nomal)
elif ctState.currentState == st.preWarn:
convert(st.nomal)
elif ctState.currentState == st.shortStay:
convert(st.preWarn)
elif ctState.currentState == st.wait:
convert(st.shortStay)