Пример #1
0
        public override void Run(ICommunicator communicator)
        {
            listener.CommandStarted();

            byte[] packet = DuoProtocol.CommandPacket(command, value);

            string valueString = value + "";

            if (value is Array)
            {
                valueString = "";
                foreach (Object o in value as Array)
                {
                    valueString += " " + o;
                }
                if (valueString.Length > 0)
                {
                    valueString = valueString.Substring(1);
                }
            }

            Context.Logger.Fine("Setting '" + command + "' to '" + valueString + "': " + BitConverter.ToString(packet));

            string    commandId = null, response = null;
            ErrorCode errorCode = ErrorCode.NoError;

            for (int i = -1; i < timeoutRetries; i++)
            {
                errorCode = communicator.SendPacket(this, packet, out commandId, out response);
                if (errorCode == ErrorCode.Timeout && i < timeoutRetries - 1)
                {
                    Context.Logger.Warning("Timeout setting '" + command + "' to '" + valueString + "', retrying");
                }
                else
                {
                    break;
                }
            }

            errorCode = DuoProtocol.TranslateSetErrorCode(command, value, errorCode);

            if (errorCode == ErrorCode.NoError)
            {
                Context.Logger.Info("Set '" + command + "' to '" + valueString + "'");
                listener.CommandCompleted(value);

                Thread.Sleep(delayAfterCommandInMillis);
            }
            else
            {
                Context.Logger.Error("Error setting '" + command + "' to '" + valueString + "': " + errorCode);
                listener.CommandError(errorCode, value);
            }
        }
Пример #2
0
        public override void Run(ICommunicator communicator)
        {
            listener.CommandStarted();

            byte[] packet = DuoProtocol.QueryPacket(command);
            Context.Logger.Fine("Querying '" + command + "': " + BitConverter.ToString(packet));

            string    commandId = null, response = null;
            ErrorCode errorCode = ErrorCode.NoError;

            for (int i = -1; i < timeoutRetries; i++)
            {
                errorCode = communicator.SendPacket(this, packet, out commandId, out response);
                if (errorCode == ErrorCode.Timeout && i < timeoutRetries - 1)
                {
                    Context.Logger.Warning("Timeout querying '" + command + "', retrying");
                }
                else
                {
                    break;
                }
            }

            if (errorCode == ErrorCode.NoError && commandId != command.Id)
            {
                errorCode = ErrorCode.UnknownResponse;
            }

            string translated = DuoProtocol.TranslateQueryErrorCode(command, errorCode);

            if (translated != null)
            {
                response  = translated;
                errorCode = ErrorCode.NoError;
            }

            if (errorCode == ErrorCode.NoError)
            {
                T value = command.StringToValue(response);
                Context.Logger.Info("Query '" + command + "' result: " + value + " (" + response + ")");
                listener.CommandCompleted(value);
            }
            else
            {
                Context.Logger.Error("Error querying '" + command + "': " + errorCode);
                listener.CommandError(errorCode, this.value);
            }
        }
Пример #3
0
        public override ErrorCode SendPacket(ISerialCommand sourceComand, byte[] packet, out string commandId, out string response)
        {
            if (!IsConnected())
            {
                commandId = null;
                response  = null;
                return(ErrorCode.NotConnected);
            }

            port.DiscardOutBuffer();
            try
            {
                port.Write(packet, 0, packet.Length);
            }
            catch (Exception e)
            {
                Context.Logger.Error(e);
                commandId = null;
                response  = null;
                return(ErrorCode.UnknownError);
            }
            return(DuoProtocol.ReadResponse(port, out commandId, out response));
        }