瀏覽代碼

Initial Leyk protocol implementation

master
Dirk Alders 4 年之前
父節點
當前提交
6ac1b584fb
共有 1 個文件被更改,包括 166 次插入0 次删除
  1. 166
    0
      __init__.py

+ 166
- 0
__init__.py 查看文件

@@ -0,0 +1,166 @@
1
+#!/usr/bin/env python
2
+# -*- coding: UTF-8 -*-
3
+
4
+import socket_protocol
5
+try:
6
+    import piface_function
7
+except ImportError:
8
+    piface_function = None
9
+
10
+
11
+class my_base_protocol(socket_protocol.pure_json_protocol):
12
+    DID_MODE = 0
13
+    DID_STATE = 1
14
+    #
15
+    DID_BAKERY = 2
16
+    DID_BAKE_HOUSE = 3
17
+    DID_MILL = 4
18
+    DID_REESE_HOUSE = 5
19
+    DID_PLOENLEIN = 6
20
+
21
+
22
+class my_server_protocol(my_base_protocol):
23
+    def __init__(self, comm_instance, secret=None):
24
+        socket_protocol.pure_json_protocol.__init__(self, comm_instance, secret)
25
+        self.leyk = piface_function.leyk()
26
+        self.register_callback(self.SID_EXECUTE_REQUEST, self.DID_MODE, self.set_mode)
27
+        self.register_callback(self.SID_EXECUTE_REQUEST, self.DID_BAKERY, self.set_bakery)
28
+        self.register_callback(self.SID_EXECUTE_REQUEST, self.DID_BAKE_HOUSE, self.set_bake_house)
29
+        self.register_callback(self.SID_EXECUTE_REQUEST, self.DID_MILL, self.set_mill)
30
+        self.register_callback(self.SID_EXECUTE_REQUEST, self.DID_REESE_HOUSE, self.set_reese_house)
31
+        self.register_callback(self.SID_EXECUTE_REQUEST, self.DID_PLOENLEIN, self.set_ploenlein)
32
+        self.register_callback(self.SID_READ_REQUEST, self.DID_MODE, self.get_mode)
33
+        self.register_callback(self.SID_READ_REQUEST, self.DID_STATE, self.get_state)
34
+        self.register_callback(self.SID_READ_REQUEST, self.DID_BAKERY, self.get_bakery)
35
+        self.register_callback(self.SID_READ_REQUEST, self.DID_BAKE_HOUSE, self.get_bake_house)
36
+        self.register_callback(self.SID_READ_REQUEST, self.DID_MILL, self.get_mill)
37
+        self.register_callback(self.SID_READ_REQUEST, self.DID_REESE_HOUSE, self.get_reese_house)
38
+        self.register_callback(self.SID_READ_REQUEST, self.DID_PLOENLEIN, self.get_ploenlein)
39
+
40
+    def set_mode(self, msg):
41
+        if self.leyk.set_mode(msg.get_data()):
42
+            return self.STATUS_OKAY, None
43
+        else:
44
+            return self.STATUS_OPERATION_NOT_PERMITTED, None
45
+
46
+    def get_mode(self, msg):
47
+        return self.STATUS_OKAY, self.leyk.get_mode()
48
+
49
+    def get_state(self, msg):
50
+        return self.STATUS_OKAY, self.leyk.get_state()
51
+
52
+    def set_bakery(self, msg):
53
+        if msg.get_data() is True or msg.get_data() is False:
54
+            if self.leyk.set_bakery(msg.get_data()) is not True:
55
+                return self.STATUS_OPERATION_NOT_PERMITTED, None
56
+            else:
57
+                return self.STATUS_OKAY, None
58
+        else:
59
+            return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
60
+
61
+    def get_bakery(self, msg):
62
+        return self.STATUS_OKAY, self.leyk.get_bakery()
63
+
64
+    def set_bake_house(self, msg):
65
+        if msg.get_data() is True or msg.get_data() is False:
66
+            if self.leyk.set_bake_house(msg.get_data()) is not True:
67
+                return self.STATUS_OPERATION_NOT_PERMITTED, None
68
+            else:
69
+                return self.STATUS_OKAY, None
70
+        else:
71
+            return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
72
+
73
+    def get_bake_house(self, msg):
74
+        return self.STATUS_OKAY, self.leyk.get_bake_house()
75
+
76
+    def set_mill(self, msg):
77
+        if msg.get_data() is True or msg.get_data() is False:
78
+            if self.leyk.set_mill(msg.get_data()) is not True:
79
+                return self.STATUS_OPERATION_NOT_PERMITTED, None
80
+            else:
81
+                return self.STATUS_OKAY, None
82
+        else:
83
+            return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
84
+
85
+    def get_mill(self, msg):
86
+        return self.STATUS_OKAY, self.leyk.get_mill()
87
+
88
+    def set_reese_house(self, msg):
89
+        if msg.get_data() is True or msg.get_data() is False:
90
+            if self.leyk.set_reese_house(msg.get_data()) is not True:
91
+                return self.STATUS_OPERATION_NOT_PERMITTED, None
92
+            else:
93
+                return self.STATUS_OKAY, None
94
+        else:
95
+            return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
96
+
97
+    def get_reese_house(self, msg):
98
+        return self.STATUS_OKAY, self.leyk.get_reese_house()
99
+
100
+    def set_ploenlein(self, msg):
101
+        if msg.get_data() is True or msg.get_data() is False:
102
+            if self.leyk.set_ploenlein(msg.get_data()) is not True:
103
+                return self.STATUS_OPERATION_NOT_PERMITTED, None
104
+            else:
105
+                return self.STATUS_OKAY, None
106
+        else:
107
+            return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
108
+
109
+    def get_ploenlein(self, msg):
110
+        return self.STATUS_OKAY, self.leyk.get_ploenlein()
111
+
112
+
113
+class my_client_protocol(my_base_protocol):
114
+    START_ROUTINE_DATA_IDS = [
115
+        my_base_protocol.DID_MODE, 
116
+        my_base_protocol.DID_BAKERY,
117
+        my_base_protocol.DID_BAKE_HOUSE,
118
+        my_base_protocol.DID_MILL,
119
+        my_base_protocol.DID_REESE_HOUSE,
120
+        my_base_protocol.DID_PLOENLEIN,
121
+    ]
122
+
123
+    def __init__(self, comm_instance, secret=None):
124
+        socket_protocol.pure_json_protocol.__init__(self, comm_instance, secret)
125
+        self.register_callback(self.SID_EXECUTE_RESPONSE, None, self.start_routine_response)
126
+        self.register_callback(self.SID_READ_RESPONSE, None, self.read_data_main_state)
127
+
128
+    def __authentificate_process_feedback__(self, msg):
129
+        if msg.get_data() is True:
130
+            print("The client is authentificated.")
131
+        else:
132
+            print("AUTHENTIFICATION ERROR")
133
+        return my_base_protocol.__authentificate_process_feedback__(self, msg)
134
+
135
+    def start_routine_response(self, msg):
136
+        if msg.get_status() == self.STATUS_OKAY and msg.get_data_id() in self.START_ROUTINE_DATA_IDS:
137
+            print("OKAY")
138
+            return self.STATUS_OKAY, None
139
+        else:
140
+            print("FAILED")
141
+            return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
142
+
143
+    def read_data_main_state(self, msg):
144
+        if msg.get_status() == self.STATUS_OKAY:
145
+            if msg.get_data_id() == self.DID_MODE:
146
+                print("Mode: %s" % repr(msg.get_data()))
147
+                return self.STATUS_OKAY, None
148
+            elif msg.get_data_id() == self.DID_STATE:
149
+                print("State: %s" % repr(msg.get_data()))
150
+                return self.STATUS_OKAY, None
151
+            elif msg.get_data_id() in [self.DID_BAKE_HOUSE, self.DID_BAKERY, self.DID_MILL, self.DID_PLOENLEIN, self.DID_REESE_HOUSE]:
152
+                state = {
153
+                    0: 'Off',
154
+                    1: 'On',
155
+                }.get(msg.get_data())
156
+                target = {
157
+                    self.DID_BAKE_HOUSE: 'Bakehouse',
158
+                    self.DID_BAKERY: 'Bakery',
159
+                    self.DID_MILL: 'Mill',
160
+                    self.DID_REESE_HOUSE: 'Reesehouse',
161
+                    self.DID_PLOENLEIN: 'Ploenlein',
162
+                }.get(msg.get_data_id())
163
+                print('The light in {target} is {state}'.format(state=state, target=target))
164
+                return self.STATUS_OKAY, None
165
+        print('Error receiving data!')
166
+        return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None

Loading…
取消
儲存