|
1
|
|
|
import abc |
|
2
|
|
|
|
|
3
|
|
|
import six |
|
4
|
|
|
import eventlet |
|
5
|
|
|
|
|
6
|
|
|
__all__ = [ |
|
7
|
|
|
'Sensor', |
|
8
|
|
|
'PollingSensor' |
|
9
|
|
|
] |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
@six.add_metaclass(abc.ABCMeta) |
|
13
|
|
|
class BaseSensor(object): |
|
14
|
|
|
""" |
|
15
|
|
|
Base Sensor class - not to be instantiated directly. |
|
16
|
|
|
""" |
|
17
|
|
|
|
|
18
|
|
|
def __init__(self, sensor_service, config=None): |
|
19
|
|
|
""" |
|
20
|
|
|
:param sensor_service: Sensor Service instance. |
|
21
|
|
|
:type sensor_service: :class:``st2reactor.container.sensor_wrapper.SensorService`` |
|
22
|
|
|
|
|
23
|
|
|
:keyword config: Sensor config. |
|
24
|
|
|
:type config: ``dict`` or None |
|
25
|
|
|
""" |
|
26
|
|
|
self._sensor_service = sensor_service |
|
27
|
|
|
self._config = config or {} |
|
28
|
|
|
|
|
29
|
|
|
@abc.abstractmethod |
|
30
|
|
|
def setup(self): |
|
31
|
|
|
""" |
|
32
|
|
|
Run the sensor initialization / setup code (if any). |
|
33
|
|
|
""" |
|
34
|
|
|
pass |
|
35
|
|
|
|
|
36
|
|
|
@abc.abstractmethod |
|
37
|
|
|
def run(self): |
|
38
|
|
|
""" |
|
39
|
|
|
Run the sensor. |
|
40
|
|
|
""" |
|
41
|
|
|
pass |
|
42
|
|
|
|
|
43
|
|
|
@abc.abstractmethod |
|
44
|
|
|
def cleanup(self): |
|
45
|
|
|
""" |
|
46
|
|
|
Run the sensor cleanup code (if any). |
|
47
|
|
|
""" |
|
48
|
|
|
pass |
|
49
|
|
|
|
|
50
|
|
|
@abc.abstractmethod |
|
51
|
|
|
def add_trigger(self, trigger): |
|
52
|
|
|
""" |
|
53
|
|
|
Runs when trigger is created |
|
54
|
|
|
""" |
|
55
|
|
|
pass |
|
56
|
|
|
|
|
57
|
|
|
@abc.abstractmethod |
|
58
|
|
|
def update_trigger(self, trigger): |
|
59
|
|
|
""" |
|
60
|
|
|
Runs when trigger is updated |
|
61
|
|
|
""" |
|
62
|
|
|
pass |
|
63
|
|
|
|
|
64
|
|
|
@abc.abstractmethod |
|
65
|
|
|
def remove_trigger(self, trigger): |
|
66
|
|
|
""" |
|
67
|
|
|
Runs when trigger is deleted |
|
68
|
|
|
""" |
|
69
|
|
|
pass |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
class Sensor(BaseSensor): |
|
73
|
|
|
""" |
|
74
|
|
|
Base class to be inherited from by the passive sensors. |
|
75
|
|
|
""" |
|
76
|
|
|
|
|
77
|
|
|
@abc.abstractmethod |
|
78
|
|
|
def run(self): |
|
79
|
|
|
pass |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
class PollingSensor(BaseSensor): |
|
83
|
|
|
""" |
|
84
|
|
|
Base class to be inherited from by the active sensors. |
|
85
|
|
|
|
|
86
|
|
|
Active sensors periodically poll a 3rd party system for new information. |
|
87
|
|
|
""" |
|
88
|
|
|
|
|
89
|
|
|
def __init__(self, sensor_service, config=None, poll_interval=5): |
|
90
|
|
|
super(PollingSensor, self).__init__(sensor_service=sensor_service, config=config) |
|
91
|
|
|
self._poll_interval = poll_interval |
|
92
|
|
|
|
|
93
|
|
|
@abc.abstractmethod |
|
94
|
|
|
def poll(self): |
|
95
|
|
|
""" |
|
96
|
|
|
Poll 3rd party system for new information. |
|
97
|
|
|
""" |
|
98
|
|
|
pass |
|
99
|
|
|
|
|
100
|
|
|
def run(self): |
|
101
|
|
|
while True: |
|
102
|
|
|
self.poll() |
|
103
|
|
|
eventlet.sleep(self._poll_interval) |
|
104
|
|
|
|
|
105
|
|
|
def get_poll_interval(self): |
|
106
|
|
|
""" |
|
107
|
|
|
Retrieve current poll interval. |
|
108
|
|
|
|
|
109
|
|
|
:return: Current poll interval. |
|
110
|
|
|
:rtype: ``float`` |
|
111
|
|
|
""" |
|
112
|
|
|
return self._poll_interval |
|
113
|
|
|
|
|
114
|
|
|
def set_poll_interval(self, poll_interval): |
|
115
|
|
|
""" |
|
116
|
|
|
Set the poll interval. |
|
117
|
|
|
|
|
118
|
|
|
:param poll_interval: Poll interval to use. |
|
119
|
|
|
:type poll_interval: ``float`` |
|
120
|
|
|
""" |
|
121
|
|
|
self._poll_interval = poll_interval |
|
122
|
|
|
|