feat: adding timeout
This commit is contained in:
parent
63e8cd271e
commit
18ab06c16c
1 changed files with 33 additions and 7 deletions
40
pulse.py
40
pulse.py
|
@ -1,6 +1,8 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import os
|
import os
|
||||||
|
import signal
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
import tibber
|
import tibber
|
||||||
from influxdb_client import InfluxDBClient
|
from influxdb_client import InfluxDBClient
|
||||||
|
@ -23,12 +25,18 @@ ch.setFormatter(formatter)
|
||||||
logger.addHandler(ch)
|
logger.addHandler(ch)
|
||||||
logger.setLevel(logging.INFO)
|
logger.setLevel(logging.INFO)
|
||||||
|
|
||||||
__version__ = "v0.3.0"
|
__version__ = "v0.3.1"
|
||||||
logger.info(__version__)
|
logger.info(__version__)
|
||||||
|
|
||||||
|
|
||||||
|
logger.info("connecting to db...")
|
||||||
client = InfluxDBClient(url=URL, token=TOKEN, org=ORG)
|
client = InfluxDBClient(url=URL, token=TOKEN, org=ORG)
|
||||||
write_api = client.write_api(write_options=SYNCHRONOUS)
|
write_api = client.write_api(write_options=SYNCHRONOUS)
|
||||||
query_api = client.query_api()
|
query_api = client.query_api()
|
||||||
|
logger.info("connecting to tibber...")
|
||||||
|
account = tibber.Account(TIBBERTOKEN)
|
||||||
|
home = account.homes[0]
|
||||||
|
logger.info("starting subscription...")
|
||||||
|
|
||||||
def _incoming(data):
|
def _incoming(data):
|
||||||
try:
|
try:
|
||||||
|
@ -39,14 +47,32 @@ def _incoming(data):
|
||||||
logger.exception("Error in _incoming():")
|
logger.exception("Error in _incoming():")
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
account = tibber.Account(TIBBERTOKEN)
|
def timeout_handler(signum, frame):
|
||||||
home = account.homes[0]
|
raise TypeError("Timeout occurred")
|
||||||
|
|
||||||
@home.event("live_measurement")
|
@home.event("live_measurement")
|
||||||
async def show_current_power(data):
|
async def show_current_power(data):
|
||||||
|
signal.alarm(5)
|
||||||
_incoming(data)
|
_incoming(data)
|
||||||
|
|
||||||
home.start_live_feed(user_agent="UserAgent/0.0.1")
|
def stop(home):
|
||||||
logger.info("Livefeed started")
|
return False
|
||||||
while home.live_feed.running:
|
|
||||||
event = home.event("live_measurement")
|
try:
|
||||||
|
signal.signal(signal.SIGALRM, timeout_handler)
|
||||||
|
signal.alarm(8)
|
||||||
|
home.start_live_feed(user_agent="pulse.py/0.3.1",exit_condition=stop(home),retries=1,retry_interval=3.0)
|
||||||
|
except TypeError:
|
||||||
|
logger.exception("Timeout occurred while executing start_live_feed()")
|
||||||
|
client.close()
|
||||||
|
exit(1)
|
||||||
|
except Exception:
|
||||||
|
logger.exception("Error in start_live_feed()")
|
||||||
|
client.close()
|
||||||
|
exit(41)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue