Esempio n. 1
0
        public static void Main(string[] args)
        {
            // NOTE: To disable debug output uncomment the following two lines
            //LogManager.Configuration.LoggingRules.RemoveAt(0);
            //LogManager.Configuration.Reload();

            Console.WriteLine("XTenLib test program, waiting for connection.");
            var x10 = new XTenManager();

            // Listen to XTenManager events
            x10.ConnectionStatusChanged += X10_ConnectionStatusChanged;
            x10.ModuleChanged           += X10_ModuleChanged;
            x10.PlcAddressReceived      += X10_PlcAddressReceived;
            x10.PlcFunctionReceived     += X10_PlcFunctionReceived;
            // These RF events are only used for CM15
            x10.RfDataReceived     += X10_RfDataReceived;
            x10.RfCommandReceived  += X10_RfCommandReceived;
            x10.RfSecurityReceived += X10_RfSecurityReceived;
            // Setup X10 interface. For CM15 set PortName = "USB"; for CM11 use serial port path intead (eg. "COM7" or "/dev/ttyUSB0")
            x10.PortName  = "USB";
            x10.HouseCode = "A,C";
            // Connect to the interface
            x10.Connect();

            // Sends A1 ON / OFF via RF
            Thread.Sleep(1000);
            x10.SendMessage(new byte[] { 0xEB, 0x20, 0x60, 0x9F, 0x00, 0xFF });
            Thread.Sleep(1000);
            x10.SendMessage(new byte[] { 0xEB, 0x20, 0x60, 0x9F, 0x20, 0xDF });

            // Prevent the program from quitting with a noop loop
            while (true)
            {
                Thread.Sleep(1000);
            }
        }
Esempio n. 2
0
        public object InterfaceControl(MigInterfaceCommand request)
        {
            var response = new ResponseText("OK");

            string nodeId = lastAddressedModule = request.Address;
            string option = request.GetOption(0);

            Commands command;

            Enum.TryParse <Commands>(request.Command.Replace(".", "_"), out command);

            if (portName == Cm19LibDriverPort)
            {
                // Parse house/unit
                var houseCode = CM19Lib.Utility.HouseCodeFromString(nodeId);
                var unitCode  = CM19Lib.Utility.UnitCodeFromString(nodeId);
                var module    = GetSecurityModuleByAddress(nodeId, ModuleTypes.Switch);
                // module.CustomData is used to store the current level
                switch (command)
                {
                case Commands.Control_On:
                    cm19Lib.UnitOn(houseCode, unitCode);
                    module.CustomData = 1D;
                    UpdateModuleLevel(module);
                    break;

                case Commands.Control_Off:
                    cm19Lib.UnitOff(houseCode, unitCode);
                    module.CustomData = 0D;
                    UpdateModuleLevel(module);
                    break;

                case Commands.Control_Bright:
                    cm19Lib.Bright(houseCode);
                    module.CustomData = module.CustomData + (5 / 100D);
                    if (module.CustomData > 1)
                    {
                        module.CustomData = 1D;
                    }
                    UpdateModuleLevel(module);
                    break;

                case Commands.Control_Dim:
                    cm19Lib.Dim(houseCode);
                    module.CustomData = module.CustomData - (5 / 100D);
                    if (module.CustomData < 0)
                    {
                        module.CustomData = 0D;
                    }
                    UpdateModuleLevel(module);
                    break;

                case Commands.Control_Level:
                    int dimValue = (int.Parse(option) - (int)(module.CustomData * 100.0)) / 5;
                    if (dimValue > 0)
                    {
                        cm19Lib.Bright(houseCode);
                        for (int i = 0; i < (dimValue / Cm19Manager.SendRepeatCount); i++)
                        {
                            cm19Lib.Bright(houseCode);
                        }
                    }
                    else if (dimValue < 0)
                    {
                        cm19Lib.Dim(houseCode);
                        for (int i = 0; i < -(dimValue / Cm19Manager.SendRepeatCount); i++)
                        {
                            cm19Lib.Dim(houseCode);
                        }
                    }
                    module.CustomData = module.CustomData + (dimValue * 5 / 100D);
                    UpdateModuleLevel(module);
                    break;

                case Commands.Control_Toggle:
                    if (module.CustomData == 0D)
                    {
                        cm19Lib.UnitOn(houseCode, unitCode);
                        UpdateModuleLevel(module);
                    }
                    else
                    {
                        cm19Lib.UnitOff(houseCode, unitCode);
                        UpdateModuleLevel(module);
                    }
                    break;

                case Commands.Control_AllLightsOn:
                    cm19Lib.AllLightsOn(houseCode);
                    // TODO: update modules status
                    break;

                case Commands.Control_AllUnitsOff:
                    cm19Lib.AllUnitsOff(houseCode);
                    // TODO: update modules status
                    break;

                case Commands.Control_RfSend:
                    byte[] data = CM19Lib.Utility.StringToByteArray(option.Replace(" ", ""));
                    cm19Lib.SendMessage(data);
                    break;
                }
            }
            else
            {
                // Parse house/unit
                var houseCode = XTenLib.Utility.HouseCodeFromString(nodeId);
                var unitCode  = XTenLib.Utility.UnitCodeFromString(nodeId);
                switch (command)
                {
                case Commands.Parameter_Status:
                    x10Lib.StatusRequest(houseCode, unitCode);
                    break;

                case Commands.Control_On:
                    x10Lib.UnitOn(houseCode, unitCode);
                    break;

                case Commands.Control_Off:
                    x10Lib.UnitOff(houseCode, unitCode);
                    break;

                case Commands.Control_Bright:
                    x10Lib.Bright(houseCode, unitCode, int.Parse(option));
                    break;

                case Commands.Control_Dim:
                    x10Lib.Dim(houseCode, unitCode, int.Parse(option));
                    break;

                case Commands.Control_Level_Adjust:
                    //int adjvalue = int.Parse(option);
                    //x10Lib.Modules[nodeId].Level = ((double)adjvalue/100D);
                    OnInterfacePropertyChanged(this.GetDomain(), nodeId, "X10 Module", ModuleEvents.Status_Level, x10Lib.Modules[nodeId].Level);
                    throw(new NotImplementedException("X10 CONTROL_LEVEL_ADJUST Not Implemented"));
                    break;

                case Commands.Control_Level:
                    int dimvalue = int.Parse(option) - (int)(x10Lib.Modules[nodeId].Level * 100.0);
                    if (dimvalue > 0)
                    {
                        x10Lib.Bright(houseCode, unitCode, dimvalue);
                    }
                    else if (dimvalue < 0)
                    {
                        x10Lib.Dim(houseCode, unitCode, -dimvalue);
                    }
                    break;

                case Commands.Control_Toggle:
                    string huc = XTenLib.Utility.HouseUnitCodeFromEnum(houseCode, unitCode);
                    if (x10Lib.Modules[huc].Level == 0)
                    {
                        x10Lib.UnitOn(houseCode, unitCode);
                    }
                    else
                    {
                        x10Lib.UnitOff(houseCode, unitCode);
                    }
                    break;

                case Commands.Control_AllLightsOn:
                    x10Lib.AllLightsOn(houseCode);
                    break;

                case Commands.Control_AllUnitsOff:
                    x10Lib.AllUnitsOff(houseCode);
                    break;

                case Commands.Control_RfSend:
                    byte[] data = CM19Lib.Utility.StringToByteArray("EB" + option.Replace(" ", ""));
                    // SendRepeatCount is not implemented in XTenLib, so a for loop in required here
                    for (int i = 0; i < Cm19Manager.SendRepeatCount; i++)
                    {
                        x10Lib.SendMessage(data);
                        Thread.Sleep(Cm19Manager.SendPauseMs);
                    }
                    break;
                }
            }

            return(response);
        }