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