Page 1 of 1

JSON to query device by name

Posted: Friday 23 December 2016 19:46
by kilbukas
Hello All,

i have looked for the feature, but have failed to find. Pls, excuse me, if something similar exists.

Is it possible to extend JSON processing to query device information name, e.g. the following query

Code: Select all

http://domoticz:8080/json.htm?type=devices&name=Temp_test1
could respond with

Code: Select all

{

    "ActTime": 1474795292,
    "ServerTime": "2016-12-23 12:21:32",
    "Sunrise": "07:11",
    "Sunset": "19:08",
    "result": [
        {
            "AddjMulti": 1.0,
            "AddjMulti2": 1.0,
            "AddjValue": 0.0,
            "AddjValue2": 0.0,
            "BatteryLevel": 255,
            "CustomImage": 0,
            "Data": "0.0 C",
            "Description": "",
            "Favorite": 0,
            "HardwareID": 2,
            "HardwareName": "test",
            "HardwareType": "Dummy (Does nothing, use for virtual switches only)",
            "HardwareTypeVal": 15,
            "HaveTimeout": false,
            "ID": "14053",
            "LastUpdate": "2016-12-23 12:21:13",
            "Name": "Temp_test1",
            "Notifications": "false",
            "PlanID": "0",
            "PlanIDs": [
                0
            ],
            "Protected": false,
            "ShowNotifications": true,
            "SignalLevel": "-",
            "SubType": "LaCrosse TX3",
            "Temp": 0.0,
            "Timers": "false",
            "Type": "Temp",
            "TypeImg": "temperature",
            "Unit": 1,
            "Used": 1,
            "XOffset": "0",
            "YOffset": "0",
            "idx": "4"
        }
    ],
    "status": "OK",
    "title": "Devices"
}
this would make IoT devices to find respective Idx easier.

As far as i understand current JSON solution can query/filter devices by type. However this could lead to quite big response, which would require far more processing in IoT device, hence more energy consumption, than with proposed solution.

Thank you

Re: JSON to query device by name

Posted: Friday 23 December 2016 22:13
by Egregius
You're IDX should remain the same, so you only need to set it once.

A workaround could be to query the api during startup of the device and store the IDX in a file or memory.

Re: JSON to query device by name

Posted: Thursday 29 December 2016 18:11
by kilbukas
Thanks for the answer. That is exactly the intention, to read Idx on the first access and then store it for subsequent use.

Then the question is, how would you suggest to query the api?

Re: JSON to query device by name

Posted: Thursday 29 December 2016 19:29
by Egregius
In the __CRON.php script on my github there's an example how I store idx, status and timestamp in cache for all used devices in one run.

Re: JSON to query device by name

Posted: Thursday 29 December 2016 20:07
by kilbukas
Thanks a lot for pointing it out. too bad, such php scripts are beyond my comprehension

Re: JSON to query device by name

Posted: Thursday 29 December 2016 22:03
by Egregius
You only need this part of it:

Code: Select all

$domoticz=json_decode(file_get_contents('http://127.0.0.1:8084/json.htm?type=devices&used=true'),true);
    if($domoticz){
        foreach($domoticz['result'] as $dom){
            $name=$dom['Name'];
            $type=$dom['Type'];
            if(isset($dom['SwitchType']))$switchtype=$dom['SwitchType'];else $switchtype='none';
            apcu_store('t'.$name,strtotime($dom['LastUpdate']));
            apcu_store('i'.$name,$dom['idx']);
            if($type=='Temp')apcu_store('s'.$name,str_replace(' C','',$dom['Data']));
            elseif($switchtype=='Dimmer'){
                if($dom['Data']=='Off')apcu_store('s'.$name,'Off');
                else apcu_store('s'.$name,filter_var($dom['Data'],FILTER_SANITIZE_NUMBER_INT));
            }
            else apcu_store('s'.$name,$dom['Data']);
        }
        usleep(10000);
    }
 

Re: JSON to query device by name

Posted: Monday 02 January 2017 15:44
by kilbukas
Thank you very much, this has helped me out.
Now I see the idea - to use PHP to process JSON to give better response to IoT device. It has helped me on the other topic as well - Iot device time synchronization with domoticz server.