-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageHandler.py
executable file
·160 lines (138 loc) · 4.02 KB
/
imageHandler.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
#!/usr/bin/env python
import os
import sys
import json
import base64
import shutil
import threading
import glob
import time
import random
import Queue
from watchdog import Watchdog
from specs import Specs
from debug import Debug
from hosts import Hosts
from utils import mkpath
from singleton import Singleton
from server import Server
from display import Display
class ImageHandler(threading.Thread):
__metaclass__ = Singleton
def __init__(self):
super(ImageHandler,self).__init__()
self.name = "ImageHandler"
print("starting: %s"%self.name)
Watchdog().add(self)
self.currentId = None
self.queue = Queue.Queue()
if Hosts().getLocalAttr('hasServer'):
Server().register({
'AddImage' : self.addImage
,'RmCacheDir' : self.rmCacheDir
,'SetImageDir' : self.setImageDir
,'ClearCache' : self.clearCache
})
def setImageDir(self,args):
rval = "ok"
id = args[0]
path = self.getCacheDir(id)
self.queue.put(path)
print("SetImageDir to %s: %s"%(path,rval))
return Hosts.jsonStatus(rval)
def addImage(self,args):
id = args['id']
imgData = args['imgData']
if self.currentId is None or self.currentId != id:
self.currentId = id
print("addImage currentId: %d"%self.currentId)
path = self.getCacheDir(self.currentId)
for d in imgData:
file = path + "/%s"%d['name']
print("---name: %s"%file)
with open(file, 'wb') as f:
f.write(base64.b64decode(d['img']))
return Hosts.jsonStatus("ok");
@staticmethod
def getCacheDir(id):
path = "%s/%d"%(ImageHandler.getImageCache(),id)
return mkpath(path)
@staticmethod
def getCacheDir(id):
path = ImageHandler.getImageCache()+"/%d"%id
return mkpath(path)
@staticmethod
def rmCacheDir(args):
rval = "ok"
id = args[0]
path = ImageHandler.getCacheDir(id)
try:
shutil.rmtree(path)
except OSError as e:
rval = "Error: %s - %s." % (e.filename, e.strerror)
print("rmCacheDir: path %s %s"%(path,rval))
return Hosts.jsonStatus(rval)
@staticmethod
def getImageCache():
path = "%s/imageCache"%(Specs().s['tmpdir'])
return mkpath(path)
@staticmethod
def clearCache(args):
path = ImageHandler.getImageCache()
for f in os.listdir(path):
try:
r = path+"/%s"%f
print("rm: %s"%r)
shutil.rmtree(path)
except OSError as e:
rval = "Error: %s - %s." % (e.filename, e.strerror)
return Hosts.jsonStatus("ok")
def run(self):
print ("%s starting"%self.name)
afiles = []
(minTime,maxTime) = Specs().s["displayTimeRange"]
lastImageDir=""
imageIndex=0
splash = Specs().s['splashImg']
print("displaying f:%s"%splash)
Display().image(splash)
ts = None
while True:
Watchdog().feed(self)
try:
path = self.queue.get(timeout=ts)
except Queue.Empty:
path = lastImageDir
if path == "__stop__":
print("%s is stopping"%self.name)
break
print("%s: path %s lastImageDir %s"%(self.name,path,lastImageDir))
if path != lastImageDir:
print("%s reseting imageIndex"%self.name)
imageIndex = 0
lastImageDir = path
if len(afiles) == 0:
print("empty image file. waiting")
ts = 1
continue
afiles=glob.glob(path+"/*.jpg")
numFiles = len(afiles)
if numFiles == 0:
print("%s directory empty!!"%self.name)
print("displaying f:%s"%splash)
Display().image(splash)
ts = 1
continue
print("imageIndex %d len afiles %d"%(imageIndex,numFiles))
if imageIndex >= numFiles:
print("resetting imageIndex");
imageIndex = 0
f = afiles[imageIndex]
imageIndex += 1
print("displaying f:%s"%f)
if Display().image(f) == False:
print "skipping bad images:%s"%f
ts = .1
continue
ts = (random.random() * (maxTime - minTime)) + minTime
print("next display %f"%ts)