3 Commits
0.0.2 ... 0.0.6

Author SHA1 Message Date
7abec12620 new mqtt callback api
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2024-11-07 10:11:04 +01:00
046430d1d1 reload configmap
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2024-11-06 17:53:01 +01:00
000510202e separate configmap and deployment, add reloader
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2024-11-06 17:47:00 +01:00
4 changed files with 55 additions and 51 deletions

45
deployment/configmap.yml Normal file
View File

@ -0,0 +1,45 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: heating-controller-config
namespace: homea
data:
MQTT_BROKER: "emqx01-anonymous-cluster-internal.broker.svc.cluster.local"
MQTT_PORT: "1883"
MQTT_CLIENT_PREFIX: "HeatingController"
MQTT_BOX_TOPIC_PREFIXES: |
{
"high_temp": "heating/config/high_temp/",
"cmd": "heating/command/"
}
MQTT_CENTRAL_TOPICS: |
{
"general_off": "heating/system/general_off",
"maintenance_mode": "heating/system/maintenance_mode",
"status": "heating/system/status"
}
MQTT_STATUS_TOPIC: "heating/status"
OFF_TEMPERATURE: "5.0"
LOW_TEMPERATURE: "15.0"
DEFAULT_HIGH_TEMPERATURE: "21.0"
MAINTENANCE_TEMPERATURE: "30.0"
BOXES: |
{
"box1": {
"label": "living_room",
"windows": [
{ "topic": "window/living_room/garden_side", "label": "garden_side" }
],
"output_topic": "output/living_room"
},
"box2": {
"label": "kitchen",
"windows": [
{ "topic": "window/kitchen/street_side", "label": "street_side" },
{ "topic": "window/kitchen/garden_side", "label": "garden_side" },
{ "topic": "window/kitchen/garden_door", "label": "garden_door" }
],
"output_topic": "output/kitchen"
}
}

View File

@ -1,52 +1,9 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: heating-controller-config
data:
MQTT_BROKER: "emqx01-anonymous-cluster-internal.broker.svc.cluster.local"
MQTT_PORT: "1883"
MQTT_CLIENT_PREFIX: "HeatingController"
MQTT_BOX_TOPIC_PREFIXES: |
{
"high_temp": "heating/config/high_temp/",
"cmd": "heating/command/"
}
MQTT_CENTRAL_TOPICS: |
{
"general_off": "heating/system/general_off",
"maintenance_mode": "heating/system/maintenance_mode",
"status": "heating/system/status"
}
MQTT_STATUS_TOPIC: "heating/status"
OFF_TEMPERATURE: "5.0"
LOW_TEMPERATURE: "15.0"
DEFAULT_HIGH_TEMPERATURE: "21.0"
MAINTENANCE_TEMPERATURE: "30.0"
BOXES: |
{
"box1": {
"label": "living_room",
"windows": [
{ "topic": "window/living_room/street_side", "label": "street_side" },
{ "topic": "window/living_room/garden_side", "label": "garden_side" }
],
"output_topic": "output/living_room"
},
"box2": {
"label": "kitchen",
"windows": [
{ "topic": "window/kitchen/street_side", "label": "street_side" },
{ "topic": "window/kitchen/garden_side", "label": "garden_side" },
{ "topic": "window/kitchen/garden_door", "label": "garden_door" }
],
"output_topic": "output/kitchen"
}
}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: heating-controller
annotations:
configmap.reloader.stakater.com/reload: "heating-controller-config"
spec:
replicas: 1
selector:

View File

@ -16,6 +16,8 @@ kubectl create namespace $NAMESPACE \
-o yaml | \
kubectl -f - apply
kubectl apply -f $DEPLOYMENT_DIR/configmap.yml -n $NAMESPACE
cat $DEPLOYMENT_DIR/deploy-yml.tmpl | \
sed -e 's,%IMAGE%,'$IMAGE_NAME':'$IMAGE_TAG','g | \
kubectl apply -f - -n $NAMESPACE

View File

@ -91,8 +91,8 @@ CLIENT_ID = f"{CLIENT_PREFIX}_{uuid.uuid4()}"
topic_mapping = {}
# Callback function for successful connection to the broker
def on_connect(client, userdata, flags, rc):
if rc == 0:
def on_connect(client, userdata, flags, reason_code, properties):
if reason_code == 0:
logger.info("Connected to the broker")
# Subscribe to dynamically generated topics for each box and create mappings
@ -125,7 +125,7 @@ def on_connect(client, userdata, flags, rc):
topic_mapping[central_topic] = ("__central__", central_key)
logger.info(f"Subscribed to central topic '{central_topic}' (Key: '{central_key}')")
else:
logger.error(f"Connection error with code {rc}")
logger.error(f"Connection error with code {reason_code}")
# Callback function for received messages
def on_message(client, userdata, msg):
@ -149,8 +149,8 @@ def on_message(client, userdata, msg):
logger.error(f"Error processing message from '{msg.topic}': {e}")
# Callback function for disconnection
def on_disconnect(client, userdata, rc):
if rc != 0:
def on_disconnect(client, userdata, flags, reason_code, properties):
if reason_code != 0:
logger.warning("Unexpected disconnection, attempting to reconnect...")
else:
logger.info("Disconnected from the broker.")
@ -161,7 +161,7 @@ def handle_exit_signal(signum, frame):
client.disconnect() # Disconnects from the broker and stops loop_forever()
# Initialize the MQTT client and configure callbacks
client = mqtt.Client(client_id=CLIENT_ID)
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id=CLIENT_ID, )
context['client'] = client