Files
dojo/lib/Dojo/Controller/Data.pm

74 lines
1.8 KiB
Perl
Raw Normal View History

2018-07-13 19:06:08 -05:00
package Dojo::Controller::Data;
2019-03-14 03:19:54 -06:00
use Mojo::File 'path';
use Mojo::JSON qw(decode_json encode_json);
2018-07-13 19:06:08 -05:00
use Mojo::Base 'Mojolicious::Controller';
2019-03-21 18:50:06 +01:00
use Dojo::Support qw{ log };
2018-07-13 19:06:08 -05:00
use Net::Telnet;
2018-08-14 19:28:16 -05:00
my $server_name = "";
2018-07-13 19:06:08 -05:00
our $t; #telnet server object
2019-03-14 03:19:54 -06:00
my $data_path = path('lib/Dojo/Model/Data')->make_path;
2020-01-20 14:49:59 -06:00
2018-07-13 19:06:08 -05:00
sub simple{
my $c=shift;
my $n=$c->param("dreq")//"";
my $json = {status => "304"};
2018-08-14 19:28:16 -05:00
$json = { srv =>$c->config->{radio_server} } if $n =~m/^radio$/ ;
2019-03-14 03:19:54 -06:00
$json = decode_json($data_path->child("$n")->slurp)
if $n=~m/^podcast\/podcast.json$/;
2018-07-13 19:06:08 -05:00
$c->render(json=>$json);
}
2018-07-18 16:04:47 -05:00
#==== candy =====================================================
2018-07-13 19:06:08 -05:00
sub candy{
my $c=shift;
my $r="-1";
2019-03-21 18:50:06 +01:00
$server_name = $c->config->{chat_srv};
2018-07-13 19:06:08 -05:00
if (connectT()) {
2018-07-25 20:48:58 +00:00
$r = isOn() if ($c->param("command") =~/^isOn$/);
$r = turnOn() if ($c->param("command") =~/^on$/);
2018-07-13 19:06:08 -05:00
$r = turnOff() if ($c->param("command") =~/^off$/);
2018-07-25 20:48:58 +00:00
disconnectT();
}
2018-07-13 19:06:08 -05:00
else {$r="connection error";};
$c->render(json => {a=>$r});
}
2020-01-20 14:49:59 -06:00
sub turnOff{
grep(/Result: true/,join('',sendT('host:deactivate("'.$server_name.'")')))?1:0
unless (isOn()==0);
}
sub turnOn{
grep(/Result: true/,join('',sendT('host:activate("'.$server_name.'")')))?1:0
unless (isOn()==1);
2018-07-13 19:06:08 -05:00
}
2020-01-20 14:49:59 -06:00
sub isOn{return grep(/\s$server_name/,sendT("host:list()"))? 1:0; }
2018-07-13 19:06:08 -05:00
2018-07-27 16:45:29 -05:00
sub sendT{
2018-07-13 19:06:08 -05:00
commandT(shift);
my @r = $t->getlines(All=>0);
return @r
2018-07-27 16:45:29 -05:00
}
2020-01-20 14:49:59 -06:00
2018-07-27 16:45:29 -05:00
sub connectT{
2018-07-13 19:06:08 -05:00
$t = new Net::Telnet ( Port=>5582, Timeout=>1, Errmode=>'return' );
# $t = new Net::Telnet ( Port=>5582, Timeout=>1, Errmode=>'die' );
return 1 if $t->open();
return 0;
2018-07-27 16:45:29 -05:00
}
2020-01-20 14:49:59 -06:00
2018-07-27 16:45:29 -05:00
sub commandT{
2018-07-13 19:06:08 -05:00
$t->getlines(All=>0);#empty buffer
$t->print(shift); #run istruction
2018-07-27 16:45:29 -05:00
}
2020-01-20 14:49:59 -06:00
2018-07-27 16:45:29 -05:00
sub disconnectT{ $t->close();}
2018-07-13 19:06:08 -05:00
2018-07-18 16:04:47 -05:00
#=========================================================
2018-07-27 16:45:29 -05:00
1;
2018-07-13 19:06:08 -05:00
__END__