Skip to content

Commit

Permalink
Add a reboot mqtt subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
bjpirt committed Dec 14, 2023
1 parent 8095899 commit f99a181
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
27 changes: 20 additions & 7 deletions bms/mqtt_output.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ruff: noqa: E722
from hal.network import NetworkConnectionInterface
from .bms_interface import BmsInterface
from hal import get_interval, Memory
from hal import get_interval, Memory, reset
from config import Config
from mqtt import MQTTClient # type: ignore
import json
Expand Down Expand Up @@ -34,7 +34,10 @@ def _connect(self) -> None:
try:
print(f"Connecting to MQTT server: {self._config.mqtt_host}")
self._client.connect()
self._client.subscribe(f"{self._config.mqtt_topic_prefix}/set-config/#")
self._client.subscribe(
f"{self._config.mqtt_topic_prefix}/set-config/#")
self._client.subscribe(
f"{self._config.mqtt_topic_prefix}/reboot")
print("Connected to MQTT")
self.connected = True
except OSError:
Expand Down Expand Up @@ -88,15 +91,25 @@ def _should_publish(self, topic: str, value: Union[int, bool, float]) -> bool:
def _publish_topic(self, topic: str, value: Union[int, bool, float]) -> None:
if self._should_publish(topic, value):
to_send = json.dumps({"value": value})
self._client.publish(f"{self._config.mqtt_topic_prefix}{topic}", to_send)
self._client.publish(
f"{self._config.mqtt_topic_prefix}{topic}", to_send)

def _sub_cb(self, topic: str, msg) -> None:
try:
setting = topic.replace(f"{self._config.mqtt_topic_prefix}/set-config/", "")
data = json.loads(msg)
print(f"Setting '{setting}' to '{data['value']}'")
self._config.set_value(setting, data["value"])
self._config.save()
if "value" not in data:
print("Invalid message received")
return
if topic.startswith("{self._config.mqtt_topic_prefix}/set-config/"):
setting = topic.replace(
f"{self._config.mqtt_topic_prefix}/set-config/", "")
print(f"Setting '{setting}' to '{data['value']}'")
self._config.set_value(setting, data["value"])
self._config.save()
elif topic == "{self._config.mqtt_topic_prefix}/reboot":
if data["value"] == "reboot":
print("Rebooting device")
reset()
except: # pylint: disable=bare-except
print("Error decoding json from MQTT")

Expand Down
1 change: 1 addition & 0 deletions hal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from .wdt import WDT as WDT
from .i2c import I2C as I2C
from .memory import Memory as Memory
from .machine import reset as reset
4 changes: 4 additions & 0 deletions hal/machine/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
try:
from machine import reset as reset # type: ignore
except ModuleNotFoundError:
from .dummy_machine import reset as reset
2 changes: 2 additions & 0 deletions hal/machine/dummy_machine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def reset():
print("Resetting on dummy machine")

0 comments on commit f99a181

Please sign in to comment.