PHP_SMART_HOME_V3/app/plugins/DameJidlo.php
2021-02-10 16:13:22 +01:00

86 lines
3.5 KiB
PHP

<?php
class DameJidlo extends VirtualDeviceManager
{
private $virtual_device_name = "Dáme Jídlo";
private $device_type = "virtual-device";
private $subdevice_type = "dame-jidlo";
public function deliveryTime($token)
{
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://cz.fd-api.com/api/v5/tracking/active-orders?time_variation=Variation2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Authority: cz.fd-api.com';
$headers[] = 'Sec-Ch-Ua: \"Chromium\";v=\"88\", \"Google Chrome\";v=\"88\", \";Not A Brand\";v=\"99\"';
$headers[] = 'Accept: application/json, text/plain, */*';
$headers[] = 'Authorization: Bearer ' . $token;
$headers[] = 'X-Pd-Language-Id: 2';
$headers[] = 'Sec-Ch-Ua-Mobile: ?0';
$headers[] = 'X-Fp-Api-Key: volo';
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36';
$headers[] = 'Origin: https://www.damejidlo.cz';
$headers[] = 'Sec-Fetch-Site: cross-site';
$headers[] = 'Sec-Fetch-Mode: cors';
$headers[] = 'Sec-Fetch-Dest: empty';
$headers[] = 'Referer: https://www.damejidlo.cz/';
$headers[] = 'Accept-Language: en-US,en;q=0.9,cs-CZ;q=0.8,cs;q=0.7';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return (isset(json_decode($result, true)['data']['active_orders'][0]['delivery']['time']['eta']) ? json_decode($result, true)['data']['active_orders'][0]['delivery']['time']['eta'] : 0);
}
function make()
{
//Register the settings
$settingMng = new SettingsManager();
if (!($settingField = $settingMng->getByName("damejidlo_token","damejidlo"))) {
$settingMng->create("damejidlo_token", "", "damejidlo");
} else {
$bearer = $settingField['value'];
}
try {
if (DeviceManager::registeret($this->virtual_device_name)) {
$deviceId = DeviceManager::getDeviceByToken($this->virtual_device_name)['device_id'];
if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, $this->subdevice_type)) {
SubDeviceManager::create($deviceId, $this->subdevice_type, '');
sleep(1);
$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($this->subdevice_type));
}
//if (!$this->fetchEnabled($deviceId,$subDevice['subdevice_id'])) die();
RecordManager::create($deviceId, $this->subdevice_type, $this->deliveryTime($bearer), 'plugin');
} else {
DeviceManager::create($this->virtual_device_name, $this->virtual_device_name, 'senzore-virtual');
DeviceManager::approved($this->virtual_device_name);
}
return 'sucessful';
} catch (Exception $e) {
return 'exception: ' . $e->getMessage();
}
}
function translate($value){
if ($value == 0) {
return " Delivered";
} else if ($value <= 1) {
return "less " . $value;
}
return '';
}
}