screen_status_to_mqtt/main.py

91 lines
2.8 KiB
Python
Raw Normal View History

2022-03-11 18:56:49 +01:00
import logging
import socket
import sys
2022-03-12 16:30:14 +01:00
from pathlib import Path
2022-03-11 18:56:49 +01:00
from time import sleep
import servicemanager
import win32event
import win32service
import win32serviceutil
2022-03-12 16:30:14 +01:00
import app_path
from mqtt_client import MqttClient
2022-03-11 18:56:49 +01:00
from win32_notification_receiver import Win32NotificationReceiver
2022-03-12 16:30:14 +01:00
Path(app_path.base_pash).mkdir(parents=True, exist_ok=True)
2022-03-11 18:56:49 +01:00
logging.basicConfig(
level=logging.DEBUG,
2022-03-12 16:30:14 +01:00
format="%(asctime)s [%(name)-12.12s] [%(levelname)-5.5s] %(message)s",
2022-03-11 18:56:49 +01:00
handlers=[
2022-03-12 16:30:14 +01:00
logging.FileHandler(app_path.log_path),
2022-03-11 18:56:49 +01:00
logging.StreamHandler()
]
)
2022-03-12 16:30:14 +01:00
logger = logging.getLogger("Main")
2022-03-11 18:56:49 +01:00
class ScreenStatusToMqttService(win32serviceutil.ServiceFramework):
_svc_name_ = "ScreenStatusToMqtt"
_svc_display_name_ = "Screen Status To MQTT"
_svc_description_ = "Send your screen status to MQTT Broker"
win32_notification_receiver: Win32NotificationReceiver = None
2022-03-12 16:30:14 +01:00
mqtt_client: MqttClient = None
2022-03-11 18:56:49 +01:00
running_flag = False
def __init__(self, args):
super().__init__(args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
def SvcStop(self):
logger.info("Stopping Service...")
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
2022-03-12 16:30:14 +01:00
self.mqtt_client.disconnect()
2022-03-11 18:56:49 +01:00
win32event.SetEvent(self.hWaitStop)
2022-03-12 16:30:14 +01:00
self.running_flag = False
2022-03-11 18:56:49 +01:00
def SvcDoRun(self):
self.running_flag = True
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
if self.ssh is None:
logger.critical("Can't be run in debug mode !")
sys.exit(1)
else:
self.main()
def SvcOtherEx(self, control, event_type, data):
if control == win32service.SERVICE_CONTROL_POWEREVENT:
self.win32_notification_receiver.event_handler(event_type, data)
else:
logger.warning("Unsupported control: ")
logger.warning(control)
def main(self):
2022-03-12 16:30:14 +01:00
logger.info("********************** Starting Service ********************** ")
try:
self.mqtt_client = MqttClient("ScreenStatusMQTT")
self.mqtt_client.init_connection()
self.win32_notification_receiver = Win32NotificationReceiver(self.ssh, self.mqtt_client)
except Exception:
logger.exception("Error during init:")
sys.exit(1)
2022-03-11 18:56:49 +01:00
while self.running_flag:
sleep(1)
if __name__ == '__main__':
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(ScreenStatusToMqttService)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(ScreenStatusToMqttService)