/// <summary> /// Send the group Action (State) to the specified group. /// </summary> /// <param name="ID">ID of the group</param> /// <param name="action">The action of the group.</param> /// <returns>A list of MessageCollection from the bridge.</returns> public MessageCollection SetGroupAction(string ID, Action action) { CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/groups/" + ID + "/action"), WebRequestType.PUT, Serializer.SerializeToJson <Action>(action)); switch (comres.status) { case WebExceptionStatus.Success: List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data); if (lstmsg == null) { goto default; } else { lastMessages = new MessageCollection(lstmsg); } break; case WebExceptionStatus.Timeout: lastMessages = new MessageCollection { _bridgeNotResponding }; BridgeNotResponding?.Invoke(this, _e); break; default: lastMessages = new MessageCollection { new UnkownError(comres) }; break; } return(lastMessages); }
static ConsoleHandler() { noprompt = false; nomsg = false; cmd = Command.NONE; #region MAIN_OPTIONS mainOpts = new OptionSet() { { "noprompt", "Set this option if you want to ignore prompts. (This will assume yes for all questions)", delegate(string v){ noprompt = true; } }, { "nomsg", "Cancel the output of messages from the bridge.(The application will not output anything) ", delegate(string v){ nomsg = true; } }, }; #endregion #region OBJECT_OPTIONS objOpts = new OptionSet() { { "l|light=", "Set the mode to light.", delegate(string v) { if (cmd == Command.NONE) { cmd = Command.LIGHT; id = v; state = new State(); } else { error = true; } } }, { "g|group=", "Set the mode to group.", delegate(string v) { if (cmd == Command.NONE) { action = new Action(); id = v; cmd = Command.GROUP; } else { error = true; } } }, { "sc|schedule=", "Set the mode to schedule.", delegate(string v) { if (cmd == Command.NONE) { id = v; cmd = Command.SCHEDULE; } else { error = true; } } }, { "sn|scene=", "Set the mode to scene.", delegate(string v) { if (cmd == Command.NONE) { id = v; cmd = Command.SCENE; } else { error = true; } } }, { "ss|sensor=", "Set the mode to sensor.", delegate(string v) { if (cmd == Command.NONE) { id = v; cmd = Command.SENSOR; } else { error = true; } } }, { "cg|creategroup", "Create a new group.", delegate(string v) { grp = new Group(); if (cmd == Command.NONE) { cmd = Command.CREATEGROUP; } else { error = true; } } }, { "dg|deletegroup=", "Delete the selected group", delegate(string v) { if (cmd == Command.NONE) { cmd = Command.DELETEGROUP; if (PromptYesNo() || noprompt) { CommandResult bresult = bridge.RemoveObject <Group>(v); if (bresult.Success) { WriteMessageToConsole($"Group {v} deleted succesfully."); } else { WriteMessageToConsole("Error while deleting group :" + v); } } } else { error = true; } } }, { "dc|deleteschedule=", "Delete the selected schedule", delegate(string v) { if (cmd == Command.NONE) { if (PromptYesNo() || noprompt) { CommandResult bresult = bridge.RemoveObject <Schedule>(v); if (bresult.Success) { WriteMessageToConsole($"Schedule {v} deleted succesfully."); } else { WriteMessageToConsole("Error while deleting schedule :" + v); } } } else { error = true; } } }, { "dl|deletelight=", "Delete the selected light", delegate(string v) { if (cmd == Command.NONE) { if (PromptYesNo() || noprompt) { CommandResult bresult = bridge.RemoveObject <Light>(v); if (bresult.Success) { WriteMessageToConsole($"Light {v} deleted succesfully."); } else { WriteMessageToConsole("Error while deleting light :" + v); } } } else { error = true; } } }, { "do|deletesensor=", "Delete the selected sensor", delegate(string v) { if (cmd == Command.NONE) { if (PromptYesNo() || noprompt) { CommandResult bresult = bridge.RemoveObject <Sensor>(v); if (bresult.Success) { WriteMessageToConsole($"Sensor {v} deleted succesfully."); } else { WriteMessageToConsole("Error while deleting sensor :" + v); } } } else { error = true; } } }, { "dr|deleterule=", "Delete the selected rule", delegate(string v) { if (cmd == Command.NONE) { if (PromptYesNo() || noprompt) { CommandResult bresult = bridge.RemoveObject <HueLib2.Rule>(v); if (bresult.Success) { WriteMessageToConsole($"Rule {v} deleted succesfully."); } else { WriteMessageToConsole("Error while deleting rule :" + v); } } } else { error = true; } } }, { "ll", "List the lights available in the bridge", delegate(string v) { CommandResult bresult = bridge.GetListObjects <Light>(); if (bresult.Success) { Dictionary <string, Light> listLights = (Dictionary <string, Light>)bresult.resultobject; foreach (KeyValuePair <string, Light> kvp in listLights) { WriteMessageToConsole( $"[ID]={kvp.Key}, Name={kvp.Value.name}, ModelID={kvp.Value.modelid}, Type={kvp.Value.type}"); } } else { WriteMessageToConsole("An error occured while fetching the list of lights :" + bresult.resultobject); } } }, { "lg", "List the groups available in the bridge", delegate(string v) { CommandResult bresult = bridge.GetListObjects <Group>(); if (bresult.Success) { Dictionary <string, Group> listgroups = (Dictionary <string, Group>)bresult.resultobject; foreach (KeyValuePair <string, Group> kvp in listgroups) { WriteMessageToConsole( $"[ID]={kvp.Key}, Name={kvp.Value.name}, MembersID=[{string.Join(",", kvp.Value.lights)}], Type={kvp.Value.type}"); } } else { WriteMessageToConsole("An error occured while fetching the list of groups : " + bresult.resultobject); } } }, { "ls", "List the scenes available in the bridge", delegate(string v) { CommandResult bresult = bridge.GetListObjects <Scene>(); if (bresult.Success) { Dictionary <string, Scene> listscenes = (Dictionary <string, Scene>)bresult.resultobject; foreach (KeyValuePair <string, Scene> kvp in listscenes) { WriteMessageToConsole( $"[ID]={kvp.Key}, Name={kvp.Value.name}, MembersID=[{string.Join(",", kvp.Value.lights)}]"); } } else { WriteMessageToConsole("An error occured while fetching the list of scenes : " + bresult.resultobject); } } }, { "lc", "List the schedules available in the bridge", delegate(string v) { CommandResult bresult = bridge.GetListObjects <Schedule>(); if (bresult.Success) { Dictionary <string, Schedule> listscenes = (Dictionary <string, Schedule>)bresult.resultobject; foreach (KeyValuePair <string, Schedule> kvp in listscenes) { WriteMessageToConsole($"[ID]={kvp.Key}, Name={kvp.Value.name}, Time={kvp.Value.localtime}"); } } else { WriteMessageToConsole("An error occured while fetching the list of schedules : " + bresult.resultobject); } } }, { "lo", "List the sensors available in the bridge", delegate(string v) { CommandResult bresult = bridge.GetListObjects <Sensor>(); if (bresult.Success) { Dictionary <string, Sensor> listsensors = (Dictionary <string, Sensor>)bresult.resultobject; foreach (KeyValuePair <string, Sensor> kvp in listsensors) { WriteMessageToConsole( $"[ID]={kvp.Key}, Name={kvp.Value.name}, Type={kvp.Value.type}, Model={kvp.Value.modelid}, SwVersion={kvp.Value.swversion}, Manufacturer={kvp.Value.manufacturername}"); } } else { WriteMessageToConsole("An error occured while fetching the list of sensors : " + bresult.resultobject); } } }, { "lr", "List the rules available in the bridge", delegate(string v) { CommandResult bresult = bridge.GetListObjects <HueLib2.Rule>(); if (bresult.Success) { Dictionary <string, HueLib2.Rule> listrules = (Dictionary <string, HueLib2.Rule>)bresult.resultobject; foreach (KeyValuePair <string, HueLib2.Rule> kvp in listrules) { WriteMessageToConsole( $"[ID]={kvp.Key}, Name={kvp.Value.name}, Owner={kvp.Value.owner}, Status={kvp.Value.status}, TimesTriggered={kvp.Value.timestriggered}"); } } else { WriteMessageToConsole("An error occured while fetching the list of rules : " + bresult.resultobject); } } }, { "lightstate=", "Get the state of a light", delegate(string v) { byte value = 0; bool isValidid = byte.TryParse(v, out value); if (isValidid && v != "0") { CommandResult bresult = bridge.GetObject <Light>(v); if (bresult.Success) { Light light = (Light)bresult.resultobject; WriteMessageToConsole(@light.state.ToString() ?? bridge.lastMessages.ToString()); } else { WriteMessageToConsole("Error getting the light : " + bresult.resultobject); } } else { WriteMessageToConsole($"ERROR: The provided value {v} is not a valid id for the light. Please use a number between 1 and 255"); } } }, { "groupstate=", "Get the state of a group", delegate(string v) { byte value = 0; bool isValidid = byte.TryParse(v, out value); if (isValidid) { CommandResult bresult = bridge.GetObject <Group>(v); if (bresult.Success) { Group group = (Group)bresult.resultobject; WriteMessageToConsole(@group.action.ToString()); } else { WriteMessageToConsole("Error getting group : " + bresult.resultobject); } } else { WriteMessageToConsole($"ERROR: The provided value {v} is not a valid id for the light. Please use a number between 1 and 255"); } } }, }; #endregion #region LIGHT_OPTIONS lightOpts = new OptionSet() { { "bri=", "Brightness of a light [0-254]", delegate(string v) { byte bri; if (byte.TryParse(v, out bri)) { state.bri = bri; } else { WriteMessageToConsole("Error BRI invalid value " + v); error = true; } } }, { "sat=", "Saturation of a light [0-254]", delegate(string v) { byte sat; if (byte.TryParse(v, out sat)) { state.sat = sat; } else { WriteMessageToConsole("Error SAT invalid value " + v); error = true; } } }, { "ct=", "Colortemp of a light [153-500]", delegate(string v) { ushort ct; if (ushort.TryParse(v, out ct)) { state.ct = ct; } else { WriteMessageToConsole("Error CT invalid value " + v); error = true; } } }, { "hue=", "Hue of a light [0-65534]", delegate(string v) { ushort hue; if (ushort.TryParse(v, out hue)) { if (hue >= 0 && hue <= 65535) { state.hue = hue; } else { WriteMessageToConsole("Error HUE invalid value " + v); error = true; } } else { WriteMessageToConsole("Error HUE invalid value " + v); error = true; } } }, { "on:", "On state of a light (true or false)", delegate(string v) { if (v == null) { CommandResult bresult = bridge.GetObject <Light>(id); if (bresult.Success) { Light light = (Light)bresult.resultobject; if (light.state.@on != null && (bool)light.state.@on) { state.on = false; } else { state.on = true; } } else { WriteMessageToConsole("Error getting light " + bresult.resultobject); } } else { bool on; if (bool.TryParse(v, out on)) { state.on = on; } else { WriteMessageToConsole("Error ON invalid value " + v); error = true; } } } }, { "fx|effect=", "Effect of a light [none,colorloop]", delegate(string v) { if (v == "colorloop" || v == "none") { state.effect = v; } else { WriteMessageToConsole("Error EFFECT invalid value " + v); error = true; } } }, { "alert=", "Brightness of a light [none,select,lselect]", delegate(string v) { if (v == "none" || v == "select" || v == "lselect") { state.alert = v; } else { WriteMessageToConsole("ALERT invalid value " + v); error = true; } } }, { "x=", "x coordinate of a light [0.000-1.000]", delegate(string v) { decimal x; if (decimal.TryParse(v, out x)) { if (x >= 0 && x <= 1) { if (state.xy == null) { state.xy = new XY(); } state.xy = new XY { x = x }; } else { WriteMessageToConsole("Error X invalid value " + v); error = true; } } else { WriteMessageToConsole("Error X invalid value " + v); error = true; } } }, { "y=", "Y coordinate of a light [0.000-1.000]", delegate(string v) { decimal y; if (decimal.TryParse(v, out y)) { if (y >= 0 && y <= 1) { if (state.xy == null) { state.xy = new XY(); } state.xy.y = y; } else { WriteMessageToConsole("Error Y invalid value " + v); error = true; } } else { WriteMessageToConsole("Error Y invalid value " + v); error = true; } } }, { "tt|transitiontime=", "Transition time of the light", delegate(ushort v) { state.transitiontime = v; } }, }; #endregion #region GROUP_OPTIONS groupOpts = new OptionSet() { { "bri=", "Brightness of a group [0-254]", delegate(string v) { byte bri; if (byte.TryParse(v, out bri)) { action.bri = bri; } else { WriteMessageToConsole("BRI invalid value " + v); error = true; } } }, { "sat=", "Saturation of a group [0-254]", delegate(string v) { byte sat; if (byte.TryParse(v, out sat)) { action.sat = sat; } else { WriteMessageToConsole("SAT invalid value " + v); error = true; } } }, { "ct=", "Colortemp of a group [153-500]", delegate(string v) { ushort ct; if (ushort.TryParse(v, out ct)) { action.ct = ct; } else { WriteMessageToConsole("CT invalid value " + v); } } }, { "hue=", "Hue of a group [0-65534]", delegate(string v) { ushort hue; if (ushort.TryParse(v, out hue)) { if (hue <= 65535) { action.hue = hue; } else { WriteMessageToConsole("HUE invalid value " + v); error = true; } } else { WriteMessageToConsole("HUE invalid value " + v); error = true; } } }, { "on:", "On state of a group (true or false)", delegate(string v) { if (v == null) { CommandResult bresult = bridge.GetObject <Group>(id); if (bresult.Success) { Group group = (Group)bresult.resultobject; if (@group.action.@on != null && (bool)@group.action.@on) { action.on = false; } else { action.on = true; } } else { WriteMessageToConsole("Error getting group : " + bresult.resultobject); } } else { bool on; if (bool.TryParse(v, out on)) { action.on = on; } else { WriteMessageToConsole("ON invalid value " + v); error = true; } } } }, { "fx|effect=", "Effect of a group [none,colorloop]", delegate(string v) { if (v == "colorloop" || v == "none") { action.effect = v; } else { WriteMessageToConsole("EFFECT invalid value " + v); error = true; } } }, { "x=", "x coordinate of a group [0.000-1.000]", delegate(string v) { decimal x; if (decimal.TryParse(v, out x)) { if (x >= 0 && x <= 1) { if (action.xy == null) { action.xy = new XY(); } action.xy.x = x; } else { WriteMessageToConsole("X invalid value " + v); error = true; } } else { WriteMessageToConsole("X invalid value " + v); error = true; } } }, { "y=", "Y coordinate of a group [0.000-1.000]", delegate(string v) { decimal y; if (decimal.TryParse(v, out y)) { if (y >= 0 && y <= 1) { if (action.xy == null) { action.xy = new XY(); } action.xy.y = y; } else { WriteMessageToConsole("Y invalid value " + v); error = true; } } else { WriteMessageToConsole("Y invalid value " + v); error = true; } } }, { "tt|transitiontime=", "Transition time of the group", delegate(ushort v) { action.transitiontime = v; } }, }; #endregion #region CREATE_GROUP_OPTIONS createGrOpts = new OptionSet() { { "n|name=", "Name of the new group", delegate(string v) { grp.name = v; } }, { "members=", "ID of the lights in the group splitted by a comma. [eg:1,2,3]", delegate(string v) { grp.lights.AddRange(v.Split(',').ToArray()); } }, }; #endregion #region SENSOR sensorOpts = new OptionSet() { { "open=", "Open attribute of the sensor", delegate(string v) { ClipOpenCloseSensorState ss = new ClipOpenCloseSensorState(); bool open; if (bool.TryParse(v, out open)) { ss.open = open; sensorstate = ss; } else { WriteMessageToConsole($"open invalid value {v} expecting true or false."); } } }, { "flag=", "flag attribute of the sensor", delegate(string v) { ClipGenericFlagSensorState fs = new ClipGenericFlagSensorState(); bool flag; if (bool.TryParse(v, out flag)) { fs.flag = flag; sensorstate = fs; } else { WriteMessageToConsole($"flag invalid value {v} expecting true or false."); } } }, { "status=", "status attribute of the sensor", delegate(string v) { ClipGenericStatusState gs = new ClipGenericStatusState(); int status; if (int.TryParse(v, out status)) { gs.status = status; sensorstate = gs; } else { WriteMessageToConsole($"status invalid value {v} expecting integer"); } } }, { "humidity=", "humidity attribute of the sensor", delegate(string v) { ClipHumiditySensorState hs = new ClipHumiditySensorState(); int humidity; if (int.TryParse(v, out humidity)) { hs.humidity = humidity; sensorstate = hs; } else { WriteMessageToConsole($"humidity invalid value {v} expecting integer."); } } }, { "presence=", "Presence attribute of the sensor", delegate(string v) { ClipPresenceSensorState ps = new ClipPresenceSensorState(); bool presence; if (bool.TryParse(v, out presence)) { ps.presence = presence; sensorstate = ps; } else { WriteMessageToConsole($"presence invalid value {v} expecting true or false."); } } }, { "temperature=", "Temperature attribute of the sensor", delegate(string v) { TemperatureSensorState ts = new TemperatureSensorState(); int temperature; if (int.TryParse(v, out temperature)) { ts.temperature = temperature; sensorstate = ts; } else { WriteMessageToConsole($"temperature invalid value {v} expecting integer."); } } } }; #endregion }
protected void LightSetterProcess() { while (bRun) { System.Threading.Thread.Sleep(10); State setState; switch (Settings.scanMethod) { case ScanMethod.HueBoost: { /*methodb*/ ColorRGB huedColor = new ColorRGB(scanner.AvarageSaturatedColor); //ColorRGB totalColor = new ColorRGB(scanner.AvarageColor); float luma = scanner.AvarageLuma; setState = new State() { hue = (ushort)(huedColor.H * ushort.MaxValue), sat = (byte)(huedColor.S * 255), bri = (byte)(luma * 255), on = true, transitiontime = Settings.transitiontime }; break; } case ScanMethod.Avarage: { //total avarages ColorRGB c = new ColorRGB(scanner.AvarageColor); setState = new State() { hue = (ushort)(c.H * ushort.MaxValue), sat = (byte)(c.S * 255), bri = (byte)(c.L * 255), on = true, transitiontime = Settings.transitiontime }; break; } case ScanMethod.LumaOnly: setState = new State() { bri = (byte)(scanner.AvarageLuma * 255), on = true, transitiontime = Settings.transitiontime }; break; default: { throw new Exception("Unexpected scanmode in settings"); } } /*Do luma boost/reduction and limit min max light levels*/ setState.bri = (byte)Math.Max(Settings.minLight, Math.Min((float)setState.bri * Settings.luminuosity, Settings.maxLight)); /*Do sat boost/reduction*/ if (Settings.saturation != 1) { setState.sat = (byte)Math.Max(0, Math.Min((float)setState.sat * Settings.saturation, 255)); } /*Decide if change shall be send to the bridge*/ bool bSendUpdate = false; if (lastState == null) /*Send update if none sent yet*/ { bSendUpdate = true; } else if (DateTime.Now.Second % 5 == 0) /*Force send update every 5 seconds*/ { bSendUpdate = true; } else /*Decide if scene changes are big enough to send an update*/ { float delta = 0; delta += Math.Abs(((float)lastState.bri - (float)setState.bri) / Byte.MaxValue); delta += Math.Abs(((float)lastState.hue - (float)setState.hue) / ushort.MaxValue); delta += Math.Abs(((float)lastState.sat - (float)setState.sat) / Byte.MaxValue); delta /= 3; if (delta >= Settings.minDelta) { bSendUpdate = true; } } if (bSendUpdate) { lastState = setState; foreach (int lid in Settings.lights) { bridge.SetLightState(lid.ToString(), setState); } foreach (int gid in Settings.groups) { HueLib2.Action a = new HueLib2.Action(); a.on = true; a.sat = setState.sat; a.hue = setState.hue; a.bri = setState.bri; bridge.SetGroupAction(gid.ToString(), a); } lock (this) lightUpdatesSent++; } } }