-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheese.py
executable file
·301 lines (236 loc) · 7.32 KB
/
cheese.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from datetime import datetime, timedelta
import subprocess
import os, shutil
import keen
import requests
import logging
import serial
import time
import re
import glob
import sys
CAMERA_ID = '001'
CAMERA_TYPE = 'Canon'
LOCAL_FOLDER = '/home/pi/cheese'
PHOTO_FOLDER = '/media/photo'
RAW_EXTENSION = 'cr2'
JPG_EXTENSION = 'jpg'
THUMBNAIL_RESIZE_FACTOR = '50%%'
POST_URL = ''
keen.project_id = ''
keen.write_key = ''
def log (message):
print datetime.utcnow(), message
pass
def resetUsb (cameraType):
log(">>> reset USB")
process = subprocess.Popen(["lsusb"], stdout=subprocess.PIPE)
(output, err) = process.communicate()
exit_code = process.wait()
for line in output.split('\n') :
if cameraType in line:
process = subprocess.Popen(["./usbreset", "/dev/bus/usb/%s/%s" % (line[4:7], line[15:18])], stdout=subprocess.PIPE)
(output, err) = process.communicate()
exit_code = process.wait()
if exit_code != 0:
print err
log("<<< reset USB")
def remountFilesystem ():
log(">>> remount file system")
command = ["sudo", "mount", "-a"]
result = subprocess.check_output(command)
log("<<< remount file system")
def signalOnPin (pin, message, timeout):
log(">>> %s" % message)
command = ["gpio", "export", str(pin), "out"]
result = subprocess.check_output(command)
command = ["gpio", "-g", "write", str(pin), "1"]
result = subprocess.check_output(command)
time.sleep(timeout)
command = ["gpio", "-g", "write", str(pin), "0"]
result = subprocess.check_output(command)
log("<<< %s" % message)
def turnCameraOn ():
signalOnPin(23, "turning camera on", 1)
time.sleep(4)
def takePhoto (filename):
log(">>> take photo")
command = ["gphoto2", "--force-overwrite", "--capture-image-and-download", "--filename=%s.%%C" % filename]
result = subprocess.check_output(command)
log("<<< take photo")
def movePicture (filename):
import traceback
log(">>> move pictures")
for extension in [RAW_EXTENSION]:
log("-- extension: %s" % extension)
localFile = "%s/%s.%s" % (LOCAL_FOLDER, filename, extension)
log("-- local file: %s" % localFile)
targetFile = "%s/%s.%s" % (PHOTO_FOLDER, filename, extension)
log("-- target file: %s" % targetFile)
try:
log("-> copying ...")
shutil.copy(localFile, targetFile)
log("<- copying - DONE")
except Exception as e:
log("exception: %s" % str(e))
pass
if os.path.isfile(targetFile):
log("-> removing local file ...")
os.remove(localFile)
log("<- removing local file - DONE")
else:
raise Exception("something went wrong copying the file")
log("<<< move pictures")
def resizeThumbnail (filename):
log(">>> resize thumbnail")
imageFile = "%s.%s" % (filename, JPG_EXTENSION)
command = ["convert", imageFile, "-resize", THUMBNAIL_RESIZE_FACTOR, imageFile]
result = subprocess.check_output(command)
log("<<< resize thumbnail")
def sendThumbnailViaHttpPost (filename):
log(">>> sendThumbnailViaHttpPost - %s" % filename)
files = {'Filedata': (filename, open(filename, 'rb'), 'image/jpeg')}
headers = {
'Accept-Encoding': 'gzip, deflate',
'Accept': '*/*',
'Accept-Language': 'en-us'
}
request = requests.Request('POST', POST_URL, files=files, headers=headers)
preparedRequest = request.prepare()
session = requests.Session()
response = session.send(preparedRequest)
log("<<< sendThumbnailViaHttpPost - %s" % response.status_code)
return response
def sendThumbnails ():
log(">>> send thumbnail")
for file in glob.glob("*.%s" % JPG_EXTENSION):
try:
response = sendThumbnailViaHttpPost(file)
if response.status_code == 200:
log("--- removing thumbnail file %s" % file)
os.remove(file)
except:
log("error uploading file")
pass
log("<<< send thumbnail")
def connectGPRS ():
log(">>> connect GPRS")
process = subprocess.Popen(["sudo", "sakis3g", "connect", "USBMODEM=\"12d1:1001\"", "APN=\"ibox.tim.it\""], stdout=subprocess.PIPE)
(output, err) = process.communicate()
exit_code = process.wait()
if exit_code != 0:
raise Exception("something went wrong setting up GPRS connection")
log("<<< connect GPRS")
def disconnectGPRS ():
log(">>> disconnect GPRS")
process = subprocess.Popen(["sudo", "sakis3g", "disconnect"], stdout=subprocess.PIPE)
(output, err) = process.communicate()
exit_code = process.wait()
if exit_code != 0:
raise Exception("something went wrong closing the GPRS connection")
log("<<< disconnect GPRS")
def readLineFromSerialPort ():
process = subprocess.Popen(["./usbDevice", "Prolific"], stdout=subprocess.PIPE)
(output, err) = process.communicate()
exit_code = process.wait()
device = output.rstrip()
log("serial connector device: %s" % device)
data = serial.Serial(device, 9600).readline()
log("serial data: %s" % data)
return data
def convertDiskSpace (value):
return (value.f_bavail * value.f_frsize) / 1024 / 1024
def collectStats (filename):
log(">>> collect stats")
now = datetime.now()
try:
statInfo = readLineFromSerialPort()
# match = re.search(r"[^\d]*(\d*):(\d*):(\d*)\s*[^\d]*([\d\.]*)[^\d]*([\d\.]*)", statInfo)
match = re.search(r"[^\d]*(\d*):(\d*):(\d*)\s*[^\d]*([\d\.]*)[^\d]*([\d\.]*)[^\d]*([\d\.]*)[^\d]*([\d]*)", statInfo)
hours = int(match.group(1))
minutes = int(match.group(2))
seconds = int(match.group(3))
batteryLevel = float(match.group(4))
cameraTemperature = float(match.group(5))
try:
caseTemperature = float(match.group(6))
reset = int(match.group(7))
except:
caseTemperature = 1
reset = 0
except:
hours = 0
minutes = 0
seconds = 0
batteryLevel = 1
cameraTemperature = 1
caseTemperature = 1
reset = 0
time = now.strftime('%Y-%m-%d') + "T" + "%02d:%02d:%02d" % (hours, minutes, seconds) + ".000Z"
raspberryTime = now.strftime('%Y-%m-%dT%H:%M:%S.000Z')
rawSize = os.path.getsize("%s/%s.%s" % (PHOTO_FOLDER, filename, RAW_EXTENSION))
bootAvailableSpace = convertDiskSpace(os.statvfs('/'))
photoAvailableSpace = convertDiskSpace(os.statvfs(PHOTO_FOLDER))
stats = {
"environment": [ {
"camera": CAMERA_ID,
"temperature": cameraTemperature,
"caseTemperature": caseTemperature,
"batteryLevel": batteryLevel,
"reset": reset,
"time": time
} ],
"image": [ {
"camera": CAMERA_ID,
"name": filename,
"rawSize": rawSize
} ],
"raspberry": [ {
"camera": CAMERA_ID,
"boot": bootAvailableSpace,
"photo": photoAvailableSpace,
"dateTime": raspberryTime
}]
}
log("<<< collect stats")
return stats
def sendStats (stats):
log(">>> send stats")
keen.add_events(stats)
log("<<< send stats")
def signalShuttingDown ():
signalOnPin(18, "signal shutting down", 1)
def shutdown ():
log(">>> shutting down")
os.system('sudo shutdown now -h')
log("<<< shutting down")
def cheese (filename):
turnCameraOn()
# resetUsb(CAMERA_TYPE)
remountFilesystem()
takePhoto(filename)
movePicture(filename)
resizeThumbnail(filename)
connectGPRS()
sendThumbnails()
sendStats(collectStats(filename))
disconnectGPRS()
def main ():
now = datetime.now()
filename = now.strftime('%Y-%m-%d_%H%M%S')
log("Filename: %s" % filename)
try:
cheese (filename)
except Exception, exception:
logging.exception(exception)
log("=== waiting for two minutes")
signalOnPin(17, "turning error leg on", 120)
finally:
signalShuttingDown()
shutdown()
pass
if __name__ == "__main__":
# log("readline: %s" % readLineFromSerialPort())
main()