-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_connect.py
58 lines (45 loc) · 1.98 KB
/
mqtt_connect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# mqtt_connect.py
import os
import paho.mqtt.client as mqtt
from flask import Blueprint, jsonify
from models import Device, FeedTime
from datetime import datetime
# Blueprint for adding and editing user devices
mqtt_connect_bp = Blueprint('mqtt_connect', __name__)
MQTT_BROKER = 'mqtt_broker'
MQTT_PORT = 1883
class MQTTClient:
def __init__(self):
self.client = mqtt.Client()
self.client.on_message = self.on_message
self.client.connect(MQTT_BROKER, MQTT_PORT, 60)
self.client.subscribe("sensor_data/food_level")
self.client.loop_start()
self.mqtt_data = {}
def on_message(self, client, userdata, message):
payload = message.payload.decode()
try:
nickname, sensor_data = payload.split(';')
nickname = nickname.split(': ')[1].strip()
food_level = sensor_data.split(': ')[1].strip()
self.mqtt_data[nickname] = food_level
except ValueError:
print("Invalid MQTT message format")
def publish_feedtimes(self, device_id):
device = Device.query.get(device_id)
if not device:
return jsonify({"error": "Device not found!"}), 404
feedtimes = FeedTime.query.filter_by(device_id=device_id).all()
time_portion_pairs = [f"{datetime.strptime(feedtime.time, '%H:%M:%S').strftime('%H:%M')}:{feedtime.portions}" for feedtime in feedtimes]
topic = "feedtimes/" + device.nickname
payload = ",".join(time_portion_pairs)
self.client.publish(topic, payload, retain=True, qos=1)
print("Message published")
return jsonify({"message": "Feed times and portions published successfully!"}), 200
mqtt_client = MQTTClient()
@mqtt_connect_bp.route('/publish_feedtimes/<int:device_id>', methods=['POST'])
def publish_feedtimes(device_id):
return mqtt_client.publish_feedtimes(device_id)
@mqtt_connect_bp.route('/latest_mqtt_message')
def get_latest_mqtt_message():
return jsonify({'message': mqtt_client.mqtt_data})