from win32com.client import Dispatch, GetObject from datetime import datetime from pytz import utc from math import floor from time import timezone, sleep from serial import Serial from os import system, path import tornado.ioloop import tornado.web import tornado.websocket import asyncio import threading import pythoncom skipMeeting = False class NotConnectedException(Exception): pass class MainHandler(tornado.web.RequestHandler): def get(self): #dirname = path.dirname(__file__) #print(dirname) #fullpath = path.join(dirname, "/index.html") self.render("./index.html") class DataHandler(tornado.websocket.WebSocketHandler): def get(self): self.write(str(getMinutes())) class ButtonHandler(tornado.web.RequestHandler): def get(self): skipMeeting = True def make_app(): staticPath = path.abspath("./Meeting Minder_files") #print(staticPath) return tornado.web.Application([ (r"/meetingminder", MainHandler), (r"/meetingminderdata", DataHandler), (r"/meetingminderbutton", ButtonHandler), (r"/static/(.*)", tornado.web.StaticFileHandler, {"path": staticPath}) ]) def getMinutes(): dateRest = utc.localize(datetime.now()) #print(dateRest) Outlook = Dispatch("Outlook.Application") ns = Outlook.GetNamespace("MAPI") appointments = ns.GetDefaultFolder(9).Items appointments.Sort("[Start]") appointments.IncludeRecurrences = "True" appointments = appointments.Restrict("[Start] >= '" +dateRest.strftime("%m/%d/%Y %I:%M %p")+ "'") apptTime = appointments(1).Start #print(apptTime) numMins = floor((apptTime - dateRest).total_seconds() / 60) + 1 return numMins def sendArduino(data): wmi = GetObject("winmgmts:") for ser in wmi.InstancesOf("Win32_SerialPort"): if ser.Description == 'Arduino Yun': arduino = ser #print(arduino.DeviceID) try: arduino except NameError: raise NotConnectedException() toSend = str(data) + "|" print(str(data) + " minutes until the next meeting...") try: outport = Serial(arduino.DeviceID, 9600, timeout=1) outport.close() outport.open() outport.write(toSend.encode()) except Exception as msg: print(msg) pass finally: outport.close() def mainLoop(): asyncio.set_event_loop(asyncio.new_event_loop()) pythoncom.CoInitialize() while(1): while(datetime.now().second > 0): pass startTime = datetime.now() ## if skipMeeting: ## skipMeeting = False ## try: ## sendArduino(0) ## except NotConnectedException: ## print("Arduino Not Connected! Waiting...") ## pass ## except Exception: ## print("An Error Occured! " + str(Exception.strerror)) ## pass ## ## while((datetime.now() - startTime).total_seconds() < (getMinutes()*60)): ## pass try: sendArduino(getMinutes()) except NotConnectedException: print("Arduino Not Connected! Waiting...") pass except Exception: print("An Error Occured! " + str(Exception.strerror)) pass while((datetime.now() - startTime).total_seconds() < 60): pass def tornadoLoop(): asyncio.set_event_loop(asyncio.new_event_loop()) pythoncom.CoInitialize() app = make_app() app.listen(80) tornado.ioloop.IOLoop.current().start() if __name__ == "__main__": mainThread = threading.Thread(target=mainLoop, args=()) tornadoThread = threading.Thread(target=tornadoLoop, args=()) tornadoThread.daemon = True mainThread.start() tornadoThread.start()