-
Notifications
You must be signed in to change notification settings - Fork 30
/
_C003_Nodo_Telnet.ino
111 lines (92 loc) · 3.48 KB
/
_C003_Nodo_Telnet.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
//#######################################################################################################
//########################### Controller Plugin 003: Nodo Telnet #######################################
//#######################################################################################################
#define CPLUGIN_003
#define CPLUGIN_ID_003 3
#define CPLUGIN_NAME_003 "Nodo Telnet"
boolean CPlugin_003(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case CPLUGIN_PROTOCOL_ADD:
{
Protocol[++protocolCount].Number = CPLUGIN_ID_003;
Protocol[protocolCount].usesMQTT = false;
Protocol[protocolCount].usesAccount = false;
Protocol[protocolCount].usesPassword = true;
Protocol[protocolCount].defaultPort = 23;
Protocol[protocolCount].usesID = true;
break;
}
case CPLUGIN_GET_DEVICENAME:
{
string = F(CPLUGIN_NAME_003);
break;
}
case CPLUGIN_PROTOCOL_SEND:
{
ControllerSettingsStruct ControllerSettings;
LoadControllerSettings(event->ControllerIndex, (byte*)&ControllerSettings, sizeof(ControllerSettings));
char log[80];
boolean success = false;
char host[20];
sprintf_P(host, PSTR("%u.%u.%u.%u"), ControllerSettings.IP[0], ControllerSettings.IP[1], ControllerSettings.IP[2], ControllerSettings.IP[3]);
sprintf_P(log, PSTR("%s%s using port %u"), "TELNT: connecting to ", host,ControllerSettings.Port);
addLog(LOG_LEVEL_DEBUG, log);
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, ControllerSettings.Port))
{
connectionFailures++;
strcpy_P(log, PSTR("TELNT: connection failed"));
addLog(LOG_LEVEL_ERROR, log);
return false;
}
statusLED(true);
if (connectionFailures)
connectionFailures--;
// We now create a URI for the request
String url = F("variableset ");
url += event->idx;
url += ",";
url += toString(UserVar[event->BaseVarIndex],ExtraTaskSettings.TaskDeviceValueDecimals[0]);
url += "\n";
strcpy_P(log, PSTR("TELNT: Sending enter"));
addLog(LOG_LEVEL_ERROR, log);
client.print(" \n");
unsigned long timer = millis() + 200;
while (!client.available() && millis() < timer)
delay(1);
timer = millis() + 1000;
while (client.available() && millis() < timer && !success)
{
String line = client.readStringUntil('\n');
if (line.substring(0, 20) == "Enter your password:")
{
success = true;
strcpy_P(log, PSTR("TELNT: Password request ok"));
addLog(LOG_LEVEL_ERROR, log);
}
delay(1);
}
strcpy_P(log, PSTR("TELNT: Sending pw"));
addLog(LOG_LEVEL_ERROR, log);
client.println(SecuritySettings.ControllerPassword[event->ControllerIndex]);
delay(100);
while (client.available())
client.read();
strcpy_P(log, PSTR("TELNT: Sending cmd"));
addLog(LOG_LEVEL_ERROR, log);
client.print(url);
delay(10);
while (client.available())
client.read();
strcpy_P(log, PSTR("TELNT: closing connection"));
addLog(LOG_LEVEL_DEBUG, log);
client.stop();
break;
}
}
return success;
}