From: Benjamin Braatz Date: Wed, 2 Jun 2021 08:54:01 +0000 (+0200) Subject: Get/Set with GUID and key attribute. X-Git-Url: http://git.graph-it.com/?a=commitdiff_plain;h=35e83fb39feac7624ea7476c58f78a8210fb22c2;p=graphit%2Fcontrolpi-graph.git Get/Set with GUID and key attribute. --- diff --git a/conf.json b/conf.json index a454cfb..a0754fc 100644 --- a/conf.json +++ b/conf.json @@ -24,18 +24,18 @@ "node": "testnode", "guid": "0123456789", "attribute": "testboolean", - "value": "1" } + "value": true } }, "State to Graph Off": { "plugin": "Alias", "from": { "sender": { "const": "Example State" }, - "state": { "const": true } }, + "state": { "const": false } }, "to": { "target": "Test Graph", "command": "set", "node": "testnode", "guid": "0123456789", "attribute": "testboolean", - "value": "0" } + "value": false } }, "Get from Graph": { "plugin": "Alias", @@ -46,5 +46,28 @@ "node": "testnode", "guid": "0123456789", "attribute": "testboolean" } + }, + "Set with Key": { + "plugin": "Alias", + "from": { "target": { "const": "Set with Key" }, + "command": { "const": "execute" } }, + "to": { "target": "Test Graph", + "command": "set key", + "node": "testnode", + "key attribute": "name", + "key value": "test", + "attribute": "testnumber", + "value": 42000 } + }, + "Get with Key": { + "plugin": "Alias", + "from": { "target": { "const": "Get with Key" }, + "command": { "const": "execute" } }, + "to": { "target": "Test Graph", + "command": "get key", + "node": "testnode", + "key attribute": "name", + "key value": "test", + "attribute": "testnumber" } } } diff --git a/controlpi_plugins/graph.py b/controlpi_plugins/graph.py index 8e59cb2..3fb9305 100644 --- a/controlpi_plugins/graph.py +++ b/controlpi_plugins/graph.py @@ -26,7 +26,54 @@ class GraphConnection(BasePlugin): 'required': ['url', 'crt']} async def _receive(self, message: Message) -> None: - await self.bus.send(Message(self.name, {'spam': self.conf['spam']})) + if message['command'] == 'set': + await self._call('setze', + [message['guid'], + f"{message['node']}_{message['attribute']}", + message['value']]) + await self.bus.send(Message(self.name, + {'event': 'attributevalue', + 'node': message['node'], + 'guid': message['guid'], + 'attribute': message['attribute'], + 'value': message['value']})) + elif message['command'] == 'get': + value = await self._call('attribut', + [message['guid'], + f"{message['node']}_{message['attribute']}"]) + await self.bus.send(Message(self.name, + {'event': 'attributevalue', + 'node': message['node'], + 'guid': message['guid'], + 'attribute': message['attribute'], + 'value': value})) + elif message['command'] == 'set key': + guid = await self._call('attributsknoten', + [f"{message['node']}_{message['key attribute']}", + message['key value']]) + await self._call('setze', + [guid, + f"{message['node']}_{message['attribute']}", + message['value']]) + await self.bus.send(Message(self.name, + {'event': 'attributevalue', + 'node': message['node'], + 'guid': guid, + 'attribute': message['attribute'], + 'value': message['value']})) + elif message['command'] == 'get key': + guid = await self._call('attributsknoten', + [f"{message['node']}_{message['key attribute']}", + message['key value']]) + value = await self._call('attribut', + [guid, + f"{message['node']}_{message['attribute']}"]) + await self.bus.send(Message(self.name, + {'event': 'attributevalue', + 'node': message['node'], + 'guid': guid, + 'attribute': message['attribute'], + 'value': value})) async def _call(self, method, params): await self._lock.acquire() @@ -65,18 +112,45 @@ class GraphConnection(BasePlugin): self._ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) self._ssl_ctx.load_cert_chain(self.conf['crt']) self._call_id = 0 - #message = Message(self.name, {'spam': self.conf['spam']}) - #sends = [MessageTemplate.from_message(message)] - #receives = [MessageTemplate({'target': {'const': self.name}})] - #self.bus.register(self.name, 'Plugin', sends, receives, self._receive) + sends = [MessageTemplate({'event': {'const': 'attributevalue'}, + 'node': {'type': 'string'}, + 'guid': {'type': 'string'}, + 'attribute': {'type': 'string'}, + 'value': {}})] + receives = [MessageTemplate({'target': {'const': self.name}, + 'command': {'const': 'set'}, + 'node': {'type': 'string'}, + 'guid': {'type': 'string'}, + 'attribute': {'type': 'string'}, + 'value': {}}), + MessageTemplate({'target': {'const': self.name}, + 'command': {'const': 'get'}, + 'node': {'type': 'string'}, + 'guid': {'type': 'string'}, + 'attribute': {'type': 'string'}}), + MessageTemplate({'target': {'const': self.name}, + 'command': {'const': 'set key'}, + 'node': {'type': 'string'}, + 'key attribute': {'type': 'string'}, + 'key value': {}, + 'attribute': {'type': 'string'}, + 'value': {}}), + MessageTemplate({'target': {'const': self.name}, + 'command': {'const': 'get key'}, + 'node': {'type': 'string'}, + 'key attribute': {'type': 'string'}, + 'key value': {}, + 'attribute': {'type': 'string'}})] + self.bus.register(self.name, 'GraphConnection', + sends, receives, self._receive) async def run(self) -> None: """Open connection and test it.""" self._lock = asyncio.Lock() (self._reader, self._writer) = await asyncio.open_connection( self._host, self._port, ssl=self._ssl_ctx) - size = await self._reader.readexactly(4) - size = struct.unpack('