🔨 Code

This commit is contained in:
SebClem 2022-03-12 16:30:14 +01:00
parent eaf5083442
commit e15341f181
5 changed files with 58 additions and 11 deletions

View File

@ -1,2 +1,25 @@
# screen_status_to_mqtt
### Build:
```powershell
pyinstaller.exe -F -n ScreenStatusToMQTT --hidden-import=win32timezone --paths .\venv\Lib\site-packages\ .\main.py
```
### Install
Copy `build/ScreenStatusToMQTT.exe` to `C:\Program Files\ScreenStatusToMQTT`
In powershell as admin:
```powershell
# Install autostart
.\ScreenStatusToMQTT.exe --startup auto install
# Start it
.\ScreenStatusToMQTT.exe start
# Stop it
.\ScreenStatusToMQTT.exe stop
# Uninstall
.\ScreenStatusToMQTT.exe remove
```

26
main.py
View File

@ -1,6 +1,7 @@
import logging
import socket
import sys
from pathlib import Path
from time import sleep
import servicemanager
@ -8,20 +9,22 @@ import win32event
import win32service
import win32serviceutil
import app_path
from mqtt_client import MqttClient
from win32_notification_receiver import Win32NotificationReceiver
logPath = 'C:/Users/seb65/Documents/Gitea/screen_status_to_mqtt/'
Path(app_path.base_pash).mkdir(parents=True, exist_ok=True)
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s",
format="%(asctime)s [%(name)-12.12s] [%(levelname)-5.5s] %(message)s",
handlers=[
logging.FileHandler(logPath + "debug.log"),
logging.FileHandler(app_path.log_path),
logging.StreamHandler()
]
)
logger = logging.getLogger()
logger = logging.getLogger("Main")
class ScreenStatusToMqttService(win32serviceutil.ServiceFramework):
@ -30,6 +33,7 @@ class ScreenStatusToMqttService(win32serviceutil.ServiceFramework):
_svc_description_ = "Send your screen status to MQTT Broker"
win32_notification_receiver: Win32NotificationReceiver = None
mqtt_client: MqttClient = None
running_flag = False
def __init__(self, args):
@ -39,9 +43,10 @@ class ScreenStatusToMqttService(win32serviceutil.ServiceFramework):
def SvcStop(self):
logger.info("Stopping Service...")
self.running_flag = False
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
self.mqtt_client.disconnect()
win32event.SetEvent(self.hWaitStop)
self.running_flag = False
def SvcDoRun(self):
self.running_flag = True
@ -62,7 +67,16 @@ class ScreenStatusToMqttService(win32serviceutil.ServiceFramework):
logger.warning(control)
def main(self):
self.win32_notification_receiver = Win32NotificationReceiver(self.ssh)
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)
while self.running_flag:
sleep(1)

View File

@ -1,3 +1,5 @@
pywin32=303
comtypes=1.1.11
pyinstaller=4.10
pywin32==303
comtypes==1.1.11
pyinstaller==4.10
paho-mqtt==1.6.1
PyYaml==6.0

View File

View File

@ -9,18 +9,21 @@ from ctypes import POINTER, windll, Structure, cast, CFUNCTYPE, c_int, c_uint, c
from comtypes import GUID
from ctypes.wintypes import HANDLE, DWORD
from mqtt_client import MqttClient
PBT_POWERSETTINGCHANGE = 0x8013 # 32787
GUID_CONSOLE_DISPLAY_STATE = "{6FE69556-704A-47A0-8F24-C28D936FDA47}"
GUID_MONITOR_POWER_ON = "{02731015-4510-4526-99E6-E5A17EBD1AEA}"
logger = logging.getLogger()
logger = logging.getLogger("Win32Notif")
class Win32NotificationReceiver():
running_flag = False
def __init__(self, recipient):
def __init__(self, recipient, mqtt_client: MqttClient):
super().__init__()
self.mqtt_client = mqtt_client
self.recipient = recipient
self.__prepare_win32gui_message()
@ -55,14 +58,19 @@ class Win32NotificationReceiver():
if power_setting == GUID_CONSOLE_DISPLAY_STATE:
if data[0] == 0:
logger.debug("Display off")
self.mqtt_client.publish_status("display", "Off")
if data[0] == 1:
logger.debug("Display on")
self.mqtt_client.publish_status("display", "On")
if data[0] == 2:
logger.debug("Display dimmed")
self.mqtt_client.publish_status("display", "Dimmed")
elif power_setting == GUID_MONITOR_POWER_ON:
if data[0] == 0:
logger.debug("Monitor off")
self.mqtt_client.publish_status("monitor", "OFF")
if data[0] == 1:
logger.debug("Monitor on")
self.mqtt_client.publish_status("monitor", "ON")
else:
logger.warning("unknown GUID")