aboutsummaryrefslogtreecommitdiffstats
path: root/serialrx.py
blob: 4e7ccf6eb1aeb684b0938b92b93e62adbba91320 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python
#
# The MIT License (MIT)
#
# Copyright (c) 2020 Matthias P. Braendli, Maximilien Cuony
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import serial
import threading
import collections
import re
import time

import config

re_cc_capa = re.compile(r"\[\d+\] CC: CAPA,(\d+),(\d+)")
re_cc_vbat_plus = re.compile(r"\[\d+\] CC: VBAT\+,\d+,(\d+)")
re_num_sv = re.compile(r"\[\d+\] T_GPS.+ (\d+) SV tracked")
re_alim = re.compile(r"\[(\d+)\] ALIM (\d+) mV")
re_temp = re.compile(r"\[\d+\] TEMP ([+-]?[0-9.]+)")
re_relay = re.compile(r"\[\d+\] CC: RELAY,\d+,(On|Off),(On|Off),(On|Off)")

class MessageParser:
    def __init__(self):
        self._lock = threading.Lock()

        self._last_timestamp = 0
        self._last_timestamp_time = 0
        self._last_cc_timestamp = 0
        self._last_cc_timestamp_time = 0
        self._last_cc_capa = 0
        self._last_cc_capa_time = 0
        self._last_vbat_plus = 0
        self._last_vbat_plus_time = 0
        self._last_num_sv = 0
        self._last_num_sv_time = 0
        self._last_alim = 0
        self._last_alim_time = 0
        self._last_temp = 0
        self._last_temp_time = 0
        self._last_relay = (False, False, False)
        self._last_relay_time = 0

    def parse_message(self, message):
        with self._lock:
            match = re_cc_capa.search(message)
            if match:
                self._last_cc_timestamp = int(match.group(1))
                self._last_cc_capa = int(match.group(2))
                self._last_cc_timestamp_time = \
                    self._last_cc_capa_time = time.time()

            match = re_cc_vbat_plus.search(message)
            if match:
                self._last_vbat_plus = int(match.group(1))
                self._last_vbat_plus_time = time.time()

            match = re_num_sv.search(message)
            if match:
                self._last_num_sv = int(match.group(1))
                self._last_num_sv_time = time.time()

            match = re_alim.search(message)
            if match:
                # Convert milliseconds to seconds so that we have consistent units between the two uptimes
                self._last_timestamp = int(int(match.group(1)) / 1000)
                self._last_alim = int(match.group(2))
                self._last_timestamp_time = \
                    self._last_alim_time = time.time()

            match = re_temp.search(message)
            if match:
                self._last_temp = float(match.group(1))
                self._last_temp_time = time.time()

            match = re_relay.search(message)
            if match:
                self._last_relay = (match.group(1) == "On", match.group(2) == "On", match.group(3) == "On")
                self._last_relay_time = time.time()

    def get_last_data(self):
        with self._lock:
            return {"capa": (self._last_cc_capa, self._last_cc_capa_time),
                    "vbat_plus": (self._last_vbat_plus, self._last_vbat_plus_time),
                    "alim": (self._last_alim, self._last_alim_time),
                    "num_sv": (self._last_num_sv, self._last_num_sv_time),
                    "temp": (self._last_temp, self._last_temp_time),
                    "relay": (self._last_relay, self._last_relay_time),
                    "cc_uptime": (self._last_cc_timestamp, self._last_cc_timestamp_time),
                    "uptime": (self._last_timestamp, self._last_timestamp_time)}

class SerialRX(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

        self._parser = MessageParser()

        print("Open Serial on {} at {}".format(config.SERIALPORT, config.BAUDRATE))
        self.ser = serial.Serial(config.SERIALPORT, baudrate=config.BAUDRATE)

        self.event_stop = threading.Event()

        self.data_lock = threading.Lock()
        self.clients = []

        self.line_accumulator = []
        self.last_lines = []

        print("Serial port ready")

    def get_parsed_values(self):
        return self._parser.get_last_data()

    def run(self):
        print("Serial port starting reception")
        while not self.event_stop.is_set():
            databyte = self.ser.read()
            self.line_accumulator.append(databyte)

            if databyte == b"\n":
                line = b"".join(self.line_accumulator).decode()
                self._parser.parse_message(line)
                self.data_lock.acquire()
                try:
                    for queue in self.clients:
                        queue.append(line)

                        if len(queue) > config.LINES_TO_KEEP:
                            queue.popleft()

                    self.last_lines.append(line)

                    if len(self.last_lines) > config.LAST_LINE_TO_KEEP:
                        self.last_lines.pop(0)
                except:
                    raise
                finally:
                    self.data_lock.release()
                self.line_accumulator = []

    def stop(self):
        self.event_stop.set()
        self.join()

    def get_last_lines(self):
        return self.last_lines

    def register_client(self):
        self.data_lock.acquire()
        try:
            new_queue = collections.deque()
            self.clients.append(new_queue)
        except:
            raise
        finally:
            self.data_lock.release()

        return new_queue

    def unregister_client(self, queue):
        self.data_lock.acquire()
        try:
            self.clients = [x for x in self.clients if id(x) != id(queue)]
        except:
            raise
        finally:
            self.data_lock.release()

if __name__ == "__main__":
    test_set = """[193583144] CW: K
    [193583168] In cw_done change 0 0
    [193584056] FSM: FSM_ECOUTE
    [193585992] In SQ 1
    [193586008] FSM: FSM_QSO
    [193592816] CC: CAPA,148111,1632707
    [193605944] CC: CAPA,148121,1632682
    [193605944] CC: VBAT+,148121,12340
    [193605944] CC: VBAT-,148121,0
    [193612144] ALIM 11811 mV
    [193672600] T_GPS 2020-04-28 19:07:30 12 SV tracked
    [193672656] TIME  2020-04-28 21:07:30 [GPS]
    [233465736] TEMP 0.75
    [193692528] TEMP invalid
    [102976] CC: RELAY,721,On,Off,Off
    """
    test_should = { "capa": 1632682, "vbat_plus": 12340, "alim": 11811, "num_sv": 12, "temp" : 0.75,
            "relay" : (True, False, False)}

    mp = MessageParser()

    print("Testing parser")
    for message in test_set.split("\n"):
        print("Parse {}".format(message))
        mp.parse_message(message)

    test_measured = mp.get_last_data()

    for k in test_measured:
        if test_measured[k][1] == 0:
            print("Value {} has time 0".format(k))

    values = {k: test_measured[k][0] for k in test_measured}
    if values != test_should:
        print("invalid values {}".format(values))

    print("Test end")