-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrentcost.py
More file actions
executable file
·68 lines (59 loc) · 2.73 KB
/
currentcost.py
File metadata and controls
executable file
·68 lines (59 loc) · 2.73 KB
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
#!/usr/bin/env python
# XML lib
from currentcostlib import Packet
import serial
import sys
import argparse
parser = argparse.ArgumentParser(description='Read XML packages from Current Cost monitor and log power and temperature readings.')
parser.add_argument('--device', metavar='DEV', default='/dev/ttyUSB0',
help='the serial device to read from, defaults to /dev/ttyUSB0')
parser.add_argument('--raw', action='store_true',
help='log raw received XML data into a file raw.log')
parser.add_argument('--log', action='store_true',
help='write single line data points into a text file parsed.log')
parser.add_argument('-o', '--stdout', action='store_true',
help='print messages to stdout')
parser.add_argument('--influx', action='store_true',
help='write single line data points into file influx.log suitable for InfluxDB import')
parser.add_argument('--logdir', type=argparse.FileType('dir'),
help='directory to store log files into')
parser.add_argument('--mqttserver', metavar='HOST', default='localhost',
help='MQTT host to connect to')
parser.add_argument('--mqttport', metavar='P', type=int, default='1883',
help='MQTT port to connect to')
parser.add_argument('--mqtttopic', metavar='TOPIC',
help='MQTT topic root, e.g., foobar/currentcost/. If set attempt to log data to MQTT broker. Will publish to TOPIC/temp, TOPIC/ccmontime and TOPIC/watts.')
parser.add_argument('--mqttuser', default='', help='MQTT user name')
parser.add_argument('--mqttpassword', default='', help='MQTT password')
parser.add_argument('--mqttcacerts', default='', help='String path to trusted CA certificates for MQTT TLS server authentication.')
args = parser.parse_args()
conn = serial.Serial(args.device,baudrate=57600, timeout=30)
if args.mqttuser:
mqtt_auth = {'username':args.mqttuser, 'password':args.mqttpassword}
else:
mqtt_auth = None
if args.mqttcacerts:
mqtt_tls = {'ca_certs':args.mqttcacerts}
else:
mqtt_tls = None
# canonicalize log directory arg
if args.logdir:
args.log_dir = args.log_dir.rstrip('/') + '/'
# Every six seconds, we get a chunk of XML terminated by a newline
while 1:
try:
p = Packet(conn)
if args.log:
p.log(args.logdir)
if args.stdout:
p.log_stdout()
if args.raw:
p.log_raw(args.logdir)
if args.influx:
p.log_influx(args.logdir)
if args.mqtttopic:
p.mqtt_publish(args.mqtttopic,args.mqttserver,args.mqttport,mqtt_auth,mqtt_tls)
except (IOError, KeyboardInterrupt, SystemExit):
raise
except:
print("Error in parsing packet, ignoring.")