Device manipulation methods (webAPI) - PJanisio/ewelinkApiPhp GitHub Wiki
Below you can find examples of utilizing class.
- Hint: You can use deviceId or device name which you specyfied f.e: Switch Kitchen as an argument when accessing single device.
Example sof each methods needs to be inserted after you initialize all classes:
<?php
require_once __DIR__ . '/autoloader.php';
//Class init
$http = new HttpClient();
//Get token
$token = $http->getToken();
if ($token->checkAndRefreshToken()) {
$devices = $http->getDevices();
/*
Initialize examples here
*/
} else {
$loginUrl = $http->getLoginUrl();
echo '<a href="' . htmlspecialchars($loginUrl) . '">Authorize ewelinkApiPhp</a>';
}
?>
List of devices with their devices Ids and few information about switch status, product name, etc:
// Get list of all devices with their status
$devicesList = $devices->getDevicesList();
echo '<pre>';
print_r($devicesList);
echo '</pre>';
This function asks API to reload all data for all devices and overwrite devices.json
// Fetch devices data
$devicesData = $devices->fetchDevicesData();
echo '<pre>';
print_r($devicesData);
echo '</pre>';
This function will load current device parameters that have been stored with last fetch:
// Get loaded devices data
$devicesData = $devices->getDevicesData();
echo '<pre>';
print_r($devicesData);
echo '</pre>';
Get all information about the device that is stored by using deviceId
// Get device data by device ID
$deviceId = 'your_device_id';
$deviceData = $devices->getDeviceById($deviceId);
echo '<pre>';
print_r($deviceData);
echo '</pre>';
Get information if device has multiple switches (so it is multichannel). This information you will also get by running getDevicesList
// Check if a device supports multiple channels
$deviceId = 'your_device_id';
$isMultiChannel = $devices->isMultiChannel($deviceId);
echo $isMultiChannel ? 'Yes' : 'No';
That is function which will search deeply in the device data structure to find information you want as a $searchKey.
In result it will output raw value. For excample: MAC address ort product name or parameter value.
/ Search for a specific parameter in the device data
$searchKey = 'your_search_key';
$deviceId = 'your_device_id';
$paramValue = $devices->searchDeviceParam($searchKey, $deviceId);
echo '<pre>';
print_r($paramValue);
echo '</pre>';
This function will not use stored data, but will do the request to API to get most updated parameter of device.
/ Get live parameters of a device
$deviceId = 'your_device_id';
$param = 'switch'; // or 'switches' for multi-channel
$liveParam = $devices->getDeviceParamLive($deviceId, $param);
echo '<pre>';
print_r($liveParam);
echo '</pre>';
As above, but this time it will fetch all parameters for chosen device from API:
// Get all live parameters of a device
$deviceId = 'your_device_id';
$liveParams = $devices->getAllDeviceParamLive($deviceId);
echo '<pre>';
print_r($liveParams);
echo '</pre>';
This function will change device state. You can use single or multiple parameters.
// Set the status of a device
$deviceId = 'your_device_id';
$params = ['switch' => 'on']; // Example parameters
$statusUpdateResult = $devices->setDeviceStatus($deviceId, $params);
echo $statusUpdateResult;
to change multiple parameters at once you need to use $params as array of array:
$params = [
['colorR' => 0],
['colorG' => 153],
['colorB' => 0]
];
For devices that are multichannel, f.e has 4 switches you need to add second argument:
$params = [
['switch' => 'off', 'outlet' => 0],
['switch' => 'off', 'outlet' => 1],
['switch' => 'off', 'outlet' => 2],
['switch' => 'off', 'outlet' => 3]
];
Hint: You can use additional third argument. The flag to determine the return type (1 for detailed report - string type, 0 for boolean). By default it is set to 1 and returns human readable status like: Device: X parameter Y has been updated to Z.
Switching to 0 will return TRUE if success and FALSE if failed.
$devices->setDeviceStatus($deviceId, $params, 0)
This method will get actual online status from API. It is recommended to use isOnline function before you use any other method.
// Check if a device is online
$identifier = 'your_device_id_or_name';
$isOnline = $devices->isOnline($identifier);
echo $isOnline ? 'Online' : 'Offline';
Check also: Websocket methods