-
Notifications
You must be signed in to change notification settings - Fork 21
/
pyHPSU.py
executable file
·152 lines (135 loc) · 5.69 KB
/
pyHPSU.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
151
152
#!/usr/bin/env python3
#
# -*- coding: utf-8 -*-
# v 0.0.4 by Vanni Brutto (Zanac)
#todo:
#
# utilizzare la formattazione del locale corrente (se ho settato Italy devo vedere date giuste, punti/virgole giusti)
# monitor mode (sniff)
# tcp_con = serial.serial_for_url('socket://<my_ip>:<my_port>')
#
#Lo script di lettura e pubblicazione deve essere facilmente schedulabile in modo controllato:
#- frequenza di aggiornamento (l'ideale sarebbe poterla personalizzare per singola variabile ma lasciamo stare)
import serial
import sys
sys.path.append('/usr/share/pyHPSU/HPSU');
sys.path.append('/usr/share/pyHPSU/plugins');
import getopt
import time
import locale
import importlib
import logging
from HPSU import HPSU
SocketPort = 7060
def main(argv):
cmd = []
port = None
driver = None
verbose = "1"
help = False
output_type = "JSON"
cloud_plugin = None
lg_code = None
languages = ["EN", "IT", "DE", "NL"]
logger = None
try:
opts, args = getopt.getopt(argv,"hc:p:d:v:o:u:l:g:", ["help", "cmd=", "port=", "driver=", "verbose=", "output_type=", "upload=", "language=", "log="])
except getopt.GetoptError:
print('pyHPSU.py -d DRIVER -c COMMAND')
print(' ')
print(' -d --driver driver name: [ELM327, PYCAN, EMU, HPSUD]')
print(' -p --port port (eg COM or /dev/tty*, only for ELM327 driver)')
print(' -o --output_type output type: [JSON, CSV, CLOUD] default JSON')
print(' -c --cmd command: [see commands domain]')
print(' -v --verbose verbosity: [1, 2] default 1')
print(' -u --upload upload on cloud: [_PLUGIN_]')
print(' -l --language set the language to use [%s]' % " ".join(languages))
print(' -g --log set the log to file [_filename]')
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
help = True
elif opt in ("-d", "--driver"):
driver = arg.upper()
elif opt in ("-p", "--port"):
port = arg
elif opt in ("-c", "--cmd"):
cmd.append(arg)
elif opt in ("-v", "--verbose"):
verbose = arg
elif opt in ("-o", "--output_type"):
output_type = arg.upper()
if output_type not in ["JSON", "CSV", "CLOUD"]:
print("Error, please specify a correct output_type [JSON, CSV, CLOUD]")
sys.exit(9)
elif opt in ("-u", "--upload"):
cloud_plugin = arg.upper()
if cloud_plugin not in ["EMONCMS"]:
print("Error, please specify a correct plugin")
sys.exit(9)
elif opt in ("-l", "--language"):
lg_code = arg.upper()
if lg_code not in languages:
print("Error, please specify a correct language [%s]" % " ".join(languages))
sys.exit(9)
elif opt in ("-g", "--log"):
logger = logging.getLogger('domon')
hdlr = logging.FileHandler(arg)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.ERROR)
if verbose == "2":
locale.setlocale(locale.LC_ALL, '')
hpsu = HPSU(driver=driver, logger=logger, port=port, cmd=cmd, lg_code=lg_code)
if help:
if len(cmd) == 0:
print("List available commands:")
print("%12s - %-10s" % ('COMMAND', 'LABEL'))
print("%12s---%-10s" % ('------------', '----------'))
for cmd in hpsu.listCommands:
print("%12s - %-10s" % (cmd['name'], cmd['label']))
else:
print("%12s - %-10s - %s" % ('COMMAND', 'LABEL', 'DESCRIPTION'))
print("%12s---%-10s---%s" % ('------------', '----------', '---------------------------------------------------'))
for c in hpsu.commands:
print("%12s - %-10s - %s" % (c['name'], c['label'], c['desc']))
sys.exit(0)
if not driver:
print("Error, please specify driver [ELM327 or PYCAN, EMU, HPSUD]")
sys.exit(9)
arrResponse = []
for c in hpsu.commands:
setValue = None
for i in cmd:
if ":" in i and c["name"] == i.split(":")[0]:
setValue = i.split(":")[1]
i = 0
while i <= 3:
rc = hpsu.sendCommand(c, setValue)
if rc != "KO":
i = 4
if not setValue:
response = hpsu.parseCommand(cmd=c, response=rc, verbose=verbose)
resp = hpsu.umConversion(cmd=c, response=response, verbose=verbose)
arrResponse.append({"name":c["name"], "resp":resp, "timestamp":response["timestamp"]})
else:
i += 1
time.sleep(2.0)
hpsu.printd('warning', 'retry %s command %s' % (i, c["name"]))
if i == 4:
hpsu.printd('error', 'command %s failed' % (c["name"]))
if output_type == "JSON":
print(arrResponse)
elif output_type == "CSV":
for r in arrResponse:
print("%s\t%s\t%s" % (r["timestamp"], r["name"], r["resp"]))
elif output_type == "CLOUD":
if not cloud_plugin:
print ("Error, please specify a cloud_plugin")
sys.exit(9)
module = importlib.import_module("cloud")
cloud = module.Cloud(plugin=cloud_plugin, hpsu=hpsu, logger=logger)
cloud.pushValues(vars=arrResponse)
if __name__ == "__main__":
main(sys.argv[1:])