diff --git a/cbpi/__init__.py b/cbpi/__init__.py index 065df38..337aa2e 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1,3 +1,3 @@ -__version__ = "4.0.4.a3" +__version__ = "4.0.5.a1" __codename__ = "Spring Break" diff --git a/cbpi/api/base.py b/cbpi/api/base.py index d698c1e..837b4a2 100644 --- a/cbpi/api/base.py +++ b/cbpi/api/base.py @@ -38,6 +38,12 @@ class CBPiBase(metaclass=ABCMeta): async def set_fermenter_target_temp(self,id, temp): await self.cbpi.fermenter.set_target_temp(id, temp) + def get_fermenter_target_pressure(self,id): + return self.cbpi.fermenter._find_by_id(id).target_pressure + + async def set_fermenter_target_pressure(self,id, temp): + await self.cbpi.fermenter.set_target_pressure(id, temp) + def get_sensor(self,id): return self.cbpi.sensor.find_by_id(id) diff --git a/cbpi/api/dataclasses.py b/cbpi/api/dataclasses.py index 921582c..614b36c 100644 --- a/cbpi/api/dataclasses.py +++ b/cbpi/api/dataclasses.py @@ -126,21 +126,23 @@ class Fermenter: id: str = None name: str = None sensor: Sensor = None + pressure_sensor : Sensor = None heater: Actor = None cooler: Actor = None + valve: Actor = None brewname: str = None description : str = None props: Props = Props() target_temp: float = 0 + target_pressure: float = 0 type: str = None steps: List[Step]= field(default_factory=list) instance: str = None def __str__(self): - return "name={} props={} temp={}".format(self.name, self.props, self.target_temp) + return "name={} props={} temp={}".format(self.name, self.props, self.target_temp, self.target_pressure) -# return "id={} name={} sensor={} heater={} cooler={} brewname={} props={} temp={} type={} steps={}".format(self.id, self.name, self.sensor, self.heater, self.cooler, self.brewname, self.props, self.target_temp, self.type, self.steps) def to_dict(self): if self.instance is not None: @@ -151,7 +153,7 @@ class Fermenter: state = False steps = list(map(lambda item: item.to_dict(), self.steps)) - return dict(id=self.id, name=self.name, state=state, sensor=self.sensor, heater=self.heater, cooler=self.cooler, brewname=self.brewname, description=self.description, props=self.props.to_dict() if self.props is not None else None, target_temp=self.target_temp, type=self.type, steps=steps) + return dict(id=self.id, name=self.name, state=state, sensor=self.sensor, pressure_sensor=self.pressure_sensor, heater=self.heater, cooler=self.cooler, valve=self.valve, brewname=self.brewname, description=self.description, props=self.props.to_dict() if self.props is not None else None, target_temp=self.target_temp, target_pressure=self.target_pressure, type=self.type, steps=steps) @dataclass diff --git a/cbpi/controller/fermentation_controller.py b/cbpi/controller/fermentation_controller.py index c869795..7641044 100644 --- a/cbpi/controller/fermentation_controller.py +++ b/cbpi/controller/fermentation_controller.py @@ -114,14 +114,17 @@ class FermentationController: id = data.get("id") name = data.get("name") sensor = data.get("sensor") + pressure_sensor = data.get("pressure_sensor") heater = data.get("heater") cooler = data.get("cooler") + valve = data.get("valve") logictype = data.get("type") temp = data.get("target_temp") + pressure = data.get("target_pressure") brewname = data.get("brewname") description = data.get("description") props = Props(data.get("props", {})) - fermenter = Fermenter(id, name, sensor, heater, cooler, brewname, description, props, temp, logictype) + fermenter = Fermenter(id, name, sensor, pressure_sensor, heater, cooler, valve, brewname, description, props, temp, pressure, logictype) fermenter.steps = list(map(lambda item: self._create_step(fermenter, item), data.get("steps", []))) self.push_update() return fermenter @@ -197,13 +200,16 @@ class FermentationController: def _update(old_item: Fermenter, item: Fermenter): old_item.name = item.name old_item.sensor = item.sensor + old_item.pressure_sensor = item.pressure_sensor old_item.heater = item.heater old_item.cooler = item.cooler + old_item.valve = item.valve old_item.type = item.type old_item.brewname = item.brewname old_item.description = item.description old_item.props = item.props old_item.target_temp = item.target_temp + old_item.target_pressure = item.target_pressure return old_item self.data = list(map(lambda old: _update(old, item) if old.id == item.id else old, self.data)) @@ -222,6 +228,17 @@ class FermentationController: except Exception as e: logging.error("Failed to set Target Temp {} {}".format(id, e)) + async def set_target_pressure(self, id: str, target_pressure): + try: + item = self._find_by_id(id) + logging.info(item.target_pressure) + if item: + item.target_pressure = target_pressure + self.save() + self.push_update() + except Exception as e: + logging.error("Failed to set Target Pressure {} {}".format(id, e)) + async def delete(self, id: str ): item = self._find_by_id(id) self.data = list(filter(lambda item: item.id != id, self.data)) diff --git a/cbpi/extension/ConfigUpdate/__init__.py b/cbpi/extension/ConfigUpdate/__init__.py index bc24110..e5dc9ac 100644 --- a/cbpi/extension/ConfigUpdate/__init__.py +++ b/cbpi/extension/ConfigUpdate/__init__.py @@ -46,7 +46,7 @@ class ConfigUpdate(CBPiExtension): influxdbpwd = self.cbpi.config.get("INFLUXDBPWD", None) influxdbcloud = self.cbpi.config.get("INFLUXDBCLOUD", None) mqttupdate = self.cbpi.config.get("MQTTUpdate", None) - + PRESSURE_UNIT = self.cbpi.config.get("PRESSURE_UNIT", None) if boil_temp is None: @@ -276,6 +276,15 @@ class ConfigUpdate(CBPiExtension): except: logger.warning('Unable to update database') + ## Check if PRESSURE_UNIT is in config + if PRESSURE_UNIT is None: + logger.info("INIT PRESSURE_UNIT") + try: + await self.cbpi.config.add("PRESSURE_UNIT", "kPa", ConfigType.SELECT, "Set unit for pressure", + [{"label": "kPa", "value": "kPa"}, + {"label": "PSI", "value": "PSI"}]) + except: + logger.warning('Unable to update config') def setup(cbpi): cbpi.plugin.register("ConfigUpdate", ConfigUpdate) diff --git a/cbpi/http_endpoints/http_fermentation.py b/cbpi/http_endpoints/http_fermentation.py index 81c4d89..2a97530 100644 --- a/cbpi/http_endpoints/http_fermentation.py +++ b/cbpi/http_endpoints/http_fermentation.py @@ -77,7 +77,9 @@ class FermentationHttpEndpoints(): description: successful operation """ data = await request.json() - fermenter = Fermenter(id=id, name=data.get("name"), sensor=data.get("sensor"), heater=data.get("heater"), cooler=data.get("cooler"), brewname=data.get("brewname"), description=data.get("description"), target_temp=data.get("target_temp"), props=Props(data.get("props", {})), type=data.get("type")) + fermenter = Fermenter(id=id, name=data.get("name"), sensor=data.get("sensor"), pressure_sensor=data.get("pressure_sensor"), heater=data.get("heater"), + cooler=data.get("cooler"), valve=data.get("valve"), brewname=data.get("brewname"), description=data.get("description"), + target_temp=data.get("target_temp"), target_pressure=data.get("target_pressure"), props=Props(data.get("props", {})), type=data.get("type")) response_data = await self.controller.create(fermenter) return web.json_response(data=response_data.to_dict()) @@ -115,7 +117,9 @@ class FermentationHttpEndpoints(): """ id = request.match_info['id'] data = await request.json() - fermenter = Fermenter(id=id, name=data.get("name"), sensor=data.get("sensor"), heater=data.get("heater"), cooler=data.get("cooler"), brewname=data.get("brewname"), description=data.get("description"), target_temp=data.get("target_temp"), props=Props(data.get("props", {})), type=data.get("type")) + fermenter = Fermenter(id=id, name=data.get("name"), sensor=data.get("sensor"), pressure_sensor=data.get("pressure_sensor"), heater=data.get("heater"), + cooler=data.get("cooler"), valve=data.get("valve"), brewname=data.get("brewname"), description=data.get("description"), + target_temp=data.get("target_temp"), target_pressure=data.get("target_pressure"), props=Props(data.get("props", {})), type=data.get("type")) return web.json_response(data=(await self.controller.update(fermenter)).to_dict()) @request_mapping(path="/{id}", method="DELETE", auth_required=False) @@ -284,6 +288,40 @@ class FermentationHttpEndpoints(): await self.controller.set_target_temp(id,data.get("temp")) return web.Response(status=204) + @request_mapping(path="/{id}/target_pressure", method="POST", auth_required=auth) + async def http_target_pressure(self, request) -> web.Response: + """ + + --- + description: Set Target pressure for Fermenter + tags: + - Fermenter + parameters: + - name: "id" + in: "path" + description: "Fermenter ID" + required: true + type: "integer" + format: "int64" + - in: body + name: body + description: Update Pressure + required: true + schema: + type: object + properties: + temp: + type: integer + responses: + "204": + description: successful operation + """ + id = request.match_info['id'] + data = await request.json() + await self.controller.set_target_pressure(id,data.get("pressure")) + return web.Response(status=204) + + @request_mapping(path="/{id}/addstep", method="POST", auth_required=False) async def http_add_step(self, request):