[F] using logging now instedt of print - closes #6
This commit is contained in:
0
.gitignore
vendored
Normal file
0
.gitignore
vendored
Normal file
40
pi/power.py
40
pi/power.py
@ -8,6 +8,7 @@ import time
|
|||||||
import queue
|
import queue
|
||||||
import requests
|
import requests
|
||||||
import configparser
|
import configparser
|
||||||
|
import logging
|
||||||
|
|
||||||
def trans():
|
def trans():
|
||||||
global serverconf
|
global serverconf
|
||||||
@ -18,8 +19,7 @@ def trans():
|
|||||||
else:
|
else:
|
||||||
uri = "http://"
|
uri = "http://"
|
||||||
uri = uri + serverconf["url"] + "/get.php"
|
uri = uri + serverconf["url"] + "/get.php"
|
||||||
if sys.stdout.isatty():
|
log.info('Remote URI: {}'.format(uri))
|
||||||
print("Remote URI: " + uri)
|
|
||||||
queuelast = time.time()
|
queuelast = time.time()
|
||||||
while True:
|
while True:
|
||||||
# get value from queue (blocking)
|
# get value from queue (blocking)
|
||||||
@ -31,8 +31,7 @@ def trans():
|
|||||||
if ((queuetime - queuelast) >= 5):
|
if ((queuetime - queuelast) >= 5):
|
||||||
# put payload together
|
# put payload together
|
||||||
payload = {"val": str(queuetime) + ";" + str(queueval)}
|
payload = {"val": str(queuetime) + ";" + str(queueval)}
|
||||||
if sys.stdout.isatty():
|
log.debug('request {} => {}'.format(uri, repr(payload)))
|
||||||
print("request " + uri + " => "+repr(payload))
|
|
||||||
# send to webserver via http post
|
# send to webserver via http post
|
||||||
#
|
#
|
||||||
# Um den Krempel hier muss noch mal eine While Schleife rum, damit die Daten nicht verloren gehen.
|
# Um den Krempel hier muss noch mal eine While Schleife rum, damit die Daten nicht verloren gehen.
|
||||||
@ -43,17 +42,13 @@ def trans():
|
|||||||
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:
|
||||||
print("Exception raised!")
|
log.error('server response: {}'.format(str(r.status_code)))
|
||||||
else:
|
|
||||||
if sys.stdout.isatty():
|
|
||||||
print("server response: " + str(r.status_code))
|
|
||||||
queuelast = queuetime
|
queuelast = queuetime
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# drop data if less than five seconds passed since last event
|
# drop data if less than five seconds passed since last event
|
||||||
del queuedata
|
del queuedata
|
||||||
if sys.stdout.isatty():
|
log.info('dropped queued element')
|
||||||
print("Drop queued element")
|
|
||||||
|
|
||||||
def readgpio():
|
def readgpio():
|
||||||
global gpiopath
|
global gpiopath
|
||||||
@ -66,21 +61,18 @@ def readgpio():
|
|||||||
gpiopoll.register(gpio, POLLERR)
|
gpiopoll.register(gpio, POLLERR)
|
||||||
|
|
||||||
# wait for two interrupts to have a "clean" starting point
|
# wait for two interrupts to have a "clean" starting point
|
||||||
if sys.stdout.isatty():
|
log.info('waiting for 2 interrupts to get a clean start')
|
||||||
print("wait for 2 interrupts...")
|
|
||||||
gpioevent = gpiopoll.poll()
|
gpioevent = gpiopoll.poll()
|
||||||
gpio.read()
|
gpio.read()
|
||||||
gpio.seek(0)
|
gpio.seek(0)
|
||||||
if sys.stdout.isatty():
|
log.info('waiting for 1 interrupt to get a clean start...')
|
||||||
print("wait for 1 interrupt....")
|
|
||||||
gpioevent = gpiopoll.poll()
|
gpioevent = gpiopoll.poll()
|
||||||
last = time.time()
|
last = time.time()
|
||||||
gpio.read()
|
gpio.read()
|
||||||
gpio.seek(0)
|
gpio.seek(0)
|
||||||
|
|
||||||
# start readgpio mainloop
|
# start readgpio mainloop
|
||||||
if sys.stdout.isatty():
|
log.info('start readgpio mainloop')
|
||||||
print("start readgpio mainloop")
|
|
||||||
while True:
|
while True:
|
||||||
gpioevent = gpiopoll.poll()
|
gpioevent = gpiopoll.poll()
|
||||||
gpioval = gpio.read(1)
|
gpioval = gpio.read(1)
|
||||||
@ -92,16 +84,14 @@ def readgpio():
|
|||||||
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)
|
||||||
if sys.stdout.isatty():
|
log.info('Current power consumption: {} Watt'.format(str(power)))
|
||||||
print("Current power consumption: " + str(power) + " Watt")
|
log.debug('GPIO state: {}'.format(gpioval))
|
||||||
print("GPIO state: " + gpioval)
|
|
||||||
# put measured value on queue
|
# put measured value on queue
|
||||||
powerqueue.put([now, power])
|
powerqueue.put([now, power])
|
||||||
last = now
|
last = now
|
||||||
# error if a tty is connected and a raising edge was triggered
|
# error if a tty is connected and a raising edge was triggered
|
||||||
else:
|
else:
|
||||||
if sys.stdout.isatty():
|
log.debug('Not a falling edge, ignoring')
|
||||||
print("ERROR: not a falling edge! Ignoring interrupt")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@ -112,8 +102,10 @@ if __name__ == "__main__":
|
|||||||
gpioconf = conf['gpio']
|
gpioconf = conf['gpio']
|
||||||
serverconf = conf['server']
|
serverconf = conf['server']
|
||||||
smconf = conf['smartmeter']
|
smconf = conf['smartmeter']
|
||||||
|
# configure logging
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
if serverconf["url"] == "power.example.com":
|
if serverconf["url"] == "power.example.com":
|
||||||
print("FATAL: configuration not adapted! Aborting!")
|
log.critical('Server -> url still default value')
|
||||||
exit(1)
|
exit(1)
|
||||||
gpiopath = "/sys/class/gpio/gpio" + gpioconf["port"] + "/"
|
gpiopath = "/sys/class/gpio/gpio" + gpioconf["port"] + "/"
|
||||||
# initialise gpio interfaces
|
# initialise gpio interfaces
|
||||||
@ -132,9 +124,7 @@ if __name__ == "__main__":
|
|||||||
gpiotype.write("falling")
|
gpiotype.write("falling")
|
||||||
gpiotype.close()
|
gpiotype.close()
|
||||||
except:
|
except:
|
||||||
# if not able to initialise gpios: abort with error
|
log.critical('can not initialize gpio interface. aborting.')
|
||||||
sys.stderr.write("can't initialize gpio interface\n")
|
|
||||||
sys.stderr.flush()
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
# defining queue for measured values
|
# defining queue for measured values
|
||||||
powerqueue = queue.Queue()
|
powerqueue = queue.Queue()
|
||||||
|
Reference in New Issue
Block a user