🎉 Initial commit

This commit is contained in:
SebClem 2022-03-11 18:56:49 +01:00
parent 2cbd8f0494
commit eaf5083442
10 changed files with 182 additions and 0 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

4
.idea/misc.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/screen_status_to_mqtt.iml" filepath="$PROJECT_DIR$/.idea/screen_status_to_mqtt.iml" />
</modules>
</component>
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

76
main.py Normal file
View File

@ -0,0 +1,76 @@
import logging
import socket
import sys
from time import sleep
import servicemanager
import win32event
import win32service
import win32serviceutil
from win32_notification_receiver import Win32NotificationReceiver
logPath = 'C:/Users/seb65/Documents/Gitea/screen_status_to_mqtt/'
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s",
handlers=[
logging.FileHandler(logPath + "debug.log"),
logging.StreamHandler()
]
)
logger = logging.getLogger()
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
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.running_flag = False
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
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):
self.win32_notification_receiver = Win32NotificationReceiver(self.ssh)
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)

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
pywin32=303
comtypes=1.1.11
pyinstaller=4.10

0
sm_win_service.py Normal file
View File

View File

@ -0,0 +1,68 @@
import threading
import win32con
import win32api
import win32gui
import time
import logging
from ctypes import POINTER, windll, Structure, cast, CFUNCTYPE, c_int, c_uint, c_void_p, c_bool
from comtypes import GUID
from ctypes.wintypes import HANDLE, DWORD
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()
class Win32NotificationReceiver():
running_flag = False
def __init__(self, recipient):
super().__init__()
self.recipient = recipient
self.__prepare_win32gui_message()
def __prepare_win32gui_message(self):
guids_info = {
"GUID_MONITOR_POWER_ON": GUID_MONITOR_POWER_ON,
"GUID_CONSOLE_DISPLAY_STATE": GUID_CONSOLE_DISPLAY_STATE,
}
logger.info("----")
for name, guid_info in guids_info.items():
logger.info(self.recipient)
result = windll.user32.RegisterPowerSettingNotification(HANDLE(self.recipient), GUID(guid_info), DWORD(1))
logger.info(f'registering {name}')
logger.info(f"result: {hex(result)}")
logger.info(f"lastError: {win32api.GetLastError()}")
logger.info("----")
def event_handler(self, event_type, raw_data):
if event_type == PBT_POWERSETTINGCHANGE:
logger.debug("Power setting changed...")
logger.debug("raw_data:")
logger.debug(raw_data)
power_setting = str(raw_data[0])
logger.debug("Power Setting: ")
logger.debug(power_setting)
data = list(raw_data[1])
logger.debug("Data: ")
logger.debug(data)
if power_setting == GUID_CONSOLE_DISPLAY_STATE:
if data[0] == 0:
logger.debug("Display off")
if data[0] == 1:
logger.debug("Display on")
if data[0] == 2:
logger.debug("Display dimmed")
elif power_setting == GUID_MONITOR_POWER_ON:
if data[0] == 0:
logger.debug("Monitor off")
if data[0] == 1:
logger.debug("Monitor on")
else:
logger.warning("unknown GUID")