forked from letscontrolit/ESPEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_C005.ino
144 lines (129 loc) · 5.43 KB
/
_C005.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
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
//#######################################################################################################
//########################### Controller Plugin 005: OpenHAB MQTT #######################################
//#######################################################################################################
#define CPLUGIN_005
#define CPLUGIN_ID_005 5
#define CPLUGIN_NAME_005 "OpenHAB MQTT"
boolean CPlugin_005(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case CPLUGIN_PROTOCOL_ADD:
{
Protocol[++protocolCount].Number = CPLUGIN_ID_005;
Protocol[protocolCount].usesMQTT = true;
Protocol[protocolCount].usesAccount = true;
Protocol[protocolCount].usesPassword = true;
Protocol[protocolCount].defaultPort = 1883;
break;
}
case CPLUGIN_GET_DEVICENAME:
{
string = F(CPLUGIN_NAME_005);
break;
}
case CPLUGIN_PROTOCOL_TEMPLATE:
{
strcpy_P(Settings.MQTTsubscribe, PSTR("/%sysname%/#"));
strcpy_P(Settings.MQTTpublish, PSTR("/%sysname%/%tskname%/%valname%"));
break;
}
case CPLUGIN_PROTOCOL_RECV:
{
// Split topic into array
String tmpTopic = event->String1.substring(1);
String topicSplit[10];
int SlashIndex = tmpTopic.indexOf('/');
byte count = 0;
while (SlashIndex > 0 && count < 10 - 1)
{
topicSplit[count] = tmpTopic.substring(0, SlashIndex);
tmpTopic = tmpTopic.substring(SlashIndex + 1);
SlashIndex = tmpTopic.indexOf('/');
count++;
}
topicSplit[count] = tmpTopic;
String cmd = "";
struct EventStruct TempEvent;
if (topicSplit[count] == "cmd")
{
cmd = event->String2;
parseCommandString(&TempEvent, cmd);
TempEvent.Source = VALUE_SOURCE_MQTT;
}
else
{
cmd = topicSplit[count-1];
TempEvent.Par1 = topicSplit[count].toInt();
TempEvent.Par2 = event->String2.toFloat();
}
// in case of event, store to buffer and return...
String command = parseString(cmd, 1);
if (command == F("event"))
eventBuffer = cmd.substring(6);
else
PluginCall(PLUGIN_WRITE, &TempEvent, cmd);
break;
}
case CPLUGIN_PROTOCOL_SEND:
{
statusLED(true);
// MQTT publish structure:
// /<unit name>/<task name>/<value name>
if (ExtraTaskSettings.TaskDeviceValueNames[0][0] == 0)
PluginCall(PLUGIN_GET_DEVICEVALUENAMES, event, dummyString);
String pubname = Settings.MQTTpublish;
pubname.replace("%sysname%", Settings.Name);
pubname.replace("%tskname%", ExtraTaskSettings.TaskDeviceName);
pubname.replace("%id%", String(event->idx));
String value = "";
byte DeviceIndex = getDeviceIndex(Settings.TaskDeviceNumber[event->TaskIndex]);
switch (event->sensorType)
{
case SENSOR_TYPE_SINGLE: // single value sensor, used for Dallas, BH1750, etc
case SENSOR_TYPE_SWITCH:
case SENSOR_TYPE_DIMMER:
pubname.replace("%valname%", ExtraTaskSettings.TaskDeviceValueNames[0]);
value = toString(UserVar[event->BaseVarIndex],ExtraTaskSettings.TaskDeviceValueDecimals[0]);
MQTTclient.publish(pubname, value);
break;
case SENSOR_TYPE_LONG:
pubname.replace("%valname%", ExtraTaskSettings.TaskDeviceValueNames[0]);
value += (unsigned long)UserVar[event->BaseVarIndex] + ((unsigned long)UserVar[event->BaseVarIndex + 1] << 16);
MQTTclient.publish(pubname, value);
break;
case SENSOR_TYPE_TEMP_HUM:
case SENSOR_TYPE_TEMP_BARO:
{
String tmppubname = pubname;
tmppubname.replace("%valname%", ExtraTaskSettings.TaskDeviceValueNames[0]);
value = toString(UserVar[event->BaseVarIndex],ExtraTaskSettings.TaskDeviceValueDecimals[0]);
MQTTclient.publish(tmppubname, value);
tmppubname = pubname;
tmppubname.replace("%valname%", ExtraTaskSettings.TaskDeviceValueNames[1]);
value = toString(UserVar[event->BaseVarIndex + 1],ExtraTaskSettings.TaskDeviceValueDecimals[1]);
MQTTclient.publish(tmppubname, value);
break;
}
case SENSOR_TYPE_TEMP_HUM_BARO:
{
String tmppubname = pubname;
tmppubname.replace("%valname%", ExtraTaskSettings.TaskDeviceValueNames[0]);
value = toString(UserVar[event->BaseVarIndex],ExtraTaskSettings.TaskDeviceValueDecimals[0]);
MQTTclient.publish(tmppubname, value);
tmppubname = pubname;
tmppubname.replace("%valname%", ExtraTaskSettings.TaskDeviceValueNames[1]);
value = toString(UserVar[event->BaseVarIndex + 1],ExtraTaskSettings.TaskDeviceValueDecimals[1]);
MQTTclient.publish(tmppubname, value);
tmppubname = pubname;
tmppubname.replace("%valname%", ExtraTaskSettings.TaskDeviceValueNames[2]);
value = toString(UserVar[event->BaseVarIndex + 2],ExtraTaskSettings.TaskDeviceValueDecimals[2]);
MQTTclient.publish(tmppubname, value);
break;
}
}
}
return success;
}
}