Hager Domovea Custom Reporting

De Hager Domovea TJA450 server biedt geen mogelijkheid om data automatisch te exporteren naar een database. Via de servermanager kan je de historische data van meetwaarden wel downloaden naar een csv of xml bestand maar daar houdt het dus ook op.

Persoonlijk vind ik het interessant om bv bij te houden hoeveel keer een bepaald event zich voorgedaan heeft. bv: het openen van de achterdeur of garagepoort.

De domovea server is wel in staat om emails te sturen. Dus de idee ontstond al snel om elke week een rapportje naar mezelf te mailen met een overzichtje van wat er allemaal gebeurd is.

BV: Magneetcontact achterdeur gaat open

–> busevent komt bij de domovea server

domoveastats1

domoveastats2

–> de domovea server doet de variabele achterdeurOpen + 1

domoveastats3

–> zondagnacht verstuurt de domoveaserver een rapportje en reset alle variabelen.

domoveastats4

domoveastats5

 

Zo een wekelijks rapportje ziet er als volgt uit

————————————————————–
START DOMOVEA REPORT
————————————————————–

Weekly Facts:

– Aantal keer garage geopend: 34
– Aantal keer schuifraam geopend: 27
– Aantal keer achterdeur geopend: 164
– Aantal keer voordeur geopend: 7
– Aantal keer alarm ingeschakeld: 6
– Buitenalarm afgegaan: 0
– Binnenkomst als alarm nog actief was (Lichten pinken): 1
– Aantal keer op de deurbel geduwd: 4
– Aantal keer de filmmodus ingeschakeld: 0
– Aantal keer nachtmodus aangezet: 7

————————————————————–
Variabelen Check:

Status Filmmodus: Vals (0)
Status AlarmNetAan: Vals (0)
Status DeurbelVerlichting: Vals (0)

————————————————————–
END DOMOVEA REPORT
————————————————————–

 

Het zou interessant zijn mocht er een mogelijkheid bestaan om zelf een logfile aan te maken of de waarden vanuit domovea rechtstreeks te kunnen wegschrijven in een database. Maar tot op heden is dit dus niet het geval.

Als workaround heb ik een python scriptje geschreven die deze mailtjes uitleest uit mijn gmail account en wegschrijft in een MySQL tabel.

import os
import sys
import datetime
from gmail import Gmail

## Important notes
# give stijn_domovea file permissions for writing files to NAS!
#
# UPDATE mysql.user SET File_priv = 'Y' WHERE user='stijn_domovea' AND host='localhost';
# 1 rij bijgewerkt.
# FLUSH PRIVILEGES;
# MySQL gaf een lege resultatenset terug (0 rijen).

# Vars
mysqlbin="/usr/bin/mysql"
mysqlhost="localhost"
mysqlport="3306"
#mysqluser="xxx"
mysqluser="xxx"
mysqldb="xxx"
#mysqlpass="xxx"
mysqlpass="xxx"

MySQLOutFile = "/tmp/tmpWRdata.txt"
LogOutFile = "/volume2/data/scripts/DomoveaStats/log/processedDomoveaStats.log"

# Classes
class Report:
def __init__(self, date, garage, schuifraam, achterdeur, voordeur, alarm_aan, alarm_actief, alarm_stil, deurbel, filmmodus, nachtmodus):
self.date = date
self.garage = garage
self.schuifraam = schuifraam
self.achterdeur = achterdeur
self.voordeur = voordeur
self.alarm_aan = alarm_aan
self.alarm_actief = alarm_actief
self.alarm_stil = alarm_stil
self.deurbel = deurbel
self.filmmodus = filmmodus
self.nachtmodus = nachtmodus

def toString(self):
datest = str(self.date)
st = "reportdate %s - garage %d - schuifraam %d - achterdeur %d - voordeur %d - alarm_aan %d - alarm_actief %d - alarm_stil %d - deurbel %d - filmmodus %d - nachtmodus %d" % (datest, self.garage, self.schuifraam, self.achterdeur, self.voordeur, self.alarm_aan, self.alarm_actief, self.alarm_stil, self.deurbel, self.filmmodus, self.nachtmodus)
return st

def sql(sqlcmd):
os.system(mysqlbin + " -h" + mysqlhost + " -P" + mysqlport + " -u" + mysqluser + " -p" + mysqlpass + " -e " + sqlcmd)

# Body
# Retrieve mails
g = Gmail()
g.login("xxx@gmail.com", "onetimeapplicationpassword")
mails = g.label("DeLinde/WeeklyStats").mail(prefetch=True)

# Build array with reports
allReports = []
x = 0
totalmails = len(mails)

# Quit when gmail label is empty !
if totalmails == 0:
print "no reports found --> exiting script!"
exit(0)

while x < totalmails:
r = Report(0,0,0,0,0,0,0,0,0,0,0)
r.date = mails[x].sent_at
bl = mails[x].body.split('\n')

r.garage = int(filter(str.isdigit, bl[7]))
r.schuifraam = int(filter(str.isdigit, bl[8]))
r.achterdeur = int(filter(str.isdigit, bl[9]))
r.voordeur = int(filter(str.isdigit, bl[10]))
r.alarm_aan = int(filter(str.isdigit, bl[11]))
r.alarm_actief = int(filter(str.isdigit, bl[12]))
r.alarm_stil = int(filter(str.isdigit, bl[13]))
r.deurbel = int(filter(str.isdigit, bl[14]))
r.filmmodus = int(filter(str.isdigit, bl[15]))
r.nachtmodus = int(filter(str.isdigit, bl[16]))
allReports.append(r)

x = x + 1
print "Done! building report objects"

# Build MySQL connection & update stijn_domovea database
try:
file = open(LogOutFile, "ab")
# get all added dates, on this base we will filter reports already added to DB. In this way we will never add duplicate files
# sql("'use stijn_domovea; select date from WeeklyReports INTO OUTFILE \"/volume2/data/scripts/DomoveaStats/tmpWRdata.txt\";'")
sql("'use stijn_domovea; select date from WeeklyReports INTO OUTFILE \"/tmp/tmpWRdata.txt\";'")

for ar in allReports:
#check date already in DB
date = str(ar.date)
if date in open(MySQLOutFile).read():
print "found %s already in db." % date
else:
sqlstr = "'use stijn_domovea; INSERT INTO WeeklyReports (date,garage,schuifraam,achterdeur,voordeur,alarm_aan,alarm_actief,alarm_stil,deurbel,filmmodus,nachtmodus) VALUES (\"%s\",%d,%d,%d,%d,%d,%d,%d,%d,%d,%d);'" % (str(ar.date), ar.garage, ar.schuifraam, ar.achterdeur, ar.voordeur, ar.alarm_aan, ar.alarm_actief, ar.alarm_stil, ar.deurbel, ar.filmmodus, ar.nachtmodus)
sql(sqlstr)
#print "%s --> Totaal aantal keer garagepoort geopend: %d" % (str(ar.date), ar.garage)
#print sqlstr
print "added %s to db." % date
# append entry to log
logline = "date added: " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " --> " + ar.toString() + "\n"
#with open(LogOutFile, "a") as myfile:
# myfile.write(logline)
file.write(logline)
file.write(u"\r\n")

# Remove temporary file
os.remove(MySQLOutFile)

file.close()

except mdb.Error, e:
# truncate temporary file
os.remove(MySQLOutFile)
# open(MySQLOutFile, 'w').close()

print "Error %d: %s" % (e.args[0],e.args[1])
sys.exit(1)

finally:

sys.exit(0)

 

Plaats een reactie