CanGrow/include/Webserver/Api_sensor.h

124 lines
5.5 KiB
C

/*
*
* include/Webserver/Api_Sensor.h - Sensor API header file
*
*
*
*/
void Api_sensor_data(AsyncWebServerRequest* request) {
AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot().to<JsonObject>();
//root["hello"] = "world";
for(byte i = 0 ; i < Max_Sensors ; i++) {
if(config.system.sensor.type[i] > 0) {
JsonObject objSensor = root["sensor"].add<JsonObject>();
objSensor["id"] = i;
objSensor["name"] = config.system.sensor.name[i];
objSensor["type"] = SensorIndex[config.system.sensor.type[i]].name;
objSensor["status"] = sensorStatus[i];
if(SensorIndex[config.system.sensor.type[i]].type == SENSOR_TYPE_I2C)
objSensor["i2c_addr"] = "0x" + String(Sensor_Addr_Init_Update(config.system.sensor.type[i], config.system.sensor.i2c_addr[i], SENSOR_AIU_MODE_ADDR), HEX);
if(SensorIndex[config.system.sensor.type[i]].type == SENSOR_TYPE_INTADC)
objSensor["gpio"] = GPIOindex[config.system.sensor.gpio[i][0]].gpio;
for(byte j = 0; j < Max_Sensors_Read; j++) {
if(SensorIndex[config.system.sensor.type[i]].read[j] > 0) {
JsonObject objReading = objSensor["reading"].add<JsonObject>();
/* when for a RAW reading rawConvert is set, return the converted description and unit */
if((SensorIndex[config.system.sensor.type[i]].read[j] == SENSOR_READ_TYPE_RAW) && (config.system.sensor.rawConvert[i][j] > 0)) {
objReading["descr"] = FPSTR(Sensor_Convert_Raw_descr[config.system.sensor.rawConvert[i][j]]);
objReading["unit"] = FPSTR(Sensor_Convert_Raw_unit[config.system.sensor.rawConvert[i][j]]);
} else {
objReading["descr"] = FPSTR(Sensor_Read_descr[SensorIndex[config.system.sensor.type[i]].read[j]]);
objReading["unit"] = FPSTR(Sensor_Read_unit[SensorIndex[config.system.sensor.type[i]].read[j]]);
}
/* read RAW values
when internal ADC */
if(SensorIndex[config.system.sensor.type[i]].type == SENSOR_TYPE_INTADC) {
objReading["raw"] = Sensor_getValue( config.system.sensor.type[i], config.system.sensor.gpio[i][0]);
} else if(SensorIndex[config.system.sensor.type[i]].type == SENSOR_TYPE_I2C) {
objReading["raw"] = Sensor_getValue( config.system.sensor.type[i], config.system.sensor.i2c_addr[i], j);
}
objReading["value"] = Sensor_getCalibratedValue(i, j);
}
}
}
}
response->setLength();
request->send(response);
}
void Api_sensor_data_raw(AsyncWebServerRequest* request) {
/* Api_sensor_data_raw returns the raw reading value of a specific reading of a sensor
* you can call it with GET http://<IP>/api/sensor/raw?sensor=1&reading=2*/
AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot().to<JsonObject>();
//root["hello"] = "world";
if((request->hasParam("sensor")) && (request->hasParam("reading"))) {
const AsyncWebParameter* paramSensor = request->getParam("sensor");
byte sensorId = paramSensor->value().toInt();
const AsyncWebParameter* paramReading = request->getParam("reading");
byte readingId = paramReading->value().toInt();
root["sensorId"] = sensorId;
root["readingId"] = readingId;
/* when reading is RAW */
if(SensorIndex[config.system.sensor.type[sensorId]].read[readingId] == SENSOR_READ_TYPE_RAW) {
/* when internal ADC */
if(SensorIndex[config.system.sensor.type[sensorId]].type == SENSOR_TYPE_INTADC) {
root["value"] = Sensor_getValue( config.system.sensor.type[sensorId], config.system.sensor.gpio[sensorId][0]);
} else if(SensorIndex[config.system.sensor.type[sensorId]].type == SENSOR_TYPE_I2C) {
root["value"] = Sensor_getValue( config.system.sensor.type[sensorId], config.system.sensor.i2c_addr[sensorId], readingId);
}
} else {
root["msg"] = String(F("not a RAW reading"));
}
} else {
root["msg"] = String(F("sensor or reading not given"));
}
response->setLength();
request->send(response);
}
void Api_sensor_driver(AsyncWebServerRequest* request) {
/* Api_sensor_data_raw returns the raw reading value of a specific reading of a sensor
* you can call it with GET http://<IP>/api/sensor/raw?sensor=1&reading=2*/
AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot().to<JsonObject>();
//root["hello"] = "world";
root["drivers"] = SensorIndex_length;
root["maxReadings"] = Max_Sensors_Read;
/* empty driver because 0 is unconfigured */
JsonObject objSensor = root["sensor"].add<JsonObject>();
for(byte i = 1; i <= SensorIndex_length; i++) {
//Log.verbose(F("%s Sensor_Index %d, Name %s, Readings" CR), LogLoc, i, SensorIndex[i].name );
JsonObject objSensor = root["sensor"].add<JsonObject>();
objSensor["index"] = i;
objSensor["name"] = FPSTR(SensorIndex[i].name);
for(byte j = 0; j < Max_Sensors_Read; j++) {
if(SensorIndex[i].read[j] > 0 ) {
//Log.verbose(F("%s %d: %s (%d %d)" CR), LogLoc, j, Sensor_Read_descr[SensorIndex[i].read[j]], SensorIndex[i].read[j], Sensor_Read_unit[SensorIndex[i].read[j]], SensorIndex[i].read[j]);
JsonObject objReading = objSensor["reading"].add<JsonObject>();
objReading["index"] = j;
objReading["descr"] = FPSTR(Sensor_Read_descr[SensorIndex[i].read[j]]);
objReading["unit"] = FPSTR(Sensor_Read_unit[SensorIndex[i].read[j]]);
}
}
}
response->setLength();
request->send(response);
}