add schedular for regular tasks

This commit is contained in:
Stefan Allius
2023-12-31 16:47:53 +01:00
parent 213bb28466
commit 97079974f1
3 changed files with 31 additions and 1 deletions

25
app/src/scheduler.py Normal file
View File

@@ -0,0 +1,25 @@
import logging
from mqtt import Mqtt
from aiocron import crontab
class Schedule:
mqtt = None
@classmethod
def start(cls):
logging.info("Scheduler init")
cls.mqtt = Mqtt(None)
# json.dumps(i.db['total']) == json.dumps({'Daily_Generation': 0.0})
# json.dumps(i.db['input']) == json.dumps({"pv1": {"Daily_Generation": 0.0}, "pv2": {"Daily_Generation": 0.0}, "pv3": {"Daily_Generation": 0.0}, "pv4": {"Daily_Generation": 0.0}}) # noqa: E501
crontab('0 0 * * *', func=cls.atmidnight, start=True)
async def atmidnight():
logging.info("Scheduler is working")
# db = self.db.db
# if key in db and self.new_data[key]:
# data_json = json.dumps(db[key])
# node_id = self.node_id
# logger_mqtt.debug(f'{key}: {data_json}')
# await cls.mqtt.publish(f'{self.entity_prfx}{node_id}{key}', data_json) # noqa: E501

View File

@@ -6,6 +6,7 @@ import os
from logging import config # noqa F401
from async_stream import AsyncStream
from inverter import Inverter
from scheduler import Schedule
from config import Config
@@ -64,6 +65,7 @@ if __name__ == "__main__":
logging.getLogger('msg').setLevel(log_level)
logging.getLogger('conn').setLevel(log_level)
logging.getLogger('data').setLevel(log_level)
# logging.getLogger('mqtt').setLevel(log_level)
# read config file
Config.read()
@@ -72,6 +74,8 @@ if __name__ == "__main__":
asyncio.set_event_loop(loop)
Inverter.class_init()
Schedule.start()
#
# Register some UNIX Signal handler for a gracefully server shutdown
# on Docker restart and stop