| 1 |
from twisted.application import service |
|---|
| 2 |
from twisted.internet import reactor, defer |
|---|
| 3 |
import simplejson |
|---|
| 4 |
from datetime import datetime |
|---|
| 5 |
|
|---|
| 6 |
import fizzjik.input.easyident |
|---|
| 7 |
import fizzjik.input.sonmicro |
|---|
| 8 |
import fizzjik.input.basic |
|---|
| 9 |
|
|---|
| 10 |
import fizzjik.output.basic |
|---|
| 11 |
import fizzjik.output.lpr |
|---|
| 12 |
|
|---|
| 13 |
from fizzjik.hub import Hub |
|---|
| 14 |
from fizzjik.input.sonmicro import SonMicroMifareSensor |
|---|
| 15 |
from fizzjik.input.basic import LineReceiver |
|---|
| 16 |
from fizzjik.input.network import NetworkConnectionSensor |
|---|
| 17 |
|
|---|
| 18 |
from fizzjik.output.basic import Echo |
|---|
| 19 |
from fizzjik.output.lpr import LPRService |
|---|
| 20 |
|
|---|
| 21 |
from fizzjik.remote.xmlrpc import XMLRPCService |
|---|
| 22 |
|
|---|
| 23 |
from fizzjik.remote.flickr import UploadService |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
from fizzjik.config import ConfigurableService, if_config |
|---|
| 27 |
from fizzjik.interfaces import IController, implements |
|---|
| 28 |
from fizzjik.rfid import TagAddedEvent, TagPresentEvent, TagRemovedEvent |
|---|
| 29 |
from fizzjik.remote.anymeta import JSONService |
|---|
| 30 |
|
|---|
| 31 |
import pprint |
|---|
| 32 |
|
|---|
| 33 |
from twisted.web import client |
|---|
| 34 |
|
|---|
| 35 |
class PhotoboothController(ConfigurableService): |
|---|
| 36 |
implements(IController) |
|---|
| 37 |
countdown = None |
|---|
| 38 |
delay = 5.0 |
|---|
| 39 |
remote = None |
|---|
| 40 |
flickr = None |
|---|
| 41 |
bucket = None |
|---|
| 42 |
people = None |
|---|
| 43 |
|
|---|
| 44 |
def __init__(self, remote, flickr): |
|---|
| 45 |
self.bucket = [] |
|---|
| 46 |
self.people = {} |
|---|
| 47 |
self.remote = remote |
|---|
| 48 |
self.flickr = flickr |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
@if_config('enabled') |
|---|
| 52 |
def startService(self): |
|---|
| 53 |
ConfigurableService.startService(self) |
|---|
| 54 |
self.test() |
|---|
| 55 |
|
|---|
| 56 |
def test(self): |
|---|
| 57 |
e = TagAddedEvent('046792D9') |
|---|
| 58 |
d = self.getCurrentTags() |
|---|
| 59 |
d.addCallback(lambda x: pprint.pprint(x) and x or x) |
|---|
| 60 |
d.addCallbacks(lambda x: self.addToBucket(e)) |
|---|
| 61 |
d.addCallbacks(lambda x: self.getCurrentTags()) |
|---|
| 62 |
d.addCallback(lambda x: pprint.pprint(x) and x or x) |
|---|
| 63 |
|
|---|
| 64 |
def registerObservers(self, hub): |
|---|
| 65 |
|
|---|
| 66 |
hub.addObserver(TagAddedEvent, self.addToBucket) |
|---|
| 67 |
|
|---|
| 68 |
def addToBucket(self, evt): |
|---|
| 69 |
if not self.bucket: |
|---|
| 70 |
self.startCountdown() |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
d = self._tagToPerson(evt.data) |
|---|
| 75 |
def _cachePerson(person): |
|---|
| 76 |
|
|---|
| 77 |
self.people[evt.data] = person |
|---|
| 78 |
return person |
|---|
| 79 |
|
|---|
| 80 |
d.addCallback(_cachePerson) |
|---|
| 81 |
d.addCallback(self.makeContacts) |
|---|
| 82 |
if not self.bucket: |
|---|
| 83 |
d.addCallback(lambda x: self.startCountdown()) |
|---|
| 84 |
self.bucket.append(evt.data) |
|---|
| 85 |
d.addErrback(lambda _: None) |
|---|
| 86 |
return d |
|---|
| 87 |
|
|---|
| 88 |
def startCountdown(self): |
|---|
| 89 |
print "counting down" |
|---|
| 90 |
if self.countdown and not self.countdown.called: |
|---|
| 91 |
self.countdown.cancel() |
|---|
| 92 |
self.countdown = reactor.callLater(self.delay, self.clearBucket) |
|---|
| 93 |
|
|---|
| 94 |
def clearBucket(self): |
|---|
| 95 |
print "clearing bucket" |
|---|
| 96 |
self.bucket = [] |
|---|
| 97 |
self.people = {} |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
def makeContacts(self, person): |
|---|
| 101 |
for tag in self.bucket: |
|---|
| 102 |
if tag == person['rfid']: |
|---|
| 103 |
print "skip" |
|---|
| 104 |
continue |
|---|
| 105 |
print "conecting to %s" % tag |
|---|
| 106 |
if tag in self.people: |
|---|
| 107 |
p = dict(id=person['id'], id2=self.people[tag]['id']) |
|---|
| 108 |
pprint.pprint(p) |
|---|
| 109 |
d = self.remote.call('picnic.relations.add', p) |
|---|
| 110 |
d.addCallback(pprint.pprint) |
|---|
| 111 |
|
|---|
| 112 |
d2 = self.remote.call('picnic.relations.add', |
|---|
| 113 |
dict(id2=person['id'], |
|---|
| 114 |
id1=self.people[tag]['id'])) |
|---|
| 115 |
d2.addCallback(pprint.pprint) |
|---|
| 116 |
return person |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
def getCurrentTags(self): |
|---|
| 120 |
people = [self.people[tag] for tag in self.bucket if tag in self.people] |
|---|
| 121 |
return defer.succeed(people) |
|---|
| 122 |
|
|---|
| 123 |
def uploadPhoto(self, path, title=None, tags=None): |
|---|
| 124 |
if not title: |
|---|
| 125 |
title = datetime.now().strftime("%A, %H:%m") |
|---|
| 126 |
d = self.flickr.upload(path, title=title, tags=tags) |
|---|
| 127 |
d.addCallback(pprint.pprint) |
|---|
| 128 |
return d |
|---|
| 129 |
|
|---|
| 130 |
def _tagToPerson(self, tag): |
|---|
| 131 |
d = self._getPersonByTag(tag) |
|---|
| 132 |
d.addCallback(self._fillContacts) |
|---|
| 133 |
def _fillTag(person): |
|---|
| 134 |
person['rfid'] = tag |
|---|
| 135 |
return person |
|---|
| 136 |
d.addCallback(_fillTag) |
|---|
| 137 |
return d |
|---|
| 138 |
|
|---|
| 139 |
def _getPersonByTag(self, tag): |
|---|
| 140 |
d = self.remote.call("identity.search.uri", |
|---|
| 141 |
dict(uri='urn:rfid:%s'%(tag))) |
|---|
| 142 |
def _getId(rv): |
|---|
| 143 |
print "rv", rv |
|---|
| 144 |
if not rv: |
|---|
| 145 |
raise NotFoundException("no user found for tag %s"%(tag)) |
|---|
| 146 |
return rv[0]['thg_id'] |
|---|
| 147 |
|
|---|
| 148 |
d.addCallback(_getId) |
|---|
| 149 |
d.addCallback(self._getPersonByThing) |
|---|
| 150 |
return d |
|---|
| 151 |
|
|---|
| 152 |
def _getPersonByThing(self, thing): |
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
d = self.remote.call("picnic.persons.get", |
|---|
| 156 |
dict(id=thing)) |
|---|
| 157 |
def _translate(rv): |
|---|
| 158 |
rv['name'] = rv['name']['full'] |
|---|
| 159 |
return rv |
|---|
| 160 |
d.addCallback(_translate) |
|---|
| 161 |
return d |
|---|
| 162 |
|
|---|
| 163 |
def _fillContacts(self, person): |
|---|
| 164 |
print "rehere" |
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
d = self.remote.call("picnic.relations.getList", |
|---|
| 168 |
dict(id=person['id'])) |
|---|
| 169 |
def _really(rv): |
|---|
| 170 |
l = [] |
|---|
| 171 |
for c in rv: |
|---|
| 172 |
|
|---|
| 173 |
if c[1] == person['id']: |
|---|
| 174 |
contact_id = c[0] |
|---|
| 175 |
else: |
|---|
| 176 |
contact_id = c[1] |
|---|
| 177 |
l.append(self._getPersonByThing(contact_id)) |
|---|
| 178 |
return defer.gatherResults(l) |
|---|
| 179 |
d.addCallback(_really) |
|---|
| 180 |
def _fill(rv): |
|---|
| 181 |
person['contacts'] = rv |
|---|
| 182 |
return person |
|---|
| 183 |
d.addCallback(_fill) |
|---|
| 184 |
return d |
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
from twisted.web2 import iweb, http, resource, stream |
|---|
| 189 |
|
|---|
| 190 |
class WebRoot(resource.Resource): |
|---|
| 191 |
def __init__(self, controller): |
|---|
| 192 |
self.controller = controller |
|---|
| 193 |
self.child_current_tags = WebCurrentTags(self.controller) |
|---|
| 194 |
self.child_upload_photo = WebUploadPhoto(self.controller) |
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
class WebCurrentTags(resource.Resource): |
|---|
| 198 |
def __init__(self, controller): |
|---|
| 199 |
self.controller = controller |
|---|
| 200 |
|
|---|
| 201 |
def render(self, ctx): |
|---|
| 202 |
req = iweb.IRequest(ctx) |
|---|
| 203 |
s = stream.ProducerStream() |
|---|
| 204 |
|
|---|
| 205 |
d = self.controller.getCurrentTags() |
|---|
| 206 |
|
|---|
| 207 |
d.addCallback(simplejson.dumps) |
|---|
| 208 |
d.addCallback(s.write) |
|---|
| 209 |
d.addCallback(s.finish) |
|---|
| 210 |
|
|---|
| 211 |
return http.Response(stream=s) |
|---|
| 212 |
|
|---|
| 213 |
class WebUploadPhoto(resource.Resource): |
|---|
| 214 |
def __init__(self, controller): |
|---|
| 215 |
self.controller = controller |
|---|
| 216 |
|
|---|
| 217 |
def render(self, ctx): |
|---|
| 218 |
req = iweb.IRequest(ctx) |
|---|
| 219 |
pprint.pprint(req) |
|---|
| 220 |
tags = req.args.get("tags", [""])[0].encode("utf-8") |
|---|
| 221 |
path = req.args.get("path", [""])[0] |
|---|
| 222 |
title = req.args.get("title", [""])[0] |
|---|
| 223 |
s = stream.ProducerStream() |
|---|
| 224 |
|
|---|
| 225 |
d = self.controller.uploadPhoto(path, tags=tags, title=title) |
|---|
| 226 |
return http.Response(stream="[]") |
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
if __name__ == "__builtin__": |
|---|
| 238 |
application = service.Application('boozer') |
|---|
| 239 |
|
|---|
| 240 |
hub = Hub("photobooth.cfg") |
|---|
| 241 |
hub.setServiceParent(application) |
|---|
| 242 |
|
|---|
| 243 |
sonmicro = SonMicroMifareSensor() |
|---|
| 244 |
sonmicro.setServiceParent(hub) |
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
flickr = UploadService() |
|---|
| 250 |
flickr.setServiceParent(hub) |
|---|
| 251 |
|
|---|
| 252 |
echo = Echo() |
|---|
| 253 |
echo.setServiceParent(hub) |
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
lr = LineReceiver(2336) |
|---|
| 259 |
lr.setServiceParent(hub) |
|---|
| 260 |
|
|---|
| 261 |
lpr = LPRService() |
|---|
| 262 |
lpr.setServiceParent(hub) |
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
json = JSONService() |
|---|
| 268 |
json.setServiceParent(hub) |
|---|
| 269 |
|
|---|
| 270 |
controller = PhotoboothController(json, flickr) |
|---|
| 271 |
controller.setServiceParent(hub) |
|---|
| 272 |
|
|---|
| 273 |
from twisted.application import internet |
|---|
| 274 |
from twisted.web2 import server, channel |
|---|
| 275 |
webroot = WebRoot(controller) |
|---|
| 276 |
s = server.Site(webroot) |
|---|
| 277 |
w = internet.TCPServer(2567, |
|---|
| 278 |
channel.HTTPFactory(s), |
|---|
| 279 |
interface='0.0.0.0') |
|---|
| 280 |
|
|---|
| 281 |
w.setServiceParent(hub) |
|---|