-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·338 lines (245 loc) · 11.3 KB
/
server.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/python
"""
HueMenorah server.
Requires unofficial Python Firebase REST API wrapper: http://ozgur.github.io/python-firebase/
$ sudo pip install requests
$ sudo pip install python-firebase
"""
# To add syntax-color highlighting in PHPStorm, follow the general instructions below:
# http://superuser.com/questions/843172/how-to-syntax-highlight-python-scripts-in-php-storm
'''
What does this program do?
+ It selects imagery.
+ It determines the mode.
+ It updates the Firebase database with new values.
'''
# curl 'https://huemenorah.firebaseio.com/.json?download=myfilename.txt'
# download JSON data
################################################################################
## Update the firebase database
def update_firebase ( fb_url, screen, image ) :
# TODO: set 'delay' and 'transition' properties
update = { 'path' : image, 'delay' : '0', 'transition' : 'cross-dissolve' }
# TODO: add authentication
# TODO: add error checking
result = fb.patch('/' + str(screen), update )
################################################################################
## Get X random images
def get_random_images ( image_count, recent_db_images, filesystem_images ) :
found_image_count = 0
new_images = []
while found_image_count < image_count:
# choose a random image
one_image = choice ( filesystem_images )
# TODO: add debugging info here
# has it been seen recently? is it an image already in the hopper? is it a mural tile?
if not one_image in recent_db_images and not one_image in new_images and not one_image in mural_tiles and one_image != credits_tile and one_image != instructions_tile and one_image != '.DS_Store':
# increment number of found images
found_image_count += 1
new_images.append ( one_image )
if debug:
print "added", one_image
else:
if debug:
print "skipped", one_image
continue
return new_images
################################################################################
from sys import argv #used only for the basename call
import os
from random import choice # to choose a random item from a list
import shelve # for pseudo-databases
from random import choice # to choose a random item from a list
from firebase import firebase
import datetime
################################################################################
################################################################################
################################################################################
################################################################################
################################################################################
# GLOBAL VARIABLES from config.py
from config import debug
from config import imagery_directory
from config import recent_imagery_db
from config import recent_imagery_db_limit
from config import fb_url
from config import instructions_tile
from config import credits_tile
from config import mural_tiles
################################################################################
# check command line arguments
if len(argv) != 2:
print ""
print "Incorrect number of arguments (" + str ( len(argv)-1 ) + "). Sample usage:"
print ""
print os.path.basename ( argv[0] ), "<mode>"
print ""
exit()
mode = argv[1]
# open the imagery directory
# pick 9 random images, none of which are repeated
# or have been picked within the last X time period
# store the choices for future reference
# open database of recently selected images
recently_selected_imagery = shelve.open ( recent_imagery_db )
# test the length of the content in the db
if len ( recently_selected_imagery ) == 0 :
# seed the db, if needed, with a key and an empty list
recently_selected_imagery['images'] = []
# assign the images list to a variable
recent_db_images = recently_selected_imagery['images']
# get the list of files from the filesystem
filesystem_images = os.listdir ( imagery_directory )
# open connection to firebase
fb = firebase.FirebaseApplication( fb_url, None )
# TODO: choose the screen mode
#mode = 'random' # for testing, assign the mode // comment this line out eventually
################################################################################
if mode == 'random':
if debug:
print "random mode"
# find 9 new images that have not appeared recently
random_image_count = 9
new_unseen_images = get_random_images ( random_image_count, recent_db_images, filesystem_images )
#####
for image in range ( len ( new_unseen_images ) ):
if debug:
print "processing", new_unseen_images[image], "..."
# TODO: update all screens simultaneously, not one at a time.
# update the database
if debug:
print "updating the firebase database: screen", str(image), "-", new_unseen_images[image]
update_firebase ( fb_url, str(image), new_unseen_images[image] )
# only track the most recent X images
if len ( recent_db_images ) == recent_imagery_db_limit :
# remove oldest record
if debug:
print "deleting oldest image from recently used list:", recent_db_images[0]
del ( recent_db_images[0] )
# add image to recently selected images
recent_db_images.append ( new_unseen_images[image] )
recently_selected_imagery['images'] = recent_db_images
################################################################################
if mode == 'mural':
if debug:
print "mural mode"
screen_count = 9
for image in range ( len ( mural_tiles ) ) :
if debug:
print "adding", image
if debug:
print "updating the firebase database: screen", str(image), "-", str(mural_tiles[image])
update_firebase ( fb_url, str(image), str(mural_tiles[image]) )
################################################################################
if mode == 'menorah':
if debug:
print "menorah mode"
today = datetime.datetime.today()
# what day and hour is today?
# how many monitors should be "lit"?
number_of_day = today.day
hour_of_day = today.hour
if debug:
last_day_of_hanukkah = 11
else:
last_day_of_hanukkah = 13 # december 13... really the day of the 14th
# need to check the hour of the day as well.
# 16:30 - 23:59, 00:00 to 6:00
candles_to_light = 9
# subtract a day if we are between midnight and 6am
if hour_of_day >= 0 and hour_of_day <= 6:
offset = 1
else:
offset = 0
image_count_to_show = candles_to_light - ( last_day_of_hanukkah - number_of_day ) - offset
print "image_count_to_show:", image_count_to_show
new_unseen_images = get_random_images ( image_count_to_show, recent_db_images, filesystem_images )
shamash = new_unseen_images.pop()
print "new_unseen_images: ",new_unseen_images
for image in range ( len ( mural_tiles ) ) :
if ( candles_to_light - image - len(new_unseen_images) ) <= 0 :
image_to_show = new_unseen_images[candles_to_light - image - len(new_unseen_images)]
else:
image_to_show = mural_tiles[image]
# if it's the 5th candle, override the default
if image == 4:
image_to_show = shamash
if debug:
print "updating the firebase database: screen", str(image), "-", image_to_show
update_firebase ( fb_url, str(image), image_to_show )
# TODO: make this into a function, since it's repeated elsewhere.
# only track the most recent X images
if len ( recent_db_images ) == recent_imagery_db_limit :
# remove oldest record
if debug:
print "deleting oldest image from recently used list:", recent_db_images[0]
del ( recent_db_images[0] )
# add image to recently selected images
recent_db_images.append ( image_to_show )
recently_selected_imagery['images'] = recent_db_images
################################################################################
if mode == 'credits':
if debug:
print "credits mode"
random_image_count = 6
new_unseen_images = get_random_images ( random_image_count, recent_db_images, filesystem_images )
print "new_unseen_images:", new_unseen_images, "(", str(len(new_unseen_images)), ")"
screen_count = 9
counter = 0 # to keep track of the random images
for screen in range ( screen_count ) :
print "screen",screen
if screen is 0 or screen is 4 or screen is 8:
image_to_show = credits_tile
else:
image_to_show = new_unseen_images[counter]
counter = counter+1
if debug:
#print "image_to_show:",image_to_show
print "updating the firebase database: screen", str(screen), "-", image_to_show
update_firebase ( fb_url, str(screen), image_to_show )
# TODO: make this into a function, since it's repeated elsewhere.
# only track the most recent X images
if image_to_show != instructions_tile:
if len ( recent_db_images ) == recent_imagery_db_limit :
# remove oldest record
if debug:
print "deleting oldest image from recently used list:", recent_db_images[0]
del ( recent_db_images[0] )
# add image to recently selected images
recent_db_images.append ( image_to_show )
recently_selected_imagery['images'] = recent_db_images
################################################################################
if mode == 'instructions':
if debug:
print "instructions mode"
random_image_count = 6
new_unseen_images = get_random_images ( random_image_count, recent_db_images, filesystem_images )
print "new_unseen_images:", new_unseen_images, "(", str(len(new_unseen_images)), ")"
screen_count = 9
counter = 0 # to keep track of the random images
for screen in range ( screen_count ) :
print "screen",screen
if screen is 0 or screen is 4 or screen is 8:
image_to_show = instructions_tile
else:
image_to_show = new_unseen_images[counter]
counter = counter+1
if debug:
#print "image_to_show:",image_to_show
print "updating the firebase database: screen", str(screen), "-", image_to_show
update_firebase ( fb_url, str(screen), image_to_show )
# TODO: make this into a function, since it's repeated elsewhere.
# only track the most recent X images
if image_to_show != instructions_tile:
if len ( recent_db_images ) == recent_imagery_db_limit :
# remove oldest record
if debug:
print "deleting oldest image from recently used list:", recent_db_images[0]
del ( recent_db_images[0] )
# add image to recently selected images
recent_db_images.append ( image_to_show )
recently_selected_imagery['images'] = recent_db_images
################################################################################
if debug:
print "recently_selected_imagery: ", recently_selected_imagery['images'], "(" + str ( len ( recently_selected_imagery['images'] ) ) + ")"
recently_selected_imagery.close ( )