void Arduilink::send(unsigned int _id, const char* _msg) { SensorItem* sensor = getSensor(_id); if (sensor == NULL) return; // TODO return error if (sensor->writter != NULL) sensor->writter(_msg); // TODO else return warning }
int Arduilink::handleInput() { while (Serial.available() > 0) { while (write) {} write = true; String str = Serial.readString(); char buf[str.length() + 1]; str.toCharArray(buf, str.length() + 1); char* opcode = NULL; int node = -1; int sensorId = -1; char* pch; pch = strtok(buf, ";"); while (pch != NULL) { if (opcode == NULL) { if (strcmp(pch, "PRESENT") == 0) { printSensors(); write = false; return 0; } if (strcmp(pch, "GET") != 0 && strcmp(pch, "INFO") != 0 && strcmp(pch, "SET") != 0) { Serial.print("400;OPCODE;"); Serial.println(pch); write = false; return 2; } opcode = pch; } else if (node == -1) { node = atoi(pch); } else if (sensorId == -1) { sensorId = atoi(pch); break; } pch = strtok(NULL, ";"); } /*Serial.print("Opcode: "); Serial.print(opcode); Serial.print(" NodeId: "); Serial.print(node); Serial.print(" SensorId: "); Serial.println(sensorId);*/ if (nodeId != node) { Serial.print("404;NODE;"); Serial.println(node); write = false; return 3; } SensorItem* sensor = getSensor(sensorId); if (sensor == NULL) { Serial.print("404;SENSOR;"); Serial.println(sensorId); write = false; return 4; } // INFO - Récupérer la description d'un capteur if (strcmp(opcode, "INFO") == 0) { printSensor(sensor, node); } // GET - Récupérer la valeur d'un capteur if (strcmp(opcode, "GET") == 0) { char buff[256]; sprintf(buff, "200;%d;%d;", node, sensor->id); Serial.print(buff); Serial.println(sensor->value); } // SET - Modifier un attribut d'un capteur else if (strcmp(opcode, "SET") == 0) { // Choose attribute mode pch = strtok(NULL, ";"); int mode = 0; if (strcmp(pch, "VERBOSE") == 0) mode = 1; else if (strcmp(pch, "VAL") == 0) mode = 2; else { Serial.print("400;ATTR;"); Serial.println(pch); write = false; return 5; } // Ack pch = strtok(NULL, ";"); bool ack = strcmp(pch, "1") == 0; // Value pch = strtok(NULL, ";"); // Handle Set Verbose if (mode == 1) { if (strcmp(pch, "1") == 0) { if (ack) { Serial.println("201;VERBOSE;1"); Serial.flush(); } sensor->verbose = true; } else if (strcmp(pch, "0") == 0) { sensor->verbose = false; if (ack) { Serial.println("201;VERBOSE;0"); // TODO Ameliorer avec ids Serial.flush(); } } else { Serial.print("400;OPT;VERBOSE;"); // TODO Ameliorer avec ids Serial.println(pch); write = false; return 6; } } // Handle Set Value else if (mode == 2) { sensor->value = pch; if (sensor->writter != NULL) sensor->writter(pch); if (ack) { Serial.print("201;SET;"); Serial.print(node); Serial.print(";"); Serial.print(sensor->id); Serial.print(";VAL;"); Serial.println(pch); } } } Serial.flush(); write = false; return 0; } return 1; }