aboutsummaryrefslogtreecommitdiff
path: root/libraries/SpacebrewYun/SpacebrewYun.cpp
diff options
context:
space:
mode:
authorFederico Fissore <f.fissore@arduino.cc>2013-08-07 17:16:13 +0200
committerFederico Fissore <f.fissore@arduino.cc>2013-08-28 10:12:33 +0200
commitaa7d03ba9c6d61597b10d06804bfee7d53b45ce2 (patch)
tree34e58aeb2d5183c44d8988cb6aef369bfeffa3cf /libraries/SpacebrewYun/SpacebrewYun.cpp
parent39d8b74f58ec5b1cb59ad7879ffed9f6bb66795c (diff)
spacebrew update
Diffstat (limited to 'libraries/SpacebrewYun/SpacebrewYun.cpp')
-rw-r--r--libraries/SpacebrewYun/SpacebrewYun.cpp187
1 files changed, 145 insertions, 42 deletions
diff --git a/libraries/SpacebrewYun/SpacebrewYun.cpp b/libraries/SpacebrewYun/SpacebrewYun.cpp
index a2d04a5..7c848e7 100644
--- a/libraries/SpacebrewYun/SpacebrewYun.cpp
+++ b/libraries/SpacebrewYun/SpacebrewYun.cpp
@@ -4,6 +4,8 @@
SpacebrewYun::SpacebrewYun(const String& _name, const String& _description) {
name = _name;
description = _description;
+ subscribers = NULL;
+ publishers = NULL;
server = "sandbox.spacebrew.cc";
port = 9000;
@@ -18,8 +20,6 @@ SpacebrewYun::SpacebrewYun(const String& _name, const String& _description) {
read_name = false;
read_msg = false;
- sub_name_max = 25;
- sub_msg_max = 50;
for ( int i = 0; i < pidLength; i++ ) {
pid [i] = '\0';
@@ -33,7 +33,10 @@ SpacebrewYun::SpacebrewYun(const String& _name, const String& _description) {
}
-// boolean SpacebrewYun::_connected = false;
+int SpacebrewYun::sub_msg_int_max = 6;
+int SpacebrewYun::sub_msg_bool_max = 5;
+int SpacebrewYun::sub_msg_str_max = 50;
+int SpacebrewYun::sub_name_max = 20;
SpacebrewYun::OnBooleanMessage SpacebrewYun::_onBooleanMessage = NULL;
SpacebrewYun::OnRangeMessage SpacebrewYun::_onRangeMessage = NULL;
SpacebrewYun::OnStringMessage SpacebrewYun::_onStringMessage = NULL;
@@ -65,40 +68,57 @@ void SpacebrewYun::onError(OnSBError function){
}
void SpacebrewYun::addPublish(const String& name, const String& type) {
- Publisher *p = new Publisher();
+ struct Publisher *p = new Publisher();
p->name = createString(name.length() + 1);
p->type = createString(type.length() + 1);
+ p->confirmed = false;
+ p->time = 0;
+ if (type == "range") {
+ p->lastMsg = createString(sub_msg_int_max);
+ emptyString(p->lastMsg, sub_msg_int_max);
+ }
+ else if (type == "boolean") {
+ p->lastMsg = createString(sub_msg_bool_max);
+ emptyString(p->lastMsg, sub_msg_bool_max);
+ }
+ else {
+ p->lastMsg = createString(sub_msg_str_max);
+ emptyString(p->lastMsg, sub_msg_str_max);
+ }
name.toCharArray(p->name, name.length() + 1);
type.toCharArray(p->type, type.length() + 1);
if (publishers == NULL){
publishers = p;
- } else {
- Publisher *curr = publishers;
+ }
+ else {
+ struct Publisher *curr = publishers;
+ int counter = 1;
while(curr->next != NULL){
curr = curr->next;
+ counter++;
}
curr->next = p;
}
+ p->next = NULL;
}
void SpacebrewYun::addSubscribe(const String& name, const String& type) {
- Subscriber *p = new Subscriber();
- p->name = createString(name.length() + 1);
- p->type = createString(type.length() + 1);
- name.toCharArray(p->name, name.length() + 1);
- type.toCharArray(p->type, type.length() + 1);
+ Subscriber *s = new Subscriber();
+ s->name = createString(name.length() + 1);
+ s->type = createString(type.length() + 1);
+ name.toCharArray(s->name, name.length() + 1);
+ type.toCharArray(s->type, type.length() + 1);
if (subscribers == NULL){
- subscribers = p;
+ subscribers = s;
}
-
else {
- Subscriber *curr = subscribers;
+ struct Subscriber *curr = subscribers;
while(curr->next != NULL){
curr = curr->next;
}
- curr->next = p;
+ curr->next = s;
}
}
@@ -108,8 +128,9 @@ void SpacebrewYun::connect(String _server, int _port) {
killPids();
- brew.begin("python"); // Process should launch the "curl" command
- brew.addParameter("/usr/lib/python2.7/spacebrew/spacebrew.py"); // Process should launch the "curl" command
+ brew.begin("run-spacebrew"); // Process should launch the "curl" command
+ // brew.begin("python"); // Process should launch the "curl" command
+ // brew.addParameter("/usr/lib/python2.7/spacebrew/spacebrew.py"); // Process should launch the "curl" command
brew.addParameter("--server");
brew.addParameter(server);
brew.addParameter("--port");
@@ -120,11 +141,13 @@ void SpacebrewYun::connect(String _server, int _port) {
brew.addParameter(description);
if (subscribers != NULL) {
- Subscriber *curr = subscribers;
+ struct Subscriber *curr = subscribers;
while(curr != NULL){
if (_verbose) {
- Serial.print(F("Creating subcribers: "));
- Serial.println(curr->name);
+ Serial.print(F("Creating subscribers: "));
+ Serial.print(curr->name);
+ Serial.print(F(" of type: "));
+ Serial.println(curr->type);
}
brew.addParameter("-s"); // Add the URL parameter to "curl"
@@ -132,29 +155,40 @@ void SpacebrewYun::connect(String _server, int _port) {
brew.addParameter(","); // Add the URL parameter to "curl"
brew.addParameter(curr->type); // Add the URL parameter to "curl"
- if (curr->next == NULL) curr = NULL;
- else curr = curr->next;
+ // if (curr->next == NULL) curr = NULL;
+ // else curr = curr->next;
+
+ curr = curr->next;
}
}
if (publishers != NULL) {
- Publisher *curr = publishers;
+ struct Publisher *curr = publishers;
while(curr != NULL){
if (_verbose) {
Serial.print(F("Creating publishers: "));
- Serial.println(curr->name);
+ Serial.print(curr->name);
+ Serial.print(F(" of type: "));
+ Serial.println(curr->type);
}
brew.addParameter("-p"); // Add the URL parameter to "curl"
brew.addParameter(curr->name); // Add the URL parameter to "curl"
brew.addParameter(","); // Add the URL parameter to "curl"
brew.addParameter(curr->type); // Add the URL parameter to "curl"
- if (curr->next == NULL) curr = NULL;
- else curr = curr->next;
+ curr = curr->next;
}
}
Console.begin();
+ if (_verbose) {
+ Serial.println(F("Console started "));
+ }
+
brew.runAsynchronously();
+
+ if (_verbose) {
+ Serial.println(F("Brew started "));
+ }
while (!Console) { ; }
}
@@ -163,7 +197,6 @@ void SpacebrewYun::monitor() {
char c = Console.read();
if (c == char(CONNECTION_START) && !_connected) {
- _connected = true;
if (_verbose) {
Serial.print(F("Connected to spacebrew server at: "));
Serial.println(server);
@@ -173,6 +206,7 @@ void SpacebrewYun::monitor() {
if (_onOpen != NULL){
_onOpen();
}
+ _connected = true;
}
else if (c == char(CONNECTION_END) && _connected) {
_connected = false;
@@ -205,12 +239,24 @@ void SpacebrewYun::monitor() {
} else if (c == char(MSG_DIV) || sub_name.length() > sub_name_max) {
read_name = false;
read_msg = true;
- } else if (c == char(MSG_END) || sub_msg.length() > sub_msg_max) {
- read_msg = false;
- onMessage();
+ } else if (c == char(MSG_END) || sub_msg.length() > sub_msg_str_max) {
+ if (read_msg == true) {
+ read_msg = false;
+ onMessage();
+ // delay(2);
+ }
+ if (read_confirm == true) {
+ read_confirm = false;
+ onConfirm();
+ delay(2);
+ }
+ } else if (c == char(MSG_CONFIRM)) {
+ read_confirm = true;
} else {
if (read_name == true) {
sub_name += c;
+ } else if (read_confirm == true) {
+ sub_name += c;
} else if (read_msg == true) {
sub_msg += c;
}
@@ -220,6 +266,43 @@ void SpacebrewYun::monitor() {
}
}
}
+
+ if (publishers != NULL) {
+ struct Publisher *curr = publishers;
+ while((curr != NULL)){
+
+ if ( (curr->confirmed == 0) && ((millis() - curr->time) > 50) ) {
+ if (_verbose) {
+ Serial.print(F("resending msg: "));
+ Serial.println(curr->name);
+ }
+ send(curr->name, curr->lastMsg);
+ }
+ curr = curr->next;
+ }
+ }
+
+}
+
+void SpacebrewYun::onConfirm() {
+ if (publishers != NULL) {
+ struct Publisher *curr = publishers;
+ while((curr != NULL)){
+ if (sub_name.equals(curr->name) == true) {
+ curr->confirmed = true;
+ // if (_verbose) {
+ // Serial.print(F("confirmed "));
+ // Serial.println(curr->name);
+ // }
+ break;
+ }
+ curr = curr->next;
+ }
+ }
+
+ sub_name = "";
+ sub_msg = "";
+ sub_type = "";
}
boolean SpacebrewYun::connected() {
@@ -232,13 +315,12 @@ void SpacebrewYun::verbose(boolean verbose = true) {
void SpacebrewYun::onMessage() {
if (subscribers != NULL) {
- Subscriber *curr = subscribers;
+ struct Subscriber *curr = subscribers;
while((curr != NULL) && (sub_type == "")){
if (sub_name.equals(curr->name) == true) {
sub_type = curr->type;
}
- if (curr->next == NULL) curr = NULL;
- else curr = curr->next;
+ curr = curr->next;
}
}
@@ -275,14 +357,34 @@ void SpacebrewYun::onMessage() {
void SpacebrewYun::send(const String& name, const String& value){
- Console.print(char(29));
- Console.print(name);
- Console.print(char(30));
- Console.print(value);
- Console.print(char(31));
- Console.flush();
-
- true;
+ if (publishers != NULL) {
+
+ Console.print(char(29));
+ Console.print(name);
+ Console.print(char(30));
+ Console.print(value);
+ Console.print(char(31));
+ Console.flush();
+
+ struct Publisher *curr = publishers;
+ while(curr != NULL){
+ if (name.equals(curr->name) == true) {
+ int msg_len = 0;
+
+ if (curr->type == "range") msg_len = sub_msg_int_max;
+ else if (curr->type == "boolean") msg_len = sub_msg_bool_max;
+ else msg_len = sub_msg_str_max;
+
+ if (value.length() < msg_len) msg_len = value.length() + 1;
+ value.toCharArray(curr->lastMsg, msg_len);
+
+ curr->confirmed = false;
+ curr->time = millis();
+
+ }
+ curr = curr->next;
+ }
+ }
}
@@ -292,8 +394,9 @@ void SpacebrewYun::send(const String& name, const String& value){
void SpacebrewYun::getPids() {
// request the pid of all python processes
+ // brew.begin("run-getsbpids"); // Process should launch the "curl" command
pids.begin("python");
- pids.addParameter("/usr/lib/python2.7/spacebrew/getProcPid.py"); // Process should launch the "curl" command
+ pids.addParameter("/usr/lib/python2.7/spacebrew/getprocpid.py"); // Process should launch the "curl" command
pids.run();
if (_verbose) {