Save brewing data as db model
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.utils import timezone
|
||||
from django.conf import settings
|
||||
from coffee_scale.models import Brewing
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
import random
|
||||
|
||||
brewing = False
|
||||
lastbrew = timezone.now()
|
||||
lastcups = 0
|
||||
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
if rc != 0:
|
||||
print("Failed to connect with result code: {}".format(rc))
|
||||
|
||||
print("Connected with result code: {}".format(rc))
|
||||
client.subscribe(settings.MQTT_SETTINGS.HOST.TOPICS.CUPS)
|
||||
client.subscribe(settings.MQTT_SETTINGS.HOST.TOPICS.BREWING)
|
||||
client.subscribe(settings.MQTT_SETTINGS.HOST.TOPICS.BREW_TIME)
|
||||
|
||||
|
||||
def on_message(client, userdata, message):
|
||||
print("%s %s".format(message.topic, message.payload.decode()))
|
||||
|
||||
|
||||
def on_message_cups(client, userdata, message):
|
||||
cups = int(message.payload.decode())
|
||||
print("cups: {}".format(cups))
|
||||
print("{}".format(timezone.now()))
|
||||
|
||||
# checks if new coffee was brewed so we don't add the same brewing again to db
|
||||
global lastcups # ;/ have to use global to store state instead of class
|
||||
if cups > lastcups:
|
||||
new_brew = Brewing(cups=cups, time=timezone.now())
|
||||
print(new_brew.time)
|
||||
new_brew.save()
|
||||
lastcups = cups
|
||||
|
||||
|
||||
def on_message_brewtime(client, userdata, message):
|
||||
brewtime = datetime.fromtimestamp(int(message.payload.decode()))
|
||||
print("brewtime: {}".format(brewtime))
|
||||
|
||||
|
||||
def on_message_brewing(client, userdata, message):
|
||||
brewing = bool(int(message.payload.decode()))
|
||||
print("brewing: {}".format(brewing))
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Fetches coffee mqtt messages"
|
||||
|
||||
def add_arquments(self, parser):
|
||||
pass
|
||||
|
||||
def handle(self, *args, **options):
|
||||
self.username = "coffee-user-%d".format(random.randint(0, 100))
|
||||
self.client = mqtt.Client("coffee")
|
||||
self.client.username_pw_set(self.username, password=None)
|
||||
self.client.tls_set()
|
||||
|
||||
# callbacks for different topics
|
||||
self.client.message_callback_add(settings.MQTT_SETTINGS.HOST.TOPICS.BREW_TIME, on_message_brewtime)
|
||||
self.client.message_callback_add(settings.MQTT_SETTINGS.HOST.TOPICS.CUPS, on_message_cups)
|
||||
self.client.message_callback_add(settings.MQTT_SETTINGS.HOST.TOPICS.BREWING, on_message_brewing)
|
||||
|
||||
# self.client.connect("localhost", port=1883) # used for local testing
|
||||
self.client.connect(settings.MQTT_SETTINGS.HOST, port=settings.MQTT_SETTINGS.PORT)
|
||||
self.client.on_message = on_message
|
||||
self.client.on_connect = on_connect
|
||||
while True:
|
||||
self.client.loop()
|
||||
@@ -0,0 +1,24 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11 on 2017-11-19 10:55
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Brewing',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('cups', models.PositiveSmallIntegerField()),
|
||||
('time', models.DateTimeField()),
|
||||
],
|
||||
),
|
||||
]
|
||||
@@ -1,3 +1,6 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
|
||||
class Brewing(models.Model):
|
||||
cups = models.PositiveSmallIntegerField()
|
||||
time = models.DateTimeField()
|
||||
|
||||
+2
-1
@@ -32,4 +32,5 @@ telepot==12.3
|
||||
django-import-export==0.5.1
|
||||
django-password-reset==1.0
|
||||
pyexcel==0.5.6
|
||||
pyexcel-xlsx==0.5.2
|
||||
pyexcel-xlsx==0.5.2
|
||||
paho-mqtt==1.3.1
|
||||
|
||||
Reference in New Issue
Block a user