-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathtools.cpp
More file actions
1176 lines (1033 loc) · 50.9 KB
/
Copy pathtools.cpp
File metadata and controls
1176 lines (1033 loc) · 50.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file tools.cpp
* @brief ESP32 tool definitions and handlers for LLM tool calling
*/
#include "tools.h"
#include "llm_client.h"
#include "devices.h"
#include "nats_hal.h"
#include "rules.h"
#include <Arduino.h>
#include <WiFi.h>
#include "soc/soc_caps.h"
#include <LittleFS.h>
#include <nats_esp32.h>
#include <esp_task_wdt.h>
#if !defined(CONFIG_IDF_TARGET_ESP32)
#include "driver/temperature_sensor.h"
#endif
/* Forward declarations (defined in main.cpp) */
extern void led(uint8_t r, uint8_t g, uint8_t b);
extern void ledOff();
extern bool g_led_user;
extern NatsClient natsClient;
extern bool g_nats_connected;
extern bool g_nats_enabled;
#if !defined(CONFIG_IDF_TARGET_ESP32)
extern temperature_sensor_handle_t g_temp_sensor;
#endif
/* NATS virtual sensor helpers (from main.cpp) */
extern void natsSubscribeDeviceSensors();
extern void natsUnsubscribeDevice(const char *name);
/*============================================================================
* Simple JSON argument parser (for tool arguments)
*============================================================================*/
static int jsonArgInt(const char *json, const char *key, int default_val) {
char pattern[48];
snprintf(pattern, sizeof(pattern), "\"%s\"", key);
const char *p = strstr(json, pattern);
if (!p) return default_val;
p += strlen(pattern);
while (*p == ' ' || *p == ':') p++;
return atoi(p);
}
static bool jsonArgString(const char *json, const char *key,
char *dst, int dst_len) {
char pattern[48];
snprintf(pattern, sizeof(pattern), "\"%s\"", key);
const char *p = strstr(json, pattern);
if (!p) return false;
p += strlen(pattern);
while (*p == ' ' || *p == ':') p++;
if (*p != '"') return false;
p++;
int w = 0;
while (*p && *p != '"' && w < dst_len - 1) {
if (*p == '\\' && *(p + 1)) p++;
dst[w++] = *p++;
}
dst[w] = '\0';
return w > 0;
}
static bool jsonArgBool(const char *json, const char *key, bool default_val) {
char pattern[48];
snprintf(pattern, sizeof(pattern), "\"%s\"", key);
const char *p = strstr(json, pattern);
if (!p) return default_val;
p += strlen(pattern);
while (*p == ' ' || *p == ':') p++;
if (strncmp(p, "true", 4) == 0) return true;
if (strncmp(p, "false", 5) == 0) return false;
return default_val;
}
/* Check if a key exists (even with empty string value or null) */
static bool jsonArgExists(const char *json, const char *key) {
char pattern[48];
snprintf(pattern, sizeof(pattern), "\"%s\"", key);
return strstr(json, pattern) != nullptr;
}
/*============================================================================
* Tool Definitions (OpenAI function calling format) - compacted
*============================================================================*/
static const char *TOOLS_JSON = R"JSON([
{"type":"function","function":{"name":"led_set","description":"Set RGB LED 0-255","parameters":{"type":"object","properties":{"r":{"type":"integer"},"g":{"type":"integer"},"b":{"type":"integer"}},"required":["r","g","b"]}}},
{"type":"function","function":{"name":"gpio_write","description":"Set GPIO pin HIGH/LOW","parameters":{"type":"object","properties":{"pin":{"type":"integer"},"value":{"type":"integer"}},"required":["pin","value"]}}},
{"type":"function","function":{"name":"gpio_read","description":"Read GPIO pin state","parameters":{"type":"object","properties":{"pin":{"type":"integer"}},"required":["pin"]}}},
{"type":"function","function":{"name":"device_info","description":"Get heap, uptime, WiFi, chip info","parameters":{"type":"object","properties":{}}}},
{"type":"function","function":{"name":"file_read","description":"Read file from filesystem","parameters":{"type":"object","properties":{"path":{"type":"string"}},"required":["path"]}}},
{"type":"function","function":{"name":"file_write","description":"Write file to filesystem","parameters":{"type":"object","properties":{"path":{"type":"string"},"content":{"type":"string"}},"required":["path","content"]}}},
{"type":"function","function":{"name":"nats_publish","description":"Publish NATS message","parameters":{"type":"object","properties":{"subject":{"type":"string"},"payload":{"type":"string"}},"required":["subject","payload"]}}},
{"type":"function","function":{"name":"temperature_read","description":"Read chip temperature (C)","parameters":{"type":"object","properties":{}}}},
{"type":"function","function":{"name":"device_register","description":"Register sensor/actuator","parameters":{"type":"object","properties":{"name":{"type":"string"},"type":{"type":"string","enum":["digital_in","analog_in","ntc_10k","ldr","nats_value","serial_text","digital_out","relay","pwm"],"description":"digital_in: GPIO digital read, analog_in: raw ADC reading, ntc_10k: NTC 10K thermistor (temp in C, set inverted=true if NTC is on the 3.3V side), ldr: light-dependent resistor (light level), nats_value: virtual sensor from NATS subject, serial_text: UART text input, digital_out: GPIO digital write, relay: relay on/off, pwm: PWM output"},"pin":{"type":"integer"},"unit":{"type":"string"},"inverted":{"type":"boolean"},"subject":{"type":"string","description":"NATS subject (for nats_value)"},"baud":{"type":"integer","description":"Baud rate for serial_text (default 9600)"}},"required":["name","type"]}}},
{"type":"function","function":{"name":"device_list","description":"List registered devices","parameters":{"type":"object","properties":{}}}},
{"type":"function","function":{"name":"device_remove","description":"Remove device by name","parameters":{"type":"object","properties":{"name":{"type":"string"}},"required":["name"]}}},
{"type":"function","function":{"name":"sensor_read","description":"Read named sensor value","parameters":{"type":"object","properties":{"name":{"type":"string"}},"required":["name"]}}},
{"type":"function","function":{"name":"actuator_set","description":"Set actuator value","parameters":{"type":"object","properties":{"name":{"type":"string"},"value":{"type":"integer"}},"required":["name","value"]}}},
{"type":"function","function":{"name":"rule_create","description":"Create automation rule. Use chained condition for chain-only targets.","parameters":{"type":"object","properties":{"rule_name":{"type":"string"},"sensor_name":{"type":"string"},"sensor_pin":{"type":"integer"},"condition":{"type":"string","description":"gt|lt|eq|neq|change|always|chained"},"threshold":{"type":"integer"},"interval_seconds":{"type":"integer"},"actuator_name":{"type":"string"},"on_action":{"type":"string","description":"gpio_write|led_set|nats_publish|actuator|telegram|serial_send"},"on_pin":{"type":"integer"},"on_value":{"type":"integer"},"on_r":{"type":"integer"},"on_g":{"type":"integer"},"on_b":{"type":"integer"},"on_nats_subject":{"type":"string"},"on_nats_payload":{"type":"string"},"on_telegram_message":{"type":"string","description":"Use {value} or {device_name}"},"on_serial_text":{"type":"string","description":"Text to send via serial_text UART"},"off_action":{"type":"string","description":"auto|none|gpio_write|led_set|nats_publish|actuator|telegram|serial_send"},"off_pin":{"type":"integer"},"off_value":{"type":"integer"},"off_r":{"type":"integer"},"off_g":{"type":"integer"},"off_b":{"type":"integer"},"off_nats_subject":{"type":"string"},"off_nats_payload":{"type":"string"},"off_telegram_message":{"type":"string"},"off_serial_text":{"type":"string","description":"Text for serial off-action"},"chain_rule":{"type":"string","description":"Rule ID to trigger after ON action (e.g. rule_01)"},"chain_delay_seconds":{"type":"integer","description":"Delay before ON chain fires (0=immediate)"},"chain_off_rule":{"type":"string","description":"Rule ID to trigger after OFF action"},"chain_off_delay_seconds":{"type":"integer","description":"Delay before OFF chain fires (0=immediate)"}},"required":["rule_name"]}}},
{"type":"function","function":{"name":"rule_list","description":"List all rules","parameters":{"type":"object","properties":{}}}},
{"type":"function","function":{"name":"rule_delete","description":"Delete rule by ID (e.g. rule_01), or pass 'all' to delete every rule at once.","parameters":{"type":"object","properties":{"rule_id":{"type":"string","description":"Rule ID or 'all'"}},"required":["rule_id"]}}},
{"type":"function","function":{"name":"rule_enable","description":"Enable/disable rule","parameters":{"type":"object","properties":{"rule_id":{"type":"string"},"enabled":{"type":"boolean"}},"required":["rule_id","enabled"]}}},
{"type":"function","function":{"name":"serial_send","description":"Send text over serial_text UART","parameters":{"type":"object","properties":{"text":{"type":"string","description":"Text to send (newline appended)"}},"required":["text"]}}},
{"type":"function","function":{"name":"remote_chat","description":"Chat with another WireClaw device via NATS","parameters":{"type":"object","properties":{"device":{"type":"string"},"message":{"type":"string"}},"required":["device","message"]}}},
{"type":"function","function":{"name":"chain_create","description":"Create multi-step automation chain (up to 5 steps) in one call. Steps execute in order with delays.","parameters":{"type":"object","properties":{"sensor_name":{"type":"string","description":"Sensor to monitor"},"condition":{"type":"string","description":"gt|lt|eq|neq|change|always"},"threshold":{"type":"integer"},"interval_seconds":{"type":"integer"},"step1_action":{"type":"string","description":"telegram|led_set|gpio_write|nats_publish|actuator|serial_send"},"step1_message":{"type":"string","description":"For telegram/nats/serial_send"},"step1_r":{"type":"integer"},"step1_g":{"type":"integer"},"step1_b":{"type":"integer"},"step1_pin":{"type":"integer"},"step1_value":{"type":"integer"},"step1_actuator":{"type":"string"},"step1_nats_subject":{"type":"string"},"step2_action":{"type":"string","description":"Action after step1"},"step2_delay":{"type":"integer","description":"Seconds before step2"},"step2_message":{"type":"string"},"step2_r":{"type":"integer"},"step2_g":{"type":"integer"},"step2_b":{"type":"integer"},"step2_pin":{"type":"integer"},"step2_value":{"type":"integer"},"step2_actuator":{"type":"string"},"step2_nats_subject":{"type":"string"},"step3_action":{"type":"string","description":"Step3 (optional)"},"step3_delay":{"type":"integer","description":"Seconds before step3"},"step3_message":{"type":"string"},"step3_r":{"type":"integer"},"step3_g":{"type":"integer"},"step3_b":{"type":"integer"},"step3_pin":{"type":"integer"},"step3_value":{"type":"integer"},"step3_actuator":{"type":"string"},"step3_nats_subject":{"type":"string"},"step4_action":{"type":"string","description":"Step4 (optional)"},"step4_delay":{"type":"integer","description":"Seconds before step4"},"step4_message":{"type":"string"},"step4_r":{"type":"integer"},"step4_g":{"type":"integer"},"step4_b":{"type":"integer"},"step4_pin":{"type":"integer"},"step4_value":{"type":"integer"},"step4_actuator":{"type":"string"},"step4_nats_subject":{"type":"string"},"step5_action":{"type":"string","description":"Step5 (optional)"},"step5_delay":{"type":"integer","description":"Seconds before step5"},"step5_message":{"type":"string"},"step5_r":{"type":"integer"},"step5_g":{"type":"integer"},"step5_b":{"type":"integer"},"step5_pin":{"type":"integer"},"step5_value":{"type":"integer"},"step5_actuator":{"type":"string"},"step5_nats_subject":{"type":"string"}},"required":["sensor_name","condition","threshold","step1_action","step2_action"]}}}
])JSON";
/*============================================================================
* Original Tool Handlers
*============================================================================*/
static void tool_led_set(const char *args, char *result, int result_len) {
int r = jsonArgInt(args, "r", 0);
int g = jsonArgInt(args, "g", 0);
int b = jsonArgInt(args, "b", 0);
r = constrain(r, 0, 255);
g = constrain(g, 0, 255);
b = constrain(b, 0, 255);
led((uint8_t)r, (uint8_t)g, (uint8_t)b);
g_led_user = true;
Device *rgb = deviceFind("rgb_led");
if (rgb) rgb->last_value = (r << 16) | (g << 8) | b;
snprintf(result, result_len, "LED set to RGB(%d, %d, %d)", r, g, b);
}
static void tool_gpio_write(const char *args, char *result, int result_len) {
int pin = jsonArgInt(args, "pin", -1);
int value = jsonArgInt(args, "value", 0);
if (pin < 0 || pin >= SOC_GPIO_PIN_COUNT) {
snprintf(result, result_len, "Error: invalid pin %d (must be 0-%d)", pin, SOC_GPIO_PIN_COUNT - 1);
return;
}
pinMode(pin, OUTPUT);
digitalWrite(pin, value ? HIGH : LOW);
snprintf(result, result_len, "GPIO %d set to %s", pin, value ? "HIGH" : "LOW");
}
static void tool_gpio_read(const char *args, char *result, int result_len) {
int pin = jsonArgInt(args, "pin", -1);
if (pin < 0 || pin >= SOC_GPIO_PIN_COUNT) {
snprintf(result, result_len, "Error: invalid pin %d (must be 0-%d)", pin, SOC_GPIO_PIN_COUNT - 1);
return;
}
int value = digitalRead(pin);
snprintf(result, result_len, "GPIO %d = %d (%s)", pin, value,
value ? "HIGH" : "LOW");
}
static void tool_device_info(const char *args, char *result, int result_len) {
(void)args;
snprintf(result, result_len,
"Free heap: %u bytes, Total heap: %u bytes, "
"Uptime: %lu seconds, "
"WiFi: %s, IP: %s, "
"Chip: %s rev %d, %d cores, %lu MHz",
ESP.getFreeHeap(), ESP.getHeapSize(),
millis() / 1000,
WiFi.status() == WL_CONNECTED ? "connected" : "disconnected",
WiFi.localIP().toString().c_str(),
ESP.getChipModel(), ESP.getChipRevision(),
ESP.getChipCores(), ESP.getCpuFreqMHz());
}
static void tool_file_read(const char *args, char *result, int result_len) {
char path[64];
if (!jsonArgString(args, "path", path, sizeof(path))) {
snprintf(result, result_len, "Error: missing 'path' argument");
return;
}
File f = LittleFS.open(path, "r");
if (!f) {
snprintf(result, result_len, "Error: file not found: %s", path);
return;
}
int len = f.readBytes(result, result_len - 1);
result[len] = '\0';
f.close();
}
static void tool_file_write(const char *args, char *result, int result_len) {
char path[64];
char content[512];
if (!jsonArgString(args, "path", path, sizeof(path))) {
snprintf(result, result_len, "Error: missing 'path' argument");
return;
}
/* Protect config.json from being overwritten */
if (strcmp(path, "/config.json") == 0) {
snprintf(result, result_len, "Error: cannot overwrite config.json via tool");
return;
}
if (!jsonArgString(args, "content", content, sizeof(content))) {
snprintf(result, result_len, "Error: missing 'content' argument");
return;
}
File f = LittleFS.open(path, "w");
if (!f) {
snprintf(result, result_len, "Error: cannot open %s for writing", path);
return;
}
f.print(content);
f.close();
snprintf(result, result_len, "Wrote %d bytes to %s", (int)strlen(content), path);
}
static void tool_nats_publish(const char *args, char *result, int result_len) {
if (!g_nats_connected) {
snprintf(result, result_len, "Error: NATS not connected");
return;
}
char subject[128];
char payload[256];
if (!jsonArgString(args, "subject", subject, sizeof(subject))) {
snprintf(result, result_len, "Error: missing 'subject' argument");
return;
}
if (!jsonArgString(args, "payload", payload, sizeof(payload))) {
snprintf(result, result_len, "Error: missing 'payload' argument");
return;
}
nats_err_t err = natsClient.publish(subject, payload);
if (err == NATS_OK) {
snprintf(result, result_len, "Published to %s: %s", subject, payload);
} else {
snprintf(result, result_len, "Error: publish failed: %s",
nats_err_str(err));
}
}
static void tool_temperature_read(const char *args, char *result, int result_len) {
(void)args;
#if !defined(CONFIG_IDF_TARGET_ESP32)
float temp = 0.0f;
if (g_temp_sensor != NULL) {
temperature_sensor_get_celsius(g_temp_sensor, &temp);
}
snprintf(result, result_len, "Chip temperature: %.1f C", temp);
#else
snprintf(result, result_len, "Error: temperature sensor not available on this chip");
#endif
}
/*============================================================================
* Device Registry Tool Handlers
*============================================================================*/
static void tool_device_register(const char *args, char *result, int result_len) {
char name[DEV_NAME_LEN];
char type_str[24];
if (!jsonArgString(args, "name", name, sizeof(name))) {
snprintf(result, result_len, "Error: missing 'name'");
return;
}
if (!jsonArgString(args, "type", type_str, sizeof(type_str))) {
snprintf(result, result_len, "Error: missing 'type'");
return;
}
int pin = jsonArgInt(args, "pin", PIN_NONE);
char unit[DEV_UNIT_LEN] = "";
jsonArgString(args, "unit", unit, sizeof(unit));
bool inverted = jsonArgBool(args, "inverted", false);
char subject[32] = "";
jsonArgString(args, "subject", subject, sizeof(subject));
/* Map type string to DeviceKind */
DeviceKind kind;
if (strcmp(type_str, "digital_in") == 0) kind = DEV_SENSOR_DIGITAL;
else if (strcmp(type_str, "analog_in") == 0) kind = DEV_SENSOR_ANALOG_RAW;
else if (strcmp(type_str, "ntc_10k") == 0) kind = DEV_SENSOR_NTC_10K;
else if (strcmp(type_str, "ldr") == 0) kind = DEV_SENSOR_LDR;
else if (strcmp(type_str, "nats_value") == 0) kind = DEV_SENSOR_NATS_VALUE;
else if (strcmp(type_str, "serial_text") == 0) kind = DEV_SENSOR_SERIAL_TEXT;
else if (strcmp(type_str, "digital_out") == 0) kind = DEV_ACTUATOR_DIGITAL;
else if (strcmp(type_str, "relay") == 0) kind = DEV_ACTUATOR_RELAY;
else if (strcmp(type_str, "pwm") == 0) kind = DEV_ACTUATOR_PWM;
else {
snprintf(result, result_len, "Error: unknown type '%s'", type_str);
return;
}
/* Validate: nats_value requires subject, serial_text enforces one-device limit */
if (kind == DEV_SENSOR_NATS_VALUE) {
if (subject[0] == '\0') {
snprintf(result, result_len, "Error: nats_value requires 'subject'");
return;
}
if (!g_nats_enabled) {
snprintf(result, result_len, "Warning: NATS not enabled. Registered but won't receive data");
}
pin = PIN_NONE;
} else if (kind == DEV_SENSOR_SERIAL_TEXT) {
/* Only one serial_text device allowed */
Device *devs = deviceGetAll();
for (int i = 0; i < MAX_DEVICES; i++) {
if (devs[i].used && devs[i].kind == DEV_SENSOR_SERIAL_TEXT) {
snprintf(result, result_len, "Error: only one serial_text device allowed (already: '%s')",
devs[i].name);
return;
}
}
pin = PIN_NONE;
} else if (pin == PIN_NONE && deviceIsActuator(kind)) {
snprintf(result, result_len, "Error: actuator requires 'pin'");
return;
}
int baud_rate = jsonArgInt(args, "baud", 9600);
if (halIsReservedName(name)) {
snprintf(result, result_len, "Error: '%s' is a reserved HAL keyword", name);
return;
}
if (!deviceRegister(name, kind, (uint8_t)pin, unit, inverted,
subject[0] ? subject : nullptr,
kind == DEV_SENSOR_SERIAL_TEXT ? (uint32_t)baud_rate : 0)) {
snprintf(result, result_len, "Error: register failed (duplicate name or full)");
return;
}
devicesSave();
if (kind == DEV_SENSOR_NATS_VALUE) {
natsSubscribeDeviceSensors();
snprintf(result, result_len, "Registered nats_value sensor '%s' on subject '%s'",
name, subject);
} else if (kind == DEV_SENSOR_SERIAL_TEXT) {
snprintf(result, result_len, "Registered serial_text sensor '%s' at %d baud (RX=%d TX=%d)",
name, baud_rate, SERIAL_TEXT_RX, SERIAL_TEXT_TX);
} else {
snprintf(result, result_len, "Registered %s '%s' on pin %d",
deviceIsSensor(kind) ? "sensor" : "output", name, pin);
}
}
static void tool_device_list(const char *args, char *result, int result_len) {
(void)args;
Device *devs = deviceGetAll();
int w = 0;
int count = 0;
for (int i = 0; i < MAX_DEVICES && w < result_len - 40; i++) {
if (!devs[i].used) continue;
Device *d = &devs[i];
if (count > 0) w += snprintf(result + w, result_len - w, "; ");
if (d->kind == DEV_SENSOR_SERIAL_TEXT) {
float val = deviceReadSensor(d);
const char *msg = serialTextGetMsg();
w += snprintf(result + w, result_len - w, "%s(serial_text %ubaud)=%.1f%s",
d->name, (unsigned)d->baud, val, d->unit);
if (msg[0]) {
w += snprintf(result + w, result_len - w, " msg='%.20s'", msg);
}
} else if (d->kind == DEV_SENSOR_NATS_VALUE) {
float val = deviceReadSensor(d);
w += snprintf(result + w, result_len - w, "%s(nats_value %s)=%.1f%s",
d->name, d->nats_subject, val, d->unit);
} else if (deviceIsSensor(d->kind)) {
float val = deviceReadSensor(d);
w += snprintf(result + w, result_len - w, "%s(%s pin%d)=%.1f%s",
d->name, deviceKindName(d->kind), d->pin, val, d->unit);
} else {
w += snprintf(result + w, result_len - w, "%s(%s pin%d%s)",
d->name, deviceKindName(d->kind), d->pin,
d->inverted ? " inv" : "");
}
count++;
}
if (count == 0) {
snprintf(result, result_len, "No devices registered");
}
}
static void tool_device_remove(const char *args, char *result, int result_len) {
char name[DEV_NAME_LEN];
if (!jsonArgString(args, "name", name, sizeof(name))) {
snprintf(result, result_len, "Error: missing 'name'");
return;
}
/* Unsubscribe NATS if this is a nats_value device */
Device *dev = deviceFind(name);
if (dev && dev->kind == DEV_SENSOR_NATS_VALUE) {
natsUnsubscribeDevice(name);
}
if (!deviceRemove(name)) {
snprintf(result, result_len, "Error: device '%s' not found", name);
return;
}
devicesSave();
snprintf(result, result_len, "Removed device '%s'", name);
}
static void tool_sensor_read(const char *args, char *result, int result_len) {
char name[DEV_NAME_LEN];
if (!jsonArgString(args, "name", name, sizeof(name))) {
snprintf(result, result_len, "Error: missing 'name'");
return;
}
Device *dev = deviceFind(name);
if (!dev) {
snprintf(result, result_len, "Error: sensor '%s' not found", name);
return;
}
if (!deviceIsSensor(dev->kind)) {
snprintf(result, result_len, "Error: '%s' is not a sensor", name);
return;
}
float val = deviceReadSensor(dev);
if (dev->kind == DEV_SENSOR_SERIAL_TEXT) {
const char *msg = serialTextGetMsg();
if (msg[0])
snprintf(result, result_len, "%s: %.1f %s (last: '%s')", name, val, dev->unit, msg);
else
snprintf(result, result_len, "%s: %.1f %s (no data yet)", name, val, dev->unit);
} else {
snprintf(result, result_len, "%s: %.1f %s", name, val, dev->unit);
}
}
static void tool_actuator_set(const char *args, char *result, int result_len) {
char name[DEV_NAME_LEN];
if (!jsonArgString(args, "name", name, sizeof(name))) {
snprintf(result, result_len, "Error: missing 'name'");
return;
}
int value = jsonArgInt(args, "value", 0);
Device *dev = deviceFind(name);
if (!dev) {
snprintf(result, result_len, "Error: actuator '%s' not found", name);
return;
}
if (!deviceIsActuator(dev->kind)) {
snprintf(result, result_len, "Error: '%s' is not an actuator", name);
return;
}
if (!deviceSetActuator(dev, value)) {
snprintf(result, result_len, "Error: failed to set '%s'", name);
return;
}
snprintf(result, result_len, "Set %s to %d", name, value);
}
/*============================================================================
* Rule Engine Tool Handlers
*============================================================================*/
static ActionType parseActionType(const char *s) {
if (strcmp(s, "gpio_write") == 0) return ACT_GPIO_WRITE;
if (strcmp(s, "led_set") == 0) return ACT_LED_SET;
if (strcmp(s, "nats_publish") == 0) return ACT_NATS_PUBLISH;
if (strcmp(s, "actuator") == 0) return ACT_ACTUATOR;
if (strcmp(s, "telegram") == 0) return ACT_TELEGRAM;
if (strcmp(s, "serial_send") == 0) return ACT_SERIAL_SEND;
return ACT_GPIO_WRITE;
}
static ConditionOp parseConditionOp(const char *s) {
if (strcmp(s, "gt") == 0) return COND_GT;
if (strcmp(s, "lt") == 0) return COND_LT;
if (strcmp(s, "eq") == 0) return COND_EQ;
if (strcmp(s, "neq") == 0) return COND_NEQ;
if (strcmp(s, "change") == 0) return COND_CHANGE;
if (strcmp(s, "always") == 0) return COND_ALWAYS;
if (strcmp(s, "chained") == 0) return COND_CHAINED;
return COND_GT;
}
static void tool_rule_create(const char *args, char *result, int result_len) {
char rule_name[RULE_NAME_LEN];
if (!jsonArgString(args, "rule_name", rule_name, sizeof(rule_name))) {
snprintf(result, result_len, "Error: missing 'rule_name'");
return;
}
/* Sensor source: device name or raw pin */
char sensor_name[DEV_NAME_LEN] = "";
jsonArgString(args, "sensor_name", sensor_name, sizeof(sensor_name));
uint8_t sensor_pin = (uint8_t)jsonArgInt(args, "sensor_pin", PIN_NONE);
char cond_str[16];
if (!jsonArgString(args, "condition", cond_str, sizeof(cond_str))) {
/* Default to "chained" if no condition and no sensor source */
if (!sensor_name[0] && sensor_pin == PIN_NONE) {
strcpy(cond_str, "chained");
} else {
snprintf(result, result_len, "Error: missing 'condition'");
return;
}
}
ConditionOp condition = parseConditionOp(cond_str);
int32_t threshold = jsonArgInt(args, "threshold", 0);
bool sensor_analog = false;
/* Validate sensor source (not required for COND_CHAINED) */
if (condition != COND_CHAINED) {
if (sensor_name[0]) {
Device *dev = deviceFind(sensor_name);
if (!dev) {
snprintf(result, result_len, "Error: sensor '%s' not found in device registry", sensor_name);
return;
}
if (!deviceIsSensor(dev->kind)) {
snprintf(result, result_len, "Error: '%s' is not a sensor", sensor_name);
return;
}
} else if (sensor_pin == PIN_NONE) {
snprintf(result, result_len, "Error: provide sensor_name or sensor_pin");
return;
}
}
int interval_s = jsonArgInt(args, "interval_seconds", 5);
if (interval_s < 5) interval_s = 5;
uint32_t interval_ms = (uint32_t)interval_s * 1000;
/* Determine ON action */
ActionType on_action = ACT_GPIO_WRITE;
char on_actuator[DEV_NAME_LEN] = "";
uint8_t on_pin = 0;
int32_t on_value = 1;
char on_nats_subj[RULE_NATS_SUBJ_LEN] = "";
char on_nats_pay[RULE_NATS_PAY_LEN] = "";
/* Determine OFF action */
bool has_off = false;
ActionType off_action = ACT_GPIO_WRITE;
char off_actuator[DEV_NAME_LEN] = "";
uint8_t off_pin = 0;
int32_t off_value = 0;
char off_nats_subj[RULE_NATS_SUBJ_LEN] = "";
char off_nats_pay[RULE_NATS_PAY_LEN] = "";
/* Check for actuator_name shorthand */
char actuator_name[DEV_NAME_LEN] = "";
jsonArgString(args, "actuator_name", actuator_name, sizeof(actuator_name));
if (actuator_name[0]) {
/* Validate actuator exists */
Device *act = deviceFind(actuator_name);
if (!act) {
snprintf(result, result_len, "Error: actuator '%s' not found", actuator_name);
return;
}
if (!deviceIsActuator(act->kind)) {
snprintf(result, result_len, "Error: '%s' is not an actuator", actuator_name);
return;
}
on_action = ACT_ACTUATOR;
strncpy(on_actuator, actuator_name, DEV_NAME_LEN - 1);
/* Auto-set off action */
has_off = true;
off_action = ACT_ACTUATOR;
strncpy(off_actuator, actuator_name, DEV_NAME_LEN - 1);
} else {
/* Explicit on_action */
char act_str[24] = "";
jsonArgString(args, "on_action", act_str, sizeof(act_str));
if (act_str[0]) {
on_action = parseActionType(act_str);
}
on_pin = (uint8_t)jsonArgInt(args, "on_pin", 0);
on_value = jsonArgInt(args, "on_value", 1);
jsonArgString(args, "on_nats_subject", on_nats_subj, sizeof(on_nats_subj));
jsonArgString(args, "on_nats_payload", on_nats_pay, sizeof(on_nats_pay));
/* For telegram action, store message in on_nats_pay (reused field) */
if (on_action == ACT_TELEGRAM) {
jsonArgString(args, "on_telegram_message", on_nats_pay, sizeof(on_nats_pay));
}
/* For serial_send action, store text in on_nats_pay (reused field) */
if (on_action == ACT_SERIAL_SEND) {
jsonArgString(args, "on_serial_text", on_nats_pay, sizeof(on_nats_pay));
}
/* Pack on_r/on_g/on_b into on_value for led_set */
if (on_action == ACT_LED_SET && jsonArgExists(args, "on_r")) {
int r = constrain(jsonArgInt(args, "on_r", 0), 0, 255);
int g = constrain(jsonArgInt(args, "on_g", 0), 0, 255);
int b = constrain(jsonArgInt(args, "on_b", 0), 0, 255);
on_value = (r << 16) | (g << 8) | b;
}
}
/* Check for explicit off_action */
char off_act_str[24] = "";
jsonArgString(args, "off_action", off_act_str, sizeof(off_act_str));
if (off_act_str[0]) {
if (strcmp(off_act_str, "none") == 0) {
has_off = false;
} else if (strcmp(off_act_str, "auto") == 0) {
has_off = true;
off_action = on_action;
strncpy(off_actuator, on_actuator, DEV_NAME_LEN - 1);
off_pin = on_pin;
off_value = 0;
/* For led_set auto-off, check if off_r/off_g/off_b provided */
if (off_action == ACT_LED_SET && jsonArgExists(args, "off_r")) {
int r = constrain(jsonArgInt(args, "off_r", 0), 0, 255);
int g = constrain(jsonArgInt(args, "off_g", 0), 0, 255);
int b = constrain(jsonArgInt(args, "off_b", 0), 0, 255);
off_value = (r << 16) | (g << 8) | b;
}
/* For telegram auto-off, check if off_telegram_message provided */
if (off_action == ACT_TELEGRAM) {
jsonArgString(args, "off_telegram_message", off_nats_pay, sizeof(off_nats_pay));
}
if (off_action == ACT_SERIAL_SEND) {
jsonArgString(args, "off_serial_text", off_nats_pay, sizeof(off_nats_pay));
}
} else {
has_off = true;
off_action = parseActionType(off_act_str);
off_pin = (uint8_t)jsonArgInt(args, "off_pin", 0);
off_value = jsonArgInt(args, "off_value", 0);
jsonArgString(args, "off_nats_subject", off_nats_subj, sizeof(off_nats_subj));
jsonArgString(args, "off_nats_payload", off_nats_pay, sizeof(off_nats_pay));
/* Pack off_r/off_g/off_b into off_value for led_set */
if (off_action == ACT_LED_SET && jsonArgExists(args, "off_r")) {
int r = constrain(jsonArgInt(args, "off_r", 0), 0, 255);
int g = constrain(jsonArgInt(args, "off_g", 0), 0, 255);
int b = constrain(jsonArgInt(args, "off_b", 0), 0, 255);
off_value = (r << 16) | (g << 8) | b;
}
/* For telegram action, store message in off_nats_pay */
if (off_action == ACT_TELEGRAM) {
jsonArgString(args, "off_telegram_message", off_nats_pay, sizeof(off_nats_pay));
}
if (off_action == ACT_SERIAL_SEND) {
jsonArgString(args, "off_serial_text", off_nats_pay, sizeof(off_nats_pay));
}
}
}
/* Chain parameters */
char chain_rule[RULE_ID_LEN] = "";
jsonArgString(args, "chain_rule", chain_rule, sizeof(chain_rule));
uint32_t chain_delay_ms = (uint32_t)jsonArgInt(args, "chain_delay_seconds", 0) * 1000;
char chain_off_rule[RULE_ID_LEN] = "";
jsonArgString(args, "chain_off_rule", chain_off_rule, sizeof(chain_off_rule));
uint32_t chain_off_delay_ms = (uint32_t)jsonArgInt(args, "chain_off_delay_seconds", 0) * 1000;
const char *id = ruleCreate(rule_name, sensor_name, sensor_pin, sensor_analog,
condition, threshold, interval_ms,
on_action, on_actuator, on_pin, on_value,
on_nats_subj, on_nats_pay,
has_off, off_action, off_actuator,
off_pin, off_value, off_nats_subj, off_nats_pay,
chain_rule, chain_delay_ms,
chain_off_rule, chain_off_delay_ms);
if (!id) {
snprintf(result, result_len, "Error: rule creation failed (max %d rules)", MAX_RULES);
return;
}
/* Detect self-reference (ruleCreate silently cleared it) */
if (chain_rule[0] && strcmp(chain_rule, id) == 0)
chain_rule[0] = '\0';
if (chain_off_rule[0] && strcmp(chain_off_rule, id) == 0)
chain_off_rule[0] = '\0';
rulesSave();
/* Build descriptive response */
const char *cond_sym = "?";
switch (condition) {
case COND_GT: cond_sym = ">"; break;
case COND_LT: cond_sym = "<"; break;
case COND_EQ: cond_sym = "=="; break;
case COND_NEQ: cond_sym = "!="; break;
case COND_CHANGE: cond_sym = "changed"; break;
case COND_ALWAYS: cond_sym = "always"; break;
case COND_CHAINED: cond_sym = "chained"; break;
}
int w = 0;
if (condition == COND_CHAINED) {
w = snprintf(result, result_len, "Rule created: %s '%s' - chained (fires only via chain)",
id, rule_name);
} else {
const char *src = sensor_name[0] ? sensor_name : "pin";
w = snprintf(result, result_len, "Rule created: %s '%s' - %s %s %d (every %ds)%s",
id, rule_name, src, cond_sym, (int)threshold, interval_s,
has_off ? " with auto-off" : "");
}
if (chain_rule[0] && w < result_len - 1) {
w += snprintf(result + w, result_len - w, " ->%s(%ds)",
chain_rule, (int)(chain_delay_ms / 1000));
}
if (chain_off_rule[0] && w < result_len - 1) {
w += snprintf(result + w, result_len - w, " off->%s(%ds)",
chain_off_rule, (int)(chain_off_delay_ms / 1000));
}
/* Warn about orphan delays */
if (!chain_rule[0] && chain_delay_ms > 0 && w < result_len - 1) {
w += snprintf(result + w, result_len - w,
" (Warning: chain_delay ignored, no chain_rule)");
}
if (!chain_off_rule[0] && chain_off_delay_ms > 0 && w < result_len - 1) {
w += snprintf(result + w, result_len - w,
" (Warning: chain_off_delay ignored, no chain_off_rule)");
}
}
static void tool_rule_list(const char *args, char *result, int result_len) {
(void)args;
const Rule *rules = ruleGetAll();
int w = 0;
int count = 0;
for (int i = 0; i < MAX_RULES && w < result_len - 60; i++) {
if (!rules[i].used) continue;
const Rule *r = &rules[i];
if (count > 0) w += snprintf(result + w, result_len - w, "; ");
uint32_t ago = r->last_triggered ? (millis() - r->last_triggered) / 1000 : 0;
if (r->condition == COND_CHAINED) {
w += snprintf(result + w, result_len - w,
"%s '%s' %s chained %s last=%us",
r->id, r->name,
r->enabled ? "ON" : "OFF",
r->fired ? "FIRED" : "idle",
(unsigned)ago);
} else {
uint32_t eval_ago = r->last_eval ? (millis() - r->last_eval) / 1000 : 0;
w += snprintf(result + w, result_len - w,
"%s '%s' %s %s%s %d val=%.1f %s last=%us eval=%us every=%us",
r->id, r->name,
r->enabled ? "ON" : "OFF",
r->sensor_name[0] ? r->sensor_name : "pin",
r->sensor_name[0] ? "" : "",
(int)r->threshold,
r->last_reading,
r->fired ? "FIRED" : "idle",
(unsigned)ago,
(unsigned)eval_ago, (unsigned)(r->interval_ms / 1000));
}
if (r->chain_id[0]) {
w += snprintf(result + w, result_len - w, " ->%s(%us)",
r->chain_id, (unsigned)(r->chain_delay_ms / 1000));
}
if (r->chain_off_id[0]) {
w += snprintf(result + w, result_len - w, " off->%s(%us)",
r->chain_off_id, (unsigned)(r->chain_off_delay_ms / 1000));
}
count++;
}
if (count == 0) {
snprintf(result, result_len, "No rules defined");
}
}
static void tool_rule_delete(const char *args, char *result, int result_len) {
char rule_id[RULE_ID_LEN];
if (!jsonArgString(args, "rule_id", rule_id, sizeof(rule_id))) {
snprintf(result, result_len, "Error: missing 'rule_id'");
return;
}
if (!ruleDelete(rule_id)) {
snprintf(result, result_len, "Error: rule '%s' not found", rule_id);
return;
}
rulesSave();
if (strcmp(rule_id, "all") == 0)
snprintf(result, result_len, "All rules deleted");
else
snprintf(result, result_len, "Deleted rule %s", rule_id);
}
static void tool_rule_enable(const char *args, char *result, int result_len) {
char rule_id[RULE_ID_LEN];
if (!jsonArgString(args, "rule_id", rule_id, sizeof(rule_id))) {
snprintf(result, result_len, "Error: missing 'rule_id'");
return;
}
bool enabled = jsonArgBool(args, "enabled", true);
if (!ruleEnable(rule_id, enabled)) {
snprintf(result, result_len, "Error: rule '%s' not found", rule_id);
return;
}
rulesSave();
snprintf(result, result_len, "Rule %s %s", rule_id, enabled ? "enabled" : "disabled");
}
/*============================================================================
* Serial Text Tool Handler
*============================================================================*/
static void tool_serial_send(const char *args, char *result, int result_len) {
if (!serialTextActive()) {
snprintf(result, result_len,
"Error: no serial_text device registered. "
"Use device_register with type='serial_text' first.");
return;
}
char text[256];
if (!jsonArgString(args, "text", text, sizeof(text))) {
snprintf(result, result_len, "Error: missing 'text' argument");
return;
}
if (serialTextSend(text)) {
snprintf(result, result_len, "Sent to serial: %s", text);
} else {
snprintf(result, result_len, "Error: serial send failed");
}
}
/*============================================================================
* Multi-Device Tool Handler
*============================================================================*/
static void tool_remote_chat(const char *args, char *result, int result_len) {
if (!g_nats_connected) {
snprintf(result, result_len, "Error: NATS not connected");
return;
}
char device[32];
char message[256];
if (!jsonArgString(args, "device", device, sizeof(device))) {
snprintf(result, result_len, "Error: missing 'device'");
return;
}
if (!jsonArgString(args, "message", message, sizeof(message))) {
snprintf(result, result_len, "Error: missing 'message'");
return;
}
/* Build subject: "{device}.chat" */
char subject[64];
snprintf(subject, sizeof(subject), "%s.chat", device);
/* NATS request/reply with 30s timeout */
static nats_request_t req;
nats_err_t err = natsClient.requestStart(&req, subject, message, 30000);
if (err != NATS_OK) {
snprintf(result, result_len, "Error: request failed: %s", nats_err_str(err));
return;
}
/* Poll until complete or timeout */
while (true) {
esp_task_wdt_reset();
natsClient.process();
nats_err_t status = natsClient.requestCheck(&req);
if (status == NATS_OK) {
/* Got response - response_data is uint8_t[], response_len is size_t */
int copy_len = (int)req.response_len < result_len - 1
? (int)req.response_len : result_len - 1;
memcpy(result, req.response_data, copy_len);
result[copy_len] = '\0';
return;
}
if (status != NATS_ERR_WOULD_BLOCK) {
snprintf(result, result_len, "Error: %s (device '%s' may be offline)",
nats_err_str(status), device);
return;
}
delay(50);
}
}
/*============================================================================
* Chain Create — multi-step chain in one call
*============================================================================*/
/* Helper: parse step action params from prefixed keys */
static void parseStepAction(const char *args, const char *prefix,
ActionType &action, uint8_t &pin, int32_t &value,
char *nats_subj, int subj_len,
char *nats_pay, int pay_len,
char *actuator, int act_len) {
char key[32];
/* action */
snprintf(key, sizeof(key), "%s_action", prefix);
char act_str[24] = "";
jsonArgString(args, key, act_str, sizeof(act_str));
if (act_str[0]) action = parseActionType(act_str);
/* message — used for telegram, nats_publish, serial_send */
snprintf(key, sizeof(key), "%s_message", prefix);
jsonArgString(args, key, nats_pay, pay_len);
/* nats_subject */
snprintf(key, sizeof(key), "%s_nats_subject", prefix);
jsonArgString(args, key, nats_subj, subj_len);
/* actuator name */
snprintf(key, sizeof(key), "%s_actuator", prefix);
jsonArgString(args, key, actuator, act_len);
/* pin / value */
snprintf(key, sizeof(key), "%s_pin", prefix);
pin = (uint8_t)jsonArgInt(args, key, 0);
snprintf(key, sizeof(key), "%s_value", prefix);
value = jsonArgInt(args, key, 1);
/* RGB packing for led_set */
if (action == ACT_LED_SET) {
snprintf(key, sizeof(key), "%s_r", prefix);
if (jsonArgExists(args, key)) {
int r = constrain(jsonArgInt(args, key, 0), 0, 255);
snprintf(key, sizeof(key), "%s_g", prefix);
int g = constrain(jsonArgInt(args, key, 0), 0, 255);
snprintf(key, sizeof(key), "%s_b", prefix);
int b = constrain(jsonArgInt(args, key, 0), 0, 255);
value = (r << 16) | (g << 8) | b;
}
}
}
/* Helper: format action name for result string */
static int fmtStepAction(char *buf, int len, ActionType action, int32_t value,
const char *message) {
switch (action) {
case ACT_LED_SET: {
int r = (value >> 16) & 0xFF;
int g = (value >> 8) & 0xFF;
int b = value & 0xFF;
return snprintf(buf, len, "LED(%d,%d,%d)", r, g, b);
}
case ACT_TELEGRAM:
return snprintf(buf, len, "telegram");
case ACT_GPIO_WRITE:
return snprintf(buf, len, "gpio(%d)", (int)value);
case ACT_NATS_PUBLISH:
return snprintf(buf, len, "nats");
case ACT_ACTUATOR:
return snprintf(buf, len, "actuator");
case ACT_SERIAL_SEND:
return snprintf(buf, len, "serial");
default:
return snprintf(buf, len, "?");
}
}
static void tool_chain_create(const char *args, char *result, int result_len) {
/* --- Parse trigger (source) params --- */
char sensor_name[DEV_NAME_LEN] = "";
if (!jsonArgString(args, "sensor_name", sensor_name, sizeof(sensor_name))) {
snprintf(result, result_len, "Error: missing 'sensor_name'");
return;
}
char cond_str[16] = "";