Skip to content

Commit

Permalink
Removing several unused functions from the device class and added the…
Browse files Browse the repository at this point in the history
… sensors to the cabnavigator screen. close #531
  • Loading branch information
wilpig committed May 31, 2015
1 parent 0e5e695 commit 707c842
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 45 deletions.
24 changes: 24 additions & 0 deletions api/v1/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,30 @@ function authenticate(\Slim\Route $route) {
}
});

$app->get( '/device/:deviceid/getsensorreadings', function($deviceid) {
$dev=new Device();
$dev->DeviceID=intval($deviceid);

if(!$dev->GetDevice(false)){
$response['error']=true;
$response['errorcode']=404;
$response['message']=__("Device not found");
}else{
$reading=$dev->GetSensorReading();
if(!$reading){
$response['error']=true;
$response['errorcode']=404;
$response['message']=__("Device is not a sensor or is missing a template");
}else{
$response['error']=false;
$response['errorcode']=200;
$response['sensor']=$reading;
}
}

echoResponse($response['errorcode'],$response);
});

// this is messy as all hell and i'm still thinking about how to do it better

//
Expand Down
78 changes: 35 additions & 43 deletions assets.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -1920,26 +1920,6 @@ function GetDeviceList( $datacenterid=null ) {
return $deviceList;
}

static function GetSensorDevices(){
global $dbh;

$sql="SELECT * FROM fac_Device WHERE TemplateID IN (SELECT TemplateID FROM fac_DeviceTemplate WHERE DeviceType='Sensor');";

$deviceList=array();
foreach($dbh->query($sql) as $deviceRow){
$deviceList[]=Device::RowToObject($deviceRow);
}

return $deviceList;
}

static function GetDeviceByID($DeviceID){
$dev=New Device();
$dev->DeviceID=$DeviceID;
$dev->GetDevice();
return $dev;
}

static function GetDevicesByTemplate($templateID) {
global $dbh;

Expand Down Expand Up @@ -2102,16 +2082,13 @@ static function GetReservationsByOwner( $Owner ) {
return $devList;
}


function WhosYourDaddy() {
global $dbh;

$dev = new Device();
function WhosYourDaddy(){
$dev=new Device();

if ( $this->ParentDevice == 0 ) {
if($this->ParentDevice==0){
return $dev;
} else {
$dev->DeviceID = $this->ParentDevice;
}else{
$dev->DeviceID=$this->ParentDevice;
$dev->GetDevice();
return $dev;
}
Expand Down Expand Up @@ -2147,20 +2124,6 @@ function ViewDevicesByCabinet($includechildren=false){
return $deviceList;
}

static function GetPatchPanels(){
global $dbh;

$sql="SELECT * FROM fac_Device WHERE DeviceType='Patch Panel' ORDER BY Label ASC;";

$panelList=array();

foreach($dbh->query($sql) as $row){
$panelList[$row["DeviceID"]]=Device::RowToObject($row);
}

return $panelList;
}

function DeleteDevice(){
global $dbh;

Expand Down Expand Up @@ -2208,7 +2171,6 @@ function DeleteDevice(){
return;
}


function SearchDevicebyLabel(){
global $dbh;

Expand Down Expand Up @@ -3220,6 +3182,36 @@ static function UpdateSensors($CabinetID=null){

return true;
}

function GetSensorReading(){
global $dbh;
if(!$this->getDevice()){
return false;
}
// If this isn't a sensor device or doesn't have a template we can't have readings from it
if($this->DeviceType!='Sensor' || $this->TemplateID==0){
return false;
}

$readings=new stdClass();

$sql="SELECT * FROM fac_SensorReadings WHERE DeviceID=$this->DeviceID LIMIT 1;";
if(!$row=$dbh->query($sql)->fetch()){
// Failed to get anything from the db so kick back bad data
$readings->DeviceID=$this->DeviceID;
$readings->Temperature=0;
$readings->Humidity=0;
$readings->LastRead=__("Error");
}else{
foreach($row as $prop => $val){
if(!is_numeric($prop)){
$readings->$prop=$val;
}
}
}

return $readings;
}
}

class DevicePorts {
Expand Down
32 changes: 30 additions & 2 deletions cabnavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ function renderUnassignedTemplateOwnership($noTemplFlag, $noOwnerFlag, $device)
$dev->Cabinet=$cab->CabinetID;
$devList=$dev->ViewDevicesByCabinet();

$dev->DeviceType="Sensor";
$SensorList=$dev->Search();

$totalWatts=0;
$totalWeight=0;
$totalMoment=0;
Expand Down Expand Up @@ -235,7 +238,7 @@ function BuildCabinet($rear=false){
while(list($dev_index,$device)=each($devList)){
list($noTemplFlag, $noOwnerFlag, $highlight) =
renderUnassignedTemplateOwnership($noTemplFlag, $noOwnerFlag, $device);
if($device->Height<1 && !$rear && $device->DeviceType!="CDU"){
if($device->Height<1 && !$rear && $device->DeviceType!="CDU" && $device->DeviceType!="Sensor"){
if($device->Rights!="None"){
$zeroheight.="\t\t\t<a href=\"devices.php?DeviceID=$device->DeviceID\" data-deviceid=$device->DeviceID>$highlight $device->Label</a>\n";
}else{
Expand Down Expand Up @@ -517,6 +520,21 @@ function BuildCabinet($rear=false){
}

$body.="\t</fieldset>\n";

$body.=' <fieldset name="sensors">
<legend>'.__("Environmental Sensors").'</legend>';

foreach($SensorList as $Sensor){
$body.="\t\t<a href=\"devices.php?DeviceID=$Sensor->DeviceID\">$Sensor->Label</a><br>\n";
}

if($person->CanWrite($cab->AssignedTo)){
$body.="\n\t\t<br>\n\t\t<ul class=\"nav\"><a href=\"devices.php?action=new&CabinetID=$cab->CabinetID&DeviceType=Sensor\"><li>".__("Add Sensor")."</li></a></ul>\n";
}

$body.="\t</fieldset>\n";


if ($person->CanWrite($cab->AssignedTo) || $person->SiteAdmin) {
$body.="\t<fieldset>\n";
if ($person->CanWrite($cab->AssignedTo) ) {
Expand Down Expand Up @@ -781,7 +799,6 @@ function flippyfloppy(){
<?php
}
?>

// This is gonna confuse the fuck out of me when I see this again
$('fieldset').wrap($('<div>').addClass('item').css('width','235px'));
$('#cabnotes').parent('div').css('width','470px');
Expand All @@ -790,6 +807,17 @@ function flippyfloppy(){
$('#infopanel').masonry('layout');
$('#cabnotes > div').html($('#cabnotes > div').text());

// Add sensor data to the page
$('fieldset[name=sensors] a:not([href$=Sensor])').each(function(){
var link=this;
$.get('api/v1/device/'+link.href.split('=').pop()+'/getsensorreadings',function(data){
if(!data.error){
$(link).after('<br>Temp:&nbsp;'+data.sensor.Temperature+'&deg;&nbsp;&nbsp;Humidity:&nbsp;'+data.sensor.Humidity);
// When we add data to the box it grows so we need to adjust the bricks
$('#infopanel').masonry('layout');
}
});
});
});
</script>
<style type="text/css">
Expand Down

0 comments on commit 707c842

Please sign in to comment.