Skip to content

Commit

Permalink
adapted brain and frontend:flet to (some time ago) changed topic sche…
Browse files Browse the repository at this point in the history
…me from arduino implementation
  • Loading branch information
juergenpabel committed Jun 1, 2024
1 parent 8683167 commit bd2ec4e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 23 deletions.
2 changes: 1 addition & 1 deletion enclosure/services/brain/demo-en_US
Submodule demo-en_US updated 2 files
+12 −22 brain.ini
+1 −1 requirements.txt
7 changes: 4 additions & 3 deletions enclosure/services/brain/package/hal9000/brain/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,10 @@ def on_mqtt(self, client, userdata, message) -> None:
signals[synapse_name] = signal
for synapse_name in signals.keys():
signal = signals[synapse_name]
self.logger.debug("SIGNAL generated from triggers = {}".format(signal))
for action_name in self.synapses[synapse_name]:
self.actions[action_name].process(signal, self.cortex)
if signal is not None and bool(signal) is not False:
self.logger.debug("SIGNAL generated from triggers = {}".format(signal))
for action_name in self.synapses[synapse_name]:
self.actions[action_name].process(signal, self.cortex)
self.logger.debug("CORTEX after actions = {}".format(self.cortex))
self.process_queued_signals()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/python3

import re
import json
from configparser import ConfigParser
from re import match as re_match
from json import loads as json_loads
from jsonpath_ng.ext import parse as jsonpath_ng_ext_parse
from configparser import ConfigParser as configparser_ConfigParser

from hal9000.brain.modules import HAL9000_Trigger

Expand All @@ -14,9 +15,10 @@ def __init__(self, trigger_name: str) -> None:
self.config = dict()


def configure(self, configuration: ConfigParser, section_name: str) -> None:
def configure(self, configuration: configparser_ConfigParser, section_name: str) -> None:
self.config['topic'] = configuration.getstring(section_name, 'mqtt-topic', fallback=None)
self.config['payload-regex'] = configuration.getstring(section_name, 'mqtt-payload-regex', fallback=None)
self.config['payload-jsonpath'] = configuration.getstring(section_name, 'mqtt-payload-jsonpath', fallback=None)
self.config['neuron-json-formatter'] = configuration.getstring(section_name, 'neuron-json-formatter', fallback=None)


Expand All @@ -30,12 +32,18 @@ def callbacks(self) -> dict:


def handle(self, message) -> dict:
neuron = None
matches = re.match(self.config['payload-regex'], message.payload.decode('utf-8'))
if matches is not None:
neuron = matches.groupdict()
formatter = self.config['neuron-json-formatter']
if formatter is not None:
neuron = json.loads(formatter % neuron)
neuron = {}
if self.config['neuron-json-formatter'] is not None:
try:
payload = message.payload.decode('utf-8')
if self.config['payload-regex'] is not None:
matches = re_match(self.config['payload-regex'], payload)
if matches is not None:
neuron = json_loads(self.config['neuron-json-formatter'] % matches.groupdict())
if self.config['payload-jsonpath'] is not None:
for match in jsonpath_ng_ext_parse(self.config['payload-jsonpath']).find(json_loads(f"[{payload}]")):
neuron = json_loads(self.config['neuron-json-formatter'] % {'jsonpath': match.value})
except Exception as e:
print(e)
return neuron

2 changes: 1 addition & 1 deletion enclosure/services/brain/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jsonpath-rw
jsonpath-ng>=1.6.1
paho-mqtt<2
requests
dbus-fast
Original file line number Diff line number Diff line change
Expand Up @@ -155,22 +155,22 @@ def show_menu(self, display, data):


def on_control_up(self, event):
self.events.put_nowait({'topic': 'rotary/control', 'payload': 'rotary:control delta=+1'})
self.events.put_nowait({'topic': 'device/event', 'payload': '{"device": {"type": "rotary", "name": "control"}, "event": {"delta": "+1"}}'})

def on_control_down(self, event):
self.events.put_nowait({'topic': 'rotary/control', 'payload': 'rotary:control delta=-1'})
self.events.put_nowait({'topic': 'device/event', 'payload': '{"device": {"type": "rotary", "name": "control"}, "event": {"delta": "-1"}}'})

def on_control_select(self, event):
self.events.put_nowait({'topic': 'button/control', 'payload': 'button:control status=clicked'})
self.events.put_nowait({'topic': 'device/event', 'payload': '{"device": {"type": "button", "name": "control"}, "event": {"status": "clicked"}}'})

def on_volume_up(self, event):
self.events.put_nowait({'topic': 'rotary/volume', 'payload': 'rotary:volume delta=+1'})
self.events.put_nowait({'topic': 'device/event', 'payload': '{"device": {"type": "rotary", "name": "volume"}, "event": {"delta": "+1"}}'})

def on_volume_down(self, event):
self.events.put_nowait({'topic': 'rotary/volume', 'payload': 'rotary:volume delta=-1'})
self.events.put_nowait({'topic': 'device/event', 'payload': '{"device": {"type": "rotary", "name": "volume"}, "event": {"delta": "-1"}}'})

def on_volume_mute(self, event):
self.events.put_nowait({'topic': 'button/volume', 'payload': 'button:volume status=clicked'})
self.events.put_nowait({'topic': 'device/event', 'payload': '{"device": {"type": "button", "name": "volume"}, "event": {"status": "clicked"}}'})


def flet_on_disconnect(self, event):
Expand Down
2 changes: 1 addition & 1 deletion resources/container/podman/localhost/podman_publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ echo "Publishing images to '$CONTAINER_REGISTRY/$CONTAINER_REPOSITORY/...' (with
UPLOAD_FAILED=""
for CONTAINER_NAME in hal9000-mosquitto hal9000-kalliope hal9000-frontend hal9000-brain hal9000-console ; do
echo "Publishing 'localhost/$CONTAINER_NAME:latest' to '$CONTAINER_REGISTRY/$CONTAINER_REPOSITORY/$CONTAINER_NAME:$CONTAINER_TAG'..."
podman manifest push -q localhost/$CONTAINER_NAME:latest $CONTAINER_REGISTRY/$CONTAINER_REPOSITORY/$CONTAINER_NAME:$CONTAINER_TAG
podman manifest push localhost/$CONTAINER_NAME:latest $CONTAINER_REGISTRY/$CONTAINER_REPOSITORY/$CONTAINER_NAME:$CONTAINER_TAG
if [ "x$?" != "x0" ]; then
UPLOAD_FAILED="$UPLOAD_FAILED$CONTAINER_NAME "
echo "NOTICE: Upload failed for $CONTAINER_NAME"
Expand Down

0 comments on commit bd2ec4e

Please sign in to comment.