mirror of
https://github.com/PiBrewing/craftbeerpi4.git
synced 2024-12-22 21:44:57 +01:00
commit
0371e74b4a
1 changed files with 21 additions and 32 deletions
|
@ -9,42 +9,31 @@ import random
|
|||
|
||||
cache = {}
|
||||
|
||||
|
||||
@parameters([Property.Text(label="Key", configurable=True, description="Http Key")])
|
||||
class HTTPSensor(CBPiSensor):
|
||||
|
||||
# Custom Properties which will can be configured by the user
|
||||
|
||||
key = Property.Text(label="Key", configurable=True)
|
||||
|
||||
def init(self):
|
||||
super().init()
|
||||
|
||||
self.state = True
|
||||
|
||||
def get_state(self):
|
||||
return self.state
|
||||
|
||||
def get_value(self):
|
||||
|
||||
return self.value
|
||||
|
||||
def stop(self):
|
||||
pass
|
||||
|
||||
async def run(self, cbpi):
|
||||
def __init__(self, cbpi, id, props):
|
||||
super(HTTPSensor, self).__init__(cbpi, id, props)
|
||||
self.running = True
|
||||
self.value = 0
|
||||
while True:
|
||||
|
||||
async def run(self):
|
||||
'''
|
||||
This method is executed asynchronousely
|
||||
In this example the code is executed every second
|
||||
'''
|
||||
while self.running is True:
|
||||
try:
|
||||
cache_value = cache.pop(self.props.get("Key"), None)
|
||||
if cache_value is not None:
|
||||
self.value = float(cache_value)
|
||||
self.push_update(self.value)
|
||||
except Exception as e:
|
||||
pass
|
||||
await asyncio.sleep(1)
|
||||
|
||||
try:
|
||||
value = cache.pop(self.key, None)
|
||||
|
||||
if value is not None:
|
||||
self.log_data(value)
|
||||
await cbpi.bus.fire("sensor/%s/data" % self.id, value=value)
|
||||
except Exception as e:
|
||||
|
||||
pass
|
||||
def get_state(self):
|
||||
# return the current state of the sensor
|
||||
return dict(value=self.value)
|
||||
|
||||
class HTTPSensorEndpoint(CBPiExtension):
|
||||
|
||||
|
|
Loading…
Reference in a new issue