public static string HouseUnitCodeFromEnum(X10HouseCode housecode, X10UnitCode unitcodes) { string unit = unitcodes.ToString(); unit = unit.Substring(unit.LastIndexOf("_") + 1); // return housecode.ToString() + unit; }
public static string HouseUnitCodeFromEnum(X10HouseCode housecode, X10UnitCode unitcodes) { string unit = unitcodes.ToString(); unit = unit.Substring(unit.LastIndexOf("_") + 1); // return(housecode.ToString() + unit); }
public static int Value(this X10UnitCode uc) { var parts = uc.ToString().Split('_'); var unitCode = 0; int.TryParse(parts[1], out unitCode); return(unitCode); }
public X10Module(XTenManager x10manager, string code) { x10 = x10manager; Code = code; houseCode = Utility.HouseCodeFromString(code); unitCode = Utility.UnitCodeFromString(code); Level = 0.0; Description = ""; }
public X10Module(X10Main x10c, string code) { x10 = x10c; Code = code; houseCode = Utility.HouseCodeFromString(code); unitCode = Utility.UnitCodeFromString(code); Level = 0.0; Description = ""; }
public void StatusRequest(X10HouseCode housecode, X10UnitCode unitcode) { lock (commandLock) { //string hcunit = String.Format("{0:X}{1:X}", (int)housecode, (int)unitcode); string hcfuntion = String.Format("{0:x1}{1:x1}", (int)housecode, (int)X10Command.Status_Request); SendModuleAddress(housecode, unitcode); SendMessage(new byte[] { (int)X10CommandType.Function, byte.Parse( hcfuntion, System.Globalization.NumberStyles.HexNumber ) }); } }
public void Bright(X10HouseCode housecode, X10UnitCode unitcode, int percentage) { lock (commandLock) { string huc = Utility.HouseUnitCodeFromEnum(housecode, unitcode); //string hcunit = String.Format("{0:X}{1:X}", (int)housecode, (int)unitcode); string hcfuntion = String.Format("{0:x1}{1:x1}", (int)housecode, (int)X10Command.Bright); // SendModuleAddress(housecode, unitcode); if (x10interface.GetType().Equals(typeof(CM15))) { double normalized = ((double)percentage / 100D); SendMessage(new byte[] { (int)X10CommandType.Function, byte.Parse( hcfuntion, System.Globalization.NumberStyles.HexNumber ), (byte)(normalized * 210) }); double newLevel = moduleStatus[huc].Level + normalized; if (newLevel > 1) { newLevel = 1; } moduleStatus[huc].Level = newLevel; } else { byte dimvalue = Utility.GetDimValue(percentage); SendMessage(new byte[] { (byte)((int)X10CommandType.Function | dimvalue | 0x04), byte.Parse( hcfuntion, System.Globalization.NumberStyles.HexNumber ) }); double newLevel = moduleStatus[huc].Level + Utility.GetPercentageValue(dimvalue); if (newLevel > 1) { newLevel = 1; } moduleStatus[huc].Level = newLevel; } } }
private void SendModuleAddress(X10HouseCode housecode, X10UnitCode unitcode) { // TODO: do more tests about this optimization //if (!addressedModules.Contains(mod) || addressedModules.Count > 1) // optimization disabled, uncomment to enable { UnselectModules(); SelectModule(Utility.HouseUnitCodeFromEnum(housecode, unitcode)); string hcUnit = String.Format("{0:X}{1:X}", (int)housecode, (int)unitcode); SendMessage(new byte[] { (int)X10CommandType.Address, byte.Parse( hcUnit, System.Globalization.NumberStyles.HexNumber ) }); newAddressData = true; } }
public void UnitOff(X10HouseCode housecode, X10UnitCode unitcode) { lock (commandLock) { //string hcunit = String.Format("{0:X}{1:X}", (int)housecode, (int)unitcode); string hcfuntion = String.Format("{0:x1}{1:x1}", (int)housecode, (int)X10Command.Off); SendModuleAddress(housecode, unitcode); SendMessage(new byte[] { (int)X10CommandType.Function, byte.Parse( hcfuntion, System.Globalization.NumberStyles.HexNumber ) }); // string huc = Utility.HouseUnitCodeFromEnum(housecode, unitcode); moduleStatus[huc].Level = 0.0; } }
/// <summary> /// Initializes a new instance of the <see cref="XTenLib.RfCommandReceivedEventArgs"/> class. /// </summary> /// <param name="function">Function.</param> /// <param name="housecode">Housecode.</param> /// <param name="unitcode">Unitcode.</param> public RfCommandReceivedEventArgs(X10RfFunction function, X10HouseCode housecode, X10UnitCode unitcode) { Command = function; HouseCode = housecode; UnitCode = unitcode; }
/// <summary> /// Turn on the specified module (housecode, unitcode). /// </summary> /// <param name="housecode">Housecode.</param> /// <param name="unitcode">Unitcode.</param> public void UnitOn(X10HouseCode housecode, X10UnitCode unitcode) { lock (commandLock) { //string hcunit = String.Format("{0:X}{1:X}", (int)housecode, (int)unitcode); string hcfuntion = String.Format("{0:x1}{1:x1}", (int)housecode, (int)X10Command.On); SendModuleAddress(housecode, unitcode); SendMessage(new byte[] { (int)X10CommandType.Function, byte.Parse(hcfuntion, System.Globalization.NumberStyles.HexNumber) }); string huc = Utility.HouseUnitCodeFromEnum(housecode, unitcode); if (modules[huc].Level == 0.0) { modules[huc].Level = 1.0; } } }
/// <summary> /// Request module status. /// </summary> /// <param name="housecode">Housecode.</param> /// <param name="unitcode">Unitcode.</param> public void StatusRequest(X10HouseCode housecode, X10UnitCode unitcode) { lock (commandLock) { //string hcunit = String.Format("{0:X}{1:X}", (int)housecode, (int)unitcode); string hcfuntion = String.Format("{0:x1}{1:x1}", (int)housecode, (int)X10Command.Status_Request); SendModuleAddress(housecode, unitcode); SendMessage(new byte[] { (int)X10CommandType.Function, byte.Parse(hcfuntion, System.Globalization.NumberStyles.HexNumber) }); } }
/// <summary> /// Dim the specified module (housecode, unitcode) by the specified percentage. /// </summary> /// <param name="housecode">Housecode.</param> /// <param name="unitcode">Unitcode.</param> /// <param name="percentage">Percentage.</param> public void Dim(X10HouseCode housecode, X10UnitCode unitcode, int percentage) { lock (commandLock) { string huc = Utility.HouseUnitCodeFromEnum(housecode, unitcode); string hcfuntion = String.Format("{0:x1}{1:x1}", (int)housecode, (int)X10Command.Dim); SendModuleAddress(housecode, unitcode); if (x10interface.GetType().Equals(typeof(CM15))) { double normalized = ((double)percentage / 100D); SendMessage(new byte[] { (int)X10CommandType.Function, byte.Parse(hcfuntion, System.Globalization.NumberStyles.HexNumber), (byte)(normalized * 210) }); double newLevel = modules[huc].Level - normalized; if (newLevel < 0) newLevel = 0; modules[huc].Level = newLevel; } else { byte dimvalue = Utility.GetDimValue(percentage); SendMessage(new byte[] { (byte)((int)X10CommandType.Function | dimvalue | 0x04), byte.Parse(hcfuntion, System.Globalization.NumberStyles.HexNumber) }); double newLevel = modules[huc].Level - Utility.GetPercentageValue(dimvalue); if (newLevel < 0) newLevel = 0; modules[huc].Level = newLevel; } } }
private void SendModuleAddress(X10HouseCode housecode, X10UnitCode unitcode) { // TODO: do more tests about this optimization (and comment out the "if" if tests are succesfully) //if (!addressedModules.Contains(mod) || addressedModules.Count > 1) // optimization disabled, uncomment to enable { UnselectModules(); SelectModule(Utility.HouseUnitCodeFromEnum(housecode, unitcode)); string hcUnit = String.Format("{0:X}{1:X}", (int)housecode, (int)unitcode); SendMessage(new byte[] { (int)X10CommandType.Address, byte.Parse(hcUnit, System.Globalization.NumberStyles.HexNumber) }); newAddressData = true; } }
/// <summary> /// Initializes a new instance of the <see cref="XTenLib.PlcAddressReceivedEventArgs"/> class. /// </summary> /// <param name="hc">Hc.</param> /// <param name="uc">Uc.</param> public PlcAddressReceivedEventArgs(X10HouseCode hc, X10UnitCode uc) { HouseCode = hc; UnitCode = uc; }
private void ReaderThreadLoop() { while (true) { try { byte[] readData = x10interface.ReadData(); if (readData.Length > 0) { Utility.DebugLog( "X10 >", Utility.ByteArrayToString(readData) ); // var elapsedFromWaitAck = DateTime.Now - waitAckTimestamp; if (elapsedFromWaitAck.TotalSeconds >= commandTimeoutSeconds && communicationState != X10CommState.Ready) { Utility.DebugLog( "X10 >", "COMMAND TIMEOUT" ); communicationState = X10CommState.Ready; } // if (communicationState == X10CommState.WaitingAck && readData[0] == (int)X10CommandType.PLC_Ready && readData.Length <= 2) // ack received { Utility.DebugLog( "X10 >", "COMMAND SUCCESSFUL" ); communicationState = X10CommState.Ready; } else if ((readData.Length >= 13 || (readData.Length == 2 && readData[0] == 0xFF && readData[1] == 0x00)) && !isInterfaceReady) { UpdateInterfaceTime(false); isInterfaceReady = true; communicationState = X10CommState.Ready; } else if (readData.Length == 2 && communicationState == X10CommState.WaitingChecksum && readData[0] == expectedChecksum && readData[1] == 0x00) { // checksum is received only from CM11 Utility.DebugLog( "X10 >", "CKSUM: " + "Expected [" + Utility.ByteArrayToString(new byte[] { expectedChecksum }) + "] Checksum ==> " + Utility.ByteArrayToString(readData) ); //TODO: checksum verification not handled, we just reply 0x00 (OK) SendMessage(new byte[] { 0x00 }); communicationState = X10CommState.WaitingAck; } else if (readData[0] == (int)X10CommandType.Macro) { lastReceivedTs = DateTime.Now; Utility.DebugLog("X10 >", "MACRO: " + Utility.ByteArrayToString(readData)); } else if (readData[0] == (int)X10CommandType.RF) { lastReceivedTs = DateTime.Now; string message = Utility.ByteArrayToString(readData); Utility.DebugLog("X10 >", "RFCOM: " + message); // repeated messages check if (lastRfMessage == message && (lastReceivedTs - lastRfReceivedTs).TotalMilliseconds < 200) { Utility.DebugLog("X10 >", "RFCOM: ^^^^^^^^^^^^^^^^^ Ignoring repeated message within 200ms"); continue; } lastRfMessage = message; lastRfReceivedTs = lastReceivedTs; // if (RfDataReceived != null) { Thread signal = new Thread(() => { try { RfDataReceived(new RfDataReceivedAction() { RawData = readData }); } catch { // TODO: handle/report exception } }); signal.Start(); } // Decode X10 RF Module Command (eg. "5D 20 70 8F 48 B7") if (readData.Length == 6 && readData[1] == 0x20 && ((readData[3] & ~readData[2]) == readData[3] && (readData[5] & ~readData[4]) == readData[5])) { byte hu = readData[2]; // house code + 4th bit of unit code byte hf = readData[4]; // unit code (3 bits) + function code string houseCode = ((X10HouseCode)(Utility.ReverseByte((byte)(hu >> 4)) >> 4)).ToString(); switch (hf) { case 0x98: // DIM ONE STEP CommandEvent_Dim(0x0F); break; case 0x88: // BRIGHT ONE STEP CommandEvent_Bright(0x0F); break; case 0x90: // ALL LIGHTS ON if (houseCode != "") { CommandEvent_AllLightsOn(houseCode); } break; case 0x80: // ALL LIGHTS OFF if (houseCode != "") { CommandEvent_AllUnitsOff(houseCode); } break; default: string houseUnit = Convert.ToString(hu, 2).PadLeft(8, '0'); string unitFunction = Convert.ToString(hf, 2).PadLeft(8, '0'); string unitCode = (Convert.ToInt16(houseUnit.Substring(5, 1) + unitFunction.Substring(1, 1) + unitFunction.Substring(4, 1) + unitFunction.Substring(3, 1), 2) + 1).ToString(); // UnselectModules(); SelectModule(houseCode + unitCode); // if (unitFunction[2] == '1') // 1 = OFF, 0 = ON { CommandEvent_Off(); } else { CommandEvent_On(); } break; } } } else if ((readData[0] == (int)X10CommandType.PLC_Poll) && readData.Length <= 2) { isInterfaceReady = true; SendMessage(new byte[] { (byte)X10CommandType.PLC_ReplyToPoll }); // reply to poll } else if ((readData[0] == (int)X10CommandType.PLC_FilterFail_Poll) && readData.Length <= 2) { isInterfaceReady = true; SendMessage(new byte[] { (int)X10CommandType.PLC_FilterFail_Poll }); // reply to filter fail poll } else if ((readData[0] == (int)X10CommandType.PLC_Poll)) { lastReceivedTs = DateTime.Now; Utility.DebugLog("X10 >", "PLCRX: " + Utility.ByteArrayToString(readData)); // if (readData.Length > 3) { int messageLength = readData[1]; if (readData.Length > messageLength - 2) { char[] bitmapData = Convert.ToString(readData[2], 2).PadLeft(8, '0').ToCharArray(); byte[] functionBitmap = new byte[messageLength - 1]; for (int i = 0; i < functionBitmap.Length; i++) { functionBitmap[i] = byte.Parse(bitmapData[7 - i].ToString()); } byte[] messageData = new byte[messageLength - 1]; Array.Copy(readData, 3, messageData, 0, messageLength - 1); // // CM15 Extended receive has got inverted data if (messageLength > 2 && x10interface.GetType().Equals(typeof(CM15))) { Array.Reverse(functionBitmap, 0, functionBitmap.Length); Array.Reverse(messageData, 0, messageData.Length); } // Utility.DebugLog("X10 >", "FNMAP: " + Utility.ByteArrayToString(functionBitmap)); Utility.DebugLog("X10 >", " DATA: " + Utility.ByteArrayToString(messageData)); for (int b = 0; b < messageData.Length; b++) { // read current byte data (type: 0x00 address, 0x01 function) if (functionBitmap[b] == (byte)X10FunctionType.Address) // address { X10HouseCode houseCode = (X10HouseCode)Convert.ToInt16(messageData[b].ToString("X2").Substring(0, 1), 16); X10UnitCode unitCode = (X10UnitCode)Convert.ToInt16(messageData[b].ToString("X2").Substring(1, 1), 16); string address = Utility.HouseUnitCodeFromEnum(houseCode, unitCode); // Utility.DebugLog("X10 >", " " + b + ") Address = " + address); // if (newAddressData) { newAddressData = false; UnselectModules(); } SelectModule(address); } else if (functionBitmap[b] == (byte)X10FunctionType.Function) // function { string function = ((X10Command)Convert.ToInt16(messageData[b].ToString("X2").Substring(1, 1), 16)).ToString().ToUpper(); string houseCode = ((X10HouseCode)Convert.ToInt16(messageData[b].ToString("X2").Substring(0, 1), 16)).ToString(); // Utility.DebugLog("X10 >", " " + b + ") House code = " + houseCode); Utility.DebugLog("X10 >", " " + b + ") Command = " + function); // switch (function) { case "ALL_UNITS_OFF": if (houseCode != "") { CommandEvent_AllUnitsOff(houseCode); } break; case "ALL_LIGHTS_ON": if (houseCode != "") { CommandEvent_AllLightsOn(houseCode); } break; case "ON": CommandEvent_On(); break; case "OFF": CommandEvent_Off(); break; case "BRIGHT": CommandEvent_Bright(messageData[++b]); break; case "DIM": CommandEvent_Dim(messageData[++b]); break; } // newAddressData = true; } } } } } else if ((readData[0] == (int)X10CommandType.PLC_TimeRequest)) // IS THIS A TIME REQUEST? { UpdateInterfaceTime(false); } else { #region This is an hack for detecting disconnection status on Linux platforms if (readData[0] == 0x00) { zeroChecksumCount++; } else { zeroChecksumCount = 0; } // if (zeroChecksumCount > 10) { zeroChecksumCount = 0; gotReadWriteError = true; Close(); } else { SendMessage(new byte[] { 0x00 }); } #endregion } } } catch (Exception e) { if (!e.GetType().Equals(typeof(TimeoutException)) && !e.GetType().Equals(typeof(OverflowException))) { gotReadWriteError = true; Utility.DebugLog("X10 !", e.Message); Utility.DebugLog("X10 !", e.StackTrace); } } } }
/// <summary> /// Initializes a new instance of the <see cref="W800Rf32Lib.RfCommandReceivedEventArgs"/> class. /// </summary> /// <param name="function">Function.</param> /// <param name="housecode">Housecode.</param> /// <param name="unitcode">Unitcode.</param> public RfCommandReceivedEventArgs(X10RfFunction function, X10HouseCode housecode, X10UnitCode unitcode) { Command = function; HouseCode = housecode; UnitCode = unitcode; }