Skip to content

Commit 8ad3080

Browse files
committed
Handle the case when an image is unavailable
1 parent f522b6b commit 8ad3080

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

otbox.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
import base64
1919
import argparse
2020
import logging
21+
import sys
2122

22-
try:
23+
try:
2324
from PIL import Image
2425
from PIL import ImageFont
2526
from PIL import ImageDraw
@@ -126,10 +127,19 @@ def _mqtt_handler_picturetoscreen(self, deviceType, deviceId, payload):
126127
{{TESTBED}}/deviceType/box/deviceId/box1/cmd/picturetoscreen
127128
'''
128129
assert deviceType==DEVICETYPE_BOX
129-
image = Image.open(requests.get(json.loads(payload)['url'], stream=True).raw)
130-
image.thumbnail((480,320),Image.ANTIALIAS)
131-
self._otbox.change_image_queue.put(image)
132-
self._otbox.change_image_queue.join()
130+
try:
131+
image_url = json.loads(payload)['url']
132+
response = requests.get(image_url, stream=True)
133+
image = Image.open(response.raw)
134+
except IOError:
135+
# the image is not available now; skip this one
136+
print('image at {0} is not available, '.format(image_url)
137+
+ 'HTTP response code {0}'.format(response.status_code))
138+
sys.stdout.flush()
139+
else:
140+
image.thumbnail((480,320),Image.ANTIALIAS)
141+
self._otbox.change_image_queue.put(image)
142+
self._otbox.change_image_queue.join()
133143
return {}
134144

135145
def _mqtt_handler_colortoscreen(self, deviceType, deviceId, payload):
@@ -283,7 +293,7 @@ def _on_mqtt_connect(self, client, userdata, flags, rc):
283293
self.mqttconnected = True
284294

285295
# subscribe to box commands (device-specific and 'all')
286-
# note that unknown commands will be ignored by _execute_command_safely
296+
# note that unknown commands will be ignored by _execute_command_safely
287297
client.subscribe('{0}/#'.format(self.mqtttopic_box_cmd_prefix))
288298
client.subscribe('{0}/deviceType/box/deviceId/all/cmd/#'.format(self.testbed))
289299

@@ -541,13 +551,13 @@ def _mqtt_handler_program(self, deviceType, deviceId, payload):
541551

542552
payload = json.loads(payload) # shorthand
543553
mote = self._eui64_to_moteinfoelem(deviceId)
544-
554+
545555
# reset the mote first
546556
self._mqtt_handler_reset(deviceType, deviceId, payload=None)
547557

548558
# disconnect from the serialports
549559
self.SerialportHandlers[mote['serialport']].disconnectSerialPort()
550-
560+
551561
if isinstance(self.tb, OpenTestbed):
552562
# For OpenTestbed, we will use a separate firmware_temp
553563
# for each deviceId. Although other testbeds may need to
@@ -667,18 +677,18 @@ def _bootload_motes(self, serialports, firmware_file):
667677
# start bootloading each mote
668678
ongoing_bootloads = {}
669679
for serialport in serialports:
670-
680+
671681
# simply the name
672682
port = serialport.split('/')[-1]
673-
683+
674684
# stop serial reader
675685
ongoing_bootloads[port] = self.tb.bootload_mote(serialport, firmware_file)
676686

677687
returnVal = []
678688
for ongoing_bootload in ongoing_bootloads:
679689
# wait for this bootload process to finish
680690
(stdout, stderr) = ongoing_bootloads[ongoing_bootload].communicate()
681-
691+
682692
# record the last output of bootload process
683693
with open("log_{0}.txt".format(ongoing_bootload),'w') as f:
684694
f.write("stdout: {0} stderr {1}".format(stdout,stderr))

0 commit comments

Comments
 (0)