alextoft / sureflap

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

Set Pet Location #7

Closed Strobelix80 closed 4 years ago

Strobelix80 commented 5 years ago

Hi,

Is it possible to include set pet Location? Would be great

Kind regards Chris

hdurdle commented 5 years ago

I've added this in my Powershell fork - for inside you just need to post: {where: 1, since: "2018-11-18 16:25:44"} to: https://app.api.surehub.io/api/pet/{pet_id}/position and for outside: {where: 2, since: "2018-11-18 16:25:44"}

Strobelix80 commented 5 years ago

Edit: found the mistake.. its a POST not a PUT Thanks!

I've tried this for example, but doesn't work. Can you see my mistake?

include_once 'getPet.php';

$json = json_encode(array("where" => "1", "since" => "2018-11-18 18:30")); $ch = curl_init($endpoint."/api/pet/$pet/position"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($json),"Authorization: Bearer $token")); $result = json_decode(curl_exec($ch),true) or die("Curl Failed");

alextoft commented 5 years ago

Glad you got it working. I've added a setPetLocation to the repo.

Easiest way to get the API vocab is to use the web portal @ https://surepetcare.io and just run the browser's network inspector to see the payloads. You can also take the martyr's approach, push your own root CA onto a mobile device, then use DNS doctoring to MiTM requests to app.api.surehub.io via a local web server (if your firewall supports such features - mine is a Cisco ASA, others vary).

hdurdle commented 5 years ago

Yes - I did a bunch of MITM to try and see if I could add chips without pushing a cat through the flap. No luck yet. I have a spare hub too, so was going to investigate.

In the mean time, I wrote this: https://github.com/hdurdle/alexa-cats so I could ask Alexa about my cats!

jmvedrine commented 5 years ago

I think that the current code of the setPetLocation function is only valid if the php code is executed on a computer with time set to Greenwich time (GMT). Otherwise it will write an incorrect time on the surepetcare.io server. To make it timezone aware, you need to change the since parameter.

jmvedrine commented 5 years ago

After some tests it seems that just changing "since" => date("Y-m-d H:i") to "since" => gmdate("Y-m-d H:i") solve the problem. Of course I can't be really sure because I just tested in my timezone. In fact it seems that the surepetcare.io server is expecting to receive a GMT time. Passing a correctly ISO 8601 formated date doesn't work because the difference to Greenwich time is ignored.