ESP32-PaxCounter/src/senddata.cpp

230 lines
5.5 KiB
C++
Raw Normal View History

2018-07-14 19:12:20 +02:00
// Basic Config
2018-11-03 20:44:54 +01:00
#include "senddata.h"
2018-07-14 19:12:20 +02:00
2022-02-28 22:35:56 +01:00
// Local logging tag
static const char TAG[] = __FILE__;
void setSendIRQ(TimerHandle_t xTimer) {
xTaskNotify(irqHandlerTask, SENDCYCLE_IRQ, eSetBits);
}
void setSendIRQ(void) { setSendIRQ(NULL); }
void initSendDataTimer(uint8_t sendcycle) {
static TimerHandle_t SendDataTimer = NULL;
if (SendDataTimer == NULL) {
SendDataTimer =
xTimerCreate("SendDataTimer", pdMS_TO_TICKS(sendcycle * 1000), pdTRUE,
(void *)0, setSendIRQ);
xTimerStart(SendDataTimer, 0);
} else {
xTimerChangePeriod(SendDataTimer, pdMS_TO_TICKS(sendcycle * 1000), 0);
}
}
2018-08-03 23:50:04 +02:00
// put data to send in RTos Queues used for transmit over channels Lora and SPI
void SendPayload(uint8_t port) {
ESP_LOGD(TAG, "sending Payload for Port %d", port);
2020-12-09 10:15:12 +01:00
2021-01-01 15:25:56 +01:00
MessageBuffer_t SendBuffer; // contains MessageSize, MessagePort, Message[]
SendBuffer.MessageSize = payload.getSize();
2018-11-25 23:39:12 +01:00
switch (PAYLOAD_ENCODER) {
2019-02-24 13:35:22 +01:00
case 1: // plain -> no mapping
case 2: // packed -> no mapping
2018-11-25 23:39:12 +01:00
SendBuffer.MessagePort = port;
break;
2019-02-24 13:35:22 +01:00
case 3: // Cayenne LPP dynamic -> all payload goes out on same port
SendBuffer.MessagePort = CAYENNE_LPP1;
2018-11-25 23:39:12 +01:00
break;
2019-02-24 13:35:22 +01:00
case 4: // Cayenne LPP packed -> we need to map some paxcounter ports
SendBuffer.MessagePort = CAYENNE_LPP2;
switch (SendBuffer.MessagePort) {
case COUNTERPORT:
SendBuffer.MessagePort = CAYENNE_LPP2;
break;
case RCMDPORT:
SendBuffer.MessagePort = CAYENNE_ACTUATOR;
break;
case TIMEPORT:
SendBuffer.MessagePort = CAYENNE_DEVICECONFIG;
break;
}
2018-11-25 23:39:12 +01:00
break;
default:
SendBuffer.MessagePort = port;
}
2019-08-28 21:55:50 +02:00
memcpy(SendBuffer.Message, payload.getBuffer(), SendBuffer.MessageSize);
2018-08-04 14:37:41 +02:00
2019-02-27 00:49:32 +01:00
// enqueue message in device's send queues
2019-03-15 20:20:21 +01:00
#if (HAS_LORA)
lora_enqueuedata(&SendBuffer);
2019-02-27 00:49:32 +01:00
#endif
#ifdef HAS_SPI
spi_enqueuedata(&SendBuffer);
2019-02-27 00:49:32 +01:00
#endif
2020-05-16 23:49:34 +02:00
#ifdef HAS_MQTT
mqtt_enqueuedata(&SendBuffer);
#endif
} // SendPayload
2018-08-04 14:37:41 +02:00
2022-02-13 17:43:05 +01:00
// timer triggered function to prepare payload to send
2019-08-30 18:54:53 +02:00
void sendData() {
2018-11-19 00:41:15 +01:00
uint8_t bitmask = cfg.payloadmask;
uint8_t mask = 1;
2020-02-25 22:18:20 +01:00
#if (HAS_GPS)
2019-11-24 18:13:42 +01:00
gpsStatus_t gps_status;
2020-02-25 22:18:20 +01:00
#endif
#if (HAS_SDS011)
sdsStatus_t sds_status;
#endif
#if ((WIFICOUNTER) || (BLECOUNTER))
struct count_payload_t count =
count_from_libpax; // copy values from global libpax var
ESP_LOGD(TAG, "Sending count results: pax=%d / wifi=%d / ble=%d", count.pax,
count.wifi_count, count.ble_count);
#endif
2018-11-19 00:41:15 +01:00
while (bitmask) {
switch (bitmask & mask) {
2019-04-13 18:44:25 +02:00
#if ((WIFICOUNTER) || (BLECOUNTER))
2018-11-19 00:41:15 +01:00
case COUNT_DATA:
payload.reset();
2019-11-19 23:27:12 +01:00
#if !(PAYLOAD_OPENSENSEBOX)
payload.addCount(count.wifi_count, MAC_SNIFF_WIFI);
2021-03-02 18:36:34 +01:00
if (cfg.blescan) {
payload.addCount(count.ble_count, MAC_SNIFF_BLE);
2021-03-02 18:36:34 +01:00
}
2019-11-19 23:27:12 +01:00
#endif
2019-12-25 23:07:34 +01:00
#if (HAS_GPS)
2019-11-24 18:13:42 +01:00
if (GPSPORT == COUNTERPORT) {
// send GPS position only if we have a fix
2020-01-03 09:59:10 +01:00
if (gps_hasfix()) {
gps_storelocation(&gps_status);
payload.addGPS(gps_status);
2019-11-24 18:13:42 +01:00
} else
ESP_LOGD(TAG, "No valid GPS position");
}
2019-11-19 23:27:12 +01:00
#endif
2019-12-25 23:07:34 +01:00
#if (PAYLOAD_OPENSENSEBOX)
payload.addCount(count.wifi_count, MAC_SNIFF_WIFI);
2021-03-02 18:36:34 +01:00
if (cfg.blescan) {
payload.addCount(count.ble_count, MAC_SNIFF_BLE);
2021-03-02 18:36:34 +01:00
#endif
2020-01-22 15:49:07 +01:00
#if (HAS_SDS011)
2021-03-25 10:30:38 +01:00
sds011_store(&sds_status);
payload.addSDS(sds_status);
#endif
2019-10-01 18:06:49 +02:00
#ifdef HAS_DISPLAY
dp_plotCurve(count.pax, true);
2019-10-01 18:06:49 +02:00
#endif
2022-01-28 20:57:31 +01:00
#if (HAS_SDCARD)
sdcardWriteData(count.wifi_count, count.ble_count
#if (defined BAT_MEASURE_ADC || defined HAS_PMU)
2022-01-28 20:57:31 +01:00
,
read_voltage()
2022-01-28 20:57:31 +01:00
#endif
);
#endif // HAS_SDCARD
SendPayload(COUNTERPORT);
2022-01-28 20:57:31 +01:00
break; // case COUNTDATA
#endif // ((WIFICOUNTER) || (BLECOUNTER))
2018-11-19 00:41:15 +01:00
2019-03-15 20:20:21 +01:00
#if (HAS_BME)
2021-03-25 10:30:38 +01:00
case MEMS_DATA:
payload.reset();
payload.addBME(bme_status);
SendPayload(BMEPORT);
break;
#endif
2018-11-19 00:41:15 +01:00
2019-12-25 23:07:34 +01:00
#if (HAS_GPS)
2021-03-25 10:30:38 +01:00
case GPS_DATA:
if (GPSPORT != COUNTERPORT) {
// send GPS position only if we have a fix
if (gps_hasfix()) {
gps_storelocation(&gps_status);
payload.reset();
payload.addGPS(gps_status);
SendPayload(GPSPORT);
} else
ESP_LOGD(TAG, "No valid GPS position");
}
break;
2018-11-19 00:41:15 +01:00
#endif
2019-03-15 20:20:21 +01:00
#if (HAS_SENSORS)
#if (HAS_SENSOR_1)
2021-03-25 10:30:38 +01:00
case SENSOR1_DATA:
payload.reset();
payload.addSensor(sensor_read(1));
SendPayload(SENSOR1PORT);
break;
#endif
#if (HAS_SENSOR_2)
2021-03-25 10:30:38 +01:00
case SENSOR2_DATA:
payload.reset();
payload.addSensor(sensor_read(2));
SendPayload(SENSOR2PORT);
break;
#endif
#if (HAS_SENSOR_3)
2021-03-25 10:30:38 +01:00
case SENSOR3_DATA:
payload.reset();
payload.addSensor(sensor_read(3));
SendPayload(SENSOR3PORT);
break;
2018-11-27 11:21:20 +01:00
#endif
#endif
2018-11-19 00:41:15 +01:00
#if (defined BAT_MEASURE_ADC || defined HAS_PMU)
2021-03-25 10:30:38 +01:00
case BATT_DATA:
payload.reset();
payload.addVoltage(read_voltage());
SendPayload(BATTPORT);
break;
#endif
2021-03-25 10:30:38 +01:00
} // switch
bitmask &= ~mask;
mask <<= 1;
} // while (bitmask)
} // sendData()
2018-08-04 14:37:41 +02:00
2021-03-25 10:30:38 +01:00
void flushQueues(void) {
rcmd_queuereset();
2019-03-15 20:20:21 +01:00
#if (HAS_LORA)
2021-03-25 10:30:38 +01:00
lora_queuereset();
2019-02-27 00:49:32 +01:00
#endif
#ifdef HAS_SPI
2021-03-25 10:30:38 +01:00
spi_queuereset();
2019-02-27 00:49:32 +01:00
#endif
2020-05-16 23:49:34 +02:00
#ifdef HAS_MQTT
2021-03-25 10:30:38 +01:00
mqtt_queuereset();
2020-05-16 23:49:34 +02:00
#endif
2021-03-25 10:30:38 +01:00
}
2020-12-09 10:15:12 +01:00
2021-03-25 10:30:38 +01:00
bool allQueuesEmtpy(void) {
uint32_t rc = rcmd_queuewaiting();
2020-12-09 10:15:12 +01:00
#if (HAS_LORA)
2021-03-25 10:30:38 +01:00
rc += lora_queuewaiting();
2020-12-09 10:15:12 +01:00
#endif
#ifdef HAS_SPI
2021-03-25 10:30:38 +01:00
rc += spi_queuewaiting();
2020-12-09 10:15:12 +01:00
#endif
#ifdef HAS_MQTT
2021-03-25 10:30:38 +01:00
rc += mqtt_queuewaiting();
2020-12-09 10:15:12 +01:00
#endif
2021-03-25 10:30:38 +01:00
return (rc == 0) ? true : false;
}