craftbeerpi4-pione/cbpi/extension/dummysensor/__init__.py

144 lines
3.2 KiB
Python
Raw Normal View History

2018-11-18 15:40:10 +01:00
import asyncio
2019-01-14 07:33:59 +01:00
from aiohttp import web
2019-01-05 20:43:48 +01:00
from cbpi.api import *
2018-11-18 15:40:10 +01:00
2019-01-14 07:33:59 +01:00
2018-11-18 15:40:10 +01:00
class CustomSensor(CBPiSensor):
2018-12-08 14:21:00 +01:00
# Custom Properties which will can be configured by the user
p1 = Property.Number(label="Test")
p2 = Property.Text(label="Test")
2019-01-14 07:33:59 +01:00
interval = Property.Number(label="interval", configurable=True)
2018-11-18 15:40:10 +01:00
2018-12-08 14:21:00 +01:00
# Internal runtime variable
2018-11-18 23:09:17 +01:00
value = 0
2018-11-18 15:40:10 +01:00
@action(key="name", parameters={})
def myAction(self):
2018-12-08 14:21:00 +01:00
'''
Custom Action Exampel
:return: None
'''
2018-11-18 15:40:10 +01:00
pass
2019-01-02 00:48:36 +01:00
def init(self):
super().init()
2019-01-08 23:31:39 +01:00
self.state = True
2019-01-02 00:48:36 +01:00
2019-01-08 23:31:39 +01:00
def get_state(self):
return self.state
2019-01-02 00:48:36 +01:00
2019-01-08 23:31:39 +01:00
def get_value(self):
return self.value
2018-11-18 15:40:10 +01:00
def stop(self):
pass
async def run(self, cbpi):
2018-11-18 23:09:17 +01:00
self.value = 0
2018-11-18 15:40:10 +01:00
while True:
2018-12-08 14:21:00 +01:00
await asyncio.sleep(self.interval)
2019-01-02 00:48:36 +01:00
self.log_data(10)
2018-11-18 23:09:17 +01:00
self.value = self.value + 1
2019-01-08 23:31:39 +01:00
2019-01-14 07:33:59 +01:00
await cbpi.bus.fire("sensor/%s/data" % self.id, value=self.value)
cache = {}
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):
self.value = 0
while True:
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" % self.id, value=value)
except:
pass
class HTTPSensorEndpoint(CBPiExtension):
2018-12-29 00:27:19 +01:00
2018-11-18 15:40:10 +01:00
2019-01-14 07:33:59 +01:00
def __init__(self, cbpi):
'''
Initializer
:param cbpi:
'''
2018-11-18 15:40:10 +01:00
2019-01-14 07:33:59 +01:00
self.cbpi = cbpi
# register component for http, events
# In addtion the sub folder static is exposed to access static content via http
self.cbpi.register(self, "/httpsensor")
@request_mapping(path="/{key}/{value}", auth_required=False)
async def http_new_value2(self, request):
"""
---
description: Kettle Heater on
tags:
- HttpSensor
parameters:
- name: "key"
in: "path"
description: "Sensor Key"
required: true
type: "string"
- name: "value"
in: "path"
description: "Value"
required: true
type: "integer"
format: "int64"
responses:
"204":
description: successful operation
"""
print("HALLO")
global cache
key = request.match_info['key']
value = request.match_info['value']
cache[key] = value
print(cache)
return web.Response(status=204)
2018-11-18 15:40:10 +01:00
def setup(cbpi):
'''
This method is called by the server during startup
Here you need to register your plugins at the server
:param cbpi: the cbpi core
:return:
'''
2019-01-14 07:33:59 +01:00
cbpi.plugin.register("HTTPSensor", HTTPSensor)
cbpi.plugin.register("HTTPSensorEndpoint", HTTPSensorEndpoint)
2018-11-18 15:40:10 +01:00
cbpi.plugin.register("CustomSensor", CustomSensor)