48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
|
def sensor(cls):
|
||
|
Sensor.register(cls)
|
||
|
|
||
|
return cls
|
||
|
|
||
|
|
||
|
class Sensor(object):
|
||
|
hostname = None
|
||
|
platform = None
|
||
|
_sensor_classes = {}
|
||
|
|
||
|
def __init__(self, id, name, type, pin, description, **sensor_conf):
|
||
|
self.id = id
|
||
|
self.name = name
|
||
|
self.type = type
|
||
|
self.pin = pin
|
||
|
self.description = description
|
||
|
|
||
|
@classmethod
|
||
|
def configure(cls, hostname, platform):
|
||
|
cls.hostname = hostname
|
||
|
cls.platform = platform
|
||
|
|
||
|
@classmethod
|
||
|
def register(cls, new_cls):
|
||
|
cls._sensor_classes[new_cls.sensor_class] = new_cls
|
||
|
|
||
|
@classmethod
|
||
|
def make_sensor(cls, **kwargs):
|
||
|
sensor_cls = cls._sensor_classes[kwargs['type']]
|
||
|
return sensor_cls(**kwargs)
|
||
|
|
||
|
def get_data(self):
|
||
|
raise NotImplementedError("You missed a spot!")
|
||
|
|
||
|
def gen_datapoint(self):
|
||
|
return {
|
||
|
'type': 'measurement',
|
||
|
'tags': {
|
||
|
'name': self.name,
|
||
|
'sub-id': self.id,
|
||
|
'sensor': self.type,
|
||
|
'hostname': self.hostname,
|
||
|
'platform': self.platform,
|
||
|
},
|
||
|
'fields': self.get_data()
|
||
|
}
|