bye bye github
This commit is contained in:
63
lib/Dojo/Controller/Data.pm
Normal file
63
lib/Dojo/Controller/Data.pm
Normal file
@@ -0,0 +1,63 @@
|
||||
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);
|
||||
}
|
||||
|
||||
sub candy{
|
||||
my $c=shift;
|
||||
my $r="-1";
|
||||
if (connectT()) {
|
||||
if ($c->param("command") =~/^isOn$/){ $r = isOn(); }
|
||||
elsif( $c->session("pmid") == 4 ){
|
||||
$r = turnOn() if ($c->param("command") =~/^on$/);
|
||||
$r = turnOff() if ($c->param("command") =~/^off$/);
|
||||
}}
|
||||
else {$r="connection error";};
|
||||
$c->render(json => {a=>$r});
|
||||
}
|
||||
|
||||
sub turnOff{
|
||||
return 1 unless (isOn());
|
||||
grep(/Result: true/,sendT("host:deactivate('$server_name')"))?1:0;
|
||||
}
|
||||
sub turnOn{
|
||||
return 1 unless (!isOn());
|
||||
grep(/Result: true/,sendT("host:activate('$server_name')"))?1:0;
|
||||
}
|
||||
|
||||
sub isOn{ grep(/\s$server_name/,sendT("host:list()"))? 1:0; }
|
||||
|
||||
sub sendT{
|
||||
commandT(shift);
|
||||
my @r = $t->getlines(All=>0);
|
||||
disconnectT();
|
||||
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();}
|
||||
|
||||
1
|
||||
__END__
|
||||
|
||||
|
||||
Reference in New Issue
Block a user