Example #1
0
File: web.py Project: benrady/roost
      service = roost.services.find(request.postpath[0])
      if request.postpath[1] == 'properties':
        return json.dumps(service.properties.export())

class EventsWebsocket(websocket.WebSocketServerProtocol):
  def onMessage(self, msg, binary):
    roost.listen_to(msg, self.on_event)

  def on_event(self, event, data):
    self.sendMessage(json.dumps({'event': event, 'eventData': data}), False)

class Web(service.Service):
  def __init__(self, opts):
    self.setName('web')
    self.web_dir = opts.get('web-dir')
    self.port = int(opts.get('port'))

  def properties(self):
    return {}

  def startService(self):
    service.Service.startService(self)
    root = static.File(self.web_dir) 
    root.putChild('services', ServiceResource())
    self.server = internet.TCPServer(self.port, server.Site(root)).setServiceParent(self.parent)
    factory = websocket.WebSocketServerFactory("ws://localhost:9090")
    factory.protocol = EventsWebsocket
    websocket.listenWS(factory)

roost.add_service(Web)
Example #2
0
from twisted.application import service
import time
import roost
from roost import properties

class DoorbellService(service.Service):
  def __init__(self, opts={}):
    self.last_notification = None
    self.setName('doorbell')

  def on_data(self, event, data):
    if any([s.get('dio-0', False) for s in data['samples']]):
      if not self.last_notification or time.time() - self.last_notification > 10:
        roost.notify('Doorbell', {'sound':'pushover'})
        self.last_notification = time.time()

  def startService(self):
    roost.listen_to('xbee.data', self.on_data)
    service.Service.startService(self)

roost.add_service(DoorbellService)
Example #3
0
# https://pushover.net/api
class PushoverService(service.Service):
  def __init__(self, opts={}):
    self.setName('pushover')

    # FIXME Duplicated in env_sensors.py
    propfile = None
    if opts.has_key('data-dir'):
      propfile = opts.get('data-dir') + '/' + self.name + '/properties'
    self.properties = properties.Properties(propfile, defaults={'users':[]})

  def _send(self, message, opts):
    for user in self.properties['users']:
      httpclient.post('https://api.pushover.net/1/messages.json', **dict(opts, **{
        'user': user,
        'message': message,
        'token': self.properties['token']
        }))

  def send_message(self, msg, opts={}):
    """Sends a low priority message to all registered devices"""
    self._send(msg, opts)

  def set_api_token(self, token):
    self.properties['token'] = token

  def add_user(self, user_id):
    self.properties['users'].append(user_id)

roost.add_service(PushoverService)
Example #4
0
        self.reader.handle_packet(pickle.load(device))

  def _schedule_test_data(self, data_dir):
    l = task.LoopingCall(self._publish_test_data, data_dir)
    l.start(1.0)

  def startService(self):
    service.Service.startService(self)
    if os.path.exists(self.device):
      if os.path.isdir(self.device):
        self._schedule_test_data(self.device)
      else:
        self.port = SerialPort(self.reader, self.device, reactor, baudrate=9600)
    else:
      log.msg("Could not find device or directory" + self.device)

  def get_sources(self):
    return self.sources

  # http://www.digi.com/support/kbase/kbaseresultdetl?id=3221
  # https://code.google.com/p/python-xbee/source/browse/xbee/zigbee.py#35
  def send_at_command(self, device_addr, command):
    source = self.sources[device_addr]
    return reactor.callFromThread(self.reader.send,
      'remote_at', 
      dest_addr_long=source['source_addr_long'], 
      dest_addr=source['source_addr'], 
      command=command)

roost.add_service(XBeeService)
Example #5
0
      'pin': 'adc-1'
    })

  def _read_humidity(self, pin):
    return {'humidity': (pin - 0.22) * 0.073632 }

  def _read_analog_pin(self, pin):
    return 1200 * (pin / 1023.0)

  def _read_pins(self, samples):
    reading = {'lastUpdate': _now_millis()}
    for sample in samples:
      volts = self._read_analog_pin(sample[self.properties['pin']])
      reading.update(self._read_humidity(volts))
    return reading

  def on_data(self, event, data):
    """Event handler for incoming XBee data"""
    reading = self._read_pins(data['samples'])
    self.properties.update_in('sources', data['source'], 'reading', reading)

  def sources(self):
    """Returns the source objects that repesent the sources this service has been notified of"""
    return self.properties['sources']

  def startService(self):
    service.Service.startService(self)
    roost.listen_to('xbee.data', self.on_data)

roost.add_service(HumiditySensor)