Get/Set with GUID and key attribute.
authorBenjamin Braatz <benjamin.braatz@graph-it.com>
Wed, 2 Jun 2021 08:54:01 +0000 (10:54 +0200)
committerBenjamin Braatz <benjamin.braatz@graph-it.com>
Wed, 2 Jun 2021 08:54:01 +0000 (10:54 +0200)
conf.json
controlpi_plugins/graph.py

index a454cfb3003633870ed08921afe4c07176d0f4d9..a0754fcd881ca592c12b26383f6a94b33bc55c70 100644 (file)
--- a/conf.json
+++ b/conf.json
                 "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",
                 "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" }
     }
 }
index 8e59cb22368cc30658cbdfc02e85366ce105ac85..3fb930576ce32bef44c9d093c35489d6ccdfdd0d 100644 (file)
@@ -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('<i', size)[0]
-        message = await self._reader.readexactly(size)
+        banner_size = await self._reader.readexactly(4)
+        banner_size = struct.unpack('<i', banner_size)[0]
+        banner_message = await self._reader.readexactly(banner_size)
         knoten_guid = await self._call('attributsknoten',
                                        ['knoten_name', 'knoten'])