🔨 Code
This commit is contained in:
parent
eaf5083442
commit
e15341f181
23
README.md
23
README.md
@ -1,2 +1,25 @@
|
|||||||
# screen_status_to_mqtt
|
# 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
26
main.py
@ -1,6 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
import socket
|
import socket
|
||||||
import sys
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
import servicemanager
|
import servicemanager
|
||||||
@ -8,20 +9,22 @@ import win32event
|
|||||||
import win32service
|
import win32service
|
||||||
import win32serviceutil
|
import win32serviceutil
|
||||||
|
|
||||||
|
import app_path
|
||||||
|
from mqtt_client import MqttClient
|
||||||
from win32_notification_receiver import Win32NotificationReceiver
|
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(
|
logging.basicConfig(
|
||||||
level=logging.DEBUG,
|
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=[
|
handlers=[
|
||||||
logging.FileHandler(logPath + "debug.log"),
|
logging.FileHandler(app_path.log_path),
|
||||||
logging.StreamHandler()
|
logging.StreamHandler()
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger("Main")
|
||||||
|
|
||||||
|
|
||||||
class ScreenStatusToMqttService(win32serviceutil.ServiceFramework):
|
class ScreenStatusToMqttService(win32serviceutil.ServiceFramework):
|
||||||
@ -30,6 +33,7 @@ class ScreenStatusToMqttService(win32serviceutil.ServiceFramework):
|
|||||||
_svc_description_ = "Send your screen status to MQTT Broker"
|
_svc_description_ = "Send your screen status to MQTT Broker"
|
||||||
|
|
||||||
win32_notification_receiver: Win32NotificationReceiver = None
|
win32_notification_receiver: Win32NotificationReceiver = None
|
||||||
|
mqtt_client: MqttClient = None
|
||||||
running_flag = False
|
running_flag = False
|
||||||
|
|
||||||
def __init__(self, args):
|
def __init__(self, args):
|
||||||
@ -39,9 +43,10 @@ class ScreenStatusToMqttService(win32serviceutil.ServiceFramework):
|
|||||||
|
|
||||||
def SvcStop(self):
|
def SvcStop(self):
|
||||||
logger.info("Stopping Service...")
|
logger.info("Stopping Service...")
|
||||||
self.running_flag = False
|
|
||||||
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
|
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
|
||||||
|
self.mqtt_client.disconnect()
|
||||||
win32event.SetEvent(self.hWaitStop)
|
win32event.SetEvent(self.hWaitStop)
|
||||||
|
self.running_flag = False
|
||||||
|
|
||||||
def SvcDoRun(self):
|
def SvcDoRun(self):
|
||||||
self.running_flag = True
|
self.running_flag = True
|
||||||
@ -62,7 +67,16 @@ class ScreenStatusToMqttService(win32serviceutil.ServiceFramework):
|
|||||||
logger.warning(control)
|
logger.warning(control)
|
||||||
|
|
||||||
def main(self):
|
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:
|
while self.running_flag:
|
||||||
sleep(1)
|
sleep(1)
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
pywin32=303
|
pywin32==303
|
||||||
comtypes=1.1.11
|
comtypes==1.1.11
|
||||||
pyinstaller=4.10
|
pyinstaller==4.10
|
||||||
|
paho-mqtt==1.6.1
|
||||||
|
PyYaml==6.0
|
@ -9,18 +9,21 @@ from ctypes import POINTER, windll, Structure, cast, CFUNCTYPE, c_int, c_uint, c
|
|||||||
from comtypes import GUID
|
from comtypes import GUID
|
||||||
from ctypes.wintypes import HANDLE, DWORD
|
from ctypes.wintypes import HANDLE, DWORD
|
||||||
|
|
||||||
|
from mqtt_client import MqttClient
|
||||||
|
|
||||||
PBT_POWERSETTINGCHANGE = 0x8013 # 32787
|
PBT_POWERSETTINGCHANGE = 0x8013 # 32787
|
||||||
GUID_CONSOLE_DISPLAY_STATE = "{6FE69556-704A-47A0-8F24-C28D936FDA47}"
|
GUID_CONSOLE_DISPLAY_STATE = "{6FE69556-704A-47A0-8F24-C28D936FDA47}"
|
||||||
GUID_MONITOR_POWER_ON = "{02731015-4510-4526-99E6-E5A17EBD1AEA}"
|
GUID_MONITOR_POWER_ON = "{02731015-4510-4526-99E6-E5A17EBD1AEA}"
|
||||||
|
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger("Win32Notif")
|
||||||
|
|
||||||
|
|
||||||
class Win32NotificationReceiver():
|
class Win32NotificationReceiver():
|
||||||
running_flag = False
|
running_flag = False
|
||||||
|
|
||||||
def __init__(self, recipient):
|
def __init__(self, recipient, mqtt_client: MqttClient):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self.mqtt_client = mqtt_client
|
||||||
self.recipient = recipient
|
self.recipient = recipient
|
||||||
self.__prepare_win32gui_message()
|
self.__prepare_win32gui_message()
|
||||||
|
|
||||||
@ -55,14 +58,19 @@ class Win32NotificationReceiver():
|
|||||||
if power_setting == GUID_CONSOLE_DISPLAY_STATE:
|
if power_setting == GUID_CONSOLE_DISPLAY_STATE:
|
||||||
if data[0] == 0:
|
if data[0] == 0:
|
||||||
logger.debug("Display off")
|
logger.debug("Display off")
|
||||||
|
self.mqtt_client.publish_status("display", "Off")
|
||||||
if data[0] == 1:
|
if data[0] == 1:
|
||||||
logger.debug("Display on")
|
logger.debug("Display on")
|
||||||
|
self.mqtt_client.publish_status("display", "On")
|
||||||
if data[0] == 2:
|
if data[0] == 2:
|
||||||
logger.debug("Display dimmed")
|
logger.debug("Display dimmed")
|
||||||
|
self.mqtt_client.publish_status("display", "Dimmed")
|
||||||
elif power_setting == GUID_MONITOR_POWER_ON:
|
elif power_setting == GUID_MONITOR_POWER_ON:
|
||||||
if data[0] == 0:
|
if data[0] == 0:
|
||||||
logger.debug("Monitor off")
|
logger.debug("Monitor off")
|
||||||
|
self.mqtt_client.publish_status("monitor", "OFF")
|
||||||
if data[0] == 1:
|
if data[0] == 1:
|
||||||
logger.debug("Monitor on")
|
logger.debug("Monitor on")
|
||||||
|
self.mqtt_client.publish_status("monitor", "ON")
|
||||||
else:
|
else:
|
||||||
logger.warning("unknown GUID")
|
logger.warning("unknown GUID")
|
||||||
|
Loading…
Reference in New Issue
Block a user