Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save sensor readings to timeseries database #194

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
649e452
Added writing to Time Series Database KairosDB
Bobsdorf May 1, 2018
32125d3
Merge branch 'master' of https://github.com/Manuel83/craftbeerpi3
Bobsdorf May 1, 2018
093d486
Added writing to Time Series Database KairosDB
Bobsdorf May 2, 2018
47d6e76
added KairosDB to installer
Bobsdorf May 3, 2018
33834c6
added KairosDB to installer
Bobsdorf May 3, 2018
ac328b8
Added writing to Time Series Database KairosDB
Bobsdorf May 3, 2018
0803e85
Added writing to Time Series Database KairosDB
Bobsdorf May 4, 2018
c34939f
Added writing to Time Series Database KairosDB
Bobsdorf May 4, 2018
8e4f509
corrected tagging of actual brew process for storing in time serires db
Bobsdorf May 16, 2018
b5c722b
corrected tagging of actual brew process for storing in time serires db
Bobsdorf May 16, 2018
869473f
simplified code for logging to logfile
Bobsdorf May 16, 2018
cc65591
added logging of "action" into time series database
Bobsdorf May 17, 2018
37068ca
Added branches to git overview on system tab
Bobsdorf May 18, 2018
b17203d
changed description for KairosDB parameter
Bobsdorf May 18, 2018
cdac4bc
Added additional parameters to configure timeseries database queries …
Bobsdorf May 23, 2018
590a523
Import boil time alerts from BeerXML
DrRataplan Jan 5, 2018
e57350e
Update __init__.py
clearwaterbrewer Apr 5, 2018
e0c2678
Remove duplicate line
archonic Apr 5, 2018
efc85d6
Remove "Steps started on xml imported" bug
swimIan Aug 3, 2018
640d921
Docker files for ease of development on PCs/macs
cgspeck Aug 13, 2018
f11f7d0
Fix typo which resulted in reference to incorrect port
cgspeck Aug 15, 2018
254a4c7
Merge pull request #4 from Manuel83/master
isarvalley Sep 23, 2018
a3bbbcc
beer.xml exports from "BrewersFriend" can contain a tag like "<STEP_T…
Bobsdorf Oct 28, 2018
1008613
beer.xml import differentates between mash step or mash in step, depe…
Bobsdorf Nov 1, 2018
b89dc3a
start event is called everytime a new step starts. So a new value for…
Bobsdorf Nov 1, 2018
2da4aff
Update package because of vulnerability
Oct 29, 2018
4d574b1
merged from original fork
Bobsdorf Nov 1, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -17,3 +17,4 @@ yarn.lock


modules/ui/package-lock.json
logs/
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -41,7 +41,7 @@ app_1 | [2018-08-13 12:54:44,264] ERROR in __init__: BUZZER not working
app_1 | (1) wsgi starting up on http://0.0.0.0:5000
```

The contents of this folder will be mounted to `/usr/src/craftbeerpi3` and the server will be accesible on `localhost:5000`.
The contents of this folder will be mounted to `/usr/src/craftbeerpi3` and the server will be accesible on `localhost:3000`.

## Donation

Expand Down
19 changes: 19 additions & 0 deletions config/logger.yaml
@@ -0,0 +1,19 @@
version: 1
formatters:
simple:
format: '%(asctime)s - %(levelname)s - %(name)s - %(message)s'
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
file:
class : logging.handlers.RotatingFileHandler
formatter: simple
filename: ./logs/app.log
maxBytes: 10000000
backupCount: 3
root:
level: DEBUG
handlers: [console, file]
26 changes: 19 additions & 7 deletions install.sh
Expand Up @@ -20,7 +20,8 @@ show_menu () {
"8" "Reset File Changes (git reset --hard)" \
"9" "Clear all logs" \
"10" "Reboot Raspberry Pi" \
"11" "Stop CraftBeerPi, Clear logs, Start CraftBeerPi" 3>&1 1>&2 2>&3)
"11" "Stop CraftBeerPi, Clear logs, Start CraftBeerPi" \
"12" "Install KairosDB" 3>&1 1>&2 2>&3)

BUTTON=$?
# Exit if user pressed cancel or escape
Expand Down Expand Up @@ -49,7 +50,7 @@ show_menu () {
apt-get -y install libpcre3-dev
pip install -r requirements.txt

confirmAnswer "Would you like to add active 1-wire support at your Raspberry PI now? IMPORTANT: The 1-wire thermometer must be conneted to GPIO 4!"
confirmAnswer "Would you like to add active 1-wire support at your Raspberry PI now? IMPORTANT: The 1-wire thermometer must be connected to GPIO 4!"
if [ $? = 0 ]; then
#apt-get -y update; apt-get -y upgrade;
echo '# CraftBeerPi 1-wire support' >> "/boot/config.txt"
Expand All @@ -62,14 +63,14 @@ show_menu () {
sed "s@#DIR#@${PWD}@g" config/craftbeerpiboot > /etc/init.d/craftbeerpiboot
chmod 755 /etc/init.d/craftbeerpiboot;

whiptail --title "Installition Finished" --msgbox "CraftBeerPi installation finished! You must hit OK to continue." 8 78
whiptail --title "Installation Finished" --msgbox "CraftBeerPi installation finished! You must hit OK to continue." 8 78
show_menu
;;
2)
confirmAnswer "Are you sure you want to clear the CraftBeerPi. All hardware setting will be deleted"
if [ $? = 0 ]; then
sudo rm -f craftbeerpi.db
whiptail --title "Database Delted" --msgbox "The CraftBeerPi database was succesfully deleted. You must hit OK to continue." 8 78
whiptail --title "Database Deleted" --msgbox "The CraftBeerPi database was successfully deleted. You must hit OK to continue." 8 78
show_menu
else
show_menu
Expand All @@ -81,7 +82,7 @@ show_menu () {
sed "s@#DIR#@${PWD}@g" config/craftbeerpiboot > /etc/init.d/craftbeerpiboot
chmod 755 /etc/init.d/craftbeerpiboot;
update-rc.d craftbeerpiboot defaults;
whiptail --title "Added succesfull to autostart" --msgbox "The CraftBeerPi was added to autostart succesfully. You must hit OK to continue." 8 78
whiptail --title "Added successful to autostart" --msgbox "The CraftBeerPi was added to autostart successfully. You must hit OK to continue." 8 78
show_menu
else
show_menu
Expand All @@ -104,7 +105,7 @@ show_menu () {
;;
6)
sudo /etc/init.d/craftbeerpiboot stop
whiptail --title "CraftBeerPi stoped" --msgbox "The software is stoped" 8 78
whiptail --title "CraftBeerPi stopped" --msgbox "The software is stopped" 8 78
show_menu
;;
7)
Expand All @@ -118,7 +119,7 @@ show_menu () {
fi
;;
8)
confirmAnswer "Are you sure you want to reset all file changes for this git respository (git reset --hard)?"
confirmAnswer "Are you sure you want to reset all file changes for this git repository (git reset --hard)?"
if [ $? = 0 ]; then
whiptail --textbox /dev/stdin 20 50 <<<"$(git reset --hard)"
show_menu
Expand Down Expand Up @@ -155,6 +156,17 @@ show_menu () {
show_menu
fi
;;
12)
confirmAnswer "Are you sure you want to install KairosDB?"
if [ $? = 0 ]; then
wget https://github.com/kairosdb/kairosdb/releases/download/v1.2.1/kairosdb_1.2.1-1_all.deb
sudo dpkg -i kairosdb_1.2.1-1_all.deb
sudo service kairosdb start
show_menu
else
show_menu
fi
;;
esac
fi
}
Expand Down
17 changes: 5 additions & 12 deletions modules/app_config.py
@@ -1,23 +1,16 @@
from flask import Flask, json, g
from flask_socketio import SocketIO

import json
import sys, os
from flask import Flask, render_template, redirect, json, g


from flask_socketio import SocketIO, emit

import logging


import yaml
import logging.config

from modules.core.core import CraftBeerPi, ActorBase, SensorBase
from modules.core.db import DBModel

app = Flask(__name__)

FORMAT = '%(asctime)-15s - %(levelname)s - %(message)s'
logging.config.dictConfig(yaml.load(open('./config/logger.yaml', 'r')))

logging.basicConfig(filename='./logs/app.log',level=logging.INFO, format=FORMAT)
app.config['SECRET_KEY'] = 'craftbeerpi'
app.config['UPLOAD_FOLDER'] = './upload'

Expand Down
56 changes: 42 additions & 14 deletions modules/core/core.py
@@ -1,23 +1,22 @@
import inspect
import pprint

import sqlite3
from flask import make_response, g
import datetime
from datetime import datetime
from flask.views import MethodView
from flask_classy import FlaskView, route

from time import localtime, strftime
from functools import wraps, update_wrapper

import requests
import json

from props import *

from hardware import *

import time
import uuid
import logging


class NotificationAPI(object):
Expand Down Expand Up @@ -86,6 +85,8 @@ def init_sensors(self):
:return:
'''

self.logger = logging.getLogger(__name__)

self.app.logger.info("Init Sensors")

t = self.cache.get("sensor_types")
Expand Down Expand Up @@ -145,22 +146,48 @@ def start_active_sensor(instance):

def receive_sensor_value(self, id, value):
self.emit("SENSOR_UPDATE", self.cache.get("sensors")[id])
self.save_to_file(id, value)
self.save_to_file(id, value, "sensor")

def save_to_file(self, id, value, prefix):
sensor_name = "%s_%s" % (prefix, str(id))
use_kairosdb = (self.cache["config"]["kairos_db"].__dict__["value"] == "YES")

if use_kairosdb:
self.write_to_tsdb(prefix, sensor_name, value)
else:
self.write_to_logfile(sensor_name, value)

def save_to_file(self, id, value, prefix="sensor"):
filename = "./logs/%s_%s.log" % (prefix, str(id))
def write_to_logfile(self, sensor_name, value):
filename = "./logs/%s.log" % sensor_name
formatted_time = strftime("%Y-%m-%d %H:%M:%S", localtime())
msg = str(formatted_time) + "," +str(value) + "\n"
msg = str(formatted_time) + "," + str(value) + "\n"

with open(filename, "a") as file:
file.write(msg)

def write_to_tsdb(self, prefix, sensor_name, value):
kairosdb_server = "http://127.0.0.1:" + self.cache["config"]["kairos_db_port"].__dict__["value"]

data = [
dict(name="cbpi." + sensor_name, datapoints=[
[int(round(time.time() * 1000)), value]
], tags={
"cbpi": prefix,
"brew": self.cache["active_brew"]
})
]

response = requests.post(kairosdb_server + "/api/v1/datapoints", json.dumps(data))
if not response.ok:
self.logger.warning("Failed to write time series entry for [%s]. Response [%s]", sensor_name, response)

def log_action(self, text):
filename = "./logs/action.log"
formatted_time = strftime("%Y-%m-%d %H:%M:%S", localtime())
with open(filename, "a") as file:
text = text.encode("utf-8")
file.write("%s,%s\n" % (formatted_time, text))
use_kairosdb = (self.cache["config"]["kairos_db"].__dict__["value"] == "YES")

if use_kairosdb:
self.write_to_tsdb("action", "action", text)
else:
self.write_to_logfile("action", text)

def shutdown_sensor(self, id):
self.cache.get("sensors")[id].stop()
Expand Down Expand Up @@ -204,7 +231,8 @@ class CraftBeerPi(ActorAPI, SensorAPI):
"messages": [],
"plugins": {},
"fermentation_controller_types": {},
"fermenter_task": {}
"fermenter_task": {},
"active_brew": "none"
}
buzzer = None
eventbus = {}
Expand Down