Ejemplo n.º 1
0
        static SendMessageResult SendCommand(InsteonBridgeStream stream, CommandMessage msg, int timeout, out byte[] data)
        {
            SendMessageResult r = WriteMessage(stream, msg.GetBytes, timeout);

            if (r != OK)
            {
                data = null;
                return(r);
            }
            return(ReadResponse(stream, msg.GetResponseHeaderBytes, timeout, CommandResponse.Length, out data));
        }
Ejemplo n.º 2
0
        static SendMessageResult ReadResponseHeader(InsteonBridgeStream stream, byte[] bytes, int timeout)
        {
            byte[] rx = stream.ReadBytes(bytes.Length, timeout);

            if (rx?.Length == 0)
            {
                return(ResponseNull);
            }
            if (rx[0] == Message.NAK)
            {
                return(ResponseNak);
            }
            if (!Enumerable.SequenceEqual(bytes, rx))
            {
                return(ResponseInvalid);
            }

            return(OK);
        }
Ejemplo n.º 3
0
        static R SendCommand <R>(InsteonBridgeStream stream, CommandMessage msg, InsteonControllerOptions opt) where R : CommandResponse, new()
        {
            int delay           = opt.RetryStartDelayMs;
            int timeout         = opt.TimeoutStartMs;
            int retry           = 0;
            SendMessageResult r = OK;

            while (retry++ < opt.MaxRetries)
            {
                if ((r = SendCommand(stream, msg, timeout, out byte[] data)) == OK)
                {
                    return(msg.CreateResponse <R>(data));
                }
                Thread.Sleep(delay);
                timeout += opt.TimeoutStepMs;
                delay   += opt.RetryStepMs;
            }
            return(msg.CreateResponse <R>(r));
        }
Ejemplo n.º 4
0
        static SendMessageResult WriteMessage(InsteonBridgeStream stream, byte[] msg, int timeout)
        {
            if (!stream.DiscardExisting() || !stream.Wri­teBytes(msg))
            {
                return(WriteException);
            }

            SendMessageResult r = ReadResponse(stream, msg, timeout, 1, out byte[] ack);

            if (r != OK)
            {
                return(r);
            }
            if (ack?[0] != Message.ACK)
            {
                return(EchoInvalid);
            }

            return(OK);
        }
Ejemplo n.º 5
0
        static SendMessageResult ReadResponse(InsteonBridgeStream stream, byte[] msg, int timeout, int responseLen, out byte[] response)
        {
            SendMessageResult r = ReadResponseHeader(stream, msg, timeout);

            if (r != OK)
            {
                response = null;
                return(r);
            }

            response = stream.ReadBytes(responseLen, timeout);

            if (response == null)
            {
                return(ResponseNull);
            }
            if (response.Length < responseLen)
            {
                return(ResponseInvalid);
            }

            return(OK);
        }
Ejemplo n.º 6
0
 public InsteonController(InsteonBridgeStream stream, InsteonId?address = null, string name = null)
     : base(address?.Value ?? InsteonId.Zero, null, name)
 {
     Stream = stream;
 }