aboutsummaryrefslogtreecommitdiff
path: root/libraries/SpacebrewYun/SpacebrewYun.h
blob: df608c1eeeba28fe0ef1d1cfa05ec2f98f378793 (plain)
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
#ifndef YUNSPACEBREW_H
#define YUNSPACEBREW_H

#include "Arduino.h"
#include <Bridge.h>
#include <Console.h>
#include <Process.h>

enum SBmsg { 
	CONNECTION_START = char(28), 
	CONNECTION_END = char(27), 
	CONNECTION_ERROR = char(26), 
	MSG_CONFIRM = char(7), 
	MSG_START = char(29), 
	MSG_DIV = char(30), 
	MSG_END = char(31) 
};

struct Publisher {
	char *name;
	char *type;
	char *lastMsg;
	Publisher *next;
	int confirmed;
	long time;
};

struct Subscriber{
	char *name;
	char *type;
	Subscriber *next;
};

int const pidLength = 6;
int const sbPidsLen = 4;

class SpacebrewYun {

	public:

		SpacebrewYun(const String&, const String&);
		void addPublish(const String&, const String&);
		void addSubscribe(const String&, const String&);

		void connect(String, int);
		void connect() { connect(server, port); };
		void connect(String _server) { connect(String(_server), port); };

		void monitor();
		void onMessage();
		void onConfirm();

		boolean connected();

		void send(const String&, const String&);
		void send(const String& name, char * value) { send(name, String(value)); }
		void send(const String& name, bool value){ send(name, (value ? String("true") : String("false"))); };
		void send(const String& name, int value) { send(name, String(value)); };
		void send(const String& name, long value) { send(name, String(value)); };
		void send(const String& name, float value) { send(name, String(value)); };

		void verbose(boolean);

		typedef void (*OnBooleanMessage)(String name, boolean value);
		typedef void (*OnRangeMessage)(String name, int value);
		typedef void (*OnStringMessage)(String name, String value);
		typedef void (*OnCustomMessage)(String name, String value, String type);
		typedef void (*OnSBOpen)();
		typedef void (*OnSBClose)();
		typedef void (*OnSBError)(int code, String message);

		void onOpen(OnSBOpen function);
		void onClose(OnSBClose function);
		void onRangeMessage(OnRangeMessage function);
		void onStringMessage(OnStringMessage function);
		void onBooleanMessage(OnBooleanMessage function);
		void onCustomMessage(OnCustomMessage function);
		void onError(OnSBError function);

	private:

		Process brew;
		String name;
		String server;
		String description;
		boolean _connected;
		boolean _error_msg;
		boolean _verbose;
		int port;

		/**Output should be at least 5 cells**/
		static OnBooleanMessage _onBooleanMessage;
		static OnRangeMessage _onRangeMessage;
		static OnStringMessage _onStringMessage;
		static OnCustomMessage _onCustomMessage;
		static OnSBOpen _onOpen;
		static OnSBClose _onClose;
		static OnSBError _onError;

		Subscriber * subscribers;
		Publisher * publishers;
		String sub_name;
		String sub_msg;
		String sub_type;

		boolean read_name;
		boolean read_msg;
		boolean read_confirm;
		static int sub_name_max;
		static int sub_msg_str_max;
		static int sub_msg_int_max;
		static int sub_msg_bool_max;
		// int sub_name_max;
		// int sub_msg_str_max;

		Process pids;
		char pid [6];
		int sbPids [4];

		void killPids();
		void getPids();

		static char * createString(int len){
			char * out = ( char * ) malloc ( len + 1 );
			return out;
		}		

		static void emptyString(char * str, int len){
			for (int i = 0; i < len; i++) {
				str[i] = '\0';
			}
		}

};

#endif