-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_websocket.pl
214 lines (172 loc) · 6.23 KB
/
test_websocket.pl
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Category=touchScreen
#@ HTML 5 Touchscreen interface backend using WebSockets
#
# Sean Mathews
# coder at f34r dot com
#
############################################################################
#noloop=start
# map click messages to functions
my %clicks = (
"power_button" => sub {
my ( $json ) = @_;
&::print_log("power_button clicked '$$json'");
set $v_reload_code ON;
},
"some_button" => sub {
my ( $json ) = @_;
&::print_log("some_button clicked");
},
"hsp" => sub {
my ( $json ) = @_;
my $value = $$json->{'value'};
therm_set_heat_setpoint($value);
&::print_log("Changed heating setpoint to $value degrees");
sendUpdate("#hsp", "text", $value);
},
"csp" => sub {
my ( $json ) = @_;
my $value = $$json->{'value'};
therm_set_cool_setpoint($value);
&::print_log("Changed cooling setpoint to $value degrees");
sendUpdate("#csp", "text", $value);
},
"therm_mode" => sub {
my ( $json ) = @_;
my $value = $$json->{'value'};
therm_set_mode($value);
&::print_log("Changed furnace mode to $value");
sendUpdate("#mode_select_val", "state", $value);
},
"therm_fan" => sub {
my ( $json ) = @_;
my $value = $$json->{'value'};
therm_set_fan_mode($value);
&::print_log("Changed furnace fan to $value");
sendUpdate("#fan_select_val", "state", $value);
}
);
# dispatch routine for "click" command sent a reference to the json object
sub TouchClick {
# the full json object sent from the browser
my ( $json ) = @_;
&::print_log("TouchClick called with '$$json'");
# get the click argument and use it to find a handler function
my $click_id = $$json->{'text'};
if ($clicks{$click_id}) {
$clicks{$click_id}->($json);
} else {
&::print_log("no such click handler: $click_id");
}
}
# subscribe to click event messages
WebSocket::subscribe('click', \&TouchClick);
#noloop=stop
# This is run on startup or reload. Tell everyone what happend.
if ($Startup or $Reload) {
&::print_log("notify all WebSockets we restarted");
sendMessage("RESTART");
}
# here we will handle thermostat changes that need to be sent to all sockets
# Only perform these operations if the thermostat object is initialized
if (defined $thermostat) {
# mode changes
use vars qw($therm_last_mode);
use vars qw($therm_last_fan);
use vars qw($therm_last_state);
use vars qw($therm_last_fan_state);
use vars qw($therm_last_heat_set);
use vars qw($therm_last_cool_set);
use vars qw($therm_last_inside_temp);
my $therm_check_mode;
my $therm_check_fan;
my $therm_check_state;
my $therm_check_fan_state;
my $therm_check_heat_set;
my $therm_check_cool_set;
my $therm_check_inside_temp;
if ($Startup or $Reload) {
$therm_last_mode = "unknown";
$therm_last_fan = "unknown";
}
if ($New_Second) {
$therm_check_mode = therm_get_mode();
$therm_check_fan = therm_get_fan_mode();
$therm_check_state = therm_get_state();
$therm_check_fan_state = therm_get_fan_state();
$therm_check_heat_set = therm_get_heat_setpoint();
$therm_check_cool_set = therm_get_cool_setpoint();
$therm_check_inside_temp = therm->get_temp();
# check if a mode change ocurred
if ($therm_check_mode ne $therm_last_mode) {
&::print_log("Notify all WebSockets that the thermostat mode has changed");
sendUpdate("#mode_select_val", "state", $therm_check_mode);
}
# check if a fan mode change ocurred
if ($therm_check_fan ne $therm_last_fan) {
&::print_log("Notify all WebSockets that the thermostats fan mode has changed");
sendUpdate("#fan_select_val", "state", $therm_check_fan);
}
#check and update the HVAC system's heating and cooling state as it changes
if ($therm_check_state ne $therm_last_state) {
&::print_log("Notify all WebSockets that the HVAC system is now in a $therm_check_state state");
sendOffset("#mode_state", "0", (($therm_check_state eq "heat") ? "0" : (($therm_check_state eq "off") ? "-32" : "-64")));
}
#Check and update the HVAC fan state as it changes
if ($therm_check_fan_state ne $therm_last_fan_state) {
&::print_log("Notify all WebSockets that the HVAC system's fan is now $therm_check_fan_state");
sendOffset("#fan_state", "0", (($therm_check_fan_state eq "off") ? "0" : "-32"));
}
#Check and update the thermostat's heating setpoint
if ($therm_check_heat_set ne $therm_last_heat_set) {
&::print_log("Notify all WebSockets that the thermostat's heating setpoint is now $therm_check_heat_set");
sendUpdate("#hsp", "text", $therm_check_heat_set);
}
#Check and update the thermostat's cooling setpoint
if ($therm_check_cool_set ne $therm_last_cool_set) {
&::print_log("Notify all WebSockets that the thermostat's cooling_setpoint is now $therm_check_cool_set");
sendUpdate("#csp", "text", $therm_check_cool_set);
}
#Check and update the thermostat's temperature
if ($therm_check_inside_temp ne $therm_last_inside_temp) {
&::print_log("Notify all WebSockets that the thermostat's temperature is now $therm_check_inside_temp");
sendUpdate("#TempIndoor", "text", $therm_check_inside_temp);
}
# save the last known modes and states so we can test for change
$therm_last_mode = $therm_check_mode;
$therm_last_fan = $therm_check_fan;
$therm_last_state = $therm_check_state;
$therm_last_fan_state = $therm_check_fan_state;
$therm_last_heat_set = $therm_check_heat_set;
$therm_last_cool_set = $therm_check_cool_set;
$therm_last_inside_temp = $therm_check_inside_temp
}
}
sub sendUpdate {
my ($id, $datatype, $data) = @_;
wsSend('type' => "upd",
'id' => $id,
'datatype' => $datatype,
'data' => $data);
}
sub sendOffset {
my ($id, $xpos, $ypos) = @_;
wsSend('type' => "upd",
'id' => $id,
'datatype' => "bgoffset",
'xpos' => $xpos,
'ypos' => $ypos);
}
sub sendMessage {
my ($text) = @_;
wsSend('type' => "msg",
'text' => $text);
}
sub wsSend {
my (%wsData) = @_;
my $wsData = \%wsData;
my $json_text;
my $json = JSON->new->allow_nonref;
$json_text = $json->encode($wsData);
WebSocket::SendToAll($json_text);
}