-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelevator.py
299 lines (189 loc) · 7.01 KB
/
elevator.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# _*_ coding:utf-8 _*_
from time import sleep
from tools import wapper_localtask
from threading import Thread
from config import MAX_FLOOR,MIN_FLOOR,INIT_FLOOR
from globaltask import GlobalTask
from traceback import format_exc
import random
class ElevatorBase(object):
##电梯
def __init__(self,min_floor=MIN_FLOOR,max_floor=MAX_FLOOR,init_floor=INIT_FLOOR,name='BASE'):
self._name=name
self.init_floor=INIT_FLOOR
self.min_floor=MIN_FLOOR
self.max_floor=MAX_FLOOR
self._current_floor=self.init_floor
print u'create elevator:%s successfully!'%(self.name)
@property
def name(self):
##设置name为只读属性
return self._name
@property
def current_floor(self):
return self._current_floor
@current_floor.setter
def current_floor(self,value):
assert value>=self.min_floor and value<=self.max_floor,\
'current_floor out of range[%s:%s] but get %s'%(self.min_floor,self.max_floor,value)
self._current_floor=value
print u'set elevator:%s at %d floor'%(self.name,self.current_floor)
def run(self):
pass
def analyze(self,):
pass
def __str__(self,):
return 'elevator:%s at %s floor'%(self.name,self.current_floor)
class Elevator(ElevatorBase):
def __init__(self,name='child'):
ElevatorBase.__init__(self,name=name)
self.globaltask=GlobalTask.task
self.localtask=set()
self.destination=None
self.isalive=True
self.ismoving=False
self.isopen=False
def __str__(self,):
return '< elevator:%s at %s floor,destination=%s,flag=%s,isalive=%s >'\
%(self.name,self.current_floor,self.destination,self.flag,self.isalive)
@property
def flag(self,):
##电梯运行方向,设置为只读
if self.destination is None:
self._flag=0
else:
if self.destination > self.current_floor:
self._flag=1
elif self.destination < self.current_floor:
self._flag=-1
else:
self._flag=0
return self._flag
def stop(self,):
##电梯停运,检修
self.isalive=False
self.localtask=set()
self.destination=None
print u'stop %s'%(self)
def restart(self,):
##重启电梯,逻辑上需要在stop之后才能调用restart
self.isalive=True
self.localtask=set()
self.destination=None
print u'restart %s'%(self)
# self.run()
def open_door(self,):
##开启电梯门
if self.ismoving:
print u'Warning: %s is runing,Disallow open the door!'%(self,)
else:
self.isopen=True
print u'%s open the door,then sleep 1 second!'%(self,)
sleep(1)
def close_door(self,):
##关闭电梯门
if self.isopen:
self.isopen=False
print u'%s close the door'%(self,)
else:
print u'%s door is closed,with no need for closing!'%(self,)
@wapper_localtask
def add_localtask(self,floor,refer='localtask'):
##上电梯内的人选择楼层
##在增加楼层的时候就确定本次电梯运行的终点
##add_localtask只会增加与其方向一致的路线
if self.isalive:
if self.flag==-1:
if floor>self.current_floor:
isaccept=False
else:
isaccept=True
self.destination=min(self.destination,floor)
elif self.flag==1:
if floor<self.current_floor:
isaccept=False
else:
isaccept=True
self.destination=max(self.destination,floor)
elif self.flag==0:
if self.current_floor == floor:
if refer == 'localtask':
isaccept=False
elif refer=='globaltask':
isaccept=True
self.destination=floor
else:
isaccept=True
self.destination=floor
else:
##电梯stop,不接受任何任务
isaccept=False
if isaccept:
print u'%s accept task %s from %s'%(self,floor,refer)
self.localtask.add(floor)
else:
print u'%s ignore task %s from %s'%(self,floor,refer)
return isaccept
def whether_open_door_when_runing(self,):
##根据本地任务判断是否打开电梯门
if self.current_floor in self.localtask:
##有可能判断是否开门的时候,isalive恰好被更改了
self.open_door()
self.close_door()
try:
print u'%s remove %s from localtask:%s'%(self,self.current_floor,self.localtask)
self.localtask.remove(self.current_floor)
except:
raise
return True
else:
print u'%s with no need for open the door,sleep 0.1 second!'%(self)
return False
def run(self,):
print u'run %s'%(self)
while self.isalive:
try:
isopen=self.whether_open_door_when_runing()
if self.current_floor == self.destination:
print u'%s reach destination'%(self)
self.destination=None
if self.flag==-1:
self.current_floor-=1
print u'%s rearch new floor'%(self)
elif self.flag==1:
self.current_floor+=1
print u'%s rearch new floor'%(self)
else:
print u'%s has no task,sleep 2 second!'%(self)
sleep(2)
except:
print u'catch exception when elevator:%s runing,will stop this elevator'%(self.name)
print u'%s traceback info: \n%s'%(self,format_exc())
self.stop()
else:
print u'%s is broken ,sleep 5 second waiting for restart!!! '%(self)
sleep(5)
def test_elevator(elevator):
elevator.add_localtask(7)
elevator.add_localtask(8)
elevator.add_localtask(-3)
elevator.add_localtask(-18)
elevator.run()
def test():
elevator=Elevator()
from threading import Thread
def test_input(elevator):
while True:
if elevator.flag ==0:
refer=random.choice(['globaltask','localtask'])
elevator.add_localtask(8,refer=refer)
else:
print u'pass add input'
sleep(2)
t1=Thread(target=test_elevator,args=(elevator,))
t2=Thread(target=test_input,args=(elevator,))
t1.start()
t2.start()
if __name__ =='__main__':
# test_elevator()
test()