def start(self): if self.brivo_account is None: self.brivo_account = BrivoId({'type':'brivo_id', 'value':self.brivo_id_value}) self.event_path = '/brivo/'+self.name self.dispatcher = BrivoDispatcher() if self.use_https: server = '/services/network/https_server' else: server = '/services/network/http_server' self.dispatcher.configure({'request_path':self.event_path, 'parent':server, 'name':'brivo_%s_handler' % urllib.encode(self.name)}) self.dispatcher.start() self.__pdo_sids = _PersistentSubscriptionIds(self) self._subscriptions = self.__pdo_sids.array[:] self.children_nodes() # force device discovery ... self.register_for_events() super(Account, self).start() self.get_all_events() self.running = True return
class Account(CompositeNode, AutoDiscoveredNode): def __init__(self, accnt_id=None): if accnt_id is not None: self.brivo_account = BrivoId(accnt_id) self.brivo_id_value = accnt_id.get('value') else: self.brivo_account = None self.running = False self.__server = None self._discovered = False super(Account, self).__init__(self) return def configure(self, cd): super(Account, self).configure(cd) # the email property is used when we register for events - if the # OnSite panel has problems communicating events to us via XML-RPC # then the 'email' recipient is notificed set_attribute(self, 'email', self.parent.email, cd) if cd.has_key('brivo_id_value') or self.brivo_account is None: set_attribute(self, 'brivo_id_value', REQUIRED, cd) return def configuration(self): cd = super(Account, self).configuration() get_attribute(self, 'email', cd) get_attribute(self, 'brivo_id_value', cd) return cd def start(self): if self.brivo_account is None: self.brivo_account = BrivoId({'type':'brivo_id', 'value':self.brivo_id_value}) self.event_path = '/brivo/'+self.name self.dispatcher = BrivoDispatcher() if self.use_https: server = '/services/network/https_server' else: server = '/services/network/http_server' self.dispatcher.configure({'request_path':self.event_path, 'parent':server, 'name':'brivo_%s_handler' % urllib.encode(self.name)}) self.dispatcher.start() self.__pdo_sids = _PersistentSubscriptionIds(self) self._subscriptions = self.__pdo_sids.array[:] self.children_nodes() # force device discovery ... self.register_for_events() super(Account, self).start() self.get_all_events() self.running = True return def stop(self): for sid in self._subscriptions: self.server.delete_event_subscription(self.brivo_account, BrivoId({'type':'brivo_id', 'value':sid})) self.dispatcher.stop() self.dispather.prune() return def _discover_children(self): answer = {} if not self._discovered and self.running is True and \ self.parent.discovery_mode == 'once': existing_devices = [] for dev in self.children_nodes({'auto_discover':False}): #self._get_children().values() existing_devices.append(accnt.configuration.get('brivo_id_value')) devs = self.server.list_devices(self.brivo_account) for dev in devs: brivo_dev = self.server.retrieve_device(self.brivo_account, dev) brivo_dev_id = brivo_dev.get('id').get('value') if brivo_dev_id not in existing_devices: # add newly discovered devices brivo_dev_name = brivo_dev.get('name') #names = [] #names.extend(self._get_children().keys()) #children_names if brivo_dev_name in self.children_names({'auto_discover':False}): msg = 'Skipping auto-creation - node %s already exists in account %s' % \ (brivo_dev_name, self.name) msglog.log('Brivo', WARN, msg) continue answer[brivo_dev_name] = Device(brivo_dev) self._discovered = True return answer def get_all_events(self): evts = self.server.list_events(self.brivo_account) # sort events based on when they occurred (occurred == XML-RPC DateTime) evts.sort(lambda x,y: cmp(x.get('occurred'), y.get('occurred'))) for evt in evts: self.dispatcher.distribute(BrivoEvent(evt)) return def register_for_events(self): for sid in self._subscriptions: # remove any existing subscriptions self.server.delete_event_subscription(self.brivo_account, BrivoId({'type':'brivo_id', 'value':sid})) self._subscriptions = [] i = 0 for dev in self.children_nodes(): # register interest in events from each device id = BrivoId({'type':'brivo_id', 'value':str(i)}) criteria = BrivoCriteria({'keyword':'device_id', 'operator':'eq', 'value':BrivoId({'type':'brivo_id', 'value':dev.brivo_id_value})}) sid = BrivoEventSubscription({'id':id, 'name':'dev_%s_subscr' % dev.name, 'url':self.event_path, 'string':self.email, 'criteria':criteria}) self._subscriptions.append(sid) i += 1 self._save_subscriptions() return def _save_subscriptions(self): self.__pdos_sids.array = self._subscriptions[:] self.__pdos.save() return def __get_server(self): if self.__server is None: self.__server = self.parent return self.__server server = property(__get_server)