Example #1
0
 def test_start(self):
     """
     Test start action
     """
     gw = Gateway('gw', data=self.valid_data)
     gw.state.set('actions', 'install', 'ok')
     gw.install = MagicMock()
     gw.start()
     gw.install.assert_called_once_with()
Example #2
0
 def test_start_not_installed(self):
     """
     Test start action, gateway isnt installed
     """
     with pytest.raises(
             StateCheckError,
             message=
             'actions should raise an error if gateway isn\'t installed'):
         gw = Gateway('gw', data=self.valid_data)
         gw.start()
Example #3
0
    data = CONFIG["opc"]
    if data["enable"]:
        opc = OPC(host=data["host"], port=data["port"], cooler_count=8)
        opc.start()

    tor = None
    data = CONFIG["tor"]
    if data["enable"]:
        tor = TorSocketProcess(ip=data["ip"], port=data["port"], hops=data["hops"])
        tor.start()

    telegram = None
    data = CONFIG["telegram"]
    if data["enable"]:
        telegram = TelegramClient(opc=opc, token=data["token"], chat_id=data["chat_id"], tor=tor)

    gateway = None
    data = CONFIG["server"]
    if data["enable"]:
        gateway = Gateway(uri=data["uri"], opc=opc)

    loop = asyncio.get_event_loop()
    tasks = []
    
    if telegram is not None:
        tasks.extend(telegram.start(loop))
    if gateway is not None:
        tasks.extend(gateway.start(loop))

    loop.run_until_complete(asyncio.wait(tasks))
Example #4
0
@post('/api')
def hue_auth_request():
    dataLen = int(request.content_length)
    data = json.loads(request.body.read(dataLen))
    clientId = data['username']
    success = hb.authenticate(clientId)
        msg = hb.AUTH_SUCCESS[1] % clientId
    return hue_send(hb.AUTH_SUCCESS[0], msg)

@put('/api/<clientId>/lights/<lightNo>/state')
def hue_handle_light(clientId='', lightNo=0):
    logging.info('PUT /api/%s/lights/%s/state' % (clientId, lightNo))
    if not hb.isWhitelisted(clientId, '/lights/%s/state' % lightNo):
        hb.AUTH_DENIED[1]['error']['address'] = path
        return hue_send(hb.AUTH_DENIED[0], json.dumps(hb.AUTH_DENIED[1]))
    dataLen = int(request.content_length)
    data = json.loads(request.body.read(dataLen))
    hb.handle_light(lightNo, data)

if __name__ == '__main__':
    stacktracer.trace_start("trace.html")

    gw.start()
    logger.info("Starting Web Server on port %s" % settings.WEB_SERVER_PORT)
    kwargs = {'host': '0.0.0.0', 'port': settings.WEB_SERVER_PORT}
    thread.start_new_thread(run, (), kwargs)
    reactor.addSystemEventTrigger('before', 'shutdown', stopIt)
    reactor.listenMulticast(SSDP_PORT, HueDiscovery(), listenMultiple=True)
    reactor.run()
Example #5
0
class TestGateway(unittest.TestCase):

    def setUp(self):
        self.message_handler = DummyMessageHandler()
        self.gw = Gateway(self.message_handler)
        self.gw.start()

    def tearDown(self):
        self.gw.stop()
        self.gw.join()

    def test_noNodesAfterStartup(self):
        self.assertEqual(len(self.gw.get_connected_nodes()), 0)

    def test_Register(self):
        led = Device("name", self.message_handler)
        self.assertEqual(led.connect(), True)
        time.sleep(0.1)
        connected_nodes = self.gw.get_connected_nodes()
        self.assertEqual(len(connected_nodes), 1)
        node = connected_nodes[led.node_id]
        self.assertEqual(node.name, led.name)
        self.assertEqual(node.registered, True)

        last_seen = node.last_seen

        led._send_status()
        time.sleep(0.1)

        connected_nodes = self.gw.get_connected_nodes()
        self.assertEqual(len(connected_nodes), 1)
        node = connected_nodes[led.node_id]
        self.assertEqual(node.name, led.name)
        self.assertEqual(node.registered, True)
        self.assertNotEqual(node.last_seen, last_seen)

        # calling connect again should change nothing
        self.assertEqual(led.connect(), True)
        connected_nodes = self.gw.get_connected_nodes()
        self.assertEqual(len(connected_nodes), 1)

    def test_sendUnknownMessage(self):
        header = MessageHeader(node_id = 1, group_id = 1, wants_ack = False)
        m = Message(99, header)
        self.message_handler.write_message_from_device(m)
        self.assertEqual(len(self.gw.get_connected_nodes()), 0)
        # wait so the gateway can finish processing
        time.sleep(0.01)

    def test_sendACKForUnknownMessage(self):
        dev = Device("name", self.message_handler)
        self.assertEqual(dev.connect(), True)
        header = MessageHeader(node_id = dev.node_id, group_id = dev.group_id, wants_ack = False)
        m = ACKMessage(header)
        self.message_handler.write_message_from_device(m)
        time.sleep(0.1)
        self.assertEqual(len(self.gw.get_connected_nodes()), 1)

    def test_sendACKForUnknownNode(self):
        header = MessageHeader(node_id = 1, group_id = 1, wants_ack = False)
        m = ACKMessage(header)
        self.message_handler.write_message_from_device(m)
        self.assertEqual(len(self.gw.get_connected_nodes()), 0)
        # wait so the gateway can finish processing
        time.sleep(0.1)

    def test_moreThan30Nodes(self):
        for i in range(0, 30):
            dev = Device('%s' % i, self.message_handler)
            if i == 29:
                # too many nodes registered
                self.assertEqual(dev.connect(), False)
            else:
                self.assertEqual(dev.connect(), True)

        time.sleep(0.1)
        connected_nodes = self.gw.get_connected_nodes()
        self.assertEqual(len(connected_nodes), 29)

    def test_sendRegisterMessageWithoutACKRequest(self):
        header = MessageHeader(node_id = 1, group_id = 1, wants_ack = False)
        m = RegisterMessage(header)
        self.message_handler.write_message_from_device(m)
        time.sleep(0.1)
        self.assertEqual(len(self.gw.get_connected_nodes()), 1)

    def test_sendRegisterMessageWithWrongNodeId(self):
        header = MessageHeader(node_id = 5, group_id = 1, wants_ack = False)
        m = RegisterMessage(header)
        self.message_handler.write_message_from_device(m)
        time.sleep(0.1)
        self.assertEqual(len(self.gw.get_connected_nodes()), 0)

    def test_sendStatusForUnknownNode(self):
        header = MessageHeader(node_id = 5, group_id = 1, wants_ack = False)
        m = StatusMessage(header, name = 'dev name')
        self.message_handler.write_message_from_device(m)
        self.assertEqual(len(self.gw.get_connected_nodes()), 0)
        # wait so the gateway can finish processing
        time.sleep(0.1)

    def test_Register_lostRegisterResponse(self):
        token = random.random()

        dev = Device('test dev', self.message_handler)

        header = MessageHeader(node_id = 1, group_id = 1, wants_ack = False)
        p = RegisterMessage(header, name = dev.name, token = token)
        self.message_handler.write_message_from_device(p)

        # ignore RegisterResponseMessage for now
        m = dev._incoming_messages.get(True, 1)

        # the gateway should already list the node as not registered
        connected_nodes = self.gw.get_connected_nodes()
        self.assertEqual(len(connected_nodes), 1)
        self.assertEqual(connected_nodes[m.new_node_id].name, dev.name)
        self.assertEqual(connected_nodes[m.new_node_id].registered, False)

        # write a register message again, let's assume the RegisterResponse
        # Message was lost
        self.message_handler.write_message_from_device(p)

        m2 = dev._incoming_messages.get(True, 1)
        self.assertEqual(m.token, m2.token)
        self.assertEqual(m.new_node_id, m2.new_node_id)

        # the gateway should still list the node as not registered
        connected_nodes = self.gw.get_connected_nodes()
        self.assertEqual(len(connected_nodes), 1)
        self.assertEqual(connected_nodes[m.new_node_id].name, dev.name)
        self.assertEqual(connected_nodes[m.new_node_id].registered, False)
Example #6
0
        p.start()
        objects.append(p)

    # Connection between chairs and tables for presence recognition and sound generation
    for k, v in table_chair_map.items():
        table = GetObjectById(v)
        chair = GetObjectById(k)
        Environment(chair, table).start()
    """
     Creates and starts motion sensor at entrance
    """
    ms = MotionSensor(typ="motion",
                      id="device_motion_1",
                      location=room3,
                      freq=5,
                      rndValues=[0, 1, 2, 3],
                      probs=[0.7, 0.2, 0.05, 0.05],
                      obj=objects)
    ms.start()
    objects.append(ms)

    gateway = Gateway(freq=update_frequency,
                      producer=Producer(),
                      consumer=Consumer("sensors", "*.*.*.*.*.*"))
    gateway.start()

    # con = ContextBridge(frequency=5, consumer=Consumer("context", "gateway.context"))
    # con.start()

    Thread(target=eelStart, args=[]).start()