-
Notifications
You must be signed in to change notification settings - Fork 1
/
formater.py
51 lines (39 loc) · 1.45 KB
/
formater.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
import time
import platform
from queuelib import FifoDiskQueue, FifoMemoryQueue
class BaseFormat(object):
def __init__(self, sensor_name, node=platform.node().replace('.', '-'),
root_name='sensors', alternate_names=None):
self.node = node
self.sensor_name = sensor_name
self.root_name = root_name
self.alternate_names = alternate_names
def msg_format(self, message):
pass
def encode(self, value):
pass
def run(self, fn, sleep=1):
pass
def message_fn(self, value):
timestamp = int(time.time())
values = value if isinstance(value, tuple) else (value,)
if len(values) > 1:
sensors_names = self.alternate_names
else:
sensors_names = (self.sensor_name,)
for value, sensor_name in zip(values, sensors_names):
yield (value, timestamp, sensor_name)
class CarbonFormat(BaseFormat):
def msg_format(self, messages):
for msg in messages:
v, timestamp, name = msg
yield "{}.{}.{} {} {}\n".format(self.root_name, self.node, name, v, timestamp)
def encode(self, value):
return self.msg_format(self.message_fn(value))
class SequenceTestFormat(BaseFormat):
def msg_format(self, messages):
for msg in messages:
v, timestamp, name = msg
yield str(v)
def encode(self, value):
return self.msg_format(self.message_fn(value))