Permalink
Browse files

Merge pull request #216 from tarasfrompir/master

Обновлен yeelight
  • Loading branch information...
T-REX-XP committed Jul 30, 2018
2 parents 2422294 + 55aa851 commit 0992811a9db9394caf66f87c1e7551844997bdf9
@@ -1,11 +1,10 @@
<?php
require(dirname(__FILE__).'/../ssdp_finder/upnp/vendor/autoload.php');
use jalder\Upnp\BelkinDevice;
$BelkinDevice= new BelkinDevice();
require(dirname(__FILE__).'/../ssdp_finder/upnp/vendor/jalder/upnp/src/Yeelight/Yeelight.class.php');
$adress = $this->getProperty("CONTROLADDRESS");
$status = $this->getProperty("turnOff");
$remote = new BelkinDevice\Devswitch($adress);
$result = $remote->off();
$yee = new Yeelight($adress);
$result = $yee->set_power("off"); // power off
//print_r($result);
if ($result) {
$this->setProperty('status', 0);
@@ -14,3 +13,24 @@
$this->setProperty('alive', 0);
say ("Выключатель ".$this->object_title." размещенный в комнате ".$this->getProperty("linkedRoom")." не сработал!", 2);
};
$yee->disconnect();
//<?php
//require "Yeelight.class.php";
//$yee = new Yeelight("10.0.0.201", 55443);
//$yee->set_power("on"); // power on
//$yee->set_rgb(0xFF0000); // color to red
//$yee->set_bright(50); // brightness to 50%
//$yee->commit(); // changes are not sent to the bulb before commit() is called
//sleep(10);
//$yee->set_rgb(0x00FF00)->set_bright(100)->commit(); // calls return the object for fast chaining of commands
//$status = $yee->get_prop("power")->commit(); // get current status
//print_r($status);
//$yee->disconnect();
@@ -1,11 +1,10 @@
<?php
require(dirname(__FILE__).'/../ssdp_finder/upnp/vendor/autoload.php');
use jalder\Upnp\BelkinDevice;
$BelkinDevice= new BelkinDevice();
require(dirname(__FILE__).'/../ssdp_finder/upnp/vendor/jalder/upnp/src/Yeelight/Yeelight.class.php');
$adress = $this->getProperty("CONTROLADDRESS");
$status = $this->getProperty("turnOn");
$remote = new BelkinDevice\Devswitch($adress);
$result = $remote->on();
$yee = new Yeelight($adress);
$result = $yee->set_power("on"); // power on
//print_r($result);
if ($result) {
$this->setProperty('status', 1);
@@ -14,3 +13,24 @@
$this->setProperty('alive', 0);
say ("Выключатель ".$this->object_title." размещенный в комнате ".$this->getProperty("linkedRoom")." не сработал!", 2);
};
$yee->disconnect();
//<?php
//require "Yeelight.class.php";
//$yee = new Yeelight("10.0.0.201", 55443);
//$yee->set_power("on"); // power on
//$yee->set_rgb(0xFF0000); // color to red
//$yee->set_bright(50); // brightness to 50%
//$yee->commit(); // changes are not sent to the bulb before commit() is called
//sleep(10);
//$yee->set_rgb(0x00FF00)->set_bright(100)->commit(); // calls return the object for fast chaining of commands
//$status = $yee->get_prop("power")->commit(); // get current status
//print_r($status);
//$yee->disconnect();
@@ -42,115 +42,111 @@ function Scan(){
$table_name='ssdp_devices';
foreach ($everything as $deviceInfo) {
// то что надо обработать в первую очередь
$device= $deviceInfo['description']['device'];
$control_url = $deviceInfo['location'];
$logo= getDefImg($deviceInfo["location"],$device);
// для начала проверяем не майкрософтовое ли это устройство
// и если да то подгружаем внутренний файл потому что он находится в ссылке на файл
// for microsoft devices
if (substr($deviceInfo['location'], 0, 9) == "Location:") {
$control_url = str_ireplace("Location:", "", $deviceInfo['location']);
libxml_use_internal_errors(true);
$xml = simplexml_load_file($control_url);
$json = json_encode($xml);
$dev = (array)json_decode($json, true);
$device= $dev['device'];
$logo= getDefImg($control_url,$device);
}
// проверяем на наличие в базе для запрета вывода
$uuid = $device["UDN"];
$existed = SQLSelectOne("SELECT * FROM $table_name WHERE UUID='".$uuid."'");
// иногда вместо serialNumber есть modelNumber
$serialnumber = $device["serialNumber"];
if (!$serialnumber){
$serialnumber = $device["modelNumber"];
}
// иногда presentationURL отсутствует
$presenturl = $device["presentationURL"];
if (!$device["presentationURL"]){
$presenturl='http://'.getIp($control_url,false);
}
// иногда modelDescription отсутствует тогда берем server
$descript = $device["modelDescription"];
if (!$device["modelDescription"]){
$descript = $deviceInfo["server"];
}
// если устройство yeelight
if (substr($deviceInfo['location'], 0, 9) == "yeelight:") {
$control_url = str_ireplace("yeelight:", "http:", $deviceInfo['location']);
$logo= getDefImg($control_url,$device);
// проверяем на наличие в базе для запрета вывода
$uuid = $deviceInfo['location'];
$existed = SQLSelectOne("SELECT * FROM $table_name WHERE UUID='".$uuid."'");
if (!array_search_result($result, 'UUID', $uuid) && !is_null($uuid) && !($existed)) {
$result[] = [
"ID" => $existed["ID"], //existed id Majordomo
"TITLE" => 'Yeelight bulb',//friendly name
"ADDRESS" => 'https://www.yeelight.com' ,//presentation url (web UI of device),//presentation url (web UI of device)
"UUID" => $deviceInfo['location'],
"LOGO" => $logo,//Logo
"DESCRIPTION" => 'Yeelight WiFi Light', //description get from xml or field "server"
"TYPE" => 'YeelightWifiBulb',//DeviceType
"SERIAL" => 'not existed', //serialnumber
"MANUFACTURERURL" => 'https://www.yeelight.com',//manufacturer url
"UPDATED" => '',
"MODEL" => 'not existed',//model
"MODELNUMBER" => 'not existed',//modelNumber
"MANUFACTURER" => 'Yeelight',//Manufacturer
"SERVICES"=> 'RGBWSmartLight',//list services of device
"CONTROLADDRESS"=> $control_url,//list services of device
];
$_SESSION[$uuid] = $logo;
session_write_close();
}
// иначе проверяем остальные устройства
} else {
if (!array_search_result($result, 'UUID', $uuid) && !is_null($uuid) && !($existed)) {
$result[] = [
"ID" => $existed["ID"], //existed id Majordomo
"TITLE" => $device["friendlyName"],//friendly name
"ADDRESS" => $presenturl ,//presentation url (web UI of device),//presentation url (web UI of device)
"UUID" => $uuid,
"LOGO" => $logo,//Logo
"DESCRIPTION" => $descript, //description get from xml or field "server"
"TYPE" => explode(":", $device["deviceType"])[3],//DeviceType
"SERIAL" => $serialnumber, //serialnumber
"MANUFACTURERURL" => $device["manufacturerURL"],//manufacturer url
"UPDATED" => '',
"MODEL" => $device["modelName"],//model
"MODELNUMBER" => $device["modelNumber"],//modelNumber
"MANUFACTURER" => $device["manufacturer"],//Manufacturer
"SERVICES"=> getServices($device),//list services of device
"CONTROLADDRESS"=> $control_url,//list services of device
];
$_SESSION[$uuid] = $logo;
session_write_close();
}
}
// scaned for yeelight devices
$everything = $upnp->discoveryeelight();
foreach ($everything as $deviceInfo) {
$logo= getDefImg($deviceInfo["location"],$device);
$control_url = str_ireplace("yeelight:", "http:", $deviceInfo['location']);
$logo= getDefImg($deviceInfo["location"],$device);
// проверяем на наличие в базе для запрета вывода
$uuid = $deviceInfo['location'];
$existed = SQLSelectOne("SELECT * FROM $table_name WHERE UUID='".$uuid."'");
if (!array_search_result($result, 'UUID', $uuid) && !is_null($uuid) && !($existed)) {
$result[] = [
"ID" => $existed["ID"], //existed id Majordomo
"TITLE" => 'Yeelight bulb',//friendly name
"ADDRESS" => 'https://www.yeelight.com' ,//presentation url (web UI of device),//presentation url (web UI of device)
"UUID" => $deviceInfo['location'],
"LOGO" => $logo,//Logo
"DESCRIPTION" => 'Yeelight WiFi Light', //description get from xml or field "server"
"TYPE" => 'YeelightWifiBulb',//DeviceType
"SERIAL" => 'not existed', //serialnumber
"MANUFACTURERURL" => 'https://www.yeelight.com',//manufacturer url
"UPDATED" => '',
"MODEL" => 'not existed',//model
"MODELNUMBER" => 'not existed',//modelNumber
"MANUFACTURER" => 'Yeelight',//Manufacturer
"SERVICES"=> 'RGBWSmartLight',//list services of device
"CONTROLADDRESS"=> $control_url,//list services of device
];
$_SESSION[$uuid] = $logo;
session_write_close();
}
}
// то что надо обработать в первую очередь
$device= $deviceInfo['description']['device'];
$control_url = $deviceInfo['location'];
$logo= getDefImg($deviceInfo["location"],$device);
// для начала проверяем не майкрософтовое ли это устройство
// и если да то подгружаем внутренний файл потому что он находится в ссылке на файл
// for microsoft devices
if (substr($deviceInfo['location'], 0, 9) == "Location:") {
$control_url = str_ireplace("Location:", "", $deviceInfo['location']);
libxml_use_internal_errors(true);
$xml = simplexml_load_file($control_url);
$json = json_encode($xml);
$dev = (array)json_decode($json, true);
$device= $dev['device'];
$logo= getDefImg($control_url,$device);
}
// проверяем на наличие в базе для запрета вывода
$uuid = $device["UDN"];
$existed = SQLSelectOne("SELECT * FROM $table_name WHERE UUID='".$uuid."'");
// иногда вместо serialNumber есть modelNumber
$serialnumber = $device["serialNumber"];
if (!$serialnumber){
$serialnumber = $device["modelNumber"];
}
// иногда presentationURL отсутствует
$presenturl = $device["presentationURL"];
if (!$device["presentationURL"]){
$presenturl='http://'.getIp($control_url,false);
}
// иногда modelDescription отсутствует тогда берем server
$descript = $device["modelDescription"];
if (!$device["modelDescription"]){
$descript = $deviceInfo["server"];
}
if (!array_search_result($result, 'UUID', $uuid) && !is_null($uuid) && !($existed)) {
$result[] = [
"ID" => $existed["ID"], //existed id Majordomo
"TITLE" => $device["friendlyName"],//friendly name
"ADDRESS" => $presenturl ,//presentation url (web UI of device),//presentation url (web UI of device)
"UUID" => $uuid,
"LOGO" => $logo,//Logo
"DESCRIPTION" => $descript, //description get from xml or field "server"
"TYPE" => explode(":", $device["deviceType"])[3],//DeviceType
"SERIAL" => $serialnumber, //serialnumber
"MANUFACTURERURL" => $device["manufacturerURL"],//manufacturer url
"UPDATED" => '',
"MODEL" => $device["modelName"],//model
"MODELNUMBER" => $device["modelNumber"],//modelNumber
"MANUFACTURER" => $device["manufacturer"],//Manufacturer
"SERVICES"=> getServices($device),//list services of device
"CONTROLADDRESS"=> $control_url,//list services of device
];
$_SESSION[$uuid] = $logo;
session_write_close();
}
}
}
return $result;
}
function array_search_result($array, $key, $value){
foreach ($array as $k => $v) {
if (array_key_exists($key, $v) && ($v[$key] == $value)) {
@@ -15,50 +15,28 @@ public function __construct()
public function search($st = 'ssdp:all', $mx = 2, $man = 'ssdp:discover', $from = null, $port = null, $sockTimout = '2')
{
$request = 'M-SEARCH * HTTP/1.1'."\r\n";
$request .= 'HOST: 239.255.255.250:1900'."\r\n";
$request = 'M-SEARCH * HTTP/1.1'."\r\n";
$request .= 'HOST: 239.255.255.250:1982'."\r\n";
$request .= 'MAN: "'.$man.'"'."\r\n";
$request .= 'MX: '.$mx.''."\r\n";
$request .= 'ST: '.$st.''."\r\n";
$request .= 'USER-AGENT: '.$this->user_agent."\r\n";
$request .= 'MX: '.$mx.''."\r\n";
$request .= 'ST: wifi_bulb'."\r\n";
$request .= "\r\n";
$socket = socket_create(AF_INET, SOCK_DGRAM, 0);
socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, true);
socket_sendto($socket, $request, strlen($request), 0, '239.255.255.250', 1900);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>$sockTimout, 'usec'=>'50'));
$response = array();
do {
$buf = null;
if (($len = @socket_recvfrom($socket, $buf, 1024, 0, $ip, $port)) == -1) {
echo "socket_read() failed: " . socket_strerror(socket_last_error()) . "\n";
}
if(!is_null($buf)){
$data = $this->parseSearchResponse($buf);
$response[$data['usn']] = $data;
}
} while(!is_null($buf));
socket_close($socket);
socket_sendto($socket, $request, strlen($request), 0, '239.255.255.250', 1982);
return $response;
}
//делаем запрос на yeelight
public function searchyeelight($st = 'wifi_bulb', $mx = 2, $man = 'ssdp:discover', $from = null, $port = null, $sockTimout = '2')
{
$request = 'M-SEARCH * HTTP/1.1'."\r\n";
$request .= 'HOST: 239.255.255.250:1982'."\r\n";
$request = 'M-SEARCH * HTTP/1.1'."\r\n";
$request .= 'HOST: 239.255.255.250:1900'."\r\n";
$request .= 'MAN: "'.$man.'"'."\r\n";
$request .= 'MX: '.$mx.''."\r\n";
$request .= 'ST: '.$st.''."\r\n";
$request .= 'MX: '.$mx.''."\r\n";
$request .= 'ST: '.$st.''."\r\n";
$request .= 'USER-AGENT: '.$this->user_agent."\r\n";
$request .= "\r\n";
$socket = socket_create(AF_INET, SOCK_DGRAM, 0);
socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, true);
socket_sendto($socket, $request, strlen($request), 0, '239.255.255.250', 1982);
socket_sendto($socket, $request, strlen($request), 0, '255.255.255.255', 1900);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>$sockTimout, 'usec'=>'50'));
$response = array();
do {
@@ -75,6 +53,8 @@ public function searchyeelight($st = 'wifi_bulb', $mx = 2, $man = 'ssdp:discover
return $response;
}
private function parseSearchResponse($response)
{
@@ -21,11 +21,6 @@ public function discover()
return parent::search();
}
public function discoveryeelight()
{
return parent::searchyeelight();
}
public function discover_ip($host)
{ if (!$host) {
$host = '239.255.255.250';

This file was deleted.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 0992811

Please sign in to comment.