Browse Source

on/off mods

master
Enzo Angiulli 4 years ago
parent
commit
05e95fb876
  1. 36
      index.php
  2. 25
      ui/home.htm

36
index.php

@ -3,6 +3,8 @@
// Kickstart the framework
// $f3=require('lib/base.php');
require_once './vendor/autoload.php';
date_default_timezone_set('Europe/Budapest');
$f3 = \Base::instance();
$f3->set('DB',new DB\Jig('data/'));
$db=new \DB\Jig('data/');
@ -63,6 +65,8 @@ $f3->route('POST /sendsignal', function($f3) {
function sendsignal($post) {
$iparray = preg_split('/\r\n|\r|\n/', $post['item']['ip']);
$macArray = preg_split('/\r\n|\r|\n/', $post['item']['macAddress']);
$label = $post['label'];
$messages = [];
switch ($post['item']['channel']) {
case 'telnet':
@ -78,10 +82,19 @@ function sendsignal($post) {
}
break;
case 'wol':
foreach ($iparray as $ip) {
$result = wol($post['item']['macAddress'], $post['item']['broadcastIP']);
array_push($messages, $result);
if ($label === 'on') {
foreach ($macArray as $mac) {
$result = wol($mac, $post['item']['broadcastIP']);
array_push($messages, $result);
}
} else {
foreach ($iparray as $ip) {
$result = shutdown($ip);
array_push($messages, $result);
}
}
break;
default:
array_push($messages, ['success' => false, 'message' => 'No channel set for these equipments']);
@ -134,13 +147,24 @@ function udp($ip, $port, $action) {
}
function shutdown($ip) {
$ipdata = explode('@', $ip);
$username = explode(':', $ipdata[0])[0];
$password = explode(':', $ipdata[0])[1];
$ipaddress = $ipdata[1];
$output = shell_exec("net rpc shutdown -f -t 0 -C 'message' -U ". $username . "%" . $password ." -I " . $ipaddress);
return ['success' => true, 'message' => $output . '(' . $ipaddress . ')'];
}
function wol($macAddress, $broadcastIP) {
$f = new \Phpwol\Factory();
$magicPacket = $f->magicPacket();
if ($result = $magicPacket->send($macAddress, $broadcastIP)) {
return ['success' => true, 'message' => 'Successful Wake On Lan'];
$result = $magicPacket->send($macAddress, $broadcastIP);
if ($result) {
return ['success' => true, 'message' => 'Successful Wake On Lan for ' . $macAddress];
} else {
return ['success' => false, 'message' => 'Unsuccessful WOL for IP ' . $macAddress];
return ['success' => false, 'message' => 'Unsuccessful WOL for MAC address ' . $macAddress];
}
}

25
ui/home.htm

@ -1,6 +1,7 @@
<div id="app">
<div v-for="message in messages" :class="{'error': !message.success}" class="messages">{{message.message}}</div>
<p v-show="sending" style="position:absolute;top: 10px; right: 10px;">Parancs küldése, kérem várjon.</p>
<p><?= date('Y.m.d H:i') ?></p>
<p v-if="loading">No equipments, please add one.</p>
<div class="buttons" v-else>
<div class="button" v-for="item in items">
@ -8,8 +9,8 @@
<p>{{item.name}}</p>
</div>
<div>
<a v-on:click="sendsignal(item, item.oncommand)" href="javascript:void(0)" class="addbtn darker"><i class="fa fa-play" aria-hidden="true"></i></a>
<a v-on:click="sendsignal(item, item.offcommand)" href="javascript:void(0)" class="addbtn darker"><i class="fa fa-stop" aria-hidden="true"></i></a>
<a v-on:click="sendsignal(item, item.oncommand, 'on')" href="javascript:void(0)" class="addbtn darker"><i class="fa fa-play" aria-hidden="true"></i></a>
<a v-on:click="sendsignal(item, item.offcommand, 'off')" href="javascript:void(0)" class="addbtn darker"><i class="fa fa-stop" aria-hidden="true"></i></a>
</div>
<div v-show="admin">
<a v-on:click="delitem(item)" href="javascript:void(0)" class="addbtn darker"><i class="fa fa-trash" aria-hidden="true"></i></a>
@ -30,7 +31,7 @@
Channel<br>
<label>UDP<br><input type="radio" name="channel" value="udp" v-model="newitem.channel"></label>
<label>TELNET<br><input type="radio" name="channel" value="telnet" v-model="newitem.channel"> </label>
<label>WOL<br><input type="radio" name="channel" value="wol" v-model="newitem.channel"> </label>
<label>ON/OFF<br><input type="radio" name="channel" value="wol" v-model="newitem.channel"> </label>
</label>
</div>
<label for="name" v-show="newitem.channel != ''">
@ -41,7 +42,7 @@
What is the on command?<br>
<input type="text" name="oncommand" v-model="newitem.oncommand"> <br>
</label>
<label for="ontime" v-show="newitem.channel != ''">
<label for="ontime" v-show="['udp', 'telnet', 'wol'].indexOf(newitem.channel) >= 0">
When to send the on command?<br>
<input type="time" name="ontime" v-model="newitem.ontime"> <br>
</label>
@ -49,7 +50,7 @@
What is the off command?<br>
<input type="text" name="offcommand" v-model="newitem.offcommand"> <br>
</label>
<label for="offtime" v-show="['udp', 'telnet'].indexOf(newitem.channel) >= 0">
<label for="offtime" v-show="['udp', 'telnet','shutdown', 'wol'].indexOf(newitem.channel) >= 0">
When to send the off command?<br>
<input type="time" name="offtime" v-model="newitem.offtime"> <br>
</label>
@ -66,12 +67,14 @@
<label>sunday<br><input type="checkbox" v-model="newitem.days" value="Sunday"></label>
</label>
</div>
<div v-show="['udp', 'telnet'].indexOf(newitem.channel) >= 0">
<div v-show="['udp', 'telnet','shutdown', 'wol'].indexOf(newitem.channel) >= 0">
<label for="ip">
What ip-s to send the signal to? (Each IP: new line!)<br>
<small v-show="newitem.channel === 'wol'">Enter <strong>username:password@IP.AD.RESS</strong><br></small>
<textarea name="ip" v-model="newitem.ip" rows="10">
</textarea>
</label>
<label for="port" v-show="newitem.channel != ''">
What is the network port?<br>
@ -80,7 +83,7 @@
</div>
<div v-show="newitem.channel === 'wol'">
<label for="macAdress">
What MAC Addresses to send the signal to? (Each MAC Address: new line!)<br>
What MAC:Add:ress:es to send the signal to? (Each MAC Address: new line!)<br>
<textarea name="macAdress" v-model="newitem.macAddress" rows="10">
</textarea>
@ -145,13 +148,13 @@ var app = new Vue({
add: function() {
this.adding = !this.adding
},
sendsignal: function(item, action) {
sendsignal: function(item, action, label) {
if (this.sending) { return alert('Please wait, the signal is sending')}
this.sending = true
console.log(`sending signal ${action} to ${item.name}`)
axios
.post('sendsignal', {'item': item, 'action': action}).then(response => {
.post('sendsignal', {'item': item, 'action': action, 'label': label}).then(response => {
this.messages = response.data
this.sending = false
}, error => {
@ -168,7 +171,9 @@ var app = new Vue({
}
},
showinfo: function(item) {
alert(this.dump(item,2))
this.newitem = item;
this.adding = true;
// alert(this.dump(item,2))
},
dump: function(arr,level) {
var dumped_text = "";

Loading…
Cancel
Save