summaryrefslogblamecommitdiffstats
path: root/modules-available/locationinfo/api.inc.php
blob: 24919ba128cdaa2ff5a44f9b07997ffc124587b9 (plain) (tree)
1
2
3
4
5
6
7
8
9

     



                           

                   
   
                              
   

                           
 

                                                      
                       


                                                                
                                                                      
                                  
                                                                          
                                                
                                      
                                                             
                                     


                                                
                                        
                                                                      
                                                           
                                            
                                                                      

                                                        
                                                                      
                                                                  
         
                               
                                                                        
                                          


                                          


         
   
                                                          




                                                          
                                                     
  

                                       
   
                                                
 
                                                                                                                            




                                        








                                                                                                                    


   

                                             
                                                 
                                     
   
                                                             
 
                            


                                       

                                        
                                       
                                      
                                       
                  
         

                                    
                                                                 




                                                                                                                           

                                                      

                                                            
                                                          
                                                             





                                                                                 

                         
         

                                       

 
   
                                                 
  
                                                    
                                   
   
                                              
 


                                                         
                                         
 
                                                  

 
                                                              
 




                                                                 
                                                                                                
                 
         
                    
 
<?php

/*
 * vvv - API to Panel - vvv
 */

HandleParameters();

/**
 * Handles the API parameters.
 */
function HandleParameters()
{

	$get = Request::get('get', 0, 'string');
	$uuid = Request::get('uuid', false, 'string');
	$output = null;
	if ($get === "timestamp") {
		$output = array('ts' => getLastChangeTs($uuid));
	} elseif ($get === "machines") {
		$locationIds = LocationInfo::getLocationsOr404($uuid);
		$output = array();
		InfoPanel::appendMachineData($output, $locationIds, true);
		$output = array_values($output);
	} elseif ($get === "config") {
		$type = InfoPanel::getConfig($uuid, $output);
		if ($type === null) {
			http_response_code(404);
			die('Panel not found');
		}
	} elseif ($get === "pcstates") {
		$locationIds = LocationInfo::getLocationsOr404($uuid);
		$output = getPcStates($locationIds, $uuid);
	} elseif ($get === "locationtree") {
		$locationIds = LocationInfo::getLocationsOr404($uuid);
		$output = getLocationTree($locationIds);
	} elseif ($get === "calendar") {
		$locationIds = LocationInfo::getLocationsOr404($uuid);
		$output = LocationInfo::getCalendar($locationIds);
	}
	if ($output !== null) {
		Header('Content-Type: application/json; charset=utf-8');
		echo json_encode($output);
	} else {
		http_response_code(404);
		echo 'Unknown get option';
	}
}

/**
 * Get last config modification timestamp for given panel.
 * This is incomplete however, as it wouldn't react to the
 * linked room plan being edited, or added/removed PCs
 * etc. So the advice would simply be "if you want the
 * panel to reload automatically, hit the edit button
 * and click save". Might even add a shortcut
 * reload-button to the list of panels at some point.
 *
 * @param string $paneluuid panels uuid
 * @return int UNIX_TIMESTAMP
 */
function getLastChangeTs(string $paneluuid): int
{
	$panel = Database::queryFirst('SELECT lastchange, locationids FROM locationinfo_panel WHERE paneluuid = :paneluuid',
		compact('paneluuid'));
	if ($panel === false) {
		http_response_code(404);
		die('Panel not found');
	}
	$lastChange = array((int)$panel['lastchange']);
	if (!empty($panel['locationids'])) {
		$res = Database::simpleQuery('SELECT lastchange FROM locationinfo_locationconfig
				WHERE locationid IN (:locs)', array('locs' => explode(',', $panel['locationids'])));
		while (($lc = $res->fetchColumn()) !== false) {
			$lastChange[] = (int)$lc;
		}
	}
	return max($lastChange);
}

/**
 * Gets the pc states of the given locations.
 *
 * @param int[] $idList list of the location ids.
 * @return array aggregated PC states
 */
function getPcStates(array $idList, string $paneluuid): array
{
	$pcStates = array();
	foreach ($idList as $id) {
		$pcStates[$id] = array(
			'id' => $id,
			'idle' => 0,
			'occupied' => 0,
			'offline' => 0,
			'broken' => 0,
			'standby' => 0,
		);
	}

	$locationInfoList = array();
	InfoPanel::appendMachineData($locationInfoList, $idList);

	$panel = Database::queryFirst('SELECT paneluuid, panelconfig FROM locationinfo_panel WHERE paneluuid = :paneluuid',
		compact('paneluuid'));
	$config = json_decode($panel['panelconfig'], true);

	foreach ($locationInfoList as $locationInfo) {
		$id = $locationInfo['id'];
		foreach ($locationInfo['machines'] as $pc) {
			$key = strtolower($pc['pcState']);
			if (isset($pcStates[$id][$key])) {
				if ($config['roomplanner']) {
					if (isset($pc['x']) && isset($pc['y'])) {
						$pcStates[$id][$key]++;
					}
				} else {
					$pcStates[$id][$key]++;
				}
			}
		}
	}

	return array_values($pcStates);
}

/**
 * Gets the location tree of the given locations.
 *
 * @param int[] $idList Array list of the locations.
 * @return array location tree data
 */
function getLocationTree(array $idList): array
{
	if (in_array(0, $idList)) {
		return array_values(Location::getTree());
	}
	$locations = Location::getTree();

	return findLocations($locations, $idList);
}

function findLocations(array $locations, array $idList): array
{
	$ret = array();
	foreach ($locations as $location) {
		if (in_array($location['locationid'], $idList)) {
			$ret[] = $location;
		} elseif (!empty($location['children'])) {
			$ret = array_merge($ret, findLocations($location['children'], $idList));
		}
	}
	return $ret;
}