Newer
Older
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
#ifndef COMMS_BEHAVIOUR_h
#define COMMS_BEHAVIOUR_h
#include "Arduino.h"
#include "Behaviours.h"
#include <WallVis.h>
/*
* --------------------------------------------------
* ---------------- SendCapabilities ----------------
* --------------------------------------------------
*/
class SendCapabilities : public Behaviour {
WallVis* _node;
public:
SendCapabilities(WallVis* node, String name = "SendCapabilities") :
Behaviour(name), _node(node){ }
String start(String args) {
//This is where you do your stuff for a simple behaviour
_node->announce_capabilities();
return "SendCapabilities behaviour " + _name;
}
};
/*
* --------------------------------------------------
* ---------------------- Link ----------------------
* --------------------------------------------------
*/
class Link : public Behaviour {
WallVis* _node;
String _peerId;
const int _timeoutInterval = 5000;
unsigned long _t = 0;
public:
Link(WallVis* node, String name = "Link") : Behaviour(name), _node(node) { _background = true; }
char* args() {return "<String peerId>"; };
String start(String args) {
_running = true;
if (args == name() || args.indexOf(" ")>0) {
return "Invalid args (" + args + ") in behaviour " + name();
}
_t = millis();
if (args == _peerId) {
return "Link ping from (" + _peerId + ")";
}
_peerId = args;
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Link\":{\"peerId\":\"" + _peerId + "\"}}";
_node->announce(str);
return "New link with (" + _peerId + ")";
}
void update() {
if (millis() > (_t+_timeoutInterval)) {
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Unlink\":{\"peerId\":\"" + _peerId + "\"}}";
_node->announce(str);
_peerId = "";
_running = false;
}
}
};
/*
* --------------------------------------------------
* ------------------- PingServer -------------------
* --------------------------------------------------
*/
class PingServer : public Behaviour {
WallVis* _node;
String str;
const int _interval = 4000;
unsigned long _t = 0;
public:
PingServer(WallVis* node, String name = "PingServer") : Behaviour(name), _node(node) { _background = true; }
String start(String args) {
_background = true;
_running = true;
_t = millis();
str = "{\"id\":\"" + String(_node->getId()) + "\",\"PingServer\":{}}";
_node->announce(str);
return "Pinging server";
}
void update() {
if (millis() > (_t+_interval)) {
_t = millis();
_node->announce(str);
}
}
};
#endif