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

76 lines
2.1 KiB
Perl
Raw Normal View History

2018-07-13 19:06:08 -05:00
package Dojo::Controller::Data;
use Mojo::Base 'Mojolicious::Controller';
use Dojo::Support qw{ dmph merge_hash load_module };
use Dojo::Conf;
use Net::Telnet;
my $server_name = $Dojo::Conf::chat->{chat_srv};
our $t; #telnet server object
sub simple{
my $c=shift;
my $n=$c->param("dreq")//"";
my $json = {status => "304"};
$json = \%Dojo::Conf::radio if $n =~m/^radio$/ ;
$json = candy() if $n =~m/^candy$/ ;
$c->render(json=>$json);
}
2018-07-18 16:04:47 -05:00
sub admin{
my $c=shift;
my $n=$c->param("dreq")//"";
my $json = {status => "304"};
2018-07-19 03:45:46 -05:00
$json = ($c->dbv->rmsgid ($c->param('id')))[0] if ($n =~m/^mensajes$/);
$json = ($c->dbv->ecourse($c->param('id')))[0] if ($n =~m/^ecourse$/);
$json = ($c->dbv->qcourse($c->param('id')))[0] if ($n =~m/^qcourse$/);
$json = ($c->dbv->qplace ($c->param('id')))[0] if ($n =~m/^qplace$/);
2018-07-18 16:04:47 -05:00
$c->render(json=>$json);
}
#==== candy =====================================================
2018-07-13 19:06:08 -05:00
sub candy{
my $c=shift;
my $r="-1";
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});
2018-07-25 20:48:58 +00:00
# $c->render(json => {a=>$server_name});
2018-07-13 19:06:08 -05:00
}
sub turnOff{
2018-07-25 20:48:58 +00:00
grep(/Result: true/,join('',sendT('host:deactivate("'.$server_name.'")')))?1:0
unless (isOn()==0);
2018-07-13 19:06:08 -05:00
}
sub turnOn{
2018-07-25 20:48:58 +00:00
grep(/Result: true/,join('',sendT('host:activate("'.$server_name.'")')))?1:0
unless (isOn()==1);
2018-07-13 19:06:08 -05:00
}
2018-07-25 20:48:58 +00:00
sub isOn{return grep(/\s$server_name/,sendT("host:list()"))? 1:0; }
2018-07-13 19:06:08 -05:00
sub sendT{
commandT(shift);
my @r = $t->getlines(All=>0);
return @r
}
sub connectT{
$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;
}
sub commandT{
$t->getlines(All=>0);#empty buffer
$t->print(shift); #run istruction
}
sub disconnectT{ $t->close();}
2018-07-18 16:04:47 -05:00
#=========================================================
2018-07-13 19:06:08 -05:00
1
__END__