Beispiel #1
0
    def save_data_node(self, data_node):
        if data_node.id and isinstance(data_node.id, int):
            self.logger.debug(
                "updating existing data node with id: {0}".format(
                    data_node.id))
            return self.update_data_node(data_node)

        self.get_data_nodes()
        for existing_id, existing_data_node in self.data_nodes.items():
            if existing_data_node.full_name() == data_node.full_name():
                self.logger.warning(
                    "data node with same name and path already exists! not saving data node."
                )
                return existing_data_node

        self.logger.debug("saving data node..")
        response = requests.post(self.base_url + "datanodes",
                                 json.dumps(data_node.to_dict()),
                                 auth=OAuth2(self.token))

        if response.status_code != 201:
            raise RequestError(self._make_error_message(response))
        data_node = DataNode.from_dict(response.json())
        self.data_nodes[data_node.id] = data_node
        self.logger.debug(
            "successfully saved data node. received id: {0}".format(
                data_node.id))
        return data_node
Beispiel #2
0
 def test_get_data_node_values(self):
     self.client.connect()
     data_node = self.client.save_data_node(DataNode("Temperature", ValueType.NUMBER, "°C", True, "Controller"))
     for index in range(15, 35):
         self.client.save_data_node_value(data_node.new_value(index))
     values = self.client.get_data_node_values(data_node)
     self.assertGreaterEqual(len(values), 20)
Beispiel #3
0
 def test_update_data_node_value(self):
     self.client.connect()
     data_node = self.client.save_data_node(
         DataNode("Temperature", ValueType.NUMBER, "°C", True, "Controller"))
     data_node_value = self.client.save_data_node_value(data_node.new_value(34))
     data_node_value.value = 35
     updated_value = self.client.update_data_node_value(data_node_value)
     self.assertEqual(updated_value.value, 35)
Beispiel #4
0
 def test_get_data_node_by_id(self):
     self.client.connect()
     original_data_node = self.client.save_data_node(
         DataNode("Temperature", ValueType.NUMBER, "°C", True, "Controller"))
     fetched_data_node = self.client.get_data_node_by_id(original_data_node.id)
     self.assertEqual(original_data_node.id, fetched_data_node.id)
     self.assertEqual(original_data_node.path, fetched_data_node.path)
     self.assertEqual(original_data_node.name, fetched_data_node.name)
Beispiel #5
0
 def test_save_data_node(self):
     data_node = DataNode("Temperature", ValueType.NUMBER, "°C", True, "Controller")
     self.client.connect()
     data_node = self.client.save_data_node(data_node)
     self.assertIsNotNone(data_node)
     self.assertEqual(data_node.name, "Temperature")
     self.assertEqual(data_node.value_type, ValueType.NUMBER)
     self.assertEqual(data_node.unit, "°C")
     self.assertEqual(data_node.keep_history, True)
     self.assertGreaterEqual(len(self.client.data_nodes), 1)
Beispiel #6
0
    def get_data_node_by_id(self, data_node_id):
        if not data_node_id:
            self.logger.error("invalid parameters. id must not be empty!")
            return None
        for identifier, data_node in self.data_nodes.items():
            if data_node_id == data_node.id:
                return data_node

        response = requests.get(self.base_url +
                                "/datanodes/{0}".format(data_node_id),
                                auth=OAuth2(self.token))

        if response.status_code != 200:
            raise RequestError(self._make_error_message(response))

        return DataNode.from_dict(response.json())
Beispiel #7
0
 def update_data_node(self, data_node):
     if not data_node.id or not isinstance(data_node.id, int):
         self.logger.error(
             "cannot update non-existing data node! data node must be saved first."
         )
         return False
     self.logger.debug("updating data node with id: {0} ..".format(
         data_node.id))
     response = requests.patch(self.base_url + "datanodes/" +
                               str(data_node.id),
                               json.dumps(data_node.to_dict()),
                               auth=OAuth2(self.token))
     if response.status_code != 202:
         raise RequestError(self._make_error_message(response))
     data_node = DataNode.from_dict(response.json())
     self.data_nodes[data_node.id] = data_node
     self.logger.debug("successfully updated data node with id: {0}".format(
         data_node.id))
     return data_node
Beispiel #8
0
 def get_data_nodes(self, limit=50, offset=0):
     self.logger.debug(
         "requesting data nodes.. (limit={0}, offset={1})".format(
             limit, offset))
     response = requests.get(self.base_url + "datanodes",
                             params={
                                 'limit': limit,
                                 'offset': offset
                             },
                             auth=OAuth2(self.token))
     if response.status_code != 200:
         raise RequestError(self._make_error_message(response))
     raw_data_nodes = response.json()['dataNodes']
     self.data_nodes = {}
     for raw_data_node in raw_data_nodes:
         data_node = DataNode.from_dict(raw_data_node)
         self.data_nodes[data_node.id] = data_node
     self.logger.debug("successfully got {0} data nodes.".format(
         len(self.data_nodes)))
     return list(self.data_nodes.values())
Beispiel #9
0
 def test_get_current_data_node_value(self):
     self.client.connect()
     data_node = self.client.save_data_node(DataNode("Temperature", ValueType.NUMBER, "°C", True, "Controller"))
     self.client.save_data_node_value(data_node.new_value(34))
     value = self.client.get_current_data_node_value(data_node)
     self.assertEqual(value.value, 34)
Beispiel #10
0
 def test_save_data_node_value(self):
     self.client.connect()
     data_node = self.client.save_data_node(DataNode("Temperature", ValueType.NUMBER, "°C", True, "Controller"))
     value = data_node.new_value(random.randint(15, 30))
     response_value = self.client.save_data_node_value(value)
     self.assertIsNotNone(response_value)
Beispiel #11
0
 def test_delete_data_node(self):
     self.client.connect()
     data_node = DataNode("AboutToGetDeleted", ValueType.NUMBER, "Kg", True, "Test")
     data_node = self.client.save_data_node(data_node)
     self.assertTrue(self.client.delete_data_node(data_node))