/// <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); }
/// <summary> /// Set the state of a group. /// </summary> /// <param name="id"></param> /// <param name="groupstate"></param> /// <returns></returns> public List <int> SetGroupState(string id, Action groupstate) { MessageCollection mc = _bridge.SetGroupAction(id, groupstate); List <int> errorcodes = new List <int>(); if (mc != null) { if (mc.FailureCount > 0) { errorcodes.AddRange(mc.OfType <Error>().Select(m => (m).type)); } } return(errorcodes); }
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) { if (bridge.DeleteGroup(v) == true) { WriteMessageToConsole(string.Format("Group {0} deleted succesfully.", v)); } 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) { if (bridge.DeleteSchedule(v) == true) { WriteMessageToConsole(string.Format("Schedule {0} deleted succesfully.", v)); } 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) { if (bridge.DeleteLight(v) == true) { WriteMessageToConsole(string.Format("Light {0} deleted succesfully.", v)); } 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) { if (bridge.DeleteSensor(v) == true) { WriteMessageToConsole(string.Format("Sensor {0} deleted succesfully.", v)); } 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) { if (bridge.DeleteSensor(v) == true) { WriteMessageToConsole(string.Format("Rule {0} deleted succesfully.", v)); } else { WriteMessageToConsole("Error while deleting rule :" + v); } } } else { error = true; } } }, { "ll", "List the lights available in the bridge", delegate(string v) { Dictionary <string, Light> listLights = bridge.GetLightList(); if (listLights != null) { foreach (KeyValuePair <string, Light> kvp in listLights) { WriteMessageToConsole(string.Format("[ID]={0}, Name={1}, ModelID={2}, Type={3}", kvp.Key, kvp.Value.name, kvp.Value.modelid, kvp.Value.type)); } } else { WriteMessageToConsole("An error occured while fetching the list of lights."); } } }, { "lg", "List the groups available in the bridge", delegate(string v) { Dictionary <string, Group> listgroups = bridge.GetGroupList(); if (listgroups != null) { foreach (KeyValuePair <string, Group> kvp in listgroups) { WriteMessageToConsole(string.Format("[ID]={0}, Name={1}, MembersID=[{2}], Type={3}", kvp.Key, kvp.Value.name, string.Join(",", kvp.Value.lights), kvp.Value.type)); } } else { WriteMessageToConsole("An error occured while fetching the list of groups."); } } }, { "ls", "List the scenes available in the bridge", delegate(string v) { Dictionary <string, Scene> listscenes = bridge.GetScenesList(); if (listscenes != null) { foreach (KeyValuePair <string, Scene> kvp in listscenes) { WriteMessageToConsole(string.Format("[ID]={0}, Name={1}, MembersID=[{2}]", kvp.Key, kvp.Value.name, string.Join(",", kvp.Value.lights))); } } else { WriteMessageToConsole("An error occured while fetching the list of scenes."); } } }, { "lc", "List the schedules available in the bridge", delegate(string v) { Dictionary <string, Schedule> listscenes = bridge.GetScheduleList(); if (listscenes != null) { foreach (KeyValuePair <string, Schedule> kvp in listscenes) { WriteMessageToConsole(string.Format("[ID]={0}, Name={1}, Time={2}", kvp.Key, kvp.Value.name, kvp.Value.localtime)); } } else { WriteMessageToConsole("An error occured while fetching the list of schedules."); } } }, { "lo", "List the sensors available in the bridge", delegate(string v) { Dictionary <string, Sensor> listsensors = bridge.GetSensorList(); if (listsensors != null) { foreach (KeyValuePair <string, Sensor> kvp in listsensors) { WriteMessageToConsole(string.Format("[ID]={0}, Name={1}, Type={2}, Model={3}, SwVersion={4}, Manufacturer={5}", kvp.Key, kvp.Value.name, kvp.Value.type, kvp.Value.modelid, kvp.Value.swversion, kvp.Value.manufacturername)); } } else { WriteMessageToConsole("An error occured while fetching the list of sensors."); } } }, { "lr", "List the rules available in the bridge", delegate(string v) { Dictionary <string, Rule> listrules = bridge.GetRulesList(); if (listrules != null) { foreach (KeyValuePair <string, Rule> kvp in listrules) { WriteMessageToConsole(string.Format("[ID]={0}, Name={1}, Owner={2}, Status={3}, TimesTriggered={4}", kvp.Key, kvp.Value.name, kvp.Value.owner, kvp.Value.status, kvp.Value.timestriggered)); } } else { WriteMessageToConsole("An error occured while fetching the list of rules."); } } }, { "lightstate=", "Get the state of a light", delegate(string v) { byte value = 0; bool isValidid = byte.TryParse(v, out value); if (isValidid && v != "0") { Light light = bridge.GetLight(v); WriteMessageToConsole(@light?.state.ToString() ?? bridge.lastMessages.ToString()); } 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) { Group group = bridge.GetGroup(v); WriteMessageToConsole(@group?.action.ToString() ?? bridge.lastMessages.ToString()); } 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) { Light light = bridge.GetLight(id); if (light.state.@on != null && (bool)light.state.@on) { state.on = false; } else { state.on = true; } } 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) { Group group = bridge.GetGroup(id); if (@group.action.@on != null && (bool)@group.action.@on) { action.on = false; } else { action.on = true; } } 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) { ClipTemperatureSensorState ts = new ClipTemperatureSensorState(); 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) { HueLib_base.Action a = new HueLib_base.Action(); a.on = true; a.sat = setState.sat; a.hue = setState.hue; a.bri = setState.bri; bridge.SetGroupAction(gid.ToString(), a); } lock (this) lightUpdatesSent++; } } }