Don't send to public channel

This depends on protobufs pr.
Shorten messages.
Allows sending Detection messages in a DM or private channel,
sending to a public channel will be blocked.
This commit is contained in:
Michael Gjelsø 2024-11-05 16:49:46 +01:00 committed by GitHub
parent 8e2a3e5728
commit 786036f15a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,7 +5,9 @@
#include "PowerFSM.h"
#include "configuration.h"
#include "main.h"
#include <Channels.h>
#include <Throttle.h>
DetectionSensorModule *detectionSensorModule;
#define GPIO_POLLING_INTERVAL 100
@ -58,8 +60,10 @@ int32_t DetectionSensorModule::runOnce()
// moduleConfig.detection_sensor.minimum_broadcast_secs = 30;
// moduleConfig.detection_sensor.state_broadcast_secs = 120;
// moduleConfig.detection_sensor.detection_trigger_type =
// meshtastic_ModuleConfig_DetectionSensorConfig_TriggerType_LOGIC_HIGH;
// meshtastic_ModuleConfig_DetectionSensorConfig_TriggerType_LOGIC_HIGH;
// strcpy(moduleConfig.detection_sensor.name, "Motion");
// moduleConfig.detection_sensor.sendTochannel = true;
// strcpy(moduleConfig.detection_sensor.sendTo, "NIU"); // Send to NodeID or Channel Name
if (moduleConfig.detection_sensor.enabled == false)
return disable();
@ -76,18 +80,17 @@ int32_t DetectionSensorModule::runOnce()
if (moduleConfig.detection_sensor.monitor_pin > 0) {
pinMode(moduleConfig.detection_sensor.monitor_pin, moduleConfig.detection_sensor.use_pullup ? INPUT_PULLUP : INPUT);
} else {
LOG_WARN("Detection Sensor Module: Set to enabled but no monitor pin is set. Disable module...");
LOG_WARN("DetectionSensor: no monitor pin set. Disabling module...");
return disable();
}
LOG_INFO("Detection Sensor Module: init");
LOG_INFO("DetectionSensor: Init");
return DELAYED_INTERVAL;
}
// LOG_DEBUG("Detection Sensor Module: Current pin state: %i", digitalRead(moduleConfig.detection_sensor.monitor_pin));
// LOG_DEBUG("Detection Sensor: Pin state: %i", digitalRead(moduleConfig.detection_sensor.monitor_pin));
if (!Throttle::isWithinTimespanMs(lastSentToMesh,
Default::getConfiguredOrDefaultMs(moduleConfig.detection_sensor.minimum_broadcast_secs))) {
Default::getConfiguredOrDefaultMs(moduleConfig.detection_sensor.minimum_broadcast_secs))) {
bool isDetected = hasDetectionEvent();
DetectionSensorTriggerVerdict verdict =
handlers[moduleConfig.detection_sensor.detection_trigger_type](wasDetected, isDetected);
@ -108,7 +111,7 @@ int32_t DetectionSensorModule::runOnce()
// change detections.
if (moduleConfig.detection_sensor.state_broadcast_secs > 0 &&
!Throttle::isWithinTimespanMs(lastSentToMesh,
Default::getConfiguredOrDefaultMs(moduleConfig.detection_sensor.state_broadcast_secs,
Default::getConfiguredOrDefaultMs(moduleConfig.detection_sensor.state_broadcast_secs,
default_telemetry_broadcast_interval_secs))) {
sendCurrentStateMessage(hasDetectionEvent());
return DELAYED_INTERVAL;
@ -118,42 +121,81 @@ int32_t DetectionSensorModule::runOnce()
void DetectionSensorModule::sendDetectionMessage()
{
LOG_DEBUG("Detected event observed. Send message");
char *message = new char[40];
sprintf(message, "%s detected", moduleConfig.detection_sensor.name);
meshtastic_MeshPacket *p = allocDataPacket();
p->want_ack = false;
p->decoded.payload.size = strlen(message);
memcpy(p->decoded.payload.bytes, message, p->decoded.payload.size);
if (moduleConfig.detection_sensor.send_bell && p->decoded.payload.size < meshtastic_Constants_DATA_PAYLOAD_LEN) {
p->decoded.payload.bytes[p->decoded.payload.size] = 7; // Bell character
p->decoded.payload.bytes[p->decoded.payload.size + 1] = '\0'; // Bell character
p->decoded.payload.size++;
}
LOG_INFO("Send message id=%d, dest=%x, msg=%.*s", p->id, p->to, p->decoded.payload.size, p->decoded.payload.bytes);
lastSentToMesh = millis();
service->sendToMesh(p);
delete[] message;
if (moduleConfig.detection_sensor.sendTo[0] != 0x00) {
LOG_DEBUG("Detected event. Sending message");
char *message = new char[40];
bool isValidrecipient = false;
sprintf(message, "%s detected", moduleConfig.detection_sensor.name);
meshtastic_MeshPacket *p = allocDataPacket();
p->want_ack = false;
if (moduleConfig.detection_sensor.sendTochannel) {
const auto &ch = channels.getByName(moduleConfig.detection_sensor.sendTo);
if (!channels.isDefaultChannel(ch.index)) {
p->channel = ch.index;
isValidrecipient = true;
} else
LOG_INFO("%s is public/not found", moduleConfig.detection_sensor.sendTo);
} else {
p->to = (uint32_t)strtoul(moduleConfig.detection_sensor.sendTo, NULL, sizeof(moduleConfig.detection_sensor.sendTo));
isValidrecipient = true;
}
p->decoded.payload.size = strlen(message);
memcpy(p->decoded.payload.bytes, message, p->decoded.payload.size);
if (moduleConfig.detection_sensor.send_bell && p->decoded.payload.size < meshtastic_Constants_DATA_PAYLOAD_LEN) {
p->decoded.payload.bytes[p->decoded.payload.size] = 7; // Bell character
p->decoded.payload.bytes[p->decoded.payload.size + 1] = '\0'; // Bell character
p->decoded.payload.size++;
}
lastSentToMesh = millis();
if (isValidrecipient) {
LOG_INFO("Send message id=%d, dest=%x, msg=%.*s", p->id,
(moduleConfig.detection_sensor.sendTochannel ? p->channel : p->to),
p->decoded.payload.size,
p->decoded.payload.bytes);
service->sendToMesh(p);
}
delete[] message;
} else
LOG_INFO("DetectionSensor: no recipient");
}
void DetectionSensorModule::sendCurrentStateMessage(bool state)
{
char *message = new char[40];
sprintf(message, "%s state: %i", moduleConfig.detection_sensor.name, state);
meshtastic_MeshPacket *p = allocDataPacket();
p->want_ack = false;
p->decoded.payload.size = strlen(message);
memcpy(p->decoded.payload.bytes, message, p->decoded.payload.size);
LOG_INFO("Send message id=%d, dest=%x, msg=%.*s", p->id, p->to, p->decoded.payload.size, p->decoded.payload.bytes);
lastSentToMesh = millis();
service->sendToMesh(p);
delete[] message;
if (moduleConfig.detection_sensor.sendTo[0] != 0x00) {
char *message = new char[40];
bool isValidrecipient = false;
sprintf(message, "%s state: %i", moduleConfig.detection_sensor.name, state);
meshtastic_MeshPacket *p = allocDataPacket();
p->want_ack = false;
if (moduleConfig.detection_sensor.sendTochannel) {
const auto &ch = channels.getByName(moduleConfig.detection_sensor.sendTo);
if (!channels.isDefaultChannel(ch.index)) {
p->channel = ch.index;
isValidrecipient = true;
} else
LOG_INFO("%s is public/not found", moduleConfig.detection_sensor.sendTo);
} else {
p->to = (uint32_t)strtoul(moduleConfig.detection_sensor.sendTo, NULL, sizeof(moduleConfig.detection_sensor.sendTo));
isValidrecipient = true;
}
p->decoded.payload.size = strlen(message);
memcpy(p->decoded.payload.bytes, message, p->decoded.payload.size);
lastSentToMesh = millis();
if (isValidrecipient) {
LOG_INFO("Send message id=%d, dest=%x, msg=%.*s", p->id,
(moduleConfig.detection_sensor.sendTochannel ? p->channel : p->to),
p->decoded.payload.size,
p->decoded.payload.bytes);
service->sendToMesh(p);
}
delete[] message;
} else
LOG_INFO("Detection Sensor: no recipient");
}
bool DetectionSensorModule::hasDetectionEvent()
{
bool currentState = digitalRead(moduleConfig.detection_sensor.monitor_pin);
// LOG_DEBUG("Detection Sensor Module: Current state: %i", currentState);
// LOG_DEBUG("Detection Sensor: state: %i", currentState);
return (moduleConfig.detection_sensor.detection_trigger_type & 1) ? currentState : !currentState;
}
}