minor fixes
This commit is contained in:
parent
6c2f762898
commit
9f8a12c1cd
52
pi/power.py
52
pi/power.py
@ -1,21 +1,23 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from select import poll, POLLPRI, POLLIN, POLLERR
|
|
||||||
import sys
|
|
||||||
import os
|
|
||||||
import threading
|
|
||||||
import time
|
|
||||||
import queue
|
|
||||||
import requests
|
|
||||||
import configparser
|
import configparser
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
import queue
|
||||||
|
import sys
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
from select import poll, POLLERR
|
||||||
|
|
||||||
import paho.mqtt.publish as mqtt
|
import paho.mqtt.publish as mqtt
|
||||||
|
import requests
|
||||||
|
|
||||||
|
|
||||||
def trans_http():
|
def trans_http():
|
||||||
global serverconf
|
global serverconf
|
||||||
# build together url and headers
|
# build together url and headers
|
||||||
uri = ""
|
uri = ""
|
||||||
if serverconf.getboolean("ssl") == True:
|
if serverconf.getboolean("ssl"):
|
||||||
uri = "https://"
|
uri = "https://"
|
||||||
else:
|
else:
|
||||||
uri = "http://"
|
uri = "http://"
|
||||||
@ -29,7 +31,7 @@ def trans_http():
|
|||||||
queuetime = int(queuedata[0])
|
queuetime = int(queuedata[0])
|
||||||
queueval = str(queuedata[1])
|
queueval = str(queuedata[1])
|
||||||
# if more than five seconds passed since last event
|
# if more than five seconds passed since last event
|
||||||
if ((queuetime - queuelast) >= 5):
|
if (queuetime - queuelast) >= 5:
|
||||||
# put payload together
|
# put payload together
|
||||||
if serverconf.getboolean('timestamp'):
|
if serverconf.getboolean('timestamp'):
|
||||||
payload = {"val": str(queuetime) + ";" + str(queueval)}
|
payload = {"val": str(queuetime) + ";" + str(queueval)}
|
||||||
@ -38,10 +40,10 @@ def trans_http():
|
|||||||
log.debug('request {}:{}{} => {}'.format(mqttconf['host'], mqttconf['port'], mqttconf['topic'], repr(payload)))
|
log.debug('request {}:{}{} => {}'.format(mqttconf['host'], mqttconf['port'], mqttconf['topic'], repr(payload)))
|
||||||
# send to http server
|
# send to http server
|
||||||
try:
|
try:
|
||||||
if serverconf.getboolean("basicauth") == True:
|
if serverconf.getboolean("basicauth"):
|
||||||
r = requests.post(uri, data=payload, verify=False, auth=(serverconf["user"], serverconf["password"]), timeout=10, headers={'connection':'close'})
|
r = requests.post(uri, data=payload, verify=False, auth=(serverconf["user"], serverconf["password"]), timeout=10, headers={'connection': 'close'})
|
||||||
else:
|
else:
|
||||||
r = requests.post(uri, data=payload, verify=False, timeout=10, headers={'connection':'close'})
|
r = requests.post(uri, data=payload, verify=False, timeout=10, headers={'connection': 'close'})
|
||||||
except:
|
except:
|
||||||
log.error('server response: {}'.format(str(r.status_code)))
|
log.error('server response: {}'.format(str(r.status_code)))
|
||||||
queuelast = queuetime
|
queuelast = queuetime
|
||||||
@ -63,7 +65,7 @@ def trans_mqtt():
|
|||||||
queuetime = int(queuedata[0])
|
queuetime = int(queuedata[0])
|
||||||
queueval = str(queuedata[1])
|
queueval = str(queuedata[1])
|
||||||
# if more than five seconds passed since last event
|
# if more than five seconds passed since last event
|
||||||
if ((queuetime - queuelast) >= 5):
|
if (queuetime - queuelast) >= 5:
|
||||||
# put payload together
|
# put payload together
|
||||||
if mqttconf.getboolean('timestamp'):
|
if mqttconf.getboolean('timestamp'):
|
||||||
payload = str(queuetime) + ";" + str(queueval)
|
payload = str(queuetime) + ";" + str(queueval)
|
||||||
@ -88,7 +90,7 @@ def readgpio():
|
|||||||
global gpiopath
|
global gpiopath
|
||||||
global gpioconf
|
global gpioconf
|
||||||
global smconf
|
global smconf
|
||||||
# open gpio filehandle
|
# open gpio file handle
|
||||||
gpio = open(gpiopath + "value", "r")
|
gpio = open(gpiopath + "value", "r")
|
||||||
# setup polling
|
# setup polling
|
||||||
gpiopoll = poll()
|
gpiopoll = poll()
|
||||||
@ -115,9 +117,9 @@ def readgpio():
|
|||||||
if gpioval == '0':
|
if gpioval == '0':
|
||||||
now = time.time()
|
now = time.time()
|
||||||
# plausibility check
|
# plausibility check
|
||||||
if ((now-last) >= gpioconf.getfloat("mintime")):
|
if (now - last) >= gpioconf.getfloat("mintime"):
|
||||||
# calculate current power consumption
|
# calculate current power consumption
|
||||||
power = round((3600000/smconf.getint("impkwh")) / (now-last),2)
|
power = round((3600000 / smconf.getint("impkwh")) / (now - last), 2)
|
||||||
log.info('Current power consumption: {} Watt'.format(str(power)))
|
log.info('Current power consumption: {} Watt'.format(str(power)))
|
||||||
log.debug('GPIO state: {}'.format(gpioval))
|
log.debug('GPIO state: {}'.format(gpioval))
|
||||||
# put measured value on queue
|
# put measured value on queue
|
||||||
@ -129,6 +131,12 @@ def readgpio():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
# configure logging
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
log.setLevel(logging.DEBUG)
|
||||||
|
ch = logging.StreamHandler()
|
||||||
|
ch.setLevel(logging.INFO)
|
||||||
|
|
||||||
# read config
|
# read config
|
||||||
dirname, filename = os.path.split(os.path.abspath(__file__))
|
dirname, filename = os.path.split(os.path.abspath(__file__))
|
||||||
conf = configparser.ConfigParser()
|
conf = configparser.ConfigParser()
|
||||||
@ -143,11 +151,7 @@ if __name__ == "__main__":
|
|||||||
serverconf = conf['server']
|
serverconf = conf['server']
|
||||||
smconf = conf['smartmeter']
|
smconf = conf['smartmeter']
|
||||||
mqttconf = conf['mqtt']
|
mqttconf = conf['mqtt']
|
||||||
# configure logging
|
|
||||||
log = logging.getLogger(__name__)
|
|
||||||
log.setLevel(logging.DEBUG)
|
|
||||||
ch = logging.StreamHandler()
|
|
||||||
ch.setLevel(logging.INFO)
|
|
||||||
log.info('Running in path {}'.format(dirname))
|
log.info('Running in path {}'.format(dirname))
|
||||||
if serverconf["url"] == "power.example.com" and mqttconf['host'] == "power.example.com":
|
if serverconf["url"] == "power.example.com" and mqttconf['host'] == "power.example.com":
|
||||||
log.critical('Server/Broker -> url still default value')
|
log.critical('Server/Broker -> url still default value')
|
||||||
@ -175,7 +179,7 @@ if __name__ == "__main__":
|
|||||||
powerqueue = queue.Queue()
|
powerqueue = queue.Queue()
|
||||||
|
|
||||||
# disable ssl "no verification" warnings
|
# disable ssl "no verification" warnings
|
||||||
if serverconf.getboolean("sslself") == True:
|
if serverconf.getboolean("sslself"):
|
||||||
requests.packages.urllib3.disable_warnings()
|
requests.packages.urllib3.disable_warnings()
|
||||||
|
|
||||||
# initialising and starting threads
|
# initialising and starting threads
|
||||||
@ -187,7 +191,7 @@ if __name__ == "__main__":
|
|||||||
log.info('method of transfer: http')
|
log.info('method of transfer: http')
|
||||||
th2 = threading.Thread(target=trans_http)
|
th2 = threading.Thread(target=trans_http)
|
||||||
else:
|
else:
|
||||||
log.error('transfer option {} is invalid. Exiting!'.format(transfer['transfer']))
|
log.error('transfer option {} is invalid. Exiting!'.format(transconf['transport']))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
th1.setDaemon(True)
|
th1.setDaemon(True)
|
||||||
@ -195,6 +199,6 @@ if __name__ == "__main__":
|
|||||||
th1.start()
|
th1.start()
|
||||||
th2.start()
|
th2.start()
|
||||||
|
|
||||||
#busy loop
|
# busy loop
|
||||||
while True:
|
while True:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user