forked from letscontrolit/ESPEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_C010.ino
100 lines (83 loc) · 3.33 KB
/
_C010.ino
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
//#######################################################################################################
//########################### Controller Plugin 010: Generic UDP ########################################
//#######################################################################################################
#define CPLUGIN_010
#define CPLUGIN_ID_010 10
#define CPLUGIN_NAME_010 "Generic UDP"
boolean CPlugin_010(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case CPLUGIN_PROTOCOL_ADD:
{
Protocol[++protocolCount].Number = CPLUGIN_ID_010;
Protocol[protocolCount].usesMQTT = false;
Protocol[protocolCount].usesTemplate = true;
Protocol[protocolCount].usesAccount = false;
Protocol[protocolCount].usesPassword = false;
Protocol[protocolCount].defaultPort = 514;
break;
}
case CPLUGIN_GET_DEVICENAME:
{
string = F(CPLUGIN_NAME_010);
break;
}
case CPLUGIN_PROTOCOL_TEMPLATE:
{
strcpy_P(Settings.MQTTpublish, PSTR("%sysname%_%tskname%_%valname%=%value%"));
break;
}
case CPLUGIN_PROTOCOL_SEND:
{
byte valueCount = getValueCountFromSensorType(event->sensorType);
for (byte x = 0; x < valueCount; x++)
{
if (event->sensorType == SENSOR_TYPE_LONG)
C010_Send(event, 0, 0, (unsigned long)UserVar[event->BaseVarIndex] + ((unsigned long)UserVar[event->BaseVarIndex + 1] << 16));
else
C010_Send(event, x, UserVar[event->BaseVarIndex + x], 0);
if (valueCount > 1)
{
unsigned long timer = millis() + Settings.MessageDelay;
while (millis() < timer)
backgroundtasks();
}
}
break;
}
}
return success;
}
//********************************************************************************
// Generic UDP message
//********************************************************************************
boolean C010_Send(struct EventStruct *event, byte varIndex, float value, unsigned long longValue)
{
char log[80];
boolean success = false;
char host[20];
sprintf_P(host, PSTR("%u.%u.%u.%u"), Settings.Controller_IP[0], Settings.Controller_IP[1], Settings.Controller_IP[2], Settings.Controller_IP[3]);
sprintf_P(log, PSTR("%s%s using port %u"), "UDP : sending to ", host, Settings.ControllerPort);
addLog(LOG_LEVEL_DEBUG, log);
statusLED(true);
if (ExtraTaskSettings.TaskDeviceValueNames[0][0] == 0)
PluginCall(PLUGIN_GET_DEVICEVALUENAMES, event, dummyString);
String msg = "";
msg += Settings.MQTTpublish;
msg.replace("%sysname%", Settings.Name);
msg.replace("%tskname%", ExtraTaskSettings.TaskDeviceName);
msg.replace("%id%", String(event->idx));
msg.replace("%valname%", ExtraTaskSettings.TaskDeviceValueNames[varIndex]);
if (longValue)
msg.replace("%value%", String(longValue));
else
msg.replace("%value%", toString(value, ExtraTaskSettings.TaskDeviceValueDecimals[varIndex]));
IPAddress IP(Settings.Controller_IP[0], Settings.Controller_IP[1], Settings.Controller_IP[2], Settings.Controller_IP[3]);
portUDP.beginPacket(IP, Settings.ControllerPort);
portUDP.write(msg.c_str());
portUDP.endPacket();
msg.toCharArray(log, 80);
addLog(LOG_LEVEL_DEBUG_MORE, log);
}