Newer
Older
#ifndef COMMS_BEHAVIOUR_h
#define COMMS_BEHAVIOUR_h
#include "Arduino.h"
#include "Behaviours.h"
/*
* --------------------------------------------------
* ---------------- SendCapabilities ----------------
* --------------------------------------------------
*/
class SendCapabilities : public Behaviour {
SendCapabilities(VizBlocks* 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 {
String _peerId;
const int _timeoutInterval = 5000;
unsigned long _t = 0;
public:
Link(VizBlocks* node, String name = "Link") : Behaviour(name), _node(node) { _background = true; }
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
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 {
String str;
const int _interval = 4000;
unsigned long _t = 0;
public:
PingServer(VizBlocks* node, String name = "PingServer");
String start(String args);
void update();