-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaster.py
executable file
·97 lines (84 loc) · 3.04 KB
/
master.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/python
import time
import serial
import csv
import paho.mqtt.publish as publish
import httplib2
import os
import datetime
import re
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
def get_credentials():
"""Gets valid user credentials from storage.
Returns:
Credentials, the obtained credential.
"""
credential_path = os.path.relpath('calendar-python-quickstart.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
raise "Stored credentials missing or invalid"
return credentials
def get_calendar_events(searchedReminder):
"""Shows basic usage of the Google Calendar API.
Creates a Google Calendar API service object and outputs a list of the next
10 events on the user's calendar.
"""
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('calendar', 'v3', http=http)
last_midnight = datetime.datetime.combine(datetime.date.today(), datetime.time.min)
last_midnight = last_midnight.isoformat() + 'Z'
print 'Getting the upcoming 250 events'
eventsResult = service.events().list(
calendarId='primary', timeMin=last_midnight, singleEvents=True,
orderBy='startTime').execute()
events = eventsResult.get('items', [])
for event in events:
summary = str.strip(str(event['summary']))
summary = re.sub('\s+', ' ', summary)
try:
reminder = re.search('REMINDER: (\S+)', summary, flags=re.IGNORECASE).group(1)
if (str.strip(reminder).lower() == str.strip(searchedReminder).lower()):
return True
except AttributeError:
pass # no matches
return False
last_payload = None
def publish_payload(box, payload):
global last_payload
if(last_payload != payload):
last_payload = payload
publish.single(box, last_payload, hostname="sMirrorMaster", port=9001, transport="websockets")
print "Published %s" % last_payload
ser=serial.Serial(
port='/dev/serial0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
actions = []
with open('actions.csv', 'rb') as f:
reader = csv.reader(f)
for tag, box, payload, reminder in list(reader):
actions.append([int(tag), str.strip(box), str.strip(payload), str.strip(reminder)])
while(True):
reading=ser.readline()
print "Reading: %s" % reading
if reading:
try:
currentTag = int(reading)
for tag, box, payload, searchedReminder in actions:
if(currentTag == tag):
if(searchedReminder):
if(get_calendar_events(searchedReminder)):
publish_payload(box, payload)
else:
publish_payload(box, payload)
except ValueError:
pass