-
Notifications
You must be signed in to change notification settings - Fork 3
/
glass.py
191 lines (163 loc) · 6.66 KB
/
glass.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from clarifai import rest
from clarifai.rest import ClarifaiApp
import signal
import sys
import websocket
import json
import requests
import SSD1331
import _thread
import time
import datetime
import os
import picamera
import threading
import math
your_access_token = "o.jd9hKkopWYUYLiyVOBnnHMeHYWH7A4EU"
url_base = "https://api.pushbullet.com/v2/"
clarifai_key = "a653edc0184247e0a6b61c556ca691a8"
def upload_file():
url_request = url_base+"upload-request"
data = json.dumps({'file_name': 'snapshot.jpg',
'file_type': 'image/jpeg'})
res = requests.post(url=url_request, data=data, headers=headers)
if res.status_code != 200:
raise Exception('failed to request upload')
r = res.json()
res = requests.post(r['upload_url'], data=r['data'], files={'file': open('snapshot.jpg', 'rb')})
if res.status_code != 204:
raise Exception('failed to upload file')
print (r['file_name'], "url: ", r['file_url'])
url_pushes = url_base+"pushes"
data = json.dumps({'file_name': r['file_name'],
'file_type': r['file_type'],
'file_url': r['file_url'],
'type': 'file'})
res = requests.post(url=url_pushes, data=data, headers=headers)
def signal_handler(sig, frame):
device.Clear()
cam.resolution = (480, 360)
y = 2
print ('taking snapshot')
cam.capture('snapshot.jpg')
t2 = threading.Thread(target=upload_file, name='t2')
t2.start()
d = model.predict_by_filename('snapshot.jpg')
for i in d['outputs'][0]['data']['concepts']:
if(y<5):
y = y+1
print (i['name'])
device.DrawString(15, y*10, i['name'], SSD1331.COLOR_WHITE)
SSD1331_PIN_CS = 23
SSD1331_PIN_DC = 24
SSD1331_PIN_RST = 25
def on_message(ws, raw):
flag=False
device.Clear()
data = json.loads(raw)
print (json.dumps(data,indent=2))
global modified
try:
type = data['type']
except:
print ("Error in reading the type")
if type == "push" and data['push']['type'] != "dismissal":
flag=True
try:
if data['push']['push']['type'] == "dismissal":
flag=False
except:
print ("no field")
try:
application=data['push']['application_name']
device.DrawString(5, 20, application, SSD1331.COLOR_WHITE)
print (data['push']['application_name'])
except:
print ("error in application name")
try:
title=data['push']['title'].rsplit(' (',1)[0]
device.DrawString(5, 30, title, SSD1331.COLOR_WHITE)
print (data['push']['title'])
except:
print ("error in title")
try:
body=data['push']['body'].rsplit('\n',1)[-1]
device.DrawString(5, 40, body, SSD1331.COLOR_WHITE)
print (data['push']['body'])
except:
print ("error in body")
elif type == "tickle":
url_pushes = url_base+"pushes?modified_after="+str(modified)
response = requests.get(url=url_pushes, headers=headers)
data=response.json()
print (json.dumps(data,indent=2))
try:
modified = data['pushes'][0]['modified']
except:
print ("not a traditional push")
flag=True
for push in data['pushes']:
try:
body=push['body'].rsplit('\n',1)[-1]
print(body)
if(body=="Cam"):
os.kill(os.getpid(), signal.SIGTSTP)
else:
device.DrawString(5, 20, "Message", SSD1331.COLOR_WHITE)
device.DrawString(5, 30, body, SSD1331.COLOR_WHITE)
except:
print ("error in reading message")
if flag:
time.sleep(5)
device.Clear()
my_now = datetime.datetime.now()
today_date = my_now.strftime("%b-%d")
today_day = my_now.strftime("%a")
today_time = my_now.strftime("%H:%M")
device.DrawString(60, 20, str(today_time), SSD1331.COLOR_WHITE)
device.DrawString(60, 30, str(today_date), SSD1331.COLOR_WHITE)
device.DrawString(60, 40, str(today_day), SSD1331.COLOR_WHITE)
hours_angle = 270 + (30 * (my_now.hour + (my_now.minute / 60.0)))
hours_dx = int(math.cos(math.radians(hours_angle)) * 12)
hours_dy = int(math.sin(math.radians(hours_angle)) * 12)
minutes_angle = 270 + (6 * my_now.minute)
minutes_dx = int(math.cos(math.radians(minutes_angle)) * 18)
minutes_dy = int(math.sin(math.radians(minutes_angle)) * 18)
device.DrawCircle(28, 32, 28, SSD1331.COLOR_RED)
device.DrawLine(28, 32, 30 + hours_dx, 32 + hours_dy, SSD1331.COLOR_WHITE)
device.DrawLine(28, 32, 30+ minutes_dx, 32 + minutes_dy, SSD1331.COLOR_WHITE)
def on_error(ws, error):
print (error)
def on_close(ws):
device.EnableDisplay(False)
device.Remove()
print ("### closed ###")
url_pushes = url_base+"pushes"
headers = { 'Access-Token': your_access_token,
'Content-Type': 'application/json'}
try:
app = ClarifaiApp(api_key=clarifai_key)
except:
print ("error in clarifai")
try:
model = app.public_models.general_model
model.model_version = 'aa7f35c01e0642fda5cf400f543e7c40'
except:
print ("error in getting model")
device = SSD1331.SSD1331(SSD1331_PIN_DC, SSD1331_PIN_RST, SSD1331_PIN_CS)
device.EnableDisplay(True)
device.DrawString(5, 20, "TEST", SSD1331.COLOR_WHITE)
device.Clear()
cam = picamera.PiCamera()
response = requests.get(url=url_pushes, headers=headers)
data = response.json()
modified = data['pushes'][0]['modified']
#print (type(modified))
#print (modified)
signal.signal(signal.SIGTSTP, signal_handler)
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://stream.pushbullet.com/websocket/"+your_access_token,
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.run_forever()