OnTrue
// Put your trigger code logic here.
// To execute the Code To Run,
// use a 'return true' or 'return false' otherwise.
Program.Setup(()=>{
Program.AddControlWidget("homegenie/generic/colorlight");
Program.AddVirtualModules("HomeAutomation.MiLightRGBW", "Light", "homegenie/generic/colorlight", 0, 4);
//Program.AddVirtualModules("HomeAutomation.MiLightWhite", "Light", "homegenie/generic/light", 0, 4);
Program.AddInputField("HomeGenieMiLightAddress", "192.168.0.?", "MiLight IP Address");
Program.AddInputField("HomeGenieMiLightPort","8899", "MiLight UDP Port");
Program.AddFeature("HomeAutomation.MiLightRGBW", "Dimmer,Light", "MiLight.LevelFix", "Set level to 100% when switched on");
//Program.AddFeature("HomeAutomation.MiLightWhite", "Dimmer,Light", "MiLight.LevelFix", "Set level to 100% when switched on");
});
return true;
#pragma warning disable 0168
#pragma warning disable 0219
#pragma warning disable 0414
// Script created by Smeghead based on and thanks to
// Version 0.1 30/11/2014
// https://github.com/yasharrashedi/LimitlessLED
// https://github.com/yasharrashedi/LimitlessLED/blob/master/Milight.php
// I used these entensivelly when creating this script
// At the moment, only RBGW lights are supported and not the White only LED lights
var MILIGHT_IP_ADDR = "HomeGenieMiLightAddress";
var MILIGHT_PORT = "HomeGenieMiLightPort";
Func<byte, byte, bool> sendCommand = (byte code, byte param) =>
{
byte[] buffer = new byte[] { code, param, 0x55 };
UdpClient.SendMessage(buffer);
return true;
};
Func<int, bool> sendRGBWOnCommand = (int zone) =>
{
byte[] codes = new byte[]{0x42, 0x45, 0x47, 0x49, 0x4B};
Program.Notify("MiLight On", zone.ToString());
//Console.WriteLine("MiLight ************************ Zone " + zone + " Is On");
return sendCommand(codes[zone], (byte) 0x00);
};
Func<int, bool> sendRGBWOffCommand = (int zone) =>
{
byte[] codes = new byte[]{0x41, 0x46, 0x48, 0x4A, 0x4C};
Program.Notify("MiLight Off", zone.ToString());
//Console.WriteLine("MiLight ************************ Zone " + zone + " Is Off");
return sendCommand(codes[zone], (byte) 0x00);
};
Func<int, bool> sendRGBWWhiteCommand = (int zone) =>
{
byte[] codes = new byte[]{(byte) 0xC2, (byte) 0xC5, (byte) 0xC7, (byte) 0xC9, (byte) 0xCB};
bool result1 = sendRGBWOnCommand(zone);
Pause(0.11);
bool result2 = sendCommand(codes[zone], (byte) 0x00);
return (result1 && result2);
};
Func<int, int, bool> sendRGBWColorCommand = (int zone, int color) =>
{
bool result1 = sendRGBWOnCommand(zone);
Pause(0.11);
bool result2 = sendCommand((byte) 0x40, (byte) color);
return (result1 && result2);
};
Func<int, bool> sendRGBWDiscoCommand = (int zone) =>
{
bool result1 = sendRGBWOnCommand(zone);
Pause(0.11);
bool result2 = sendCommand((byte) 0x4D, (byte) 0x00);
return (result1 && result2);
};
Func<int, bool> sendRGBWDiscoDecCommand = (int zone) =>
{
bool result1 = sendRGBWOnCommand(zone);
Pause(0.11);
bool result2 = sendCommand((byte) 0x43, (byte) 0x00);
return (result1 && result2);
};
Func<int, bool> sendRGBWDiscoIncCommand = (int zone) =>
{
bool result1 = sendRGBWOnCommand(zone);
Pause(0.11);
bool result2 = sendCommand((byte) 0x44, (byte) 0x00);
return (result1 && result2);
};
Func<int, int, bool> sendRGBWBrightnessCommand = (int zone, int brightness) =>
{
if ( brightness >= 0 && brightness <= 100 ) {
int[] codes = new int[]{0x02, 0x03, 0x04, 0x05, 0x08, 0x09, 0x0A, 0x0B, 0x0D, 0x0E,
0x0F, 0x10, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19};
bool result1 = sendRGBWOnCommand(zone);
Pause(0.11);
int val = Math.Max(0, Math.Min(19, brightness / 5));
bool result2 = sendCommand((byte) 0x4E, (byte) codes[val]);
return (result1 && result2);
}
return false;
};
Func <int[], int> hslToMilightColor = (int[] hsl) =>
{
int color = (256 + 176 - (int)(hsl[0] / 360.0 * 255.0)) % 256;
return color & 0xfa;
};
Func <string, int> hexdec = (string h) =>
{
h = h.Replace("x", string.Empty);
long result = 0;
long.TryParse(h, System.Globalization.NumberStyles.HexNumber, null, out result);
return (int)result;
};
Func <string, int[]> rgbHexToIntArray = (string hexColor) =>
{
hexColor = hexColor.Replace("#", string.Empty);
if ( hexColor.StartsWith("0x") ) {
hexColor = hexColor.Substring(2);
}
int hexColorLenghth = hexColor.Length;
if (hexColorLenghth != 8 && hexColorLenghth != 6) {
return new int[] {0,0,0};
}
int r,g,b;
if (hexColorLenghth == 8) {
r = hexdec(hexColor.Substring(2, 2));
g = hexdec(hexColor.Substring(4, 2));
b = hexdec(hexColor.Substring(6, 2));
if ((r == 0 && g == 0 && b == 0) || (r == 255 && g == 255 && b == 255)) {
return new int[] {0,0,0};
}
return new int[]{r, g, b};
}
r = hexdec(hexColor.Substring(0, 2));
g = hexdec(hexColor.Substring(2, 2));
b = hexdec(hexColor.Substring(4, 2));
if ((r == 0 && g == 0 && b == 0) || (r == 255 && g == 255 && b == 255)) {
return new int[] {0,0,0};
}
return new int[]{r, g, b};
};
Func <int, int, int, int[]> rgbToHsl = (int r, int g, int b) =>
{
r = r / 255;
g = g / 255;
b = b / 255;
int max = Math.Max(r, Math.Max(g, b));
int min = Math.Min(r, Math.Min(g, b));
int l = (max + min) / 2;
int d = max - min;
int h = 0;
int s = 0;
if (d == 0) {
h = s = 0;
}
else {
s = d / (1 - Math.Abs(2 * l - 1));
if ( max == r ) {
h = 60 * (((g - b) / d) % 6);
if (b > g) {
h += 360;
}
}
else if (max == g) {
h = 60 * ((b - r) / d + 2);
}
else {
h = 60 * ((r - g) / d + 4);
}
}
return new int[] {h, s, l};
};
Func <int, int[], bool> sendRGBWSetColorHsv = (int zone, int[] hsvColor) =>
{
int milightColor = hslToMilightColor(hsvColor);
int brightness = (hsvColor[2] * 100) / 250;
//Console.WriteLine(" MiLight Color " + milightColor + " brightness " + brightness);
Pause(0.11);
sendRGBWBrightnessCommand (zone, brightness);
Pause(0.11);
return sendRGBWColorCommand (zone, milightColor); //$this->sendCommand(array(0x40, $milightColor));
};
Func <int, string, bool> sendRGBWSetColorHexString = (int zone, string color) =>
//public function rgbwSetColorHexString($color)
{
int[] rgb = rgbHexToIntArray(color);
int[] hsl = rgbToHsl(rgb[0], rgb[1], rgb[2]);
int milightColor = hslToMilightColor(hsl);
return sendRGBWColorCommand (zone, milightColor);
};
var milightModules = Modules.InDomain("HomeAutomation.MiLightRGBW");
When.ModuleParameterChanged( (module, property) => {
if (module.Instance.Domain == "HomeAutomation.MiLightRGBW" )
{
//Console.WriteLine("MiLight ************************ ModuleParameterChanged RGBW");
int lightnumber = Convert.ToInt32(module.Instance.Address);
switch (property.Name) {
case "Meter.Watts":
break;
case "Control.On":
sendRGBWOnCommand(lightnumber);
Program.RaiseEvent(module, "Status.Level", "1", "MiLight Light");
break;
case "Control.Off":
sendRGBWOffCommand(lightnumber);
Program.RaiseEvent(module, "Status.Level", "0", "MiLight Light");
break;
default:
break;
}
return false;
}
else if (module.Instance.Domain == "HomeAutomation.MiLightWhite" )
{
//Console.WriteLine("MiLight ************************ ModuleParameterChanged White");
return false;
}
return true;
});
When.WebServiceCallReceived("HomeAutomation.MiLight", ( args ) =>
{
//Console.WriteLine("MiLight ************************ WebServiceCallReceived");
string[] reqs = ((string)args).Split('/');
//string domain = reqs[0];
try
{
string lightnumber = reqs[1];
string command = reqs[2];
string parameter = "";
if (reqs.Length > 3) parameter = Uri.UnescapeDataString(reqs[3]);
//
var module = milightModules.WithAddress(lightnumber).Get();
//Console.WriteLine("module name " + module.Instance.Name);
//
//Console.WriteLine("MiLight ************************ Command "+ command);
switch(command)
{
// eg. http://<hg_address>/api/HomeAutomation.PhilipsHue/<light_number>/Control.On
case "Control.On":
sendRGBWOnCommand(Convert.ToInt32(lightnumber));
Program.RaiseEvent(module, "Status.Level", "1", "MiLight Light");
break;
// eg. http://<hg_address>/api/HomeAutomation.PhilipsHue/<light_number>/Control.Off
case "Control.Off":
sendRGBWOffCommand(Convert.ToInt32(lightnumber));
Program.RaiseEvent(module, "Status.Level", "0", "MiLight Light");
break;
case "Control.Toggle":
if (module.Parameter("Status.Level").DecimalValue == 0)
{
sendRGBWOnCommand(Convert.ToInt32(lightnumber));
Program.RaiseEvent(module, "Status.Level", "1", "MiLight Light");
}
else
{
sendRGBWOffCommand(Convert.ToInt32(lightnumber));
Program.RaiseEvent(module, "Status.Level", "0", "MiLight Light");
}
break;
case "Control.Level":
int brightness = (int)(double.Parse(parameter));
sendRGBWBrightnessCommand (Convert.ToInt32(lightnumber), brightness);
//bridgeapicall(lightnumber, "{ \"bri\" : " + ((int)(double.Parse(parameter) * 2.5)).ToString() + " }");
//Console.WriteLine("MiLight ************************ brightness "+ brightness);
Program.RaiseEvent(module, "Status.Level", brightness.ToString(), "MiLight Light");
break;
// eg. http://<hg_address>/api/HomeAutomation.PhilipsHue/<light_number>/Control.ColorHsb/<hue>,<sat>,<bri>
case "Control.ColorHsb":
string[] values = parameter.Split(',');
double transition = 5;
//if (values.Length > 3) transition = ((int)(double.Parse(values[3]) * 10));
int[] hsbColor = new int[] { (int)(double.Parse(values[0]) * 255), (int)(double.Parse(values[1]) * 255), (int)(double.Parse(values[2]) * 255) };
//Console.WriteLine(" Color HSB " + hsbColor[0] + " : " + hsbColor[1] + " : " + hsbColor[2] );
sendRGBWSetColorHsv(Convert.ToInt32(lightnumber), hsbColor);
double levelHsb = hsbColor[2] / 250.0;
Program.RaiseEvent(module, "Status.Level", levelHsb.ToString(), "MiLight Light");
Program.RaiseEvent(module, "Status.ColorHsb", parameter, "MiLight Light");
break;
}
//
return "{ 'ResponseValue' : 'OK' }";
} catch (Exception e) {
Program.Notify("MiLight ERROR!", e.Message);
}
return "{ 'ResponseValue' : 'ERROR' }";
});
while (Program.IsEnabled)
{
string addr = Program.InputField(MILIGHT_IP_ADDR).Value;
string port = Program.InputField(MILIGHT_PORT).Value;
if (!UdpClient.IsConnected)
{
if (!String.IsNullOrWhiteSpace(addr) && !addr.EndsWith("?") && !String.IsNullOrWhiteSpace(port))
{
UdpClient.Sender(addr, Convert.ToInt32(port));
}
else
{
Program.Notify("MiLight", "Please configure a valid address and port.");
}
}
Pause(5);
}
[]
true
checkbox
HomeAutomation.MiLightRGBW
Dimmer,Light
MiLight.LevelFix
Set level to 100% when switched on
true
HomeAutomation.HomeGenie.Automation
1000
MiLight Control
MiLight and EasyBulb Lights
Devices and Things
2014-12-04T12:29:06.012339Z
2014-12-04T12:29:06.03226Z
CSharp
true