Browse Source

Initial wetation protocol

master
Dirk Alders 4 years ago
parent
commit
1cfa3c4274
1 changed files with 53 additions and 0 deletions
  1. 53
    0
      __init__.py

+ 53
- 0
__init__.py View File

@@ -0,0 +1,53 @@
1
+#!/usr/bin/env python
2
+# -*- coding: UTF-8 -*-
3
+
4
+import config
5
+import logging
6
+import socket_protocol
7
+
8
+import envsens
9
+
10
+logger = logging.getLogger('APP')
11
+
12
+
13
+
14
+
15
+class my_base_protocol_tcp(socket_protocol.pure_json_protocol):
16
+    # EXECUTE_REQUESTs
17
+    CURRENT_ENVDATA = 0
18
+
19
+
20
+class my_server_protocol_tcp(my_base_protocol_tcp):
21
+    def __init__(self, comm_instance, envsens, secret=None):
22
+        socket_protocol.pure_json_protocol.__init__(self, comm_instance, secret)
23
+        self.envsens = envsens
24
+        self.register_callback(self.SID_READ_REQUEST, self.CURRENT_ENVDATA, self.rd_temperature)
25
+
26
+    def rd_temperature(self, msg):
27
+        return self.STATUS_OKAY, self.envsens.get_env_data()
28
+
29
+
30
+class my_client_protocol_tcp(my_base_protocol_tcp):
31
+    START_ROUTINE_DATA_IDS = []
32
+
33
+    def __init__(self, comm_instance, secret=None):
34
+        socket_protocol.pure_json_protocol.__init__(self, comm_instance, secret)
35
+        self.register_callback(self.SID_READ_RESPONSE, self.CURRENT_ENVDATA, self.read_current_envdata)
36
+
37
+    def __authentificate_process_feedback__(self, msg):
38
+        if msg.get_data() is True:
39
+            print("The client is authentificated.")
40
+        else:
41
+            print("AUTHENTIFICATION ERROR")
42
+        return my_base_protocol_tcp.__authentificate_process_feedback__(self, msg)
43
+
44
+    def read_current_envdata(self, msg):
45
+        if msg.get_status() == self.STATUS_OKAY:
46
+            env_data = msg.get_data()
47
+            print("Temperature: %6.1f °C" % env_data[envsens.KEY_TEMPERATURE])
48
+            print("Humidity:    %6.1f %%" % env_data[envsens.KEY_HUMIDITY])
49
+            return self.STATUS_OKAY, None
50
+        else:
51
+            print('No environmental data received!')
52
+            return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
53
+

Loading…
Cancel
Save