-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.pl
168 lines (150 loc) · 4.41 KB
/
web.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
# This file is part of the LibreRVAC project
#
# Copyright © 2015-2016
# Aleks-Daniel Jakimenko-Aleksejev <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
use warnings;
use v5.10;
use utf8;
use threads;
use threads::shared;
use Thread::Queue;
use Plack::Builder;
use Plack::Request;
use Plack::Response;
use Plack::App::WebSocket;
use File::Slurper 'read_text';
use JSON::XS;
use IO::Handle;
use IO::Socket::UNIX;
use Encode qw(decode encode);
my $SOCK_PATH = "brain.sock";
my $SEP = "\x1E";
my $socket = IO::Socket::UNIX->new(
Type => SOCK_STREAM(),
Peer => $SOCK_PATH,
);
sub header {
my ($content_type) = @_;
[
'Content-Security-Policy' => "default-src 'none'; script-src 'self'; connect-src *; img-src 'self'; style-src 'self';",
'Content-Type' => "$content_type; charset=utf-8",
]
}
sub fifo_send {
my ($data) = @_;
$socket->print(encode_json($data), $SEP);
}
my %motor_bypass = (
'Left wheel' => 'wheel left',
'Right wheel' => 'wheel right',
'Main brush' => 'brush main',
'Side brushes' => 'brush sides',
'Left brush' => 'brush left',
'Right brush' => 'brush right',
'Vacuum' => 'vacuum',
);
sub process_input {
my ($text) = @_;
my $data = decode_json $text;
if (exists $data->{control}) {
if (exists $motor_bypass{$data->{control}}) {
my $motor = $motor_bypass{$data->{control}};
my $value = $data->{value};
fifo_send({ command => 'bypass',
data => { c => 'motor',
motor => $motor,
throttle => $value,
}
});
} elsif ($data->{control} eq 'Start (normal)') {
if ($data->{value} == 1.0) {
fifo_send({ command => 'clean',
type => 'normal',
});
}
} elsif ($data->{control} eq 'Start (spot)') {
} elsif ($data->{control} eq 'Docking') {
}
}
}
my $app = sub {
my $req = Plack::Request->new(shift);
if ($req->path_info =~ m{^ /home/? | ^$ }x) {
return [200, header('text/html'), [encode 'UTF-8', read_text 'main.html']];
}
if ($req->path_info =~ m{ ^/main.js }x) {
return [200, header('application/javascript'), [encode 'UTF-8', read_text 'main.js']];
}
if ($req->path_info =~ m{ ^/main.css }x) {
return [200, header('text/css'), [encode 'UTF-8', read_text 'main.css']];
}
return [404, header('text/html'), [encode 'UTF-8', 'Page not found!']];
};
my %connections :shared;
threads->create(sub {
local $/ = $SEP;
say 'Start reading';
while (<$socket>) {
chomp;
for my $conn (values %connections) {
$conn->enqueue($_);
}
}
threads->detach(); #End thread.
});
builder {
mount "/websocket" => Plack::App::WebSocket->new(
on_error => sub {
my $env = shift;
return [500,
['Content-Type' => 'text/plain; charset=utf-8'],
[encode 'UTF-8', 'Error: ' . $env->{'plack.app.websocket.error'}]];
},
on_establish => sub {
say "Client connected!";
my $conn = shift; ## Plack::App::WebSocket::Connection object
my $env = shift; ## PSGI env
my $queue :shared = Thread::Queue->new();
{
lock(%connections);
$connections{$conn} = $queue;
}
threads->create(sub {
while (1) {
$conn->send($queue->dequeue());
#$queue->dequeue();
}
threads->detach(); # End thread
});
$conn->on(
message => sub {
my ($conn, $msg) = @_;
process_input $msg;
},
finish => sub {
{
lock(%connections);
delete $connections{$conn};
}
undef $conn;
say "Client disconnected!";
},
);
}
)->to_app;
mount "/" => $app;
};