At the end of the year, the tower-type server / laptop of my house broke all at once, and I was working hard to restore the messy environment, but I couldn't handle anything new. I made a notification app for.
Last year, I connected a USB camera and an infrared LED to a Raspberry Pi and built a petite electronic work environment that feels like operating a TV with voice recognition, so I made a Windows application with the feeling of adding it to this environment.
The data flow looks like the following.
** ⇒ The message sent in 2 was also received on the laptop computer side, and the message was popped up. ** **
The pop-up looks like this using Growl. Growl Easy and easy to use! !!
Raspberry PI
Windows10
By the way, I was a little addicted to installing and using pywin32. Thank you for the information of the pioneers. How to write Windows service in python Short control for registering your own Python script in Windows Server as a service (1) Pywin32 did not work with Python3.4- Python, pywin32 cannot start Windows service
I also referred to here for Growl. (No password required, is it an environmental difference?) Growl notification on Windows
Paste the whole sauce. It's working, but I'm sorry for the various suitability. When connecting to RabbitMQ with pika, I used to rely on Blocking Connection, so It was my first time to use a generator to turn it.
win_notification.py
import logging
import logging.handlers
import pika
import win32serviceutil
import win32service
import win32event
import servicemanager
from gntp.notifier import GrowlNotifier
#log
LOG_FILE_PATH = 'C:\\tmp\win_notification_consumer.log'
LOG_BK_COUNT = 2
LOG_LEVEL = logging.INFO
# MQ
MQ_IP = "x.x.x.x"
MQ_QUEUE = "Queue2"
#notification
GROW_IP = "127.0.0.1"
GROW_PASS = None
GROW_APP_NAME = "amqp notif"
GROW_NOTIFS = ["Message"]
GROW_NOTE_TYPE = "Message"
GROW_TITLE = "Get Message"
#Windows service
WIN_SRV_NAME = "WinNotificationConsumer"
WIN_DISP_NAME = "WinNotificationConsumer"
class WinNotificationConsumer(win32serviceutil.ServiceFramework):
# Required Attributes:
_svc_name_ = WIN_SRV_NAME
_svc_display_name_ = WIN_DISP_NAME
def __init__(self, args):
#Log initialization
self.logger = logging.getLogger()
self.logger.setLevel(LOG_LEVEL)
formatter = logging.Formatter(
'%(asctime)s - %(levelname)s - %(message)s')
handler = logging.handlers.TimedRotatingFileHandler(
filename=LOG_FILE_PATH,
when='D',
backupCount=LOG_BK_COUNT
)
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
self.logger.addHandler(handler)
self.logger.debug('init log end.')
#Initialize around Win32 API
win32serviceutil.ServiceFramework.__init__(self, args)
self.stop_event = win32event.CreateEvent(None, 0, 0, None)
self.logger.debug('init win32 end.')
#Notification initialization
self.notifier = GrowlNotifier(
applicationName=GROW_APP_NAME,
notifications=GROW_NOTIFS,
hostname=GROW_IP,
password=GROW_PASS)
self.notifier.register()
self.logger.debug('init notif end.')
def SvcDoRun(self):
# service start
self.logger.info('service starting...')
#Event log output
servicemanager.LogMsg(
servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, 'test')
)
self.consuming_and_notificate()
def consuming_and_notificate(self):
self.logger.debug('consuming and notificate starting...')
#RabbitMQ connection settings
self.connection = pika.BlockingConnection(
pika.ConnectionParameters(host=MQ_IP)
)
self.channel = self.connection.channel()
#Create if there is no queue
self.channel.queue_declare(
queue=MQ_QUEUE,
durable=True
)
#The message obtained from the queue is turned by the generator.
self.logger.info('start consuming message.')
for msg in self.channel.consume(queue=MQ_QUEUE, no_ack=True):
message_body = msg[2]
self.logger.debug(message_body)
self.notifier.notify(noteType=GROW_NOTE_TYPE,
title=GROW_TITLE,
description=message_body)
def SvcStop(self):
# service stop
self.logger.info('service stopping...')
#Use higher class methods
self.ReportServiceStatus(
win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.stop_event)
#MQ disconnect
self.channel.close()
self.connection.close()
self.logger.info('service stopped.')
def main():
#argument('start','stop','install' etc.)Service start based on/Registration/Stop/Execute deletion etc.
win32serviceutil.HandleCommandLine(WinNotificationConsumer)
if __name__ == '__main__':
main()
You can register and start automatically like this.
C:\\Python27\python.exe win_notification.py --auto install
I was able to do what I wanted to do quite easily (Growl's various feelings regarding pop-ups). Since I was able to keep RabbitMQ connected from the Windows service, I felt that I could do various things with my computer by voice operation. I also want to write something about other Python apps ... I haven't been shown the existing code without refactoring ...
Recommended Posts