65 mEventInterceptor(nullptr),
68 mFailedWithError(false) {
95 mEventInterceptor = interceptor;
100 event.
put(
"eventID", eventID.c_str());
101 event.
put(
"value",
static_cast<int64_t
>(value));
102 _populateAndPublishEvent(event.
build());
117 if (mPersist && code.length() > 0) {
134 .
put(
"persist", mPersist)
135 .
put(
"checksum", (
int)mChecksum)
136 .
put(
"code", mPersist ?
getLisp().getLastCode().c_str() :
"")
156 mFailedWithError =
true;
159 packet.
put(
"type",
"error");
160 packet.
put(
"timestamp",
static_cast<int64_t
>(
Date::now()));
172 packet.
put(
"type",
"log");
173 packet.
put(
"timestamp",
static_cast<int64_t
>(
Date::now()));
203 _populateAndPublishEvent(data);
219 virtual void handle(
const String &topic,
const Bytes &payload)
override {
241 static bool firstPacketReceived =
false;
244 auto newPersist = packet.
getBool(
"persist");
245 auto newChecksum = script.terminate().checksum();
247 auto ignoreScript =
false;
249 if (!firstPacketReceived) {
250 auto isEqual = mChecksum == newChecksum;
251 ignoreScript = isEqual && mPersist && !mFailedWithError;
252 firstPacketReceived =
true;
256 mChecksum = newChecksum;
257 mPersist = newPersist;
258 mFailedWithError =
false;
281 if (payload.
size() > 0) {
283 auto eventID =
event.getString(
"eventID");
284 auto valueStr =
event.getValueAsString(
"value");
286 if (eventID.isEmpty()) {
291 if (valueStr.isEmpty()) {
292 UNIOT_LOG_WARN(
"received event '%s' with empty value, ignoring", eventID.c_str());
296 auto value = valueStr.toInt();
297 auto isNumber = value || valueStr ==
"0";
300 UNIOT_LOG_WARN(
"received event '%s' with non-numeric value '%s', ignoring", eventID.c_str(), valueStr.c_str());
304 auto sender =
event.getMap(
"sender");
307 lispEvent.
value = value;
308 lispEvent.
timestamp =
event.getInt(
"timestamp");
309 lispEvent.
sender.
type = sender.getString(
"type");
310 lispEvent.
sender.
id = sender.getString(
"id");
312 if (mEventInterceptor && !mEventInterceptor(lispEvent)) {
323 void _populateAndPublishEvent(
const Bytes &eventData) {
325 event.put(
"timestamp",
static_cast<int64_t
>(
Date::now()))
327 .put(
"type",
"device")
329 auto eventDataBuilt =
event.build();
330 auto eventID =
event.getString(
"eventID");
338 bool mFailedWithError;
Lisp interpreter event definitions for the Uniot event system.
const char * c_str() const
Gets the byte array as a C string.
Definition Bytes.h:266
size_t size() const
Gets the size of the byte array.
Definition Bytes.h:303
Definition CBORObject.h:40
long getInt(int key) const
Get an integer value at a specific integer key.
Definition CBORObject.h:399
CBORObject & put(int key, int value)
Put an integer value at a specific integer key.
Definition CBORObject.h:170
void forceDirty()
Force the object to be marked as dirty (modified)
Definition CBORObject.h:526
String getString(int key) const
Get a string value at a specific integer key.
Definition CBORObject.h:419
void clean()
Reset the object to an empty state.
Definition CBORObject.h:535
Bytes build() const
Build the CBOR data into binary format.
Definition CBORObject.h:499
bool getBool(int key) const
Get a boolean value at a specific integer key.
Definition CBORObject.h:379
virtual bool restore() override
Restore the CBOR object from the filesystem.
Definition CBORStorage.h:92
virtual bool store() override
Store the CBOR object to the filesystem.
Definition CBORStorage.h:74
CBORStorage(const String &path)
Constructs a new CBORStorage object.
Definition CBORStorage.h:46
CBORObject & object()
Get access to the underlying CBORObject.
Definition CBORStorage.h:60
static time_t now()
Returns the current Unix timestamp.
Definition Date.h:74
void emitEvent(unsigned int topic, int msg)
void receiveDataFromChannel(T_topic channel, DataChannelCallback callback)
Receives data from a specific channel on all connected EventBus instances.
Definition EventEntity.h:117
bool sendDataToChannel(T_topic channel, T_data data)
Sends data to a specific channel on all connected EventBus instances.
Definition EventEntity.h:98
EventListener * listenToEvent(unsigned int topic)
virtual void syncSubscriptions() override
Sets up MQTT topic subscriptions for the device.
Definition LispDevice.h:80
virtual void handle(const String &topic, const Bytes &payload) override
Processes MQTT messages received on subscribed topics.
Definition LispDevice.h:219
LispDevice()
Constructs a LispDevice instance.
Definition LispDevice.h:61
void runStoredCode()
Loads and executes previously stored code from persistent storage.
Definition LispDevice.h:111
void handleScript(const Bytes &payload)
Processes script payloads received via MQTT.
Definition LispDevice.h:240
void setEventInterceptor(LispEventInterceptor interceptor)
Definition LispDevice.h:94
virtual void onEventReceived(unsigned int topic, int msg) override
Processes events received from the unLisp interpreter.
Definition LispDevice.h:151
unLisp & getLisp()
Provides access to the unLisp interpreter instance.
Definition LispDevice.h:90
bool store()
Stores the current script state to persistent storage.
Definition LispDevice.h:132
void handleEvent(const Bytes &payload)
Processes event payloads received via MQTT.
Definition LispDevice.h:280
void publishLispEvent(const String &eventID, int32_t value)
Definition LispDevice.h:98
const String & subscribe(const String &topic)
Subscribes to a specific MQTT topic.
Definition MQTTDevice.cpp:74
MQTTDevice()
Constructs a new MQTTDevice instance.
Definition MQTTDevice.h:44
bool isTopicMatch(const String &storedTopic, const String &incomingTopic) const
Determines if a stored topic matches an incoming topic string using MQTT wildcards.
Definition MQTTDevice.cpp:137
void publishGroup(const String &groupId, const String &subTopic, const Bytes &payload, bool retained=false, bool sign=false)
Publishes a message to a group-specific subtopic.
Definition MQTTDevice.cpp:114
const String & subscribeGroup(const String &groupId, const String &subTopic)
Subscribes to a group-specific subtopic.
Definition MQTTDevice.cpp:92
const String & getDeviceId() const
Gets the device identifier.
Definition MQTTDevice.cpp:37
bool unsubscribe(const String &topic)
Unsubscribes from a specific topic.
Definition MQTTDevice.cpp:64
void publishEmptyDevice(const String &subTopic)
Publishes an empty message to a device-specific subtopic with retained flag set.
Definition MQTTDevice.cpp:120
void publishDevice(const String &subTopic, const Bytes &payload, bool retained=false, bool sign=false)
Publishes a message to a device-specific subtopic.
Definition MQTTDevice.cpp:108
const String & subscribeDevice(const String &subTopic)
Subscribes to a device-specific subtopic.
Definition MQTTDevice.cpp:83
static unLisp & getInstance()
Definition Singleton.h:73
void runCode(const Bytes &data)
Run Lisp code in the interpreter.
Definition unLisp.h:269
void cleanLastCode()
Clear the stored last executed code.
Definition unLisp.h:337
EventListener< unsigned int, int, Bytes > CoreEventListener
Type alias for the common EventListener configuration used in the core system.
Definition EventListener.h:100
#define UNIOT_LOG_INFO(...)
Log an INFO level message Used for general information about system operation. Only compiled if UNIOT...
Definition Logger.h:268
#define UNIOT_LOG_WARN(...)
Log an WARN level message Used for warnings about potentially problematic situations....
Definition Logger.h:247
#define UNIOT_LOG_ERROR(...)
Log an ERROR level message Used for critical errors that may prevent normal operation....
Definition Logger.h:226
@ OUT_LISP_REQUEST
Topic for Lisp requests to the application services.
Definition LispEvents.h:89
@ OUT_LISP_MSG
Topic for Lisp output messages and notifications.
Definition LispEvents.h:88
@ IN_LISP_EVENT
Topic for incoming events from application to Lisp.
Definition LispEvents.h:91
@ OUT_LISP_EVENT
Topic for outgoing events from Lisp to application.
Definition LispEvents.h:90
@ IN_NEW_EVENT
New incoming event received from application for Lisp processing.
Definition LispEvents.h:107
@ OUT_MSG_ADDED
Standard output message was added to the output buffer.
Definition LispEvents.h:102
@ OUT_MSG_LOG
Log message was generated by the Lisp interpreter.
Definition LispEvents.h:103
@ OUT_NEW_EVENT
New outgoing event generated by Lisp for the application.
Definition LispEvents.h:106
@ OUT_REFRESH_EVENTS
Request to refresh the event queue and process pending events.
Definition LispEvents.h:105
@ OUT_MSG_ERROR
Error message was generated due to Lisp execution failure.
Definition LispEvents.h:104
@ OUT_LISP_LOG
Channel for Lisp log messages and debug information.
Definition LispEvents.h:74
@ IN_EVENT
Channel for incoming events from the application to Lisp.
Definition LispEvents.h:77
@ OUT_LISP
Channel for standard Lisp output (stdout equivalent)
Definition LispEvents.h:73
@ OUT_LISP_ERR
Channel for Lisp error messages and exceptions.
Definition LispEvents.h:75
@ OUT_EVENT
Channel for outgoing events from Lisp to the application.
Definition LispEvents.h:76
Contains all classes and functions related to the Uniot Core.
std::function< bool(const LispEvent &event)> LispEventInterceptor
Definition LispDevice.h:41
Definition LispDevice.h:32
String type
Definition LispDevice.h:33
String id
Definition LispDevice.h:34
Definition LispDevice.h:31
String eventID
Definition LispDevice.h:36
uint64_t timestamp
Definition LispDevice.h:38
struct uniot::LispEvent::Sender sender
int32_t value
Definition LispDevice.h:37