9
9
10
10
#define BUFFER_SIZE 128
11
11
boolean Plugin_020_init = false ;
12
+ byte Plugin_020_SerialProcessing = 0 ;
12
13
13
14
WiFiServer *ser2netServer;
14
15
WiFiClient ser2netClient;
15
16
16
17
boolean Plugin_020 (byte function, struct EventStruct *event, String& string)
17
18
{
18
19
boolean success = false ;
19
- static byte connectionState= 0 ;
20
+ static byte connectionState = 0 ;
20
21
21
22
switch (function)
22
23
{
@@ -84,6 +85,29 @@ boolean Plugin_020(byte function, struct EventStruct *event, String& string)
84
85
sprintf_P (tmpString, PSTR (" <TR><TD>RX Receive Timeout (mSec):<TD><input type='text' name='plugin_020_rxwait' value='%u'>" ), Settings.TaskDevicePluginConfig [event->TaskIndex ][0 ]);
85
86
string += tmpString;
86
87
88
+ byte choice2 = Settings.TaskDevicePluginConfig [event->TaskIndex ][1 ];
89
+ String options2[3 ];
90
+ options2[0 ] = F (" None" );
91
+ options2[1 ] = F (" Generic" );
92
+ options2[2 ] = F (" RFLink" );
93
+ int optionValues2[3 ];
94
+ optionValues2[0 ] = 0 ;
95
+ optionValues2[1 ] = 1 ;
96
+ optionValues2[2 ] = 2 ;
97
+ string += F (" <TR><TD>Event processing:<TD><select name='plugin_020_events'>" );
98
+ for (byte x = 0 ; x < 3 ; x++)
99
+ {
100
+ string += F (" <option value='" );
101
+ string += optionValues2[x];
102
+ string += " '" ;
103
+ if (choice2 == optionValues2[x])
104
+ string += F (" selected" );
105
+ string += " >" ;
106
+ string += options2[x];
107
+ string += F (" </option>" );
108
+ }
109
+ string += F (" </select>" );
110
+
87
111
success = true ;
88
112
break ;
89
113
}
@@ -102,6 +126,8 @@ boolean Plugin_020(byte function, struct EventStruct *event, String& string)
102
126
ExtraTaskSettings.TaskDevicePluginConfigLong [4 ] = plugin5.toInt ();
103
127
String plugin6 = WebServer.arg (" plugin_020_rxwait" );
104
128
Settings.TaskDevicePluginConfig [event->TaskIndex ][0 ] = plugin6.toInt ();
129
+ String plugin7 = WebServer.arg (" plugin_020_events" );
130
+ Settings.TaskDevicePluginConfig [event->TaskIndex ][1 ] = plugin7.toInt ();
105
131
success = true ;
106
132
break ;
107
133
}
@@ -128,9 +154,10 @@ boolean Plugin_020(byte function, struct EventStruct *event, String& string)
128
154
digitalWrite (Settings.TaskDevicePin1 [event->TaskIndex ], HIGH);
129
155
pinMode (Settings.TaskDevicePin1 [event->TaskIndex ], INPUT_PULLUP);
130
156
}
131
-
157
+
132
158
Plugin_020_init = true ;
133
159
}
160
+ Plugin_020_SerialProcessing = Settings.TaskDevicePluginConfig [event->TaskIndex ][1 ];
134
161
success = true ;
135
162
break ;
136
163
}
@@ -151,7 +178,7 @@ boolean Plugin_020(byte function, struct EventStruct *event, String& string)
151
178
152
179
if (ser2netClient.connected ())
153
180
{
154
- connectionState= 1 ;
181
+ connectionState = 1 ;
155
182
uint8_t net_buf[BUFFER_SIZE];
156
183
int count = ser2netClient.available ();
157
184
if (count > 0 )
@@ -169,22 +196,22 @@ boolean Plugin_020(byte function, struct EventStruct *event, String& string)
169
196
strcpy_P (log, PSTR (" Ser2N: network buffer full!" )); // and log buffer full situation
170
197
addLog (LOG_LEVEL_ERROR, log);
171
198
}
172
- net_buf[count]= 0 ; // before logging as a char array, zero terminate the last position to be safe.
173
- char log[BUFFER_SIZE+ 40 ];
199
+ net_buf[count] = 0 ; // before logging as a char array, zero terminate the last position to be safe.
200
+ char log[BUFFER_SIZE + 40 ];
174
201
sprintf_P (log, PSTR (" Ser2N: N>: %s" ), (char *)net_buf);
175
- addLog (LOG_LEVEL_DEBUG,log);
202
+ addLog (LOG_LEVEL_DEBUG, log);
176
203
}
177
204
}
178
205
else
179
206
{
180
- if (connectionState == 1 ) // there was a client connected before...
207
+ if (connectionState == 1 ) // there was a client connected before...
181
208
{
182
- connectionState= 0 ;
209
+ connectionState = 0 ;
183
210
char log[40 ];
184
211
strcpy_P (log, PSTR (" Ser2N: Client disconnected!" ));
185
212
addLog (LOG_LEVEL_ERROR, log);
186
213
}
187
-
214
+
188
215
while (Serial.available ())
189
216
Serial.read ();
190
217
}
@@ -196,54 +223,107 @@ boolean Plugin_020(byte function, struct EventStruct *event, String& string)
196
223
197
224
case PLUGIN_SERIAL_IN:
198
225
{
199
- if (Plugin_020_init)
226
+ uint8_t serial_buf[BUFFER_SIZE];
227
+ int RXWait = Settings.TaskDevicePluginConfig [event->TaskIndex ][0 ];
228
+ if (RXWait == 0 )
229
+ RXWait = 1 ;
230
+ int timeOut = RXWait;
231
+ size_t bytes_read = 0 ;
232
+ while (timeOut > 0 )
200
233
{
201
- if (ser2netClient.connected ())
202
- {
203
- uint8_t serial_buf[BUFFER_SIZE];
204
- int RXWait = Settings.TaskDevicePluginConfig [event->TaskIndex ][0 ];
205
- if (RXWait == 0 )
206
- RXWait = 1 ;
207
- int timeOut = RXWait;
208
- size_t bytes_read = 0 ;
209
- while (timeOut > 0 )
210
- {
211
- while (Serial.available ()) {
212
- if (bytes_read < BUFFER_SIZE) {
213
- serial_buf[bytes_read] = Serial.read ();
214
- bytes_read++;
215
- }
216
- else
217
- Serial.read (); // when the buffer is full, just read remaining input, but do not store...
218
-
219
- timeOut = RXWait; // if serial received, reset timeout counter
220
- }
221
- delay (1 );
222
- timeOut--;
234
+ while (Serial.available ()) {
235
+ if (bytes_read < BUFFER_SIZE) {
236
+ serial_buf[bytes_read] = Serial.read ();
237
+ bytes_read++;
223
238
}
224
-
225
- if (bytes_read != BUFFER_SIZE)
239
+ else
240
+ Serial.read (); // when the buffer is full, just read remaining input, but do not store...
241
+
242
+ timeOut = RXWait; // if serial received, reset timeout counter
243
+ }
244
+ delay (1 );
245
+ timeOut--;
246
+ }
247
+
248
+ if (bytes_read != BUFFER_SIZE)
249
+ {
250
+ if (bytes_read > 0 ) {
251
+ if (Plugin_020_init && ser2netClient.connected ())
226
252
{
227
- if (bytes_read > 0 ) {
228
- ser2netClient.write ((const uint8_t *)serial_buf, bytes_read);
229
- ser2netClient.flush ();
230
- }
253
+ ser2netClient.write ((const uint8_t *)serial_buf, bytes_read);
254
+ ser2netClient.flush ();
231
255
}
232
- else // if we have a full buffer, drop the last position to stuff with string end marker
233
- {
234
- while (Serial.available ()) // read possible remaining data to avoid sending rubbish...
235
- Serial.read ();
236
- bytes_read--;
237
- char log[40 ];
238
- strcpy_P (log, PSTR (" Ser2N: serial buffer full!" )); // and log buffer full situation
239
- addLog (LOG_LEVEL_ERROR, log);
240
- }
241
- serial_buf[bytes_read]=0 ; // before logging as a char array, zero terminate the last position to be safe.
242
- char log[BUFFER_SIZE+40 ];
243
- sprintf_P (log, PSTR (" Ser2N: S>: %s" ), (char *)serial_buf);
244
- addLog (LOG_LEVEL_DEBUG,log);
245
256
}
257
+ }
258
+ else // if we have a full buffer, drop the last position to stuff with string end marker
259
+ {
260
+ while (Serial.available ()) // read possible remaining data to avoid sending rubbish...
261
+ Serial.read ();
262
+ bytes_read--;
263
+ char log[40 ];
264
+ strcpy_P (log, PSTR (" Ser2N: serial buffer full!" )); // and log buffer full situation
265
+ addLog (LOG_LEVEL_ERROR, log);
266
+ }
267
+ serial_buf[bytes_read] = 0 ; // before logging as a char array, zero terminate the last position to be safe.
268
+ char log[BUFFER_SIZE + 40 ];
269
+ sprintf_P (log, PSTR (" Ser2N: S>: %s" ), (char *)serial_buf);
270
+ addLog (LOG_LEVEL_DEBUG, log);
271
+
272
+ // We can also use the rules engine for local control!
273
+ if (Settings.UseRules )
274
+ {
275
+ String message = (char *)serial_buf;
276
+ int NewLinePos = message.indexOf (" \r\n " );
277
+ if (NewLinePos > 0 )
278
+ message = message.substring (0 , NewLinePos);
279
+ String eventString = " " ;
280
+
281
+ switch (Plugin_020_SerialProcessing)
282
+ {
283
+ case 0 :
284
+ {
285
+ break ;
286
+ }
287
+
288
+ case 1 : // Generic
289
+ {
290
+ eventString = F (" !Serial#" );
291
+ eventString += message;
292
+ break ;
293
+ }
294
+
295
+ case 2 : // RFLink
296
+ {
297
+ message = message.substring (6 ); // RFLink, strip 20;xx; from incoming message
298
+ // message.replace("\r\n", "");
299
+ if (message.startsWith (" ESPEASY" )) // Special treatment for gpio values, strip unneeded parts...
300
+ {
301
+ message = message.substring (8 ); // Strip "ESPEASY;"
302
+ eventString = F (" RFLink#" );
303
+ }
304
+ else
305
+ eventString = F (" !RFLink#" ); // default event as it comes in, literal match needed in rules, using '!'
306
+ eventString += message;
307
+ break ;
308
+ }
309
+ } // switch
310
+
311
+ if (eventString.length () > 0 )
312
+ rulesProcessing (eventString);
313
+
314
+ } // if rules
315
+ success = true ;
316
+ break ;
317
+ }
318
+
319
+ case PLUGIN_WRITE:
320
+ {
321
+ String command = parseString (string, 1 );
322
+ if (command == F (" serialsend" ))
323
+ {
246
324
success = true ;
325
+ String tmpString = string.substring (11 );
326
+ Serial.println (tmpString);
247
327
}
248
328
break ;
249
329
}
0 commit comments