This repository has been archived by the owner on May 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
150 lines (128 loc) · 5.38 KB
/
app.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/python3
import serial
import re
import datetime
import pytz
import requests
from config import confSerial, database, timezone, objects
# Default configuration for DSMR 5.0
scon = serial.Serial()
scon.port = confSerial["port"]
scon.baudrate = confSerial["baudrate"]
scon.ByteSize = serial.EIGHTBITS
scon.parity = serial.PARITY_NONE
scon.stopbits = serial.STOPBITS_ONE
scon.xonxoff = 0
scon.rtscts = 0
scon.timeout = 2
data = []
reMatch = re.compile("(\d{3,}\.\d*|\d{3,}|\d{2}\.\d{3,})") # This RE is used to match data between `()`
def timeConv(data):
"""Expects `data` as string. Returns string with UNIX time UTC"""
# Messing with timeConv might result in influx not liking your date/time. beware.
matches = re.findall(reMatch, data)
if len(matches):
t = matches[0]
tz = pytz.timezone(timezone['tz'])
# format string read from meter output
date = datetime.datetime.strptime(t, "%y%m%d%H%M%S")
# convert local time read from meter, to UTC time
local_dt = tz.localize(date, is_dst=None)
utc_dt = local_dt.astimezone(pytz.utc)
date = int(utc_dt.timestamp()) # convert to unix
return(date)
def floatConv(data):
"""Expects `data` as string. Returns a `float`"""
matches = re.findall(reMatch, data)
if len(matches):
k = matches[0]
k = k.lstrip("0") # Need to strip all leading zero's. Otherwise it cannot be converted to float.
return(float(k))
def intConv(data):
matches = re.findall(reMatch, data)
if len(matches):
t = matches[0]
t = t.lstrip("0") # Need to strip all leading zero's. Otherwise it cannot be converted to int.
t = t.rstrip(".0") # trailing '.0' can only be stripped from volt and amps
return(int(t))
def gasConv(data):
"""Expects `data` as string. Returns a `float`"""
matches = re.findall(reMatch, data)
if len(matches):
#time = timeConv(matches[0]) # Time returned here is only upated every 5 minutes according to DSMR standard
gas = matches[1]
return(gas)
def postData(data):
"""Function expects `data` as follows:
`'energy,tariff1=value,tariff2=value timestamp'`
Databse config is in `config.py`"""
try:
response = requests.post("http://{}:{}/write?db={}&precision=s".format(
database["host"], database['port'], database["db"]), data=data)
if response.status_code == 204:
return
except requests.ConnectionError as e:
print("[DSMRpi ERROR] Connection to InfluxDB failed: {}".format(e))
else:
print("[DSMRpi ERROR] InfluxDB did not accept the data: {}".format(response.status_code))
print(response.content)
#
#
# Opening connection to serial
firstloop = True
try:
scon.open()
_ = scon.readline() # first line of output is "b'\x00\n'". and not needed.
print("[DSMRpi INFO] Successfully opened serial connection on {c}.\nWriting data to: {i}:{p} DB:{d}".format(c=confSerial['port'], i=database['host'], p=database['port'], d=database['db']))
while True:
line = str(scon.readline())
if "!" not in line:
# read data until '!' is returned in scon.readline
# Once `!` is seen in `line`, it will defer to the else statement
data.append(line)
elif firstloop == False:
# Looping over all lines in data. Figuring out all the bits and pieces.
for i in data:
if objects['timestamp'] in i:
timestamp = timeConv(i)
elif objects['tariff1down'] in i:
tariff1= floatConv(i)
elif objects['tariff2down'] in i:
tariff2 = floatConv(i)
elif objects['powerused'] in i:
powerused = floatConv(i)
elif objects['powersent'] in i:
powersent = floatConv(i)
elif objects['gas'] in i:
gas = gasConv(i)
elif objects['tariffindicator'] in i:
tinc = intConv(i)
elif objects['voltageL1'] in i:
vl1 = intConv(i)
elif objects['currentL1'] in i:
cl1 = intConv(i)
#TODO power failure logging
# elif objects['powerfails'] in i:
# pass
# elif objects['longpowerf'] in i:
# pass
# Create the string to pass to influxDB
# according to https://docs.influxdata.com/influxdb/v1.7/guides/writing_data/
s = """tariff1,tariffindicator={tinc} value={t1} {ts}
tariff2,tariffindicator={tinc} value={t2} {ts}
powerused,tariffindicator={tinc} value={pu} {ts}
powersent,tariffindicator={tinc} value={ps} {ts}
gas,tariffindicator={tinc} value={gas} {ts}
voltageL1,tariffindicator={tinc} value={vl1} {ts}
currentL1,tariffindicator={tinc} value={cl1} {ts}""".format(tinc=tinc, ts=timestamp, t1=tariff1, t2=tariff2, pu=powerused, ps=powersent, gas=gas, vl1=vl1, cl1=cl1)
postData(s)
# cleanup
data.clear()
else:
# do nothing, and set firstloop to False
firstloop = False
print("[DSMRpi INFO] Starting to send data now. No further output expected.")
data.clear()
except serial.SerialException as e:
print("[DSMRpi ERROR] {}\n").format(e)
# END