Subversion Repositories portal2

Rev

Rev 601 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php

/*  emg - Energy Monitoring & Graphing
*  Copyright (C) 2006-2012 Romain Lievin
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software Foundation,
*  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

?>

<?php
include_once('class/discover.class.php');
include_once('class/error.class.php');
include_once('class/udp.class.php');

$error = new Error();

if(!isset($_POST['timeout'], $_POST['retry'], $_POST['port'], $_POST['operation'], $_POST['infos']))
{
        $error->push('(ERR:1) One or more fields are missing...');
}
else
{
        $timeout    = filter_var($_POST['timeout'], FILTER_SANITIZE_NUMBER_INT);
        $retry      = filter_var($_POST['retry'], FILTER_SANITIZE_NUMBER_INT);
        $port       = filter_var($_POST['port'], FILTER_SANITIZE_NUMBER_INT);

        $index      = filter_var($_POST['operation'], FILTER_SANITIZE_NUMBER_INT);
        $infos_json = filter_var($_POST['infos'], FILTER_SANITIZE_STRING);
        $infos_json = str_replace('&#34;', '\"', $infos_json);                                                         
        $infos_json = str_replace('\\', '', $infos_json);
        $infos      = json_decode($infos_json, true);

        // Enabel or disable test mode
        $test       = false;
        /*
        $handle = fopen('debug.txt', 'wt');
        fputs($handle, $infos_json);
        fclose($handle);
        */


        switch($index)
        {
                case 1:
                if($test === false)
                {
                        $discover   = DiscoverFactory::createInstance($port, $timeout, $retry);
                        $infos = $discover->queryDevices();                    
                        echo json_encode($infos);                      
                        unset($discover);
                }
                else
                {
                        echo "[{\"ip\":\"192.168.0.16\",\"mac\":\"\",\"uuid\":\"\",\"name\":\"_\"},{\"ip\":\"192.168.0.10\",\"mac\":\"\",\"uuid\":\"\",\"name\":\"\"}]";
                }
                break;

                case 2:
                if($test === false)
                {                      
                        $discover   = DiscoverFactory::createInstance($port, $timeout, $retry);
                        $infos = $discover->queryUUIDs($infos);
                        echo json_encode($infos);
                        unset($discover);
                }
                else
                {              
                        echo "[{\"ip\":\"192.168.0.16\",\"mac\":\"\",\"uuid\":\"2DC683C6-5CDC-11E1-935C-8D314824019B\",\"name\":\"_\"},{\"ip\":\"192.168.0.10\",\"mac\":\"\",\"uuid\":\"2DC683C6-5CDC-11E1-935C-8D314824019C\",\"name\":\"\"}]";
                }
                break;

                case 3:
                if($test === false)
                {
                        $discover   = DiscoverFactory::createInstance($port, $timeout, $retry);
                        $infos = $discover->queryNames($infos);
                        echo json_encode($infos);
                        unset($discover);
                }
                else
                {
                         echo "[{\"ip\":\"192.168.0.16\",\"mac\":\"\",\"uuid\":\"2DC683C6-5CDC-11E1-935C-8D314824019B\",\"name\":\"ALIX1\"},{\"ip\":\"192.168.0.10\",\"mac\":\"\",\"uuid\":\"\",\"name\":\"RASPI1\"}]";
                }
                break;

                case 4:
                if($test === false)
                {
                        $discover   = DiscoverFactory::createInstance($port, $timeout, $retry);
                        $infos = $discover->queryMACs($infos);
                        echo json_encode($infos);
                        unset($discover);
                }
                else
                {
                        echo "[{\"ip\":\"192.168.0.16\",\"mac\":\"00:1e:34:54:78\",\"uuid\":\"2DC683C6-5CDC-11E1-935C-8D314824019B\",\"name\":\"ALIX1\"},{\"ip\":\"192.168.0.10\",\"mac\":\"\",\"uuid\":\"\",\"name\":\"RASPI1\"}]";
                }
                break;

                default:
                $error->push('(ERR:2) Out-of-case operation step.');
                break;
        }
}

echo $error->display();
?>