Changeset 71
- Timestamp:
- 06/06/08 15:21:09 (6 months ago)
- Files:
-
- fizzjik/CHANGELOG.txt (modified) (1 diff)
- fizzjik/fizzjik/input/basic.py (modified) (1 diff)
- fizzjik/fizzjik/input/bluetooth.py (modified) (1 diff)
- fizzjik/fizzjik/input/btarduino.py (modified) (1 diff)
- fizzjik/fizzjik/input/easyident.py (modified) (1 diff)
- fizzjik/fizzjik/input/network.py (modified) (1 diff)
- fizzjik/fizzjik/input/rssystems.py (modified) (1 diff)
- fizzjik/fizzjik/input/sonmicro.py (modified) (1 diff)
- fizzjik/fizzjik/io (added)
- fizzjik/fizzjik/io/__init__.py (added)
- fizzjik/fizzjik/io/basic.py (added)
- fizzjik/fizzjik/io/bluetooth.py (added)
- fizzjik/fizzjik/io/browser.py (added)
- fizzjik/fizzjik/io/btarduino.py (added)
- fizzjik/fizzjik/io/easyident.py (added)
- fizzjik/fizzjik/io/growl.py (added)
- fizzjik/fizzjik/io/lpr.py (added)
- fizzjik/fizzjik/io/mplayer.py (added)
- fizzjik/fizzjik/io/network.py (added)
- fizzjik/fizzjik/io/rssystems.py (added)
- fizzjik/fizzjik/io/sonmicro.py (added)
- fizzjik/fizzjik/io/trac.py (added)
- fizzjik/fizzjik/output/basic.py (modified) (1 diff)
- fizzjik/fizzjik/output/browser.py (modified) (1 diff)
- fizzjik/fizzjik/output/growl.py (modified) (1 diff)
- fizzjik/fizzjik/output/lpr.py (modified) (1 diff)
- fizzjik/fizzjik/output/mplayer.py (modified) (1 diff)
- fizzjik/fizzjik/output/trac.py (modified) (1 diff)
- fizzjik/setup.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
fizzjik/CHANGELOG.txt
r17 r71 1 == 0.4 == 2008.06.06 2 * Moved modules in `input` and `output` to `io`, added warnings 1 3 2 4 == 0.3 == 2007.01.15 fizzjik/fizzjik/input/basic.py
r20 r71 1 from twisted.protocols import basic 2 from twisted.internet import protocol, task 1 import warnings 2 warnings.warn("fizzjik.input has been deprectaed, please use fizzjik.io instead", DeprecationWarning) 3 3 4 from twisted.application import internet 5 6 from fizzjik.interfaces import IInput, implements 7 from fizzjik.event import Event 8 from fizzjik.config import ConfigurableTCPServer, ConfigurableService, \ 9 if_config 10 11 12 class LineReceiverProtocol(basic.LineReceiver): 13 delimiter = "\n" 14 def lineReceived(self, line): 15 self.factory.observe(self.factory.builder(line.strip())) 16 17 18 class LineReceiverFactory(protocol.ServerFactory): 19 protocol = LineReceiverProtocol 20 def __init__(self, service): 21 self.service = service 22 23 @property 24 def builder(self): 25 return self.service.builder 26 27 def observe(self, evt): 28 self.service.observe(evt) 29 30 31 class LineReceiver(ConfigurableTCPServer): 32 implements(IInput) 33 34 port = 2336 35 factory = LineReceiverFactory 36 builder = Event 37 38 def observe(self, evt): 39 self.parent.observe(evt) 40 41 42 class Input(ConfigurableService): 43 implements(IInput) 44 45 def observe(self, evt): 46 self.parent.observe(evt) 47 48 class PollingInput(Input): 49 delay = 10 50 immediate = False 51 52 def __init__(self): 53 self.poller = task.LoopingCall(self.poll) 54 55 @if_config('enabled') 56 def startService(self): 57 Input.startService(self) 58 self.poller.start(self.delay, self.immediate) 59 60 @if_config('enabled') 61 def stopService(self): 62 Input.stopService(self) 63 self.poller.stop() 64 65 def poll(self): 66 self.observe(Event("POLL")) 4 from fizzjik.io.basic import * fizzjik/fizzjik/input/bluetooth.py
r32 r71 1 import re 1 import warnings 2 warnings.warn("fizzjik.input has been deprectaed, please use fizzjik.io instead", DeprecationWarning) 2 3 3 from twisted.internet import defer, task, reactor 4 from twisted.application import service 5 6 import lightblue 7 import lightblue.obex 8 9 10 from fizzjik.config import ConfigurableService, ConfigurableMixin, if_config 11 12 import AppKit 13 import Foundation 14 from PyObjCTools.AppHelper import NSDefaultRunLoopMode 15 16 17 class BluetoothSensor(ConfigurableService): 18 19 def __init__(self): 20 self.poller = task.LoopingCall(self.poll) 21 22 @if_config("enabled") 23 def startService(self): 24 ConfigurableService.startService(self) 25 self.poller.start(30, True) 26 #self.receiveFile() 27 28 @if_config("enabled") 29 def stopService(self): 30 ConfigurableService.stopService(self) 31 if self.poller.running: 32 self.poller.stop() 33 34 def poll(self): 35 print "POLL" 36 return self.findDevices(self.deviceFound) 37 38 def findDevices(self, foundDevice=None, timeout=10): 39 inquiry = lightblue.AsyncDeviceInquiry.alloc().init() 40 41 d = defer.Deferred() 42 43 def _completed(err, aborted): 44 print "COMPLETE" 45 d.callback((err, aborted)) 46 47 inquiry.cb_completed = _completed 48 inquiry.cb_founddevice = foundDevice 49 inquiry.length = timeout 50 51 started = inquiry.start() 52 53 if started != 0: 54 return defer.fail(Exception("Failed to start %s" % started)) 55 56 reactor.callLater(timeout, inquiry.stop) 57 58 return d 59 60 def receiveFile(self, channel=0): 61 sock = lightblue.socket() 62 sock.bind(("", channel)) 63 channelID = sock._getport() 64 65 print "receivee", sock, channelID 66 67 lightblue.advertise("LightBlue example OBEX service", sock, lightblue.OBEX) 68 69 server = lightblue.obex.OBEXServer.alloc().initWithChannel_(channelID) 70 71 d_connected = defer.Deferred() 72 d_disconnected = defer.Deferred() 73 d_putcompleted = defer.Deferred() 74 d_putrequested = defer.Deferred() 75 d_errored = defer.Deferred() 76 77 _fileobj = None 78 79 def _putrequested(session, filename, filetype, filesize): 80 print "req" 81 _fileobj = open("tmp.tmp", "wb") 82 filehandle = Foundation.NSFileHandle.alloc().initWithFileDescriptor_(f.fileno()) 83 d_putrequested.callback((session, filename, filetype, filesize)) 84 return filehandle 85 86 def _disconnected(session): 87 print 'disc' 88 d_disconnected.callback(session) 89 90 def _putcompleted(session): 91 print "aooa" 92 d_putcompleted.callback(session) 93 _fileobj.close() 94 95 def _errored(session, e, em): 96 print session, e, em 97 98 def _connected(session): 99 print session 100 d_connected.callback(session) 101 102 server.disconnected = _disconnected 103 server.putcompleted = _putcompleted 104 server.putrequested = _putrequested 105 server.errored = _errored 106 server.connected = _connected 107 108 #d_connected.addCallback(_putFile) 109 #d_connected.addErrback(_handleError) 110 #d_putcompleted.addCallback(_disconnect) 111 #d_putcompleted.addErrback(_handleError) 112 #d_disconnected.addBoth(_close) 113 114 server.start() 115 116 117 def sendFile(self, address, channel, source, timeout=10.0, connectTimeout=30.0): 118 device = self.getConnection(address) 119 client = lightblue.obex.OBEXClient.alloc().init() 120 121 122 d_connected = defer.Deferred() 123 d_disconnected = defer.Deferred() 124 d_putcompleted = defer.Deferred() 125 126 def _connected(e, em): 127 d_connected.callback((e, em)) 128 129 def _disconnected(e, em): 130 d_disconnected.callback((e, em)) 131 132 def _putcompleted(e, em): 133 d_putcompleted.callback((e, em)) 134 135 client.connected = _connected 136 client.disconnected = _disconnected 137 client.putcompleted = _putcompleted 138 139 def _putFile(rv): 140 client.putfile(source) 141 return True 142 143 def _handleError(e): 144 print "error", e 145 client.disconnect() 146 147 def _disconnect(e): 148 print "Discconnecting" 149 client.disconnect() 150 151 def _close(e): 152 print "CLOOSSE" 153 client.close() 154 device.closeConnection() 155 156 d_connected.addCallback(_putFile) 157 d_connected.addErrback(_handleError) 158 d_putcompleted.addCallback(_disconnect) 159 d_putcompleted.addErrback(_handleError) 160 d_disconnected.addBoth(_close) 161 162 try: 163 client.connect(address, channel) 164 except Exception, e: 165 device.closeConnection() 166 167 168 169 170 def getConnection(self, address): 171 return lightblue._IOBluetooth.IOBluetoothDevice.withAddress_( 172 lightblue._macutil.btaddrtochars(address)) 173 174 # callbacks 175 def deviceFound(self, device): 176 addr = device.getAddressString() 177 addr = addr.replace("-", ":").encode('ascii').upper() 178 name = device.getName() 179 cod = device.getClassOfDevice() 180 181 dev = {"address": addr, 182 "name": name, 183 "classOfDevice": cod, 184 "services": {}} 185 services = device.getServices() 186 if not services: services = [] 187 for s in services: 188 s_name = s.getServiceName() 189 result, channel = s.getRFCOMMChannelID_() 190 if result != lightblue._macutil.kIOReturnSuccess: 191 result, channel = s.getL2CAPPSM_() 192 if result != lightblue._macutil.kIOReturnSuccess: 193 channel = None 194 dev['services'][s_name] = {"channel": channel, "address": addr} 195 196 197 if "OBEX File Transfer" in dev['services']: 198 def _sendTest(): 199 print "SENDING" 200 source = u"hello.txt" 201 self.sendFile(dev['address'], dev['services']['OBEX File Transfer']['channel'], source) 202 print "Call me later" 203 #reactor.callLater(10, _sendTest) 204 #_sendTest() 205 206 print dev 207 #print sevices 208 209 if __name__ == "__builtin__": 210 from twisted.application import internet, service 211 212 application = service.Application("asdf") 213 214 bt = BluetoothSensor() 215 bt.setServiceParent(application) 4 from fizzjik.io.bluetooth import * fizzjik/fizzjik/input/btarduino.py
r48 r71 1 from twisted.internet import defer 2 import re 3 from fizzjik.serial import SerialPortClient, SerialPortProtocol 4 from fizzjik.rfid import TagAddedEvent, TagPresentEvent, TagRemovedEvent 5 from fizzjik.config import if_config 1 import warnings 2 warnings.warn("fizzjik.input has been deprectaed, please use fizzjik.io instead", DeprecationWarning) 6 3 7 class BTArduinoSensor(SerialPortClient): 8 def __init__(self, *args, **kw): 9 self.outputs = (0, 0) 10 SerialPortClient.__init__(self, BTArduinoSensorProtocol, *args, **kw) 11 12 class BTArduinoSensorProtocol(SerialPortProtocol): 13 """only senses one tag at a time""" 14 baudrate = 115200 15 16 buffer = "" 17 parent = None 18 19 _deferred = None 20 21 TAG_RE = re.compile(r"""\s*(\S{8})\s*""") 22 23 def __init__(self, parent): 24 self.parent = parent 25 self.timers = {} 26 27 def dataReceived(self, data): 28 print "RECV: ", "BYTES[%s]"%(", ".join(["%X"%(ord(x)) for x in data])) 29 self.buffer += data 30 31 match = self.TAG_RE.match(self.buffer) 32 if match: 33 self.buffer = self.buffer[len(match.group(0)):] 34 tag = match.group(1) 35 self._tagSensed(tag) 36 37 def _tagSensed(self, tag): 38 if tag not in self.timers: 39 self._tagAdded(tag) 40 elif self.timers[tag].called: 41 self._tagAdded(tag) 42 else: 43 self._tagPresent(tag) 44 45 def _tagPresent(self, tag): 46 evt = TagPresentEvent(tag) 47 self.bumpTimer(self._tagRemoved, tag, tag) 48 self.parent.observe(evt) 49 50 def _tagAdded(self, tag): 51 self.last = tag 52 self.bumpTimer(self._tagRemoved, tag, tag) 53 evt = TagAddedEvent(tag) 54 self.parent.observe(evt) 55 56 def _tagRemoved(self, tag): 57 self.clearTimer(tag) 58 self.last = None 59 evt = TagRemovedEvent(tag) 60 self.parent.observe(evt) 61 62 def clearTimer(self, which): 63 if which in self.timers and not self.timers[which].called: 64 self.timers[which].cancel() 65 del self.timers[which] 66 67 def bumpTimer(self, cb, which, *args): 68 if which in self.timers and not self.timers[which].called: 69 self.timers[which].cancel() 70 # XXX andy: okay, this is sort of a hack due to the weirdness that 71 # that is the serialport connector 72 self.timers[which] = self.transport.reactor.callLater(0.3, cb, *args) 73 4 from fizzjik.io.btarduino import * fizzjik/fizzjik/input/easyident.py
r17 r71 1 import re 1 import warnings 2 warnings.warn("fizzjik.input has been deprectaed, please use fizzjik.io instead", DeprecationWarning) 2 3 3 from fizzjik.serial import SerialPortClient, SerialPortProtocol 4 from fizzjik.rfid import TagAddedEvent, TagPresentEvent, TagRemovedEvent 5 6 class EasyIdentSensor(SerialPortClient): 7 def __init__(self, *args, **kw): 8 SerialPortClient.__init__(self, EasyIdentSensorProtocol, *args, **kw) 9 10 class EasyIdentSensorProtocol(SerialPortProtocol): 11 """only senses one tag at a time""" 12 baudrate = 9600 13 14 buffer = "" 15 last = None 16 timer = None 17 parent = None 18 19 TAG = re.compile("""\s*(\S{11})\s*""") 20 21 def __init__(self, parent): 22 self.parent = parent 23 24 def dataReceived(self, data): 25 data = re.sub("""\x8c""", "", data) 26 self.buffer += data 27 #print "recv", data, self.buffer 28 m = self.TAG.match(self.buffer) 29 if m: 30 self.buffer = self.buffer[len(m.group(0)):] 31 #self.buffer = "" 32 tag = m.group(1) 33 34 if tag == self.last: 35 self._tagPresent(tag) 36 else: 37 if not self.last: 38 self._tagAdded(tag) 39 else: 40 self._tagRemoved(self.last) 41 self._tagAdded(tag) 42 43 def _tagPresent(self, tag): 44 evt = TagPresentEvent(tag) 45 self.bumpTimer(tag) 46 self.parent.observe(evt) 47 48 def _tagAdded(self, tag): 49 self.last = tag 50 self.bumpTimer(tag) 51 evt = TagAddedEvent(tag) 52 self.parent.observe(evt) 53 54 def _tagRemoved(self, tag): 55 self.clearTimer() 56 self.last = None 57 evt = TagRemovedEvent(tag) 58 self.parent.observe(evt) 59 60 def clearTimer(self): 61 if self.timer and not self.timer.called: 62 self.timer.cancel() 63 self.timer = None 64 65 def bumpTimer(self, tag): 66 if self.timer and not self.timer.called: 67 self.timer.cancel() 68 # XXX andy: okay, this is sort of a hack due to the weirdness that 69 # that is the serialport connector 70 self.timer = self.transport.reactor.callLater(0.3, self._tagRemoved, 71 tag) 72 73 74 4 from fizzjik.io.easyident import * fizzjik/fizzjik/input/network.py
r19 r71 1 from twisted.internet import task, defer 2 from twisted.web import client 1 import warnings 2 warnings.warn("fizzjik.input has been deprectaed, please use fizzjik.io instead", DeprecationWarning) 3 3 4 from fizzjik.input.basic import PollingInput 5 from fizzjik.event import InputAddedEvent, InputPresentEvent, InputRemovedEvent 6 7 class NetworkConnectionAddedEvent(InputAddedEvent): 8 pass 9 10 class NetworkConnectionPresentEvent(InputPresentEvent): 11 pass 12 13 class NetworkConnectionNotPresentEvent(InputPresentEvent): 14 pass 15 16 class NetworkConnectionRemovedEvent(InputRemovedEvent): 17 pass 18 19 class NetworkConnectionSensor(PollingInput): 20 destination = "http://term.ie" 21 timeout = 10 22 23 status = True 24 25 def poll(self): 26 self._fetch() 27 28 def _fetch(self): 29 d = client.getPage(self.destination, timeout=self.timeout) 30 d.addCallbacks(self._fetchSuccess, self._fetchFailure) 31 return d 32 33 def _fetchSuccess(self, o): 34 if self.status: 35 self.observe(NetworkConnectionPresentEvent(True)) 36 else: 37 self.status = True 38 self.observe(NetworkConnectionAddedEvent(True)) 39 40 def _fetchFailure(self, o): 41 if self.status: 42 self.status = False 43 self.observe(NetworkConnectionRemovedEvent(o)) 44 else: 45 self.observe(NetworkConnectionNotPresentEvent(o)) 4 from fizzjik.io.network import * fizzjik/fizzjik/input/rssystems.py
r17 r71 1 import re 1 import warnings 2 warnings.warn("fizzjik.input has been deprectaed, please use fizzjik.io instead", DeprecationWarning) 2 3 3 from fizzjik.serial import SerialPortClient, SerialPortProtocol 4 from fizzjik.rfid import TagAddedEvent, TagPresentEvent, TagRemovedEvent 5 6 class RSSystemsSensor(SerialPortClient): 7 def __init__(self, *args, **kw): 8 SerialPortClient.__init__(self, RSSystemsSensorProtocol, *args, **kw) 9 10 class RSSystemsSensorProtocol(SerialPortProtocol): 11 """only senses one tag at a time""" 12 baudrate = 4800 13 14 buffer = "" 15 last = None 16 timer = None 17 parent = None 18 19 TAG = re.compile("""\s*(\S{10})\s*""") 20 21 def __init__(self, parent): 22 self.parent = parent 23 24 def dataReceived(self, data): 25 26 27 #data = re.sub("""\x8c""", "", data) 28 self.buffer += data 29 #print "recv", data, self.buffer 30 m = self.TAG.match(self.buffer) 31 if m: 32 self.buffer = self.buffer[len(m.group(0)):] 33 #self.buffer = "" 34 tag = m.group(1) 35 36 dec2hex = ('0', '1', '2', '3', 37 '4', '5', '6', '7', 38 '8', '9', 'A', 'B', 39 'C', 'D', 'E', 'F') 40 #print [ord(x) - 48 for x in tag] 41 try: 42 tag = "".join([dec2hex[ord(x) - 48] for x in tag]) 43 except: 44 return 45 46 47 if tag == self.last: 48 self._tagPresent(tag) 49 else: 50 if not self.last: 51 self._tagAdded(tag) 52 else: 53 self._tagRemoved(self.last) 54 self._tagAdded(tag) 55 56 def _tagPresent(self, tag): 57 evt = TagPresentEvent(tag) 58 self.bumpTimer(tag) 59 self.parent.observe(evt) 60 61 def _tagAdded(self, tag): 62 self.last = tag 63 self.bumpTimer(tag) 64 evt = TagAddedEvent(tag) 65 self.parent.observe(evt) 66 67 def _tagRemoved(self, tag): 68 self.clearTimer() 69 self.last = None 70 evt = TagRemovedEvent(tag) 71 self.parent.observe(evt) 72 73 def clearTimer(self): 74 if self.timer and not self.timer.called: 75 self.timer.cancel() 76 self.timer = None 77 78 def bumpTimer(self, tag): 79 if self.timer and not self.timer.called: 80 self.timer.cancel() 81 # XXX andy: okay, this is sort of a hack due to the weirdness that 82 # that is the serialport connector 83 self.timer = self.transport.reactor.callLater(0.3, self._tagRemoved, 84 tag) 85 86 87 4 from fizzjik.io.rssystems import * fizzjik/fizzjik/input/sonmicro.py
r34 r71 1 from twisted.internet import defer 1 import warnings 2 warnings.warn("fizzjik.input has been deprectaed, please use fizzjik.io instead", DeprecationWarning) 2 3 3 from fizzjik.serial import SerialPortClient, SerialPortProtocol 4 from fizzjik.rfid import TagAddedEvent, TagPresentEvent, TagRemovedEvent 5 from fizzjik.event import InputAddedEvent, InputPresentEvent, InputRemovedEvent 6 from fizzjik.config import if_config 7 8 class MifareInputPresentEvent(InputPresentEvent): 9 def __init__(self, data, port1, port2): 10 self.port1 = port1 11 self.port2 = port2 12 super(MifareInputPresentEvent, self).__init__(data) 13 14 class MifareInputAddedEvent(InputAddedEvent): 15 def __init__(self, data, port1, port2): 16 self.port1 = port1 17 self.port2 = port2 18 super(MifareInputAddedEvent, self).__init__(data) 19 20 class MifareInputRemovedEvent(InputRemovedEvent): 21 def __init__(self, data, port1, port2): 22 self.port1 = port1 23 self.port2 = port2 24 super(MifareInputRemovedEvent, self).__init__(data) 25 26 class SonMicroMifareSensor(SerialPortClient): 27 def __init__(self, *args, **kw): 28 self.outputs = (0, 0) 29 SerialPortClient.__init__(self, SonMicroMifareSensorProtocol, *args, **kw) 30 31 @if_config("enabled") 32 def startService(self): 33 SerialPortClient.startService(self) 34 self._startReadLoop() 35 36 @if_config("enabled") 37 def stopService(self): 38 if self.running: 39 self._stopReadLoop() 40 SerialPortClient.stopService(self) 41 42 def _startReadLoop(self): 43 self._connection.protocol._startReadLoop() 44 45 def _stopReadLoop(self): 46 self._connection.protocol._stopReadLoop() 47 48 class SonMicroMifareSensorProtocol(SerialPortProtocol): 49 """only senses one tag at a time""" 50 baudrate = 57600 51 52 buffer = "" 53 last = None 54 parent = None 55 56 has_inputs = True 57 has_outputs = True 58 59 _deferred = None 60 61 SELECT_TAG = (0xFF, 0x00, 0x01, 0x83, 0x84) 62 READ_INPUT_PORT = (0xFF, 0x00, 0x01, 0x91, 0x92) 63 RESET = (0xFF, 0x00, 0x01, 0x80, 0x81) 64 65 def __init__(self, parent): 66 self.parent = parent 67 self._currentOutputs = (0, 0) 68 self._currentInputs = (0, 0) 69 self.timers = {} 70 71 def dataReceived(self, data): 72 #print "RECV: ", "BYTES[%s]"%(", ".join(["%X"%(ord(x)) for x in data])) 73 self.buffer += data 74 75 try: 76 if len(self.buffer) < 4: 77 return 78 if ord(self.buffer[3]) == 0x83 and ord(self.buffer[2]) > 0x02: 79 if len(self.buffer) < 10: 80 return 81 if len(self.buffer) < 6: 82 return 83 except Exception, e: 84 import traceback 85 traceback.print_exc() 86 self.buffer = "" 87 return 88 89 90 if self._deferred: 91 self._deferred.callback(self.buffer); 92 self.buffer = "" 93 #print " ".join([ord(x) for x in data]).encode('utf-8') 94 #data = re.sub("""\x8c""", "", data) 95 #self.buffer += data 96 ##print "recv", data, self.buffer 97 #m = self.TAG.match(self.buffer) 98 #if m: 99 # self.buffer = self.buffer[len(m.group(0)):] 100 # #self.buffer = "" 101 # tag = m.group(1) 102 103 # if tag == self.last: 104 # self._tagPresent(tag) 105 # else: 106 # if not self.last: 107 # self._tagAdded(tag) 108 # else: 109 # self._tagRemoved(self.last) 110 # self._tagAdded(tag) 111 112 def _startReadLoop(self): 113 #self.writeBytes(self.RESET) 114 self._doLoop(self) 115 116 def _readTag(self): 117 d = defer.Deferred() 118 d.addCallback(self._parseReadTagResponse) 119 self._deferred = d 120 self.writeBytes(self.SELECT_TAG) 121 return d 122 123 def _parseReadTagResponse(self, data): 124 if ord(data[2]) == 0x02: 125 return None 126 else: 127 tag = "".join(["%02X"%(ord(x)) for x in data[5:9]]) 128 self._tagSensed(tag) 129 return None 130 131 def _readInput(self): 132 d = defer.Deferred() 133 d.addCallback(self._parseReadInputResponse) 134 self._deferred = d 135 self.writeBytes(self.READ_INPUT_PORT) 136 return d 137 138 def _parseReadInputResponse(self, data): 139 if ord(data[4]) == 0x00: 140 return None 141 else: 142 raw = ord(data[4]) 143 ports = [raw & 0x01, raw & 0x02 and 1 or 0] 144 self._inputSensed(ports) 145 return None 146 147 def _enjoyOutput(self): 148 if self.parent.outputs != self._currentOutputs: 149 d = defer.Deferred() 150 bytes = [0xFF, 0x00, 0x02, 0x92] 151 outputByte = 0x00 152 if self.parent.outputs[0]: 153 outputByte += 0x01 154 if self.parent.outputs[1]: 155 outputByte += 0x02 156 bytes.append(outputByte) 157 bytes.append(0x94 + outputByte) 158 d.addCallback(self._parseEnjoyOutputResponse) 159 self._deferred = d 160 self.writeBytes(bytes) 161 return d 162 else: 163 return None 164 165 def _parseEnjoyOutputResponse(self, data): 166 outputs = ord(data[4]) 167 self._currentOutputs = (outputs & 0x01, outputs & 0x02 and 1 or 0) 168 return 169 170 def _doLoop(self, foo=None): 171 if not self.parent.running: 172 return 173 174 175 d = self._readTag() 176 if self.has_inputs: 177 d.addCallback(lambda _: self._readInput()) 178 if self.has_outputs: 179 d.addCallback(lambda _: self._enjoyOutput()) 180 d.addCallback(lambda _: self._doLoop()) 181 def _pr(s): 182 print s 183 d.addErrback(_pr) 184 185 #d = defer.Deferred() 186 #d.addCallback(lambda _: self.writeBytes 187 188 ##self.writeBytes(self.SELECT_TAG) 189 #self.writeBytes(self.READ_INPUT_PORT) 190 #self.transport.reactor.callLater(3, self._doRead) 191 192 193 def _stopReadLoop(self): 194 pass 195 196 def _tagSensed(self, tag): 197 if tag not in self.timers: 198 self._tagAdded(tag) 199 elif self.timers[tag].called: 200 self._tagAdded(tag) 201 else: 202 self._tagPresent(tag) 203 204 def _tagPresent(self, tag): 205 evt = TagPresentEvent(tag) 206 self.bumpTimer(self._tagRemoved, tag, tag) 207 self.parent.observe(evt) 208 209 def _tagAdded(self, tag): 210 self.last = tag 211 self.bumpTimer(self._tagRemoved, tag, tag) 212 evt = TagAddedEvent(tag) 213 self.parent.observe(evt) 214 215 def _tagRemoved(self, tag): 216 self.clearTimer(tag) 217 self.last = None 218 evt = TagRemovedEvent(tag) 219 self.parent.observe(evt) 220 221 def _inputSensed(self, ports): 222 if self._currentInputs == ports: 223 self._inputPresent(ports) 224 else: 225 self._inputAdded(ports) 226 227 def _inputPresent(self, ports): 228 evt = MifareInputPresentEvent(ports, ports[0], ports[1]) 229 self.bumpTimer(self._inputRemoved, 'input', ports) 230 self.parent.observe(evt) 231 232 def _inputAdded(self, ports): 233 self._currentInputs = ports 234 evt = MifareInputAddedEvent(ports, ports[0], ports[1]) 235 self.bumpTimer(self._inputRemoved, 'input', ports) 236 self.parent.observe(evt) 237 238 def _inputRemoved(self, ports): 239 self._currentInputs = (0, 0) 240 evt = MifareInputRemovedEvent(ports, ports[0], ports[1]) 241 self.clearTimer('input') 242 self.parent.observe(evt) 243 244 245 246 def clearTimer(self, which): 247 if which in self.timers and not self.timers[which].called: 248 self.timers[which].cancel() 249 del self.timers[which] 250 251 def bumpTimer(self, cb, which, *args): 252 if which in self.timers and not self.timers[which].called: 253 self.timers[which].cancel() 254 # XXX andy: okay, this is sort of a hack due to the weirdness that 255 # that is the serialport connector 256 self.timers[which] = self.transport.reactor.callLater(0.3, cb, *args) 257 4 from fizzjik.io.basic import * fizzjik/fizzjik/output/basic.py
r17 r71 1 from twisted.application import service 1 import warnings 2 warnings.warn("fizzjik.output has been deprectaed, please use fizzjik.io instead", DeprecationWarning) 2 3 3 from fizzjik.interfaces import IOutput, IController, implements 4 from fizzjik.event import Event 5 from fizzjik.config import ConfigurableService, if_config 6 7 class Output(ConfigurableService): 8 implements(IOutput) 9 pass 10 11 12 class Echo(Output): 13 implements(IController) 14 15 def registerObservers(self, hub): 16 hub.addObserver(Event, self._echo) 17 18 @if_config('enabled') 19 def _echo(self, evt): 20 print evt 4 from fizzjik.io.basic import * fizzjik/fizzjik/output/browser.py
r17 r71 1 import os 1 import warnings 2 warnings.warn("fizzjik.output has been deprectaed, please use fizzjik.io instead", DeprecationWarning) 2 3 3 from twisted.application import service 4 from twisted.internet import protocol, reactor 5 6 7 from fizzjik.interfaces import IOutput, IConfigurable, implements 8 from fizzjik.config import ConfigurableMixin, if_config 9 10 class BrowserProcessProtocol(protocol.ProcessProtocol): 11 posix_exec = 'firefox' 12 posix_args = ['firefox'] 13 14 mac_exec = "open" 15 mac_args = ["open"] 16 17 def __init__(self, platform="posix"): 18 self.platform = platform 19 20 def connectionMade(self): 21 print "connection made..." 22 23 def spawn(self, *args): 24 self.running = True 25 reactor.spawnProcess(self, self.getExec(), args=self.getArgs()+list(args), env=os.environ) 26 27 def kill(self): 28 self.transport.signalProcess('KILL') 29 30 def processEnded(self, reason): 31 print reason 32 33 def outReceived(self, line): 34 print "\n".join(["out >> %s"%(x) for x in line.splitlines()]) 35 36 def errReceived(self, line): 37 print "\n".join(["err !! %s"%(x) for x in line.splitlines()]) 38 39 def getExec(self): 40 return getattr(self, '%s_exec'%(self.platform)) 41 42 def getArgs(self): 43 return getattr(self, '%s_args'%(self.platform)) 44 45 def browse(self, url): 46 print "url", url 47 self.spawn(url) 48 49 class BrowserService(service.Service, ConfigurableMixin): 50 implements(IOutput) 51 52 enabled = True 53 platform = "posix" 54 55 @if_config('enabled') 56 def startService(self): 57 print "broaaz" 58 self.browser = BrowserProcessProtocol(self.platform) 59 pass 60 61 def stopService(self): 62 pass 63 64 def browse(self, url): 65 self.browser.browse('%s'%url) 66 67 68 def stop(self): 69 self.browser.stop() 70 71 4 from fizzjik.io.browser import * fizzjik/fizzjik/output/growl.py
r17 r71 1 import os 1 import warnings 2 warnings.warn("fizzjik.output has been deprectaed, please use fizzjik.io instead", DeprecationWarning) 2 3 3 from twisted.application import service 4 from twisted.internet import protocol, reactor 5 6 from Growl import GrowlNotifier 7 8 from fizzjik.interfaces import IOutput, IConfigurable, implements 9 from fizzjik.config import ConfigurableMixin, if_config 10 11 class GrowlService(service.Service, ConfigurableMixin): 12 implements(IOutput) 13 14 enabled = True 15 platform = "darwin" 16 17 name = "fizzjik" 18 notifications = None 19 20 def __init__(self): 21 self.notifications = ["event"] 22 23 def _config_notifications(self, value): 24 self.notifications = value.split(",") 25 26 @if_config('enabled') 27 def startService(self): 28 service.Service.startService(self) 29 self.notifier = GrowlNotifier(self.name, self.notifications) 30 self.notifier.register() 31 32 def stopService(self): 33 service.Service.stopService(self) 34 35 def notify(self, *args, **kw): 36 self.notifier.notify(*args, **kw) 37 38 def defaultNotify(self, event): 39 self.notify("event", title=event.__class__.__name__, description=event.data) 4 from fizzjik.io.growl import * fizzjik/fizzjik/output/lpr.py
r25 r71 1 import os 1 import warnings 2 warnings.warn("fizzjik.output has been deprectaed, please use fizzjik.io instead", DeprecationWarning) 2 3 3 from fizzjik.interfaces import IOutput, implements 4 from fizzjik.process import ProcessService, ProcessProtocol 5 6 class LPRProcessProtocol(ProcessProtocol): 7 posix_exec = 'lpr' 8 posix_args = ['lpr'] 9 10 def print_(self, f, *args): 11 self.spawn(*args) 12 data = f.read() 13 self.transport.write(data) 14 self.transport.closeStdin() 15 16 class LPRService(ProcessService): 17 implements(IOutput) 18 enabled = True 19 platform = "posix" 20 protocol = LPRProcessProtocol 21 really_print = False 22 23 def print_(self, f, *args): 24 if self.really_print: 25 self.process.print_(f, *args) 26 else: 27 print "would be printing:", 28 print f.name 4 from fizzjik.io.lpr import * fizzjik/fizzjik/output/mplayer.py
r17 r71 1 import os 1 import warnings 2 warnings.warn("fizzjik.output has been deprectaed, please use fizzjik.io instead", DeprecationWarning) 2 3 3 from twisted.application import service 4 from twisted.internet import protocol, reactor 5 6 7 from fizzjik.interfaces import IMediaOutput, IConfigurable, implements 8 from fizzjik.config import ConfigurableMixin, if_config 9 10 class MPlayerProcessProtocol(protocol.ProcessProtocol): 11 posix_exec = 'mplayer' 12 posix_args = ['mplayer', '-slave', '-quiet', '-fs'] 13 mac_exec = '/Applications/MPlayer OSX.app/Contents/Resources/External_Binaries/mplayer.app/Contents/MacOS/mplayer' 14 mac_args = ['/Applications/MPlayer OSX.app/Contents/Resources/External_Binaries/mplayer.app/Contents/MacOS/mplayer', 15 '-slave', '-vo', 'quartz', '-fs', '-quiet'] 16 17 running = False 18 last = None 19 state = 0 20 PLAYING = 1 21 PAUSED = 2 22 STOPPED = 0 23 WAITING = -1 24 25 def __init__(self, platform="posix"): 26 self.platform = platform 27 28 def connectionMade(self): 29 print "connection made..." 30 31 def spawn(self, *args): 32 <
