forked from Atlas-Scientific/Ezo_I2c_lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iot_cmd.cpp
142 lines (120 loc) · 6.77 KB
/
iot_cmd.cpp
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
#include "iot_cmd.h"
#include <Ezo_i2c_util.h>
//bool receive_command(String &string_buffer){
// if (WebSerial.available()) { //if theres any characters in the UART buffer
//
// string_buffer = WebSerial.readString(); //get them until its a complete command
// WebSerial.print("> "); //print whatever we received
// WebSerial.println(string_buffer);
// string_buffer.toUpperCase(); //turn the command to uppercase for easier comparisions
// string_buffer.trim(); //remove all extra spaces and newlines
// return true;
// }
// return false;
//}
// determines how long we wait depending on the command
void select_delay(const String &str) {
if (str.indexOf("CAL") != -1 || str.indexOf("R") != -1) {
delay(1200);
} else {
delay(300);
}
}
void process_command(const String &string_buffer, Ezo_board device_list[], uint8_t device_list_len, Ezo_board* &default_board){
Ezo_board* device_list_ptrs[32]; //making this arbitrarily 32 sensors long
if(device_list_len > 32) {
return;
}
for (uint8_t i = 0; i < device_list_len; i++){
device_list_ptrs[i] = &device_list[i];
}
process_command(string_buffer, device_list_ptrs, device_list_len, default_board);
}
void process_command(const String &string_buffer, Ezo_board* device_list[], uint8_t device_list_len, Ezo_board* &default_board){
if (string_buffer == "LIST") { //if our command is list
list_devices(device_list, device_list_len, default_board);
}
//the all command sends a command to all available boards and shows their responses
else if (string_buffer.startsWith("ALL:")) { //if the command starts with ALL:
String cmd = string_buffer.substring(string_buffer.indexOf(':') + 1); //get the rest of the command after the : character
for (uint8_t i = 0; i < device_list_len; i++) { //then send it to every board
device_list[i]->send_cmd(cmd.c_str());
}
select_delay(cmd); //wait for some time depending on the command
for (uint8_t i = 0; i < device_list_len; i++) { //go through our list of boards and get their response
receive_and_print_response(*device_list[i]);
}
}
// all other commands are passed through to the default board unless they have an address prepended
else if (string_buffer != "" ) { //if we received any other commands
int16_t index = string_buffer.indexOf(':'); //check if the command contains a colon character, which is used for changing addresses
if (index != -1) { //if it contains a colon character
bool addr_found = false;
String name_to_find = string_buffer.substring(0, index); //get the address out of the command
name_to_find.toUpperCase();
if (name_to_find.length() != 0) { //if its valid
//search through list and make device match the address
for (uint8_t i = 0; i < device_list_len; i++) {
if (name_to_find == device_list[i]->get_name()) { //if the address matches one of the boards in the list
default_board = device_list[i]; //set that board as the default
addr_found = true; //indicate we changed the address
break; //and exit the loop
}
}
if (addr_found) { //then send the rest of the command to that board
default_board->send_cmd(string_buffer.substring(index + 1).c_str());
} else { //otherwise print that we didnt find
WebSerial.print("No device named ");
WebSerial.println(name_to_find);
return;
}
} else {
WebSerial.println("Invalid name");
}
}
else { //if theres no colon just pass the command to the default board
default_board->send_cmd(string_buffer.c_str());
}
if(string_buffer != "SLEEP"){
select_delay(string_buffer); //wait for some time depending on the command
receive_and_print_response(*default_board);
}
}
}
void list_devices(Ezo_board device_list[], uint8_t device_list_len, Ezo_board* default_board) {
for (uint8_t i = 0; i < device_list_len; i++) { //go thorugh the list of boards
if (default_board == &device_list[i]) { //if its our default board
WebSerial.print("--> "); //print the pointer arrow
} else { //otherwise
WebSerial.print(" - "); //print a normal dash
}
print_device_info(device_list[i]); //then print the boards info
WebSerial.println("");
}
}
void list_devices(Ezo_board* device_list[], uint8_t device_list_len, Ezo_board* default_board) {
for (uint8_t i = 0; i < device_list_len; i++) { //go thorugh the list of boards
if (default_board == device_list[i]) { //if its our default board
WebSerial.print("--> "); //print the pointer arrow
} else { //otherwise
WebSerial.print(" - "); //print a normal dash
}
print_device_info(*device_list[i]); //then print the boards info
WebSerial.println("");
}
}
void iot_cmd_print_listcmd_help(){
//WebSerial.println(F(" "));
WebSerial.println(F("list prints the names and addresses of all the devices in the "));
WebSerial.println(F(" device list, with an arrow pointing to the default board "));
}
void iot_cmd_print_allcmd_help(){
WebSerial.println(F("all:[query] sends a query to all devices in the device list, and "));
WebSerial.println(F(" prints their responses"));
}
void iot_cmd_print_namedquery_help(){
WebSerial.println(F("[name]:[query] sends a query to the device with the name [name], and "));
WebSerial.println(F(" makes that device the default board"));
WebSerial.println(F(" ex: PH:status sends a status query to the device "));
WebSerial.println(F(" named PH"));
}