Exemplo n.º 1
0
        public byte[] CreateCommand(X10Command command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            byte[] buffer = new byte[1];

            if (typeof(X10AddressCommand) == command.GetType())
            {
                X10AddressCommand addressCommand = command as X10AddressCommand;

                buffer = new byte[2];
                buffer[0] = CreateHeader(false, false, true, 0);
                buffer[1] = CreateAddressCode(addressCommand.House, addressCommand.Device);
            }
            else if (typeof(X10FunctionCommand) == command.GetType())
            {
                X10FunctionCommand functionCommand = command as X10FunctionCommand;

                buffer = new byte[2];
                buffer[0] = CreateHeader(false, true, true, functionCommand.Value);
                buffer[1] = CreateFunctionCode(functionCommand.House, functionCommand.Function);
            }
            else if (typeof(X10ExtendedCommand) == command.GetType())
            {
                buffer = CreateExtendedCommand((X10ExtendedCommand)command);
            }

            return buffer;
        }
Exemplo n.º 2
0
        private void testReceiveX10Command(string houseCode, X10Command command, byte x10Code)
        {
            using (var scenario = new SerialPortScenario())
            {
                scenario
                .IncomingMessage(0x02, 0x52, x10Code, 0x80);

                var plm  = buildPlmForTest(scenario.Playback());
                var test = plm.Network.X10;

                int        eventCount     = 0;
                object     sender         = null;
                string     eventHouseCode = null;
                X10Command eventCommand   = 0;
                test.CommandReceived += new X10CommandReceivedHandler((s, e) =>
                {
                    sender         = s;
                    eventHouseCode = e.HouseCode;
                    eventCommand   = e.Command;
                    eventCount++;
                });

                plm.Receive();
                Assert.AreEqual(1, eventCount);
                Assert.AreEqual(test, sender);
                Assert.AreEqual(houseCode, eventHouseCode);
                Assert.AreEqual(command, eventCommand);
            }
        }
Exemplo n.º 3
0
        ///////////////////////////////////////////////////////////////////////
        protected override void SendCommand(X10Command cmd)
        {
            uint payload = GetBits(cmd);

            _logger.Debug("Send({0}) => {2} [0x{1:X4}]", cmd, payload, _port.PortName);

            Transmit(payload, kCmdDataLen);
        }
Exemplo n.º 4
0
 ///////////////////////////////////////////////////////////////////////
 public static char GetX10CommandCodeValue(X10Command.Command cmd)
 {
     switch (cmd) {
         case X10Command.Command.ALF: return '6';
         case X10Command.Command.ALO: return '1';
         case X10Command.Command.AUF: return '0';
         case X10Command.Command.BRI: return '5';
         case X10Command.Command.DIM: return '4';
         case X10Command.Command.OFF: return '3';
         case X10Command.Command.ON: return '2';
     }
     throw new ArgumentException("Invalid Command Code");
 }
Exemplo n.º 5
0
 ///////////////////////////////////////////////////////////////////////
 public static char GetX10CommandCodeValue(X10Command.Command cmd)
 {
     switch (cmd) {
         case X10Command.Command.ALF: return 'M';
         case X10Command.Command.ALO: return 'C';
         case X10Command.Command.AUF: return 'A';
         case X10Command.Command.BRI: return 'K';
         case X10Command.Command.DIM: return 'I';
         case X10Command.Command.OFF: return 'G';
         case X10Command.Command.ON:  return 'E';
     }
     throw new ArgumentException("Invalid Command Code");
 }
Exemplo n.º 6
0
            public HouseContext Command(X10Command command)
            {
                delaySendingIfNecessary();
                byte x10Code = (byte)(
                    Constants.X10HouseCodeLookup[this.houseCode] |
                    (byte)command);

                this.plm.exceptionHandler(() =>
                {
                    this.plm.sendCommandWithEchoAndAck(0x02, 0x63, x10Code, 0x80);
                    updateLastSendTime();
                });
                return(this);
            }
Exemplo n.º 7
0
        ///////////////////////////////////////////////////////////////////////
        private static ushort GetActionBits(X10Command.Command action)
        {
            switch (action) {
                case X10Command.Command.ON: return 0x0000;
                case X10Command.Command.OFF: return 0x0020;
                case X10Command.Command.DIM: return 0x0098;
                case X10Command.Command.BRI: return 0x0088;
                case X10Command.Command.ALO: return 0x0094;
                case X10Command.Command.ALF: return 0x0084;
                case X10Command.Command.AUF: return 0x0080;
                case X10Command.Command.AUO: return 0x0091;
            }

            throw new ArgumentException("Invalid Action Code");
        }
Exemplo n.º 8
0
        private void testX10Command(string houseCode, X10Command command, byte x10Code)
        {
            using (var scenario = new SerialPortScenario())
            {
                scenario
                .ShouldSend(0x02, 0x63, x10Code, 0x80)
                .AndReceive(0x02, 0x63, x10Code, 0x80, Constants.ACK);

                var plm = buildPlmForTest(scenario.Playback());

                plm.Network.X10
                .House(houseCode)
                .Command(command);
            }
        }
Exemplo n.º 9
0
        ///////////////////////////////////////////////////////////////////////
        private void TestParse(char house, uint device, X10Command.Command action)
        {
            String x10 = null;

            if (device == 0) {
                x10 = String.Format("{0}_{1}", house, action);
            } else {
                x10 = String.Format("{0}{1}_{2}", house, device, action);
            }

            X10Command cmd = X10Command.Parse(x10);

            Assert.AreEqual(house, cmd.House);
            Assert.AreEqual(device, cmd.Device);
            Assert.AreEqual(action, cmd.Action);
        }
Exemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="XTenLib.PlcFunctionReceivedEventArgs"/> class.
 /// </summary>
 /// <param name="cmd">Cmd.</param>
 /// <param name="hc">Hc.</param>
 public PlcFunctionReceivedEventArgs(X10Command cmd, X10HouseCode hc)
 {
     Command = cmd;
     HouseCode = hc;
 }
Exemplo n.º 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="XTenLib.PlcFunctionReceivedEventArgs"/> class.
 /// </summary>
 /// <param name="cmd">Cmd.</param>
 /// <param name="hc">Hc.</param>
 public PlcFunctionReceivedEventArgs(X10Command cmd, X10HouseCode hc)
 {
     Command   = cmd;
     HouseCode = hc;
 }
 public HouseContext Command(X10Command command)
 {
     delaySendingIfNecessary();
     byte x10Code = (byte)(
         Constants.X10HouseCodeLookup[this.houseCode] |
         (byte)command);
     this.plm.exceptionHandler(() =>
     {
         this.plm.sendCommandWithEchoAndAck(0x02, 0x63, x10Code, 0x80);
         updateLastSendTime();
     });
     return this;
 }
        private void testReceiveX10Command(string houseCode, X10Command command, byte x10Code)
        {
            using (var scenario = new SerialPortScenario())
            {
                scenario
                    .IncomingMessage(0x02, 0x52, x10Code, 0x80);

                var plm = buildPlmForTest(scenario.Playback());
                var test = plm.Network.X10;

                int eventCount = 0;
                object sender = null;
                string eventHouseCode = null;
                X10Command eventCommand = 0;
                test.CommandReceived += new X10CommandReceivedHandler((s, e) =>
                {
                    sender = s;
                    eventHouseCode = e.HouseCode;
                    eventCommand = e.Command;
                    eventCount++;
                });

                plm.Receive();
                Assert.AreEqual(1, eventCount);
                Assert.AreEqual(test, sender);
                Assert.AreEqual(houseCode, eventHouseCode);
                Assert.AreEqual(command, eventCommand);
            }
        }
        private void testX10Command(string houseCode, X10Command command, byte x10Code)
        {
            using (var scenario = new SerialPortScenario())
            {
                scenario
                    .ShouldSend(0x02, 0x63, x10Code, 0x80)
                    .AndReceive(0x02, 0x63, x10Code, 0x80, Constants.ACK);

                var plm = buildPlmForTest(scenario.Playback());

                plm.Network.X10
                    .House(houseCode)
                    .Command(command);
            }
        }
Exemplo n.º 15
0
        ///////////////////////////////////////////////////////////////////////
        public void Send(X10Command cmd)
        {
            var str = new StringBuilder('c');  // 0x63

            char house = GetX10HouseCodeValue(cmd.House);
            str.Append(house);

            if (cmd.Device == 0) {
                char func = GetX10CommandCodeValue(cmd.Action);
                str.Append(func).Append('\0');
            } else {
                char unit = GetX10DeviceCodeValue(cmd.Device);
                char func = GetX10CommandCodeValue(cmd.Action);
                str.Append(unit).Append(func);
            }

            _logger.Debug("Send({0}) => {2} [{1}]", cmd, str, _port.PortName);

            Transmit(str.ToString());
        }
Exemplo n.º 16
0
        public bool SendCommand(X10Command command)
        {
            byte[] buffer = this.CreateCommand(command);

            int numberOfRetries = 0;
            bool transmittedSuccessfully = false;
            bool success = false;

            // wait for the interface to send a polling
            //////while (true)
            ////{
            ////    //if (_serialPort.ReadByte() == 0x5a)
            ////    {
            ////        //  break;
            ////    }
            ////}

            // send acknowledgement to stop the interface polling
            this.Send(0xc3);

            do
            {
                this.Send(buffer);

                byte response = (byte)this.serialPort.ReadByte();

                if (response == CreateChecksum(buffer))
                {
                    // send acknowledgement
                    transmittedSuccessfully = true;

                    this.DoCommandSent(EventArgs.Empty);

                    this.Send(0x00);

                    success = true;
                }
                else
                {
                    // invalid checksum, retransmit
                    numberOfRetries++;
                }
            }
            while (numberOfRetries <= 1 && !transmittedSuccessfully);

            this.isReady = false;

            // wait for the controller to be ready before sending the next command
            while (!this.IsReady)
            {
            }

            return success;
        }
Exemplo n.º 17
0
 public override string ToString()
 {
     return("Devices:" + mDevices0 + " Command:" + X10Command.ToString() + " Brightness (scale 22):" + mBrightness.ToString() + " Data1:" + Data1.ToString() + " Data2:" + Data2.ToString());
 }
Exemplo n.º 18
0
        ///////////////////////////////////////////////////////////////////////
        public void Send(X10Command cmd)
        {
            var str = new StringBuilder("3?0263");

            char house = GetX10HouseCodeValue(cmd.House);
            str.Append(house);

            if (cmd.Device != 0) {
                char unit = GetX10DeviceCodeValue(cmd.Device);
                str.Append(unit).Append("00P10263").Append(house);
            }

            char code = GetX10CommandCodeValue(cmd.Action);
            str.Append(code).Append("80P1=I=3");

            Transmit(str.ToString());
        }
Exemplo n.º 19
0
        ///////////////////////////////////////////////////////////////////
        private static ushort GetBits(X10Command cmd)
        {
            ushort houseBits = GetHouseBits(cmd.House);
            ushort deviceBits = GetDeviceBits(cmd.Device);
            ushort actionBits = GetActionBits(cmd.Action);

            return (ushort) (houseBits | deviceBits | actionBits);
        }