Example #1
0
 def __init__(self):
     self.nodes = dict()
     self.clients_pubsub = PubSub(self, pub_port=settings.CLIENT_SUB, sub_port=settings.CLIENT_PUB, broadcast=False)
     self.nodes_pubsub = PubSub(self, pub_port=settings.NODE_SUB, sub_port=settings.NODE_PUB, parse_message=self.parse_message)
     self.logger = util.get_logger("%s.%s" % (self.__module__, self.__class__.__name__))
     Logger(self)
     self.run()
Example #2
0
 def __init__(self, name, *args, **kwargs):
     self.name = name
     self.initializing = True
     if LIVE: self.interface_kit = InterfaceKit()
     self.manager = PubSub(self, pub_port=settings.NODE_PUB, sub_port=settings.NODE_SUB, sub_filter=self.name)
     self.logger = util.get_logger("%s.%s" % (self.__module__, self.__class__.__name__))
     self.initialize()    
     self.run()
Example #3
0
class Node(object):

    name = None
    sensors = []
    outputs = []
    inputs = []
    triggers = []
    clocks = []
    repeaters = []
    interface_kit = None

    def __init__(self, name, *args, **kwargs):
        self.name = name
        self.initializing = True
        if LIVE: self.interface_kit = InterfaceKit()
        self.manager = PubSub(self, pub_port=settings.NODE_PUB, sub_port=settings.NODE_SUB, sub_filter=self.name)
        self.logger = util.get_logger("%s.%s" % (self.__module__, self.__class__.__name__))
        self.initialize()    
        self.run()

    def initialize(self):
        while self.initializing:            
            self.logger.info("Waiting for manager")
            json = dict(name=self.name, method='add_node')
            self.publish(json)
            gevent.sleep(1)
        return

    def publish(self, message):
        message['name'] = self.name
        message['method'] = message.get('method', 'node_change')
        self.manager.publish(aes.encrypt(json.dumps(message, cls=ComplexEncoder), settings.KEY))
        self.test_triggers(message)

    def test_triggers(self, message):
        for t in self.triggers:
            t.handle_event(message)

    def initialize_rpc(self, obj, **kwargs):
        rpc = zmq.Context()
        rpc_socket = rpc.socket(zmq.REP)
        rpc_socket.bind("tcp://*:%s" % obj.get('port'))
        self.logger.info("RPC listening on: %s" % obj.get('port'))
        settings.KEY = base64.urlsafe_b64decode(str(obj.get('key')))
        self.logger.info("%s Initialized" % self.name)        
        while True:                    
            if self.initializing:
                self.initializing = False
                self.publish(dict(method='initialized'))

            message = aes.decrypt(rpc_socket.recv(), settings.KEY)
            ob = json.loads(message)
            try:
                res = getattr(self, ob.get("method"))(ob)
                st = json.dumps(res, cls=ComplexEncoder)
                rpc_socket.send(aes.encrypt(st, settings.KEY))
            except Exception as e:
                self.logger.exception(e)
            gevent.sleep(.1)

    def hello(self, obj):
        return self.json()

    def get_sensor(self, index):
        for sensor in self.sensors:
            if sensor.index == index: return sensor

        return False

    def get_output(self, index):
        for output in self.outputs:
            if output.index == index: return output

        return False

    def get_input(self, index):
        for input in self.inputs:
            if input.index == index: return input

        return False

    def get_sensor_values(self, ob):
        res = {}
        for sensor in self.sensors:
            res[sensor.id] = sensor.json()

        return res

    def get_output_values(self):
        res = {}
        for output in self.outputs:
            res[ouput.id] = output.json()

        return res    

    def set_output_state(self, ob):
        output = self.get_output(ob.get('index'))
        if output:
            self.logger.info("%s: turning %s to %s index: %s" % (self.name, ob.get('type'), ob.get('state'), output.index))
            output.set_state(ob.get('state'))
            return dict(state=output.current_state)

    def json(self, ob=None):
        return dict(
            name=self.name,
            sensors=[s.json() for s in self.sensors],
            outputs=[o.json() for o in self.outputs],
            inputs=[i.json() for i in self.inputs],
            triggers=[t.json() for t in self.triggers],
            repeaters=[r.json() for r in self.repeaters],
            clocks=[c.json() for c in self.clocks],
            cls=self.__class__.__name__
        )

    def __conform__(self, protocol):
        return json.dumps(self.json(), cls=ComplexEncoder)

    def displayDeviceInfo(self):pass
        
    #Event Handler Callback Functions
    def inferfaceKitAttached(self, e):
        attached = e.device
        self.logger.info("InterfaceKit %i Attached!" % (attached.getSerialNum()))

    def interfaceKitDetached(self, e):
        detached = e.device
        self.logger.info("InterfaceKit %i Detached!" % (detached.getSerialNum()))

    def interfaceKitError(self, e):
        try:
            if e.eCode not in (36866,):
                source = e.device
                self.logger.info("InterfaceKit %i: Phidget Error %i: %s" % (source.getSerialNum(), e.eCode, e.description))
        except PhidgetException as e:
            self.logger.exception(e)

    def interfaceKitInputChanged(self, e):
        input = self.get_input(e.index)
        if not input: return
        val = input.do_conversion(e.value)
        ob = input.json()
        self.publish(ob)
        self.logger.info("%s Input: %s" % (input.display, val))

    def interfaceKitSensorChanged(self, e):
        sensor = self.get_sensor(e.index)
        if not sensor: return
        val = sensor.do_conversion(float(e.value)) if sensor else 0
        ob = sensor.json()
        self.publish(ob)
        self.logger.info("%s Sensor: %s" % (sensor.display, val))

    def interfaceKitOutputChanged(self, e):
        output = self.get_output(e.index)
        if not output: return
        output.current_state = e.state
        ob = output.json()
        self.publish(ob)
        self.logger.info("%s Output: %s" % (output.display, output.current_state))


    def run(self):
        if LIVE: self.init_kit()
        while True: gevent.sleep(.1)

    def init_kit(self):
        try:
            self.interface_kit.setOnAttachHandler(self.inferfaceKitAttached)
            self.interface_kit.setOnDetachHandler(self.interfaceKitDetached)
            self.interface_kit.setOnErrorhandler(self.interfaceKitError)
            self.interface_kit.setOnInputChangeHandler(self.interfaceKitInputChanged)
            self.interface_kit.setOnOutputChangeHandler(self.interfaceKitOutputChanged)
            self.interface_kit.setOnSensorChangeHandler(self.interfaceKitSensorChanged)
        except PhidgetException as e:
            self.logger.exception(e)
            
        self.logger.info("Opening phidget object....")

        try:
            self.interface_kit.openPhidget()
        except PhidgetException as e:
            self.logger.exception(e)
            
        self.logger.info("Waiting for attach....")

        try:
            self.interface_kit.waitForAttach(10000)
        except PhidgetException as e:
            self.logger.exception(e)
            try:
                self.interface_kit.closePhidget()
            except PhidgetException as e:
                self.logger.exception(e)
                self.logger.info("Exiting....")
                exit(1)
            self.logger.info("Exiting....")
        else:
            self.displayDeviceInfo()

        self.logger.info("Initializing Sensors")
        for i in range(self.interface_kit.getSensorCount()):
            try:
                sensor = self.get_sensor(i)
                if sensor:
                    self.logger.info("Setting Up: %s" % sensor.display)
                    self.logger.info("Change: %s" % sensor.change)
                    self.logger.info("Data Rate: %s" % sensor.data_rate)
                    self.interface_kit.setSensorChangeTrigger(i, sensor.change)
                    self.interface_kit.setDataRate(i, sensor.data_rate)
            except PhidgetException as e:
                self.logger.exception(e)
Example #4
0
class Manager(object):
    nodes = dict()

    def __init__(self):
        self.nodes = dict()
        self.clients_pubsub = PubSub(self, pub_port=settings.CLIENT_SUB, sub_port=settings.CLIENT_PUB, broadcast=False)
        self.nodes_pubsub = PubSub(self, pub_port=settings.NODE_SUB, sub_port=settings.NODE_PUB, parse_message=self.parse_message)
        self.logger = util.get_logger("%s.%s" % (self.__module__, self.__class__.__name__))
        Logger(self)
        self.run()

    def add_node(self, obj, **kwargs):
        rpc_port = settings.NODE_RPC+len(self.nodes.keys())
        key = aes.generate_key()
        n = Node(name=obj.get('name'), address=obj.get('address'), port=rpc_port, pubsub=self.nodes_pubsub, key=key)
        self.nodes[n.name] = n
        n.publish(method='initialize_rpc', message=dict(port=rpc_port, key=base64.urlsafe_b64encode(key)), key=settings.KEY)
        return True

    def run(self):
        rpc = zmq.Context()
        rpc_socket = rpc.socket(zmq.REP)
        rpc_socket.bind("tcp://*:%s" % settings.CLIENT_RPC)
        self.logger.info("RPC listening on: %s" % settings.CLIENT_RPC)
        while True: 
            try:
                self.logger.info("Waiting for RPC")
                message = rpc_socket.recv()
                self.logger.info("RPC Got: %s" % message)
                message = aes.decrypt(message, settings.KEY)
                ob = json.loads(message)
                res = getattr(self, ob.get("method"))(ob)
                self.logger.info("Result: %s" % res)
                st = json.dumps(res, cls=ComplexEncoder)
                st = aes.encrypt(st, settings.KEY)
                self.logger.info("Result: %s" % st)
                rpc_socket.send(st)
            except Exception as e:
                rpc_socket.send("{'error':true}")
                self.logger.exception(e)


    def get_node(self, name):
        return self.nodes.get(name)

    def initialized(self, obj, **kwargs):
        node = self.get_node(obj.get('name'))
        if node: 
            obj = node.call(method='hello')
            self.logger.info("Yeah!")
            node.set_obj = obj

        return True

    def get_nodes(self, obj):
        return [n.call('json') for k,n in self.nodes.iteritems()]

    def get_sensor_values(self):
        res = dict()
        for k, n in self.nodes.iteritems():
            res[k] = n.call('get_sensor_values')

        return res

    def node_change(self, obj):
        mes = json.dumps(obj, cls=ComplexEncoder)
        mes = aes.encrypt(mes, settings.KEY)
        self.clients_pubsub.publish(mes)
        return True

    def set_output_state(self, obj):
        node = self.get_node(obj.get('node'))
        res = node.call('set_output_state', obj)

    def parse_message(self, message):
        for k,n in self.nodes.iteritems():
            try:
                return n.parse_message(message)
            except Exception as e:
                self.logger.exception(e)

        try:
            return aes.decrypt(message, settings.KEY)
        except Exception as e:
            self.logger.exception(e)
            return message