alextoft / sureflap

Basic PHP Examples for SureFlap API (IoT cat flap)
73 stars 17 forks source link

get Device Info #16

Open zaxxon72 opened 4 years ago

zaxxon72 commented 4 years ago

not an issue, but I never submitted any code, so I'm trying this route... staying under the radar ;)

My use case: I set/unset locking status in automatically at 01:00 and 05:00 so the cat doesn't play catch-and-crunch with mice during the night under our bed.... and when I manually set the flap to 'out' because of [some reason], the script at 01:00 would override that with 'in' so the cat got out again. So I needed my script to check the locking status before setting, to include the status from before...

I thus frankensteined setLockMode.php and some stuff I gleaned from rcastberg/sure_petcare so I could read the locking status of my flap. The status includes some more data, so I tried some other stuff. This needs more interpretation and reality-alignment, but comes in handy.

What do you think. is this usable?

<?php

include_once 'getDevices.php';

$json = json_encode(array("locking" => "$lock"));
$ch = curl_init($endpoint."/api/device/$flap/status");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $token"));
$result = json_decode(curl_exec($ch),true) or die("Curl Failed\n");

/* Output of status page, can be used to extend this script
Array
(
    [data] => Array
        (
            [locking] => Array
                (
                    [mode] => 1
                )

            [version] => Array
                (
                    [device] => Array
                        (
                            [hardware] => 0
                            [firmware] => 335
                        )

                )

            [battery] => 5.25
            [learn_mode] => 
            [online] => 1
            [signal] => Array
                (
                    [device_rssi] => -87.25
                    [hub_rssi] => -85
                )

        )

)
*/

switch($argv[1]) {
    case "locking":
        switch($result['data']['locking']['mode']) {
                case "2":
                        $lock = "in";
                        break;
                case "1":
                        $lock = "out";
                        break;
                case "3":
                        $lock = "both";
                        break;
                case "0":
                        $lock = "none";
                        break;
        }
        print "Device locking status: ".$lock."\n";
        break;
    case "battery":
        $b_volts=$result['data']['battery'];
        print "Device battery status: ".$b_volts." Volts\n";        
        break;
    case "signal":
        $rssi=$result['data']['signal']['hub_rssi'];
        print "Hub RSSI: ".$rssi." dB\n";       
        break;
    case "version":
        $firm_version=$result['data']['version']['device']['firmware'];
        print "Device Firmware Version: ".$firm_version." dB\n";        
        break;
    default:
        die("Usage: php ".$_SERVER['PHP_SELF']." [locking|battery|signal|version]\n");
}

?>