Commit 8d944d0e authored by Daniel Carden's avatar Daniel Carden

Update resource.php

parent f6f21c20
Pipeline #526 failed with stage
<?php
require_once __DIR__.'/server.php';
error_reporting(0);
ini_set('display_errors', '0');
// include our OAuth2 Server object
require_once('../lmce-admin/oauth2-server.php');
// Handle a request to a resource and authenticate the access token
if (!$server->verifyResourceRequest(OAuth2\Request::createFromGlobals())) {
$server->getResponse()->send();
die;
}
echo json_encode(array('success' => true, 'message' => 'You accessed my APIs!'));
?>
\ No newline at end of file
require_once(__DIR__.'/databaseconnector.php');
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
function Checkdeviceactions($PK_DeviceTemplate){
switch($PK_DeviceTemplate){
case 37:
case 50:
case 68:
case 1786:
case 2181:
$actions = array("turnOn","turnOff");
break;
case 38:
case 1634:
case 1993:
case 2257:
case 2318:
case 2319:
$actions = array("turnOn","turnOff","setPercentage","incrementPercentage","decrementPercentage");
break;
default:
$actions = array();
break;
}
return $actions;
}
function getcommandstring($msgtype,$command,$valuetoset){
if($msgtype == 'command'){
switch($command){
case 'DeviceOff':
return '193 97 "'.$valuetoset.'"';
break;
case 'DeviceOn':
return '192 97 "'.$valuetoset.'"';
break;
case 'setPercentage':
return '184 76 "'.$valuetoset.'"';
break;
}
}
}
if(isset($_POST['service']) && $_POST['service'] == 'discoverdevices'){
mysql_select_db($database_lmceapi, $lmceapi);
$query = "SELECT
`Device`.`PK_Device` AS 'deviceID',
`Device`.`Description` AS 'deviceName',
`DeviceTemplate`.`Description` AS 'deviceDesc',
`DeviceTemplate`.`PK_DeviceTemplate`,
IFNULL(`Room`.`Description`,'Unassigned Room') AS 'roomName',
IFNULL(`Manufacturer`.`Description`,'Unknown Manufacture') AS 'ManufactureName'
FROM
`Device`
JOIN `DeviceTemplate`
ON `Device`.`FK_DeviceTemplate` = `DeviceTemplate`.`PK_DeviceTemplate`
LEFT JOIN `Room`
ON `Device`.`FK_Room` = `Room`.`PK_Room`
LEFT JOIN `Manufacturer`
ON `DeviceTemplate`.`FK_Manufacturer` = `Manufacturer`.`PK_Manufacturer`
WHERE
`DeviceTemplate`.`FK_DeviceCategory` IN(73)
AND `DeviceTemplate`.`PK_DeviceTemplate` <> 1745";
$devices = array();
$query_devices = mysql_query($query, $lmceapi) or die(mysql_error());
while ($row = mysql_fetch_array($query_devices, MYSQL_ASSOC)) {
$row_array['deviceID'] = $row['deviceID'];
$row_array['devicename'] = $row['deviceName'];
$row_array['devicedesc'] = $row['deviceDesc'];
$row_array['deviceroom'] = $row['roomName'];
$row_array['devicemanufacture'] = $row['ManufactureName'];
$row_array['deviceactive'] = true;
$row_array['deviceactions'] = Checkdeviceactions($row['PK_DeviceTemplate']);
array_push($devices, $row_array);
}
$output = ['success' => true, 'devices' => $devices];
$jsonstring = json_encode($output);
echo $jsonstring;
} else if(isset($_POST['service']) && $_POST['service'] == 'sendmessage'){
if($_POST['msgtype'] == 'command'){
$msgType = 1;
} else if($_POST['msgtype'] == 'event'){
$msgType = 2;
} else {
$msgType = 0;
}
exec('/usr/pluto/bin/MessageSend localhost -r 0 '.$_POST['deviceID'].' '.$msgType.' '.getcommandstring($_POST['msgtype'],$_POST['command'],ceil($_POST['valuetoassign'])).'',$output,$return);
echo json_encode(array('success' => true));
}
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment