Device manipulation methods (websockets) - PJanisio/ewelinkApiPhp GitHub Wiki

Websocket introduction

As documentation states: websocket is more robust to update the device parameters. From experience I had with the devices I own, it is not completely true. All devices updates without any issues with just http gateway and websocket solution is 10x slower. But websocket method has its benefits. It is able to wake up the device and refresh parameters.

For example:

There is an issue with Sonoff POWR2 device and could be it is related with other devices that meters the energy consumption and reports:

  • current
  • voltage
  • power

In case of POWR2 - those parameters doesnt update until you open the app, or use websocket methods to get those parameters.

That is why, those methods have been implemented.

Requirements

  • enabled posix_kill
  • enabled pcntl_fork

Example: Websocket Get Data

Featch parameter from device using websocket. When $params will be empty, it will fetch all device parameters.

// Identifier can be device ID or device name
$identifier = 'YourDeviceNameOrID';
$params = ['power', 'voltage']; // Parameters to query

// Initialize WebSocket connection
$wsClient = $devices->initializeWebSocketConnection($identifier);

// Get data using WebSocket
$data = $devices->getDataWebSocket($identifier, $params);

echo "<pre>";
print_r($data);
echo "</pre>";

Example: Websocket Set Data

Set parameters of the device using websocket.

// Identifier can be device ID or device name
$identifier = 'YourDeviceNameOrID';
$params = ['switch' => 'on']; // Parameters to set

// Initialize WebSocket connection
$wsClient = $devices->initializeWebSocketConnection($identifier);

// Set data using WebSocket
$response = $devices->setDataWebSocket($identifier, $params);

echo "<pre>";
print_r($response);
echo "</pre>";

Example: Force Wake Up

That is special function that replaced: forceGetData and forceUpdateData.

As many devices needs a special trigger to refresh their parameters (like energy, temp etc) this function can be used just in front fetching current parameters.

Returns TRUE when successfull and FALSE when failed.

            $forceWakeUpRes = $devs->forceWakeUp($devIdent);

            if ($forceWakeUpRes) {
                $allLiveParams = $devs->getAllDeviceParamLive($devIdent);
                echo '<h1>Get All Device Parameters Live After Force Wake Up</h1>';
                echo '<pre>' . print_r($allLiveParams, true) . '</pre>';
            }
⚠️ **GitHub.com Fallback** ⚠️