|
@@ -0,0 +1,50 @@
|
|
1
|
+#!/usr/bin/env python
|
|
2
|
+# -*- coding: UTF-8 -*-
|
|
3
|
+
|
|
4
|
+import sys
|
|
5
|
+sys.path.append('../..')
|
|
6
|
+import logging
|
|
7
|
+import os
|
|
8
|
+
|
|
9
|
+import report
|
|
10
|
+import state_machine
|
|
11
|
+
|
|
12
|
+logger = logging.getLogger('root')
|
|
13
|
+report.stdoutLoggingConfigure(log_name_lvl=[('root', 'DEBUG'), ])
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+class trafic_lights(state_machine.state_machine):
|
|
17
|
+ LOG_PREFIX = 'TraficLights:'
|
|
18
|
+
|
|
19
|
+ STATE_RED = 'state_red'
|
|
20
|
+ STATE_GREEN = 'state_green'
|
|
21
|
+
|
|
22
|
+ CONDITION_TRUE = 'condition_true'
|
|
23
|
+ CONDITION_PEDASTRIAN_REQUEST = 'condition_pedastrian_request'
|
|
24
|
+
|
|
25
|
+ TRANSITIONS = {
|
|
26
|
+ STATE_RED: (
|
|
27
|
+ (CONDITION_PEDASTRIAN_REQUEST, 1, STATE_GREEN),
|
|
28
|
+ ),
|
|
29
|
+ STATE_GREEN: (
|
|
30
|
+ (CONDITION_TRUE, 3, STATE_RED),
|
|
31
|
+ )
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ def condition_true(self):
|
|
35
|
+ return True
|
|
36
|
+
|
|
37
|
+ def set_padestrian_request(self):
|
|
38
|
+ logger.log(self.__log_lvl__, '%s Pedestrian gave state change request.', self.LOG_PREFIX)
|
|
39
|
+ self.pedastrian_request = True
|
|
40
|
+
|
|
41
|
+ def condition_pedastrian_request(self):
|
|
42
|
+ return self.pedastrian_request
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+sm = trafic_lights(trafic_lights.STATE_RED, logging.INFO, pedastrian_request=False)
|
|
46
|
+sm.register_state_change_callback(sm.STATE_GREEN, sm.CONDITION_PEDASTRIAN_REQUEST, logger.info, 'Callback information: Traffic light had been changed to green caused by pedastrian request')
|
|
47
|
+while not sm.previous_state_was(sm.STATE_GREEN):
|
|
48
|
+ if sm.this_state_is(sm.STATE_RED) and sm.this_state_duration() > 0.2 and not sm.condition_pedastrian_request():
|
|
49
|
+ sm.set_padestrian_request()
|
|
50
|
+ sm.work()
|