/// <summary> /// Do CAL Management comms transaction, expecting local echo /// </summary> /// <param name="communicationChannel"></param> /// <param name="calCommand"></param> /// <returns></returns> bool DoCALCommsTransaction(Interface.Communication.ICommunicationChannel communicationChannel, ICBusCommand calCommand) { var rxBuffer = new byte[128]; var commandString = calCommand.ToCBusString(); Logger.DebugFormat("TX {0}:{1}", calCommand, commandString); CBus.Protocol.CBusProtcol.SendCommand(communicationChannel, calCommand); Thread.Sleep(200); var rxLen = communicationChannel.ReceiveBytes(rxBuffer, rxBuffer.Length); var rxDataString = GetReceivedString(rxBuffer, rxLen); if (rxDataString.StartsWith(commandString)) { Logger.DebugFormat("RX:{0}", rxDataString.Substring(commandString.Length)); return(true); } else { Logger.WarnFormat("RX:{0}", rxDataString); return(false); } }
public CBusLightingDevice(string Address, Tools.Interface.ILightingDefinition LightingDefinition, Interface.Communication.ICommunicationChannel CommunicationChannel, byte CBusLightControlApplication, byte CBusPresetApplication) : base(Address) { _ActiveApplications.Add((byte)CBusProtcol.CBusApplicationId.LIGHTING_DEFAULT); _ActiveApplications.Add((byte)CBusProtcol.CBusApplicationId.TRIGGER); _LightingDefinition = LightingDefinition; _CommunicationChannel = CommunicationChannel; if (!_ActiveApplications.Contains(CBusLightControlApplication)) { _ActiveApplications.Add(CBusLightControlApplication); } if (!_ActiveApplications.Contains(CBusPresetApplication)) { _ActiveApplications.Add(CBusPresetApplication); } _configBuilder = new CBusCALCommandBuilder(); _CBusAddressMap = new CBusApplicationAddressMap(); var presetMapTemp = new List <PresetMap>(); foreach (var item in LightingDefinition.PresetMapping) { byte applicationID = 0; byte groupAddress = 0; byte triggerGroupAddress = 0; bool overrideDefaultApplication = (item.Application.TryParseHexOrDecimal(out applicationID)); bool lightGroupSpecified = (item.PresetData.TryParseHexOrDecimal(out groupAddress)); bool triggerGroupSpecified = (item.PresetValue.TryParseHexOrDecimal(out triggerGroupAddress)); if ((!overrideDefaultApplication) || (applicationID == 0)) { if (lightGroupSpecified) { applicationID = (byte)CBusProtcol.CBusApplicationId.LIGHTING_DEFAULT; } else if (triggerGroupSpecified) { applicationID = (byte)CBusProtcol.CBusApplicationId.TRIGGER; } } if (applicationID != 0) { if (!_ActiveApplications.Contains(applicationID)) { _ActiveApplications.Add(applicationID); } } if (lightGroupSpecified || triggerGroupSpecified) { if (applicationID == ((byte)CBusProtcol.CBusApplicationId.TRIGGER)) { if (!triggerGroupSpecified) { triggerGroupAddress = groupAddress; } presetMapTemp.Add( new PresetMap( item.PresetName, item.Description, applicationID, triggerGroupAddress) ); _CBusAddressMap.AddMapping(CBusProtcol.ApplicationTypes.TRIGGER, applicationID); } else if ((applicationID >= ((byte)CBusProtcol.CBusApplicationId.LIGHTING_BASE) && (applicationID <= ((byte)CBusProtcol.CBusApplicationId.LIGHTING_MAX)))) { if (!lightGroupSpecified) { groupAddress = triggerGroupAddress; } presetMapTemp.Add( new PresetMap( item.PresetName, item.Description, applicationID, groupAddress) ); _CBusAddressMap.AddMapping(CBusProtcol.ApplicationTypes.LIGHTING, applicationID); } } } _presetMap = presetMapTemp; }
private bool PerformLogon(Interface.Communication.ICommunicationChannel communicationChannel) { var rxBuffer = new byte[128]; string rxString = string.Empty; var resetCount = 3; var resetSuccess = false; do { ICBusCommand calCommand = _configBuilder.ResetCommand(); Logger.DebugFormat("TX {0}:{1}", calCommand, calCommand.ToCBusString()); CBus.Protocol.CBusProtcol.SendCommand(communicationChannel, calCommand); CBus.Protocol.CBusProtcol.SendCommand(communicationChannel, calCommand); //Wait for response Thread.Sleep(200); var rxLen = communicationChannel.ReceiveBytes(rxBuffer, rxBuffer.Length); rxString = GetReceivedString(rxBuffer, rxLen); Logger.DebugFormat("RX:{0}", rxString); resetCount--; resetSuccess = rxString.Contains(CBus.Protocol.CBusProtcol.MODE_RESET_CHAR); } while (!resetSuccess && resetCount > 0); if (resetSuccess) { _ActiveApplications.Sort(); for (int i = 0; i < _ActiveApplications.Count(); i++) { var calCommand_App = _configBuilder.RegisterApplication1Monitor(_ActiveApplications[i]); if (!DoCALCommsTransaction(communicationChannel, calCommand_App)) { return(false); } } var calCommand = _configBuilder.Set_CAL_Options3(CBusProtcol.Interface_Options_3.LOCAL_SAL); if (!DoCALCommsTransaction(communicationChannel, calCommand)) { return(false); } //See 4.3.3.1 CAL Reply, to under stand response (Long Form response as SMART selected) //Tx:@A3300059 //Response: 86FAFA0032300024 calCommand = _configBuilder.Set_CAL_Options1( CBusProtcol.Interface_Options_1.CONNECT | CBusProtcol.Interface_Options_1.SRCHK | CBusProtcol.Interface_Options_1.SMART | CBusProtcol.Interface_Options_1.IDIOM ); if (!DoCALCommsTransaction(communicationChannel, calCommand)) { return(false); } return(true); } return(false); }
public CBusLightingDevice(string Address, Tools.Interface.ILightingDefinition LightingDefinition, Interface.Communication.ICommunicationChannel CommunicationChannel) : this( Address, LightingDefinition, CommunicationChannel, Convert.ToByte(CBus.Protocol.CBusProtcol.CBusApplicationId.LIGHTING_DEFAULT), Convert.ToByte(CBus.Protocol.CBusProtcol.CBusApplicationId.TRIGGER) ) { }