-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmessage.py
60 lines (54 loc) · 1.75 KB
/
message.py
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
"""Classes & variables used for representing messages from board."""
from enum import Enum
from collections import namedtuple
Datatuple = namedtuple("Datatuple", ["time", "type", "subtype", "content"], defaults=[None] * 4)
class MsgType(Enum):
EVENT = b"E" # External event
STATE = b"S" # State transition
PRINT = b"P" # User print
HARDW = b"H" # Hardware callback
VARBL = b"V" # Variable change
WARNG = b"!" # Warning
ERROR = b"!!" # Error
STOPF = b"X" # Stop framework
ANLOG = b"A" # Analog
THRSH = b"T" # Threshold
@classmethod
def from_byte(cls, byte_value):
"""Get member given value byte"""
for member in cls:
if member.value == byte_value:
return member
return byte_value
def get_subtype(self, subtype_char):
"""Get subtype name from character"""
if subtype_char == "_":
return None
else:
return {
MsgType.VARBL: {
"g": "get",
"s": "user_set",
"a": "api_set",
"p": "print",
"t": "run_start",
"e": "run_end",
},
MsgType.EVENT: {
"i": "input",
"t": "timer",
"p": "publish",
"u": "user",
"a": "api",
"s": "sync",
},
MsgType.PRINT: {
"t": "task",
"a": "api",
"u": "user",
"s": "trigger",
},
MsgType.THRSH: {
"s": "set",
},
}[self][subtype_char]