[LIVE] Web application with groups of devices

Moderator: Telldus

Manticus2
Posts: 108
Joined: Fri Mar 17, 2023 9:45 am
Location: France - Bordeaux
Contact:

[LIVE] Web application with groups of devices

Post by Manticus2 »

Hello,
I present to you my web app using my PHP class for Telldus API

URL :
http://files.manticus.info/telldus/telldus-live/

I've named my devices :
Chambre - Néon
Chambre - Chauffage
....

And the application splits names on the " - " to create groups of devices (you can create groups in groups in groups if you want :) )

PREVIEW :
Image


I'm working on a "remote" in my desktop (Widget Windows Vista/7) but for the moment I have a problem with login/password ...
I will share this widget to you when I will resolve this issue



My PHP Class for Telldus LIVE API :

Code: Select all

<?php
CLASS TELLDUS_LIVE
{

  private $consumer;
  
  public function __construct()
  {
    $this->consumer = new HTTP_OAuth_Consumer(constant('TELLDUS_KEY'), constant('TELLDUS_SECRET'), $_SESSION['accessToken'], $_SESSION['accessTokenSecret']);  
  } 
  
  public function getDevices()
  {
  	$params = array('supportedMethods' => constant('TELLSTICK_TURNON') | constant('TELLSTICK_TURNOFF'));
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/devices/list', $params, 'GET')->getBody());
  }
  
  public function getDevice($id, $supportedMetods = '', $extras = '')
  {
  	$params = array('id' => $id);
  	if($supportedMetods)
    {
      $params['supportedMethods'] = $supportedMetods;
    }
  	if($extras)
    {
      $params['extras'] = $extras;
    }
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/device/info', $params, 'GET')->getBody());
  }
  
  public function addDevice($idClient, $name, $protocol, $model)
  {
  	$params = array(
      'clientId'=>$idClient, 
      'name'=>$name, 
      'protocol'=>$protocol, 
      'model'=>$model
      );
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/device/add', $params, 'GET')->getBody());
  }
  
  public function learnDevice($id)
  {
  	$params = array('id'=>$id);
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/device/learn', $params, 'GET')->getBody());
  }
  
  public function removeDevice($id)
  {
  	$params = array('id'=>$id);
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/device/remove', $params, 'GET')->getBody());
  }
  
  public function turnOnDevice($id)
  {
  	$params = array('id'=>$id);
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/device/turnOn', $params, 'GET')->getBody());
  }
  
  public function turnOffDevice($id)
  {
  	$params = array('id'=>$id);
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/device/turnOff', $params, 'GET')->getBody());
  }
  
  public function upDevice($id)
  {
  	$params = array('id'=>$id);
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/device/up', $params, 'GET')->getBody());
  }
  
  public function downDevice($id)
  {
  	$params = array('id'=>$id);
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/device/down', $params, 'GET')->getBody());
  }
  
  public function stopDevice($id)
  {
  	$params = array('id'=>$id);
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/device/stop', $params, 'GET')->getBody());
  }
  
  public function setParameterDevice($id, $parameter, $value)
  {
  	$params = array(      
      'id'=>$id,
      'parameter'=>$parameter,
      'value'=>$value
      );
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/device/setParameter', $params, 'GET')->getBody());
  }
  
  public function dimDevice($id, $level)
  {
  	$params = array(      
      'id'=>$id,
      'level'=>$level,
      );
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/device/dim', $params, 'GET')->getBody());
  }
  
  public function commandDevice($id, $method, $value)
  {
  	$params = array(      
      'id'=>$id,
      'method'=>$method,
      'value'=>$value
      );
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/device/command', $params, 'GET')->getBody());
  }
  
  public function bellDevice($id)
  {
  	$params = array('id'=>$id);
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/device/bell', $params, 'GET')->getBody());
  }
  
  public function setProtocolDevice($id, $protocol)
  {
  	$params = array(      
      'id'=>$id,
      'protocol'=>$protocol,
      );
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/device/setProtocol', $params, 'GET')->getBody());
  }
  
  public function setModelDevice($id, $model)
  {
  	$params = array(      
      'id'=>$id,
      'model'=>$model,
      );
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/device/setModel', $params, 'GET')->getBody());
  }
  
  public function setNameDevice($id, $name)
  {
  	$params = array(      
      'id'=>$id,
      'name'=>$name
      );
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/device/setName', $params, 'GET')->getBody());
  }

  public function getSensors()
  {
        $params = array();
        return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/sensors/list', $params, 'GET')->getBody());

  }

  public function getSensor($id)
  {
        $params = array('id'=>$id);
        return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/sensor/info', $params, 'GET')->getBody());

  }
  
  public function setSensorName($id, $name)
  {
        $params = array('id'=>$id, 'name'=>$name);
        return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/sensor/setName', $params, 'GET')->getBody());

  }

  public function getClients($extras = '')
  {
  	$params = array();
  	if($extras)
  	{
      $params['extras'] = $extras;
    }
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/clients/list', $params, 'GET')->getBody());
  }

  public function getClient($id, $uuid = '', $code = '', $extras = '')
  {
	  $params = array('id'=>$id);
	  if($uuid)
	  {
      $params['uuid'] = $uuid;
    }
	  if($code)
	  {
      $params['code'] = $code;
    }
	  if($extras)
	  {
      $params['extras'] = $extras;
    }
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/client/info', $params, 'GET')->getBody());
  }

  public function removeClient($id)
  {
	  $params = array('id'=>$id);
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/client/remove', $params, 'GET')->getBody());
  }

  public function registerClient($id, $uuid)
  {
	  $params = array(
      'id'=>$id,
      'uuid'=>$uuid
    );
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/client/register', $params, 'GET')->getBody());
  }

  public function setNameClient($id, $name)
  {
	  $params = array(
      'id'=>$id,
      'name'=>$name
    );
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/client/setName', $params, 'GET')->getBody());
  }

  public function jobInfoScheduler($id)
  {
	  $params = array('id'=>$id);
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/scheduler/jobInfo', $params, 'GET')->getBody());
  }

  public function jobListScheduler()
  {
	  $params = array();
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/scheduler/jobList', $params, 'GET')->getBody());
  }

  public function removeJobScheduler($id)
  {
	  $params = array('id'=>$id);
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/scheduler/removeJob', $params, 'GET')->getBody());
  }

  public function setJobScheduler($id, $deviceId, $method='2', $methodValue='0', $type='time', $hour=date('H'), $minute=date('i'), $offset='0', $randomInterval='0', $retries='3', $retryInterval='0', $reps='1', $weekdays='1,2,3,4,5,6,7')
  {
	  $params = array(
      'id'=>$id,
      'deviceId'=>$deviceId,
      'method'=>$method,
      'methodValue'=>$methodValue,
      'type'=>$type,
      'hour'=>$hour,
      'minute'=>$minute,
      'offset'=>$offset,
      'randomInterval'=>$randomInterval,
      'retries'=>$retries,
      'retryInterval'=>$retryInterval,
      'reps'=>$reps,
      'weekdays'=>$weekdays
    );
  	return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/scheduler/setJob', $params, 'GET')->getBody());
  }

}

?>
The index.php file :

Code: Select all

<?php         
  if(isset($_GET))
  {           
    require_once 'common.php';
    require_once 'class_telldus_live.php';
    $telldus_api = new TELLDUS_LIVE();
  }      
  
  if(isset($_GET))
  {           
    require_once('common.php');
    require_once('class_telldus_live.php');
    $telldus_api = new TELLDUS_LIVE();
    if(isset($_GET['PHPSESSID']))
    {
        setcookie('PHPSESSID', $_GET['PHPSESSID']);
        header('Location: index.php');
    }
  }
   
  if (isset($_GET['clear'])) {
  	session_destroy();
  	header('location:index.php');
  	exit();
  }

  if(!isset($_GET['ajax']) || !isset($_SESSION['accessToken']))
  {
    ?>
    <!DOCTYPE html>
    <html>
      <head>
        <title>Telldus remote</title>
        <link rel="stylesheet" href="remote.css" />
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
        <script type="text/javascript">
          function request(sUrl, sId)
          {
            var responseHtml = '';
            responseHtml = $.ajax(
              {
                url:'?ajax&'+sUrl,
                async:false
              }
            ).responseText;
            if(sId)
            {
              $('#'+sId).hide();
              $('#'+sId).html(responseHtml);
              $('#'+sId).slideDown();
            }
            else
            {
              alert(responseHtml);
            }
          }
        </script>
        <meta charset="utf-8">
      </head>
      <body>
        <!--<h1>Telldus remote</h1>--> 
        <?php
        if (!isset($_SESSION['accessToken'])) {
        	?>
          <h2>Session :</h2>
          No access token: <a href="getRequestToken.php">connexion</a><?php
        	exit();
        }
    }

    if(!empty($_SESSION['token']) && !empty($_SESSION['tokenSecret']) && !isset($_GET['ajax'])) {
      $oClients = $telldus_api->getClients();
      echo '<div id="clients">';
      foreach($oClients->client as $client)
      {
        if(isset($_POST['origin']) && $_POST['origin'] == 'widget_windows')
        {
          echo '<h2 class="client" onclick="changeContent(\'?getDevices&idClient='.$client->id.'\');">'.$client->name.'</h2>';
          echo '<a href="javascript:changeContent(\'?getDevices&idClient='.$client->id.'\');">'.$client->name.'</a>';
        }
        else
        {
          echo '<h2 class="client" onClick="request(\'getDevices&idClient='.$client->id.'\', \'client'.$client->id.'\');">'.$client->name.'</h2>';
        }
        echo '<div id="client'.$client->id.'"></div>';
      }
      echo '</div>';
    }

    if(isset($_GET['getDevices']))
    {
      $oSensors = $telldus_api->getSensors()->sensor;
      if($oSensors != array())
      {
          foreach($oSensors as $sensor)
          {
              $oSensor = $telldus_api->getSensor($sensor->id);
              echo '<div class="sensor tab">
                    <h3>'.ucfirst($oSensor->name).'</h3>
                    <span class="tab">'.$oSensor->data[0]->value.'°C ('.date('d M H\hi\ms\s', $oSensor->lastUpdated).')</span>
<hr />
                  </div>';

          }
      }
      $oDevices = $telldus_api->getDevices();
      if(isset($_GET['idClient']) && is_numeric($_GET['idClient']))
      {
        echo '<div id="devices">';
        $lastGroup = '';
        $group = '';
        foreach($oDevices->device as $device)
        {
          if($device->client == $_GET['idClient'])
          {
            $nbTab = 0;
            if(strpos($device->name, ' - '))
            {
              $group = explode(' - ', $device->name);
              if(count($group) > 1)
              {
                for($i=0; $i<count($group)-1; $i++)
                {
                  if(!isset($lastGroup[$i]) || $lastGroup[$i] != $group[$i])
                  {
                    echo '<div class="tab"><h2>'.$group[$i].'</h2>';
                    $nbTab++;
                  }
                  else
                  {
                    echo '<div class="tab">';
                    $nbTab++;
                  }
                }
                $name = $group[count($group)-1];
              }
              else
              {
                $name = $device->name;
              }
              $lastGroup = $group;
            }               
            echo '<div class="device tab state'.$device->state.'">
                    <h3>'.$name.'</h3>
                    <span onClick="request(\'deviceOff&idDevice='.$device->id.'\')"><img title="Off" alt="[ Off ]" src="http://live.telldus.com/img/state/state_2.png" /></span>
                    <span onClick="request(\'deviceOn&idDevice='.$device->id.'\')"><img title="On" alt="[ On ]" src="http://live.telldus.com/img/state/state_1.png" /></span>
                  </div>';
          }
          for($i=0; $i<$nbTab;$i++)
          {
            echo '</div>';
          }
        }
        echo '</div>';
      }  
    }
    
    if (isset($_GET['deviceOn'])) 
    { 
      if(isset($_GET['idDevice']) && is_numeric($_GET['idDevice']))
      {                                                        
      	echo $telldus_api->turnOnDevice($_GET['idDevice'])->status;
    	}
    }
    
    if (isset($_GET['deviceOff'])) 
    { 
      if(isset($_GET['idDevice']) && is_numeric($_GET['idDevice']))
      {                                                        
      	echo $telldus_api->turnOffDevice($_GET['idDevice'])->status;
    	}
    }
    
    
    if(!isset($_GET['ajax']) || !isset($_SESSION['accessToken']))
    {
    ?>   
      </body>
    </html>
    <?php
  }
?>
Last edited by Manticus2 on Sat Jul 07, 2012 5:28 pm, edited 3 times in total.
Image Using a Tellstick Net
Web app with group of devices : here
Map House + scheduler with sensors + Speech recognition : here
Android app : Domospeak
remylpat
Posts: 15
Joined: Fri Mar 17, 2023 9:45 am

Re: [LIVE] Web application with groups of devices

Post by remylpat »

Really cool.
I am really interrested on what you did.
I am French too (chambre ;-) and would be happy if I can help you on some testing.
Rémylpat
markjohnson

Re: [LIVE] Web application with groups of devices

Post by markjohnson »

Post removed (spam)
Manticus2
Posts: 108
Joined: Fri Mar 17, 2023 9:45 am
Location: France - Bordeaux
Contact:

Re: [LIVE] Web application with groups of devices

Post by Manticus2 »

---
Last edited by Manticus2 on Wed Jul 11, 2012 12:17 pm, edited 2 times in total.
Image Using a Tellstick Net
Web app with group of devices : here
Map House + scheduler with sensors + Speech recognition : here
Android app : Domospeak
lioelec
Posts: 60
Joined: Fri Mar 17, 2023 9:45 am
Location: TARN 81 France
Contact:

Re: [LIVE] Web application with groups of devices

Post by lioelec »

Superbe appli dans l'attente d'accéder au lien ...
telldus Live!, http://tellmon.net, http://templight.se, Domospeak
tellstickNet rev17
20x otio,chacon,phoenix,GAO
2x THN132N
1x niclasf wireless t° sensor
Manticus2
Posts: 108
Joined: Fri Mar 17, 2023 9:45 am
Location: France - Bordeaux
Contact:

Re: [LIVE] Web application with groups of devices

Post by Manticus2 »

Update of the first post (the PHP class included now all the telldus live API)
Image Using a Tellstick Net
Web app with group of devices : here
Map House + scheduler with sensors + Speech recognition : here
Android app : Domospeak
jobe64
Posts: 5
Joined: Fri Mar 17, 2023 9:45 am
Location: Björkling Sweden

Re: [LIVE] Web application with groups of devices

Post by jobe64 »

Hello Manticus

Can you please help a total n00b on how to implement this on my own web server? Just a brief How-To would be much apreciated.
The ability to group devices is nice, thats why I want to try this out.
Thank you.
Manticus2
Posts: 108
Joined: Fri Mar 17, 2023 9:45 am
Location: France - Bordeaux
Contact:

Re: [LIVE] Web application with groups of devices

Post by Manticus2 »

First post edited

If you need more help or a .zip package, tell me ;)
Image Using a Tellstick Net
Web app with group of devices : here
Map House + scheduler with sensors + Speech recognition : here
Android app : Domospeak
jobe64
Posts: 5
Joined: Fri Mar 17, 2023 9:45 am
Location: Björkling Sweden

Re: [LIVE] Web application with groups of devices

Post by jobe64 »

Thank you for the quick update, but I am still confused... In the index.php file there is two lines, require_once, naming two files. As I get it the class_telldus_live.php is the php file you allready have posted, but what about the common.php file? Or am I totaly wrong?
Manticus2
Posts: 108
Joined: Fri Mar 17, 2023 9:45 am
Location: France - Bordeaux
Contact:

Re: [LIVE] Web application with groups of devices

Post by Manticus2 »

you can download the : ZIP File
you need to edit the common.php file to enter your secret and public key (your key is available here : http://api.telldus.com/keys/index )
Image Using a Tellstick Net
Web app with group of devices : here
Map House + scheduler with sensors + Speech recognition : here
Android app : Domospeak
FredJ
Posts: 3
Joined: Fri Mar 17, 2023 9:45 am

Re: [LIVE] Web application with groups of devices

Post by FredJ »

Hello Manticus

I have been playing around with your TELLDUS_LIVE class. This is exactly what I needed. Thanks!

Everything works fine excepte the JSON_Decode return. The return value is always NULL. There seems to be some parsing error.
If parse the getBody() result by other means, the expected information is there. But I would really like to use the JSON result.

I understand that this is not a problem in your code/class. I just wonder if you have experienced similar behaviour. Am I missing something?

Thanks,
Fred
Manticus2
Posts: 108
Joined: Fri Mar 17, 2023 9:45 am
Location: France - Bordeaux
Contact:

Re: [LIVE] Web application with groups of devices

Post by Manticus2 »

it's always or only in some cases ?

for testing maybe you can edit one function to show the response before return the result of json_decode,
exemple with getSensor() function :

Code: Select all

public function getSensor($id)
  {
        $params = array('id'=>$id);
        $result = $this->consumer->sendRequest(constant('REQUEST_URI').'/sensor/info', $params, 'GET')->getBody();
        var_dump($result);
        return json_decode($result);
  }

no error in logs from appache ?
Image Using a Tellstick Net
Web app with group of devices : here
Map House + scheduler with sensors + Speech recognition : here
Android app : Domospeak
FredJ
Posts: 3
Joined: Fri Mar 17, 2023 9:45 am

Re: [LIVE] Web application with groups of devices

Post by FredJ »

With var_dump($result) I get:
string(433) " 55071 Fred Home Outdoor_Temp 1342001093 0 1 oregon 96 7200 "

With echo htmlentities($result) I get:
<?xml version="1.0" encoding="utf-8" ?> <sensor> <id> 55071 </id> <clientName> Fred Home </clientName> <name> Outdoor_Temp </name> <lastUpdated> 1342001093 </lastUpdated> <ignored> 0 </ignored> <editable> 1 </editable> <data name="temp" value="19.1" /> <data name="humidity" value="92" /> <protocol> oregon </protocol> <sensorId> 96 </sensorId> <timezoneoffset> 7200 </timezoneoffset> </sensor>

So the result is there. It is just that json_decode($result) will always result in NULL. The json_last_error I get is JSON_ERROR_SYNTAX
Manticus2
Posts: 108
Joined: Fri Mar 17, 2023 9:45 am
Location: France - Bordeaux
Contact:

Re: [LIVE] Web application with groups of devices

Post by Manticus2 »

EDIT :
your response is not in JSON but in XML format :p


in common.php :

Code: Select all

define('REQUEST_URI', constant('URL').'/json');
Image Using a Tellstick Net
Web app with group of devices : here
Map House + scheduler with sensors + Speech recognition : here
Android app : Domospeak
FredJ
Posts: 3
Joined: Fri Mar 17, 2023 9:45 am

Re: [LIVE] Web application with groups of devices

Post by FredJ »

Manticus, you are absolutley right. I totally forgot that I had changed the REQUEST_URI to /xml for previous test applications. And I should have relized this when I saw the XML results (black-out blunder). Now everything works as expected.

Thanks for your support.
Post Reply