Commit e72c9d2a authored by Tino Dietel's avatar Tino Dietel Committed by Clemens John

Ddding new api-call to deliver nodelist as json

parent 7f07e145
<?php
try {
require_once("../runtime.php");
require_once(ROOT_DIR.'/config/config.local.inc.php');
require_once(ROOT_DIR.'/lib/core/db.class.php');
require_once(ROOT_DIR.'/lib/core/crawling.class.php');
require_once(ROOT_DIR.'/lib/extern/node_list.php');
require_once(ROOT_DIR.'/lib/extern/node.php');
try
{
$query = "SELECT r.id,
r.hostname,
r.latitude,
r.longitude,
u.id as u_id,
u.nickname,
s.status,
s.client_count,
UNIX_TIMESTAMP(max(cr.crawl_date)) as last_seen
FROM
routers AS r
JOIN crawl_routers AS s ON r.id = s.router_id
JOIN users AS u ON r.user_id = u.id
LEFT JOIN crawl_routers AS cr ON cr.router_id = r.id AND cr.status = 'online'
WHERE
s.crawl_cycle_id = :crawl_cycle_id
GROUP BY r.id";
$stmt = DB::getInstance()->prepare($query);
$lastCycleID = (int)crawling::getLastEndedCrawlCycle()['id'];
$stmt->bindParam(':crawl_cycle_id', $lastCycleID, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch(PDOException $e)
{
echo $e->getMessage();
echo $e->getTraceAsString();
}
// no that we have something - compule the data
$nodeList = new nodeList();
$nodeList->setCommunityName('FreifunkEmskirchen');
$nodeList->setWebsite('http://www.freifunk-emskirchen.de');
$nodeList->setCommunityFile('https://raw.githubusercontent.com/ffansbach/community-files/master/emskirchen.json');
foreach($result as $resultNode)
{
$node = new node($resultNode['id'], $resultNode['hostname']);
$node->setType('AccessPoint');
$node->setHref('https://netmon.freifunk-emskirchen.de/router.php?router_id='.$resultNode['id']);
$node->setStatus(
($resultNode['status'] == 'online'),
$resultNode['client_count'],
$resultNode['last_seen']
);
$node->setGeo(
$resultNode['latitude'],
$resultNode['longitude']
);
$node->setUserId($resultNode['u_id']);
$nodeList->addPerson(
$resultNode['u_id'],
$resultNode['nickname'],
'https://netmon.freifunk-emskirchen.de/user.php?user_id='.$resultNode['u_id']
);
$nodeList->addNode($node->getNode());
}
try
{
header('Cache-Control: no-cache, must-revalidate');
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + (60*15)));
header('Content-type: application/json');
echo json_encode($nodeList->getList(), JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
}
catch (Exception $e)
{
echo 'Unable to create nodelist: ', $e->getMessage(), "\n";
}
}
catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
\ No newline at end of file
<?php
/**
* a single node of a network
*/
class node
{
/**
* node id
* @var int|string|null
*/
private $_id = null;
/**
* readable name
*
* @var string|null
*/
private $_name = null;
/**
* nodetype
*
* AccesPoint, Gateway, Switch
*
* @var string
*/
private $_type = 'AccessPoint';
/**
* url for details about that node
*
* @var stringnull
*/
private $_href = null;
/**
* status of the node
*
* will contain state (online, offline),
* clients, lastcontact (ts)
*
* @var mixed|null
*/
private $_status = null;
/**
* owner of that node
*
* will contain id, name, href
*
* @var mixed|null
*/
private $_user = null;
/**
* geolocation of that node
*
* lat, long
* @var array|null
*/
private $_geo = null;
/**
* create a new node with id and name
*
* @param int|string $id
* @param string $name
*/
public function __construct($id, $name)
{
$this->setId($id);
$this->setName($name);
}
/**
* @param int $id
*/
public function setId($id)
{
$this->_id = $id;
}
/**
* @param string $name
*/
public function setName($name)
{
$this->_name = $name;
}
/**
* @param string $type
*/
public function setType($type)
{
$this->_type = $type;
}
/**
* @param string $href
*/
public function setHref($href)
{
$this->_href = $href;
}
/**
* set the status
*
* @param string $status
* @param int $clients
* @param int $lastcontact
*/
public function setStatus($status, $clients = false, $lastcontact = false)
{
$this->_status = array(
'online' => (boolean)$status
);
if($clients !== false)
{
$this->_status['clients'] = (int)$clients;
}
if($lastcontact != false)
{
$this->_status['lastcontact'] = date('c', (int)$lastcontact);
}
}
/**
* @param int|string $id
* @param string|boolean $name
* @param string|boolean $href
*/
public function setUserId($id)
{
$this->_user = $id;
}
/**
* set geolocation of that node
*
* @param string $lat
* @param string $long
*/
public function setGeo($lat, $long)
{
$this->_geo = array('lat' => $lat, 'long' => $long);
}
/**
* returns the node-object
*
* @return mixed
*/
public function getNode()
{
if(empty($this->_id))
{
throw new Exception('NodeID missing');
return false;
}
if(empty($this->_name))
{
throw new Exception('Nodename missing');
return false;
}
$node = array(
'id' => $this->_id,
'name' => $this->_name
);
if(!empty($this->_type))
{
$node['node_type'] = $this->_type;
}
if(!empty($this->_href))
{
$node['href'] = $this->_href;
}
if(!empty($this->_status))
{
$node['status'] = $this->_status;
}
if(!empty($this->_user))
{
$node['links'] = array(
'admin-c' => array(
'type' => 'people',
'id' => $this->_user
)
);
}
if(!empty($this->_geo))
{
$node['location'] = $this->_geo;
}
return $node;
}
}
\ No newline at end of file
<?php
/**
* a list of networknodes
*/
class nodeList
{
/**
* version for json
*
* @var string
*/
private $_version = '1.0.0';
/**
* Human readable name for this community
*
* @var string
*/
private $_communityName = '';
/**
* website/homepage of the community
*
* @var string
*/
private $_website = '';
/**
* the url of the community-file used for freifunk-api
*
* @var string
*/
private $_communityFile = '';
/**
* will hold all nodes
*
* @var array
*/
private $_nodeList = array();
/**
* will contain all the admin-c
*
* @var array
*/
private $_peopleList = array();
/**
* @param string
*/
public function setCommunityName($name)
{
if(!empty($name))
{
$this->_communityName = $name;
}
}
/**
* @param string
*/
public function setWebsite($website)
{
if(!empty($website))
{
$this->_website = $website;
}
}
/**
* @param string
*/
public function setCommunityFile($url)
{
if(!empty($url))
{
$this->_communityFile = $url;
}
}
/**
* adds a single node to the list
*
* @param mixed[]
*/
public function addNode($node)
{
array_push($this->_nodeList, $node);
}
public function addPerson($id, $name, $href)
{
$this->_peopleList[$id] = array(
'id' => $id,
'name' => $name,
'href' => $href
);
}
/**
* returns the data containing all nodes
*
* @return mixed[]
*/
public function getList()
{
if(empty($this->_communityName))
{
throw new Exception('CommunityName missing');
}
if(empty($this->_website))
{
throw new Exception('Website missing');
}
if(empty($this->_nodeList))
{
throw new Exception('no Routers');
}
$list = array(
'version' => $this->_version,
'updated_at' => date("c"),
'community' => array(
'name' => $this->_communityName
),
);
if(!empty($this->_communityFile))
{
$list['community']['href'] = $this->_communityFile;
}
$list['nodes '] = $this->_nodeList;
foreach($this->_peopleList as $person)
{
$list['linked '][] = $person;
}
return $list;
}
}
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