-
Notifications
You must be signed in to change notification settings - Fork 2
/
MessageCommon.sedona
145 lines (126 loc) · 3.71 KB
/
MessageCommon.sedona
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
@palette=false
abstract internal class MessageCommon extends Component
{
////////////////////////////////////////////////////////////////
// Properties
////////////////////////////////////////////////////////////////
@summary=true
@config @asStr property Buf(topicNameLen) topic = ""
define int Ok = 0
define int Err_InvalidParent = 1
define int Err_InvalidTopic = 2
define Str ErrorMessages = "Ok, InvalidParent, InvalidTopic"
@range=ErrorMessages
@readonly property byte error = 0
////////////////////////////////////////////////////////////////
// Interface Methods
////////////////////////////////////////////////////////////////
**
** reset current message's local state
**
public abstract void reset()
**
** publish linked value to MQTT broker
**
public abstract void publish(Obj handle)
**
** subscribe topic on MQTT broker refered by given handle
**
public abstract void subscribe(Obj handle)
**
** unsubscribe topic on MQTT broker refered by given handle
**
public abstract void unsubscribe(Obj handle)
public virtual void onTopicChanged() {}
////////////////////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////////////////////
override virtual void start()
{
validateInputs()
}
override virtual void changed(Slot slot)
{
super.changed(slot)
if (slot.name == "topic")
{
validateInputs()
if (error == Ok)
onTopicChanged()
}
}
protected void validateInputs()
{
Component parentComponent = Sys.app.lookup(parent)
Str parentName = parentComponent?.type?.name
if (!parentComponent.type.is(Publisher.type) &&
!parentComponent.type.is(Subscriber.type) &&
!parentComponent.type.is(TopicGroup.type))
{
error := Err_InvalidParent;
}
else if (topic.toStr().length() > 0)
{
if (parentComponent.type.is(TopicGroup.type))
error := ((TopicGroup)parentComponent).error
else
error := Ok
}
else
error := Err_InvalidTopic
}
static private bool appendStr(Str dst, Str src, int maxDstLen)
{
bool res = true;
int startIndx = dst.length()
int bytesCopy = src.length()
bytesCopy = bytesCopy + 1
if ((dst.length() + bytesCopy) > maxDstLen)
{
bytesCopy = maxDstLen - dst.length()
res = false
}
for(int i = 0; i < bytesCopy; i++)
dst.set(startIndx + i, src.get(i))
return res
}
protected void collectTopics(Str buf, int maxBufLen)
{
Component parentComp = Sys.app.lookup(parent)
if (parentComp!=null && parentComp.type.is(MessageCommon.type))
{
MessageCommon parentMsg = (MessageCommon)parentComp
parentMsg.collectTopics(buf, maxBufLen)
}
//make sure topics are connected with '/'
if (buf.length() > 0 && buf.get(buf.length()-1) != '/' && topic.get(0) != '/')
appendStr(buf, "/", maxBufLen);
appendStr(buf, topic.toStr(), maxBufLen);
}
protected Obj getHandle()
{
Component parentComp = Sys.app.lookup(parent)
while(parentComp!=null)
{
if (parentComp.type.is(Worker.type)) {
Worker w = (Worker)parentComp
return w.getHandle()
}
parentComp = Sys.app.lookup(parentComp.parent)
}
return null
}
override int parentEvent(int eType, Component parent)
{
if (eType == Component.ADDED &&
(!parent.type.is(Publisher.type) &&
!parent.type.is(Subscriber.type)&&
!parent.type.is(TopicGroup.type)))
error := Err_InvalidParent;
return 0
}
////////////////////////////////////////////////////////////////
// Fields
////////////////////////////////////////////////////////////////
define int topicNameLen = 64
}