-
Notifications
You must be signed in to change notification settings - Fork 2
/
ml2web.ino
139 lines (115 loc) · 3.13 KB
/
ml2web.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
#include <utility/w5100.h>
#define TCP_RETRANSMISSION_TIME 150
#define ITEMS_PER_PAGE 10
#define NAMELEN 16
#define VALUELEN 128
#define PREFIX ""
WebServer webserver(PREFIX, 80);
const char CONFIG_FILE[] = "config.txt";
void parseBytes(const char* str, char sep, byte* bytes, int maxBytes, int base) {
for (int i = 0; i < maxBytes; i++) {
bytes[i] = strtoul(str, NULL, base); // Convert byte
str = strchr(str, sep); // Find next separator
if (str == NULL || *str == '\0') {
break; // No more separators, exit
}
str++; // Point to next character after separator
}
}
void defaultCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
server.httpSuccess();
}
void stateCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
server.httpSuccess("text/plain");
if (type != WebServer::GET)
{
if (type != WebServer::HEAD)
server.httpFail();
return;
}
URLPARAM_RESULT rc;
char name[NAMELEN];
char value[VALUELEN];
char c[NAMELEN];
char id[ID_SIZE];
int state = -1;
int on = -1;
int inc = 0;
uint32_t timeout = 0;
if (!strlen(url_tail))
return;
while (strlen(url_tail))
{
rc = server.nextURLparam(&url_tail, name, NAMELEN, value, VALUELEN);
if (rc == URLPARAM_OK)
{
if (name[0] == 'c')
strcpy(c, value);
else if (name[0] == 'n')
strcpy(id , value);
else if (name[0] == 'o')
on = atoi(value);
else if (name[0] == 'v')
state = atoi(value);
else if (name[0] == 'i')
inc = atoi(value);
else if (name[0] == 't')
timeout = parseTime(value);
}
}
if (!strlen(id))
return;
if (strcmp(c, "get") == 0)
{
ML2Output *output = outputList.find(id);
if (!output)
{
server.httpNoContent();
return;
}
server.print(output->on());
server.print(";");
server.print(output->value());
}
else if (strcmp(c, "set") == 0)
{
ML2Output *output = outputList.find(id);
if (!output)
{
server.httpNoContent();
return;
}
if (state >= 0 )
outputEM.queueEvent(OutputAction::Value, new OutputEventParam(output, state, timeout));
if (inc != 0 )
outputEM.queueEvent(OutputAction::IncValue, new OutputEventParam(output, inc, timeout));
if (on >= 0 )
outputEM.queueEvent(on ? OutputAction::On : OutputAction::Off, new OutputEventParam(output, on, timeout));
}
else if (strcmp(c, "button") == 0)
{
ML2Input *button = inputList.find(id);
if (!button)
{
server.httpNoContent();
return;
}
server.print(button->state() & ButtonState::Down);
}
}
void setupWeb()
{
if (!ip[0] && !ip[1] && !ip[2] && !ip[3])
Ethernet.begin(mac);
else
Ethernet.begin(mac, ip);
W5100.setRetransmissionTime(TCP_RETRANSMISSION_TIME);
W5100.setRetransmissionCount(3);
webserver.begin();
Serialprint("Server started at ");
Serial.println(Ethernet.localIP());
webserver.setDefaultCommand(&defaultCmd);
webserver.addCommand("state", &stateCmd);
}