ein wenig überarbeitet:
Im Scripts die defines entsprechend euren Werten adaptieren.
- Client Socket Instanz verworfen, die Daten werden aktuell direkt eingelesen
- folgende Funktionen stehen zur Verfügung:
GetBulbList () -> liest den Werte aller Lampen und erstellt entsprechende Variablen unter OSR_CATEGORYLIGHT
SetBulbState($MAC, true/false) -> Schaltet die Lampe mit der $MAC ein. bzw. aus
$State = GetBulbInfo($MAC) -> Liest die Werte der Lampe mit der $MAC ein und liefert den ein/aus Status der Lampe
SetBulbBrightness($MAC, $Value) -> Dimmt die Lampe $MAC aud $Value (0 - 100, wobei 0 die Lampe ausschaltet)
Osram Funktionen
<?
//Start writing your scripts between the brackets
# Commands
# 13 all light status (returns list of light address, light status, light name)
# 1E group list (returns list of group id, and group name)
# 26 group status (returns group id, group name, and list of light addresses)
# 31 set group brightness
# 32 set group on/off
# 33 set group temp
# 36 set group colour
# 68 light status (returns light address and light status)
//Local definitions
define('OSR_GATEWAYIP', "192.168.1.1");
define('OSR_CATEGORYLIGHT', 12842);
define('OSR_CATEGORYGROUP', 32768);
define('OSR_GETLIGHTLIST', 19);
define('OSR_GETGROUPLIST', 0x1E);
define('OSR_GETLIGHTINFO', 104);
define('OSR_SETLIGHTTOGGLE', 50);
define('OSR_TRANSITION', 0x5);
function OpenSocket($ip) {
$socket = fsockopen($ip, 4000, $errno, $errstr, 5);
if(!$socket) {
throw new Exception($errno .": ".$errstr);
return false;
}
stream_set_timeout($socket, 1);
return $socket;
}
function CloseSocket($socket) {
fclose($socket);
}
function SendData($socket, $data, $size) {
fwrite($socket, chr(strlen($data)).chr(0x00).$data);
return fread($socket, $size);
}
function GetBulbInfo($MAC) {
$socket = OpenSocket(OSR_GATEWAYIP);
if ($socket != false) {
$data = chr(0x0).chr(0x68).chr(0x0).chr(0x0).chr(0x0).chr(0x0).ConvertMACtoChr($MAC);
$buffer = SendData($socket, $data, 1024);
//IPS_LogMessage("length", strlen($buffer));
if (strlen($buffer) >= 29) {
$State = GetBulbData($buffer, false);
}
CloseSocket($socket);
return (isset($State) ? $State : -1);
}
}
function GetBulbList() {
$socket = OpenSocket(OSR_GATEWAYIP);
if ($socket != false) {
$data = chr(0x00).chr(0x13).chr(0x00).chr(0x00).chr(0x00).chr(0x00).chr(0x01).chr(0x00).chr(0x00).chr(0x00).chr(0x00);
$buffer = SendData($socket, $data, 4096);
//fwrite($socket, $data);
//$buffer = fread($socket, 4096);
//IPS_LogMessage("length", strlen($buffer));
if (strlen($buffer) >= 11) {
$cnt = ord($buffer{9});
$buffer = substr($buffer, 11);
for ($i = 0; $i < $cnt; $i++) {
$len = GetBulbData(substr($buffer, 0, 60), true);
$buffer = substr($buffer, $len);
}
}
CloseSocket($socket);
return true;
}
return false;
}
function SetBulbState($MAC, $State) {
$socket = OpenSocket(OSR_GATEWAYIP);
if ($socket != false) {
$data =chr(0x00).chr(0x32).chr(0x00).chr(0x00).chr(0x00).chr(0x00).ConvertMACtoChr($MAC).chr(($State == "1") ? 1 : 0);
$buffer = SendData($socket, $data, 1024);
CloseSocket($socket);
return true;
}
return false;
}
function SetBulbBrightness($MAC, $Value) {
$socket = OpenSocket(OSR_GATEWAYIP);
if ($socket != false) {
$data = chr(0x0).chr(0x31).chr(0x0).chr(0x0).chr(0x0).chr(0x0).ConvertMACtoChr($MAC).chr($Value).chr(OSR_TRANSITION).chr(0x0);
$buffer = SendData($socket, $data, 1024);
CloseSocket($socket);
return true;
}
return false;
}
function GetBulbData($data, $list) {
$MAC = ConvertChrToMAC(($list) ? substr($data, 2, 8) : substr($data, 11, 8));
//IPS_LogMessage("MAC", $MAC);
$State =-1;
$eob = false;
if ($list) {
$len = 42; //We expect 42 bytes per light, but some lights may have bigger space
for($i = 26; $i < strlen($data); $i++) {
if ($eob && $data{$i} !== chr(0)) {
$len = $i;
break;
}
if ($data{$i} == chr(0)) $eob = true;
}
$id = CreateBulbVariables(OSR_CATEGORYLIGHT, (string)trim(substr($data, 26, 15)), $MAC, null, 0);
CreateBulbVariables($id, "Firmware", (string)(GetBulbFirmware($data{11}).GetBulbFirmware($data{12}).GetBulbFirmware($data{13})."xx"), null, 6);
} else {
$Child = IPS_GetChildrenIDs(OSR_CATEGORYLIGHT);
foreach($Child as $key => $value) {
if (GetValue($value) == $MAC) {
$data = substr($data, 3);
$id = $value;
break;
}
}
}
if (isset($id)) {
$State = (bool)((ord($data{18}) == 1) ? true : false);
CreateBulbVariables($id, "State",$State, "~Switch", 0);
CreateBulbVariables($id, "Color Temperature", (int)hexdec(dechex(ord($data{21})).dechex(ord($data{20}))), null, 1);
CreateBulbVariables($id, "Color Brightness", (int)ord($data{19}), "~Intensity.100", 2);
CreateBulbVariables($id, "Red", (int)ord($data{22}), null, 3);
CreateBulbVariables($id, "Green", (int)ord($data{23}), null, 4);
CreateBulbVariables($id, "Blue", (int)ord($data{24}), null, 5);
}
return (($list) ? $len : $State);
}
function GetBulbFirmware($data) {
$String = ord($data);
return (strlen($String) == 1) ? "0".$String : $String;
}
function CreateBulbVariables($ParentID, $Name, $Value, $Profile, $pos) {
$id = @IPS_GetVariableIDByName($Name, $ParentID);
if ($id === false) {
$id = IPS_CreateVariable(ConvertPHPTypeToIPS(gettype($Value)));
IPS_SetName($id, $Name);
IPS_SetParent($id, $ParentID);
IPS_SetPosition($id, $pos);
if ($Profile) IPS_SetVariableCustomProfile($id, $Profile);
}
SetValue($id, $Value);
return $id;
}
function ConvertMACtoChr($MAC) {
$MAC = explode(":", $MAC);
$Result = "";
foreach($MAC as $value) {
$Result = chr(hexdec($value)).$Result;
}
return $Result;
}
function ConvertChrToMAC($chr) {
$Result = array();
for($i = 0; $i < 8; $i++) {
$hex = dechex(ord(substr($chr, $i, 1)));
if (strlen($hex) == 1) $hex = "0".$hex;
$Result[] = $hex;
}
return implode(":", array_reverse($Result));
}
function ConvertPHPTypeToIPS($type) {
switch (strtolower($type)) {
case "boolean": return 0;
case "integer": return 1;
case "float": return 2;
case "string": return 3;
}
}
?>
Besipiel Script
<?
//Start writing your scripts between the brackets
//Global includes
include(IPS_GetScriptFile(50035)); //Osram fucntions [script]
GetBulbList();
$MAC = GetValue(53031);
SetBulbState($MAC, true);
$State = GetBulbInfo($MAC);
var_dump($State);
?>