Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Sensor-Endpoints #38

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cbpi/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
"parameters",
"background_task",
"CBPiKettleLogic",
"CBPiFermenterLogic",
"CBPiException",
"KettleException",
"FermenterException",
"SensorException",
"ActorException",
"CBPiSensor",
Expand All @@ -22,5 +24,6 @@
from cbpi.api.property import *
from cbpi.api.decorator import *
from cbpi.api.kettle_logic import *
from cbpi.api.fermenter_logic import *
from cbpi.api.step import *
from cbpi.api.exceptions import *
9 changes: 9 additions & 0 deletions cbpi/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ def get_kettle_target_temp(self,id):

async def set_target_temp(self,id, temp):
await self.cbpi.kettle.set_target_temp(id, temp)

def get_fermenter(self,id):
return self.cbpi.fermenter.find_by_id(id)

def get_fermenter_target_temp(self,id):
return self.cbpi.fermenter.find_by_id(id).target_temp

async def set_fermenter_target_temp(self,id, temp):
await self.cbpi.fermenter.set_target_temp(id, temp)

def get_sensor(self,id):
return self.cbpi.sensor.find_by_id(id)
Expand Down
1 change: 1 addition & 0 deletions cbpi/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class ConfigType(Enum):
NUMBER = "number"
SELECT = "select"
KETTLE = "kettle"
FERMENTER = "fermenter"
ACTOR = "actor"
SENSOR = "sensor"

Expand Down
24 changes: 24 additions & 0 deletions cbpi/api/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,29 @@ def to_dict(self):
state = False
return dict(id=self.id, name=self.name, state=state, target_temp=self.target_temp, heater=self.heater, agitator=self.agitator, sensor=self.sensor, type=self.type, props=self.props.to_dict())

@dataclass
class Fermenter:
id: str = None
name: str = None
props: Props = Props()
instance: str = None
cooler: Actor = None
sensor: Sensor = None
type: str = None
target_temp: int = 0

def __str__(self):
return "name={} props={} temp={}".format(self.name, self.props, self.target_temp)
def to_dict(self):

if self.instance is not None:

state = self.instance.state
print("READ STATE", state)
else:
state = False
return dict(id=self.id, name=self.name, state=state, target_temp=self.target_temp, heater=self.cooler, sensor=self.sensor, type=self.type, props=self.props.to_dict())

@dataclass
class Step:
id: str = None
Expand All @@ -126,6 +149,7 @@ class ConfigType(Enum):
ACTOR="actor"
SENSOR="sensor"
KETTLE="kettle"
FERMENTER="fermenter"
NUMBER="number"
SELECT="select"

Expand Down
5 changes: 4 additions & 1 deletion cbpi/api/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__all__ = ["CBPiException","KettleException","SensorException","ActorException"]
__all__ = ["CBPiException","KettleException","FermenterException","SensorException","ActorException"]


class CBPiException(Exception):
Expand All @@ -7,6 +7,9 @@ class CBPiException(Exception):
class KettleException(CBPiException):
pass

class FermenterException(CBPiException):
pass

class SensorException(CBPiException):
pass

Expand Down
51 changes: 51 additions & 0 deletions cbpi/api/fermenter_logic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from cbpi.api.base import CBPiBase
from cbpi.api.extension import CBPiExtension
from abc import ABCMeta
import logging
import asyncio



class CBPiFermenterLogic(CBPiBase, metaclass=ABCMeta):

def __init__(self, cbpi, id, props):
self.cbpi = cbpi
self.id = id
self.props = props
self.state = False
self.running = False

def init(self):
pass

async def on_start(self):
pass

async def on_stop(self):
pass

async def run(self):
pass

async def _run(self):

try:
await self.on_start()
self.cancel_reason = await self.run()
except asyncio.CancelledError as e:
pass
finally:
await self.on_stop()

def get_state(self):
return dict(running=self.state)

async def start(self):

self.state = True

async def stop(self):

self.task.cancel()
await self.task
self.state = False
19 changes: 18 additions & 1 deletion cbpi/api/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,24 @@ def __init__(self, label, description=""):

class Kettle(PropertyType):
'''
The user select a kettle which is available in the system. The value of this variable will be the kettle id
The user selects a kettle which is available in the system. The value of this variable will be the kettle id
'''

def __init__(self, label, description=""):
'''

:param label:
:param description:
'''

PropertyType.__init__(self)
self.label = label
self.configurable = True
self.description = description

class Fermenter(PropertyType):
'''
The user selects a fermenter which is available in the system. The value of this variable will be the fermenter id
'''

def __init__(self, label, description=""):
Expand Down
7 changes: 6 additions & 1 deletion cbpi/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from subprocess import call

from jinja2 import Template

MAIN_DIR = "."

def create_config_file():
if os.path.exists(os.path.join(".", 'config', "config.yaml")) is False:
Expand All @@ -40,6 +40,11 @@ def create_config_file():
destfile = os.path.join(".", 'config')
shutil.copy(srcfile, destfile)

if os.path.exists(os.path.join(".", 'config', "fermenter.json")) is False:
srcfile = os.path.join(os.path.dirname(__file__), "config", "fermenter.json")
destfile = os.path.join(".", 'config')
shutil.copy(srcfile, destfile)

if os.path.exists(os.path.join(".", 'config', "step_data.json")) is False:
srcfile = os.path.join(os.path.dirname(__file__), "config", "step_data.json")
destfile = os.path.join(".", 'config')
Expand Down
22 changes: 21 additions & 1 deletion cbpi/config/actor.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
{
"data": [

{
"id": "6CNB5aS7qQxDFhF4zPqRJd",
"name": "dsfsd",
"props": {},
"state": false,
"type": "MyCustomActors"
},
{
"id": "TtTuZZ7hE8r46cNTCkHz3D",
"name": "abc",
"props": {},
"state": false,
"type": "DummyActor"
},
{
"id": "Sk6VnCE2629RxukRVFkhMB",
"name": "fgfdg",
"props": {},
"state": false,
"type": "MyCustomActors"
}
]
}
17 changes: 7 additions & 10 deletions cbpi/config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@

name: CraftBeerPi
version: 4.0.8

index_url: /cbpi_ui/static/index.html

port: 8000

username: cbpi
name: CraftBeerPi
password: 123

plugins:
- cbpi4ui

- myplugin2
- test
- tester
port: 8000
username: cbpi
version: 4.0.8
14 changes: 14 additions & 0 deletions cbpi/config/create_database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ CREATE TABLE IF NOT EXISTS kettle
diameter INTEGER
);

CREATE TABLE IF NOT EXISTS fermenter
(
id INTEGER PRIMARY KEY NOT NULL,
name VARCHAR(80),
sensor INTEGER,
cooler INTEGER,
automatic VARCHAR(255),
logic VARCHAR(50),
config VARCHAR(1000),
target_temp INTEGER,
height INTEGER,
diameter INTEGER
);

CREATE TABLE IF NOT EXISTS config
(
name VARCHAR(50) PRIMARY KEY NOT NULL,
Expand Down
34 changes: 34 additions & 0 deletions cbpi/config/fermenter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"data": [
{
"heater": null,
"id": "gTetr9nv3FN8ZkcAtYsYjc",
"name": "testing",
"props": {},
"sensor": "eCa5Mnmr25G24oAAcqFZY2",
"state": false,
"target_temp": 0,
"type": null
},
{
"heater": null,
"id": "LE4EorYLq4bNugRkvR2wc6",
"name": "tzest",
"props": {},
"sensor": "eCa5Mnmr25G24oAAcqFZY2",
"state": false,
"target_temp": 0,
"type": "Hysteresis"
},
{
"heater": null,
"id": "6ScZZMfqGswGfFcmNggzST",
"name": "a",
"props": {},
"sensor": "kr6vwViDyusVsVJMMmoHqQ",
"state": false,
"target_temp": 0,
"type": "Hysteresis"
}
]
}
4 changes: 1 addition & 3 deletions cbpi/config/kettle.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{
"data": [

]
"data": []
}
22 changes: 21 additions & 1 deletion cbpi/config/sensor.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
{
"data": [

{
"id": "kr6vwViDyusVsVJMMmoHqQ",
"name": "abc",
"props": {},
"state": false,
"type": "MyCustomSensorss"
},
{
"id": "ehD8mesfHRnqk35GrxHZc6",
"name": "tttt",
"props": {},
"state": false,
"type": "MyCustomSensorss"
},
{
"id": "UYCNrTf8bWPxX5TXEbpB76",
"name": "abv",
"props": {},
"state": false,
"type": "HTTPSensor"
}
]
}
4 changes: 1 addition & 3 deletions cbpi/config/step_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@
"basic": {
"name": ""
},
"steps": [

]
"steps": []
}
35 changes: 35 additions & 0 deletions cbpi/controller/fermenter_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from cbpi.api.dataclasses import Fermenter, Props
from cbpi.controller.basic_controller2 import BasicController
import logging
from tabulate import tabulate
class FermenterController(BasicController):

def __init__(self, cbpi):
super(FermenterController, self).__init__(cbpi, Fermenter, "fermenter.json")
self.update_key = "fermenterupdate"
self.autostart = False

def create(self, data):
return Fermenter(data.get("id"), data.get("name"), type=data.get("type"), props=Props(data.get("props", {})), sensor=data.get("sensor"), cooler=data.get("cooler"))

async def toggle(self, id):

try:
item = self.find_by_id(id)

if item.instance is None or item.instance.state == False:
await self.start(id)
else:
await item.instance.stop()
await self.push_udpate()
except Exception as e:
logging.error("Faild to switch on FermenterLogic {} {}".format(id, e))

async def set_target_temp(self, id, target_temp):
try:
item = self.find_by_id(id)
item.target_temp = target_temp
await self.save()
except Exception as e:
logging.error("Faild to set Target Temp {} {}".format(id, e))

Loading