internal Color GetColor(string switchFriendlyName) { if (switchFriendlyNames.ContainsKey(switchFriendlyName)) { VPort switchPort = switchFriendlyNames[switchFriendlyName]; if (registeredSwitches.ContainsKey(switchPort)) { SwitchInfo switchInfo = registeredSwitches[switchPort]; if (!switchInfo.IsColored) { throw new Exception("GetColor called on non-color switch " + switchFriendlyName); } return(switchInfo.Color); } else { throw new Exception("Switch with friendly name " + switchFriendlyName + " is not registered"); } } else { throw new Exception("Switch with friendly name " + switchFriendlyName + " not found"); } }
internal void SetColor(string switchFriendlyName, Color color) { if (switchFriendlyNames.ContainsKey(switchFriendlyName)) { VPort switchPort = switchFriendlyNames[switchFriendlyName]; if (registeredSwitches.ContainsKey(switchPort)) { SwitchInfo switchInfo = registeredSwitches[switchPort]; if (!switchInfo.IsColored) { throw new Exception("SetColor called on non-color switch " + switchFriendlyName); } IList <VParamType> args = new List <VParamType>(); var retVal = Invoke(switchPort, RoleLightColor.Instance, RoleLightColor.OpSetName, new ParamType(color.R), new ParamType(color.G), new ParamType(color.B)); if (retVal != null && retVal.Count == 1 && retVal[0].Maintype() == (int)ParamType.SimpleType.error) { logger.Log("Error in setting color: {0}", retVal[0].Value().ToString()); throw new Exception(retVal[0].Value().ToString()); } lock (this) { this.lastSet = DateTime.Now; } switchInfo.Color = color; } else { throw new Exception("Switch with friendly name " + switchFriendlyName + " is not registered"); } } else { throw new Exception("Switch with friendly name " + switchFriendlyName + " not found"); } }
//returns a 8-tuple for each switch: (name, location, type, level, isColored, red, green, blue) internal List <string> GetAllSwitches() { List <string> retList = new List <string>(); foreach (string friendlyName in switchFriendlyNames.Keys) { VPort switchPort = switchFriendlyNames[friendlyName]; SwitchInfo switchInfo = registeredSwitches[switchPort]; retList.Add(friendlyName); retList.Add(switchPort.GetInfo().GetLocation().ToString()); retList.Add(switchInfo.Type.ToString()); retList.Add(switchInfo.Level.ToString()); retList.Add(switchInfo.IsColored.ToString()); retList.Add(switchInfo.Color.R.ToString()); retList.Add(switchInfo.Color.G.ToString()); retList.Add(switchInfo.Color.B.ToString()); } return(retList); }
void InitSwitch(VPort switchPort, SwitchType switchType, bool isColored) { logger.Log("{0} adding switch {1} {2}", this.ToString(), switchType.ToString(), switchPort.ToString()); SwitchInfo switchInfo = new SwitchInfo(); switchInfo.Capability = GetCapability(switchPort, Constants.UserSystem); switchInfo.Level = 0; switchInfo.Type = switchType; switchInfo.IsColored = isColored; switchInfo.Color = Color.Black; registeredSwitches.Add(switchPort, switchInfo); string switchFriendlyName = switchPort.GetInfo().GetFriendlyName(); switchFriendlyNames.Add(switchFriendlyName, switchPort); if (switchInfo.Capability != null) { IList<VParamType> retVals; if (switchType == SwitchType.Multi) { retVals = switchPort.Invoke(RoleSwitchMultiLevel.RoleName, RoleSwitchMultiLevel.OpGetName, null, ControlPort, switchInfo.Capability, ControlPortCapability); switchPort.Subscribe(RoleSwitchMultiLevel.RoleName, RoleSwitchMultiLevel.OpGetName, ControlPort, switchInfo.Capability, ControlPortCapability); if (retVals[0].Maintype() < 0) { logger.Log("SwitchController could not get current level for {0}", switchFriendlyName); } else { switchInfo.Level = (double)retVals[0].Value(); } } else { retVals = switchPort.Invoke(RoleSwitchBinary.RoleName, RoleSwitchBinary.OpGetName, null, ControlPort, switchInfo.Capability, ControlPortCapability); switchPort.Subscribe(RoleSwitchBinary.RoleName, RoleSwitchBinary.OpGetName, ControlPort, switchInfo.Capability, ControlPortCapability); if (retVals[0].Maintype() < 0) { logger.Log("SwitchController could not get current level for {0}", switchFriendlyName); } else { bool boolLevel = (bool)retVals[0].Value(); switchInfo.Level = (boolLevel) ? 1 : 0; } } //fix the color up now if (isColored) { var retValsColor = switchPort.Invoke(RoleLightColor.RoleName, RoleLightColor.OpGetName, null, ControlPort, switchInfo.Capability, ControlPortCapability); switchPort.Subscribe(RoleLightColor.RoleName, RoleLightColor.OpGetName, ControlPort, switchInfo.Capability, ControlPortCapability); if (retVals[0].Maintype() < 0) { logger.Log("SwitchController could not get color for {0}", switchFriendlyName); } else { byte red, green, blue; red = Math.Min(Math.Max((byte) (int) retValsColor[0].Value(), (byte) 0), (byte) 255); green = Math.Min(Math.Max((byte) (int) retValsColor[1].Value(), (byte) 0), (byte)255); blue = Math.Min(Math.Max((byte) (int) retValsColor[2].Value(), (byte) 0), (byte)255); switchInfo.Color = Color.FromArgb(red, green, blue); } } } }
internal void SetLevel(string switchFriendlyName, double level) { if (switchFriendlyNames.ContainsKey(switchFriendlyName)) { VPort switchPort = switchFriendlyNames[switchFriendlyName]; if (registeredSwitches.ContainsKey(switchPort)) { SwitchInfo switchInfo = registeredSwitches[switchPort]; IList <VParamType> args = new List <VParamType>(); //make sure that the level is between zero and 1 if (level < 0) { level = 0; } if (level > 1) { level = 1; } if (switchInfo.Type == SwitchType.Multi) { var retVal = Invoke(switchPort, RoleSwitchMultiLevel.Instance, RoleSwitchMultiLevel.OpSetName, new ParamType(level)); if (retVal != null && retVal.Count == 1 && retVal[0].Maintype() == (int)ParamType.SimpleType.error) { logger.Log("Error in setting level: {0}", retVal[0].Value().ToString()); throw new Exception(retVal[0].Value().ToString()); } } else { //interpret all non-zero values as ON bool boolLevel = (level > 0)? true : false; var retVal = Invoke(switchPort, RoleSwitchBinary.Instance, RoleSwitchBinary.OpSetName, new ParamType(boolLevel)); if (retVal != null && retVal.Count == 1 && retVal[0].Maintype() == (int)ParamType.SimpleType.error) { logger.Log("Error in setting level: {0}", retVal[0].Value().ToString()); throw new Exception(retVal[0].Value().ToString()); } } lock (this) { this.lastSet = DateTime.Now; } switchInfo.Level = level; } } else { throw new Exception("Switch with friendly name " + switchFriendlyName + " not found"); } }
void InitSwitch(VPort switchPort, SwitchType switchType, bool isColored) { logger.Log("{0} adding switch {1} {2}", this.ToString(), switchType.ToString(), switchPort.ToString()); SwitchInfo switchInfo = new SwitchInfo(); switchInfo.Capability = GetCapability(switchPort, Constants.UserSystem); switchInfo.Level = 0; switchInfo.Type = switchType; switchInfo.IsColored = isColored; switchInfo.Color = Color.Black; registeredSwitches.Add(switchPort, switchInfo); string switchFriendlyName = switchPort.GetInfo().GetFriendlyName(); switchFriendlyNames.Add(switchFriendlyName, switchPort); if (switchInfo.Capability != null) { IList <VParamType> retVals; if (switchType == SwitchType.Multi) { retVals = switchPort.Invoke(RoleSwitchMultiLevel.RoleName, RoleSwitchMultiLevel.OpGetName, null, ControlPort, switchInfo.Capability, ControlPortCapability); switchPort.Subscribe(RoleSwitchMultiLevel.RoleName, RoleSwitchMultiLevel.OpGetName, ControlPort, switchInfo.Capability, ControlPortCapability); if (retVals[0].Maintype() < 0) { logger.Log("SwitchController could not get current level for {0}", switchFriendlyName); } else { switchInfo.Level = (double)retVals[0].Value(); } } else { retVals = switchPort.Invoke(RoleSwitchBinary.RoleName, RoleSwitchBinary.OpGetName, null, ControlPort, switchInfo.Capability, ControlPortCapability); switchPort.Subscribe(RoleSwitchBinary.RoleName, RoleSwitchBinary.OpGetName, ControlPort, switchInfo.Capability, ControlPortCapability); if (retVals[0].Maintype() < 0) { logger.Log("SwitchController could not get current level for {0}", switchFriendlyName); } else { bool boolLevel = (bool)retVals[0].Value(); switchInfo.Level = (boolLevel) ? 1 : 0; } } //fix the color up now if (isColored) { var retValsColor = switchPort.Invoke(RoleLightColor.RoleName, RoleLightColor.OpGetName, null, ControlPort, switchInfo.Capability, ControlPortCapability); switchPort.Subscribe(RoleLightColor.RoleName, RoleLightColor.OpGetName, ControlPort, switchInfo.Capability, ControlPortCapability); if (retVals[0].Maintype() < 0) { logger.Log("SwitchController could not get color for {0}", switchFriendlyName); } else { byte red, green, blue; red = Math.Min(Math.Max((byte)(int)retValsColor[0].Value(), (byte)0), (byte)255); green = Math.Min(Math.Max((byte)(int)retValsColor[1].Value(), (byte)0), (byte)255); blue = Math.Min(Math.Max((byte)(int)retValsColor[2].Value(), (byte)0), (byte)255); switchInfo.Color = Color.FromArgb(red, green, blue); } } } }