Browse Source

Cyclic reconnect and fast and slow request tasks implemented

mod_update
Dirk Alders 4 years ago
parent
commit
2e01d3b503
6 changed files with 55 additions and 12 deletions
  1. 4
    0
      kill.sh
  2. 8
    0
      restart.sh
  3. 35
    10
      smarthome.py
  4. 1
    1
      socket_protocol
  5. 6
    0
      start.sh
  6. 1
    1
      tcp_socket

+ 4
- 0
kill.sh View File

@@ -0,0 +1,4 @@
1
+#!/bin/sh
2
+#
3
+pid=`ps -aef | grep -i 'smarthome.py' | grep -v 'grep' | awk '{print $2}'`
4
+kill $pid

+ 8
- 0
restart.sh View File

@@ -0,0 +1,8 @@
1
+#!/bin/sh
2
+#
3
+
4
+BASEDIR=`dirname $0`
5
+#
6
+$BASEDIR/kill.sh
7
+rm -f $BASEDIR/messages.*
8
+$BASEDIR/start.sh

+ 35
- 10
smarthome.py View File

@@ -24,7 +24,9 @@ class WetationFrameProt(gui.Wetation):
24 24
     PROT_ID_GARAGE = 0
25 25
     PROT_ID_IN = 1
26 26
     PROT_ID_OUT = 2
27
-    ALL_PROT_IDS = [PROT_ID_GARAGE, PROT_ID_IN, PROT_ID_OUT]
27
+    ALL_PROT_IDS = [PROT_ID_GARAGE, PROT_ID_IN, PROT_ID_OUT, ]
28
+    SLOW_PROT_IDS = [PROT_ID_IN, PROT_ID_OUT, ]
29
+    FAST_PROT_IDS = [PROT_ID_GARAGE, ]
28 30
     
29 31
     REQUEST_MSGS = {
30 32
         PROT_ID_GARAGE: [
@@ -59,7 +61,9 @@ class WetationFrameProt(gui.Wetation):
59 61
         self.ShowFullScreen(config.FULL_SCREEN)
60 62
 
61 63
         self.__init_communication__()
62
-        self.__task_data_request__ = task.periodic(10, self.__initiate_data_request__)
64
+        self.__task_1s__ = task.periodic(1, self.__task_1s_callback__)
65
+        self.__task_10s__ = task.periodic(10, self.__task_10s_callback__)
66
+        self.__task_60s__ = task.periodic(60, self.__task_60s_callback__)
63 67
 
64 68
     def __init_protocol__(self, prot_id, ip, port, prot_class, secret):
65 69
         c_tcp = tcp_socket.tcp_client_stp(ip, port, rx_log_lvl=logging.DEBUG)
@@ -97,8 +101,25 @@ class WetationFrameProt(gui.Wetation):
97 101
         )
98 102
         self.__prot__[self.PROT_ID_OUT].register_callback(wetation_protocol.my_base_protocol_tcp.SID_READ_RESPONSE, None, self.__out_data__)
99 103
 
100
-    def __initiate_data_request__(self, rt):
104
+    def __task_1s_callback__(self, rt):
105
+        # request data from fast prot ids
106
+        self.__initiate_data_request__(rt, self.FAST_PROT_IDS)
107
+        # set date and time
108
+        wx.CallAfter(self.update_time)
109
+
110
+    def __task_10s_callback__(self, rt):
111
+        # request data from slow prot ids
112
+        self.__initiate_data_request__(rt, self.SLOW_PROT_IDS)
113
+
114
+    def __task_60s_callback__(self, rt):
115
+        # reconnect prots if needed
101 116
         for prot_id in self.ALL_PROT_IDS:
117
+            if not self.__prot__[prot_id].is_connected():
118
+                logger.debug("Trying to reconnect prot_id %d", prot_id)
119
+                self.__prot__[prot_id].reconnect()
120
+
121
+    def __initiate_data_request__(self, rt, prot_ids):
122
+        for prot_id in prot_ids:
102 123
             if self.__prot__[prot_id].is_connected():
103 124
                 for request_msg in self.REQUEST_MSGS.get(prot_id, []):
104 125
                     service_id = request_msg['service_id']
@@ -109,9 +130,6 @@ class WetationFrameProt(gui.Wetation):
109 130
                 logger.debug('Resetting GUI for prot_id %d', prot_id)
110 131
                 wx.CallAfter(self.no_data[prot_id], None)
111 132
 
112
-        # TODO: Move the following three lines to the wx idle task
113
-        wx.CallAfter(self.update_time)
114
-
115 133
     def __in_data__(self, msg):
116 134
         return self.__env_data__(msg, self.PROT_ID_IN)
117 135
 
@@ -193,15 +211,22 @@ class WetationFrameProt(gui.Wetation):
193 211
         self.Layout()
194 212
 
195 213
     def gate_oc_evt(self, event):  # wxGlade: Wetation.<event_handler>
196
-        self.prot_garage.send(garage_protocol.my_base_protocol_tcp.SID_EXECUTE_REQUEST, garage_protocol.my_base_protocol_tcp.OPEN_CLOSE_GATE, None)
214
+        logger.debug("Gate open/close request")
215
+        self.__prot__[self.PROT_ID_GARAGE].send(garage_protocol.my_base_protocol_tcp.SID_EXECUTE_REQUEST, garage_protocol.my_base_protocol_tcp.OPEN_CLOSE_GATE, None)
197 216
         event.Skip()
198 217
 
199 218
     def run(self):
200
-        self.__task_data_request__.run()
219
+        self.__task_1s__.run()
220
+        self.__task_10s__.run()
221
+        self.__task_60s__.run()
201 222
 
202 223
     def close(self):
203
-        self.__task_data_request__.stop()
204
-        self.__task_data_request__.join()
224
+        self.__task_1s__.stop()
225
+        self.__task_1s__.join()
226
+        self.__task_10s__.stop()
227
+        self.__task_10s__.join()
228
+        self.__task_60s__.stop()
229
+        self.__task_60s__.join()
205 230
 
206 231
     def __del__(self):
207 232
         self.close()

+ 1
- 1
socket_protocol

@@ -1 +1 @@
1
-Subproject commit 1fa2e2fcf952f4c4a1518a0fe7fcfd06377e84e2
1
+Subproject commit f4a7f1e7a4156f397ee76bee01b0fdab88a727d1

+ 6
- 0
start.sh View File

@@ -0,0 +1,6 @@
1
+#!/bin/sh
2
+#
3
+
4
+BASEDIR=`dirname $0`
5
+rm -f $BASEDIR/messages.*
6
+/usr/bin/python3 $BASEDIR/smarthome.py 2> $BASEDIR/messages.err --display :0 &

+ 1
- 1
tcp_socket

@@ -1 +1 @@
1
-Subproject commit 9d8dac2f943de493ebd33da62ca9329c155ea8a9
1
+Subproject commit 3e6e74f976a1322d97f1d50480df133455486fd6

Loading…
Cancel
Save