/** * Makes requests to devices. * * @param string $controlUrl * @param string $service * @param string $method * @param array $arguments * * @return array|string * @throws \Exception */ public function request($controlUrl, $service, $method, $arguments = []) { $controlUrl = ltrim($controlUrl, '/'); $url = 'http://' . $this->ip . '/' . $controlUrl; $action = $service . '#' . $method; $xmlHeader = '<?xml version="1.0" encoding="utf-8"?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <s:Body> <u:' . $method . ' xmlns:u="' . $service . '">'; $xmlFooter = '</u:' . $method . '></s:Body></s:Envelope>'; $xmlBody = ''; foreach ($arguments as $key => $value) { $xmlBody .= '<' . $key . '>' . $value . '</' . $key . '>'; } $xml = $xmlHeader . $xmlBody . $xmlFooter; try { $options = [CURLOPT_URL => $url, CURLOPT_POST => true, CURLOPT_PORT => $this->port, CURLOPT_POSTFIELDS => $xml, CURLOPT_RETURNTRANSFER => true, CURLOPT_VERBOSE => WS::config()->get('debug', false), CURLOPT_HTTPHEADER => ['Content-Type:text/xml', 'SOAPACTION:"' . $action . '"']]; $ch = curl_init(); curl_setopt_array($ch, $options); $response = curl_exec($ch); } catch (\Exception $e) { throw $e; } return $this->formatResponse($response); }
/** * BaseDevice constructor. * * @param string $id Device ip or id * @param null $port */ public function __construct($id, $port = null) { $this->ip = self::isIp($id) ? $id : static::getDeviceIpById($id); $port = !empty($port) ? $port : WS::config()->get('port'); $this->client = new WemoClient($this->ip, $port); }
/** * Retrieves devices from cache * * @return mixed|null */ protected static function getDevicesFromStorage() { try { $file = static::$deviceFile !== null ? static::$deviceFile : WS::config()->get('device_storage'); $content = @file_get_contents($file); if (!empty($content)) { $devices = json_decode($content, true); return $devices; } else { return null; } } catch (\Exception $e) { return null; } }