Exemple #1
0
        public void Cancel()
        {
            if (remoteCancellationTokenSource != null)
            {
                remoteCancellationTokenSource.Cancel();
            }

            requestTask = BusinessLogic.CommunicationTasks.Nothing;
        }
Exemple #2
0
        public RemoteControl(BusinessLogic tmp)
        {
            remote           = new TCPListenerResource();
            remote.Delimiter = "\r\n";
            logic            = tmp;

            requestTask = BusinessLogic.CommunicationTasks.Nothing;

            viewSettingBuffer = new ViewSetting();

            requestTask = BusinessLogic.CommunicationTasks.Nothing;

            logTextDataBuff = new ConcurrentQueue <string>();
            logDataBuffSize = 500;

            DumpDataBuff = new ConcurrentQueue <byte>();

            requestTask = BusinessLogic.CommunicationTasks.Nothing;
        }
Exemple #3
0
        private string AnalyzeCommand(string message)
        {
            string failedText = "Failed";
            string okText     = "OK";
            string busyText   = "Busy";
            string emptyText  = "Empty";

            string command    = string.Empty;
            string parameters = string.Empty;
            string answer     = failedText;

            var splitIndex = message.IndexOf(' ');

            if (splitIndex != -1)
            {
                command    = message.Substring(0, splitIndex);
                parameters = message.Substring(splitIndex + 1, (message.Length - command.Length - 1));
            }
            else
            {
                command = message;
            }

            switch (command)
            {
            case CommandResource:
                var config = new Configuration();
                if (ValidateCommunicationResource(parameters, out config))
                {
                    if (logic.UpdateResource(config))
                    {
                        answer = okText;
                    }
                }

                break;

            case CommandOpen:
                answer = busyText;
                if (requestTask != BusinessLogic.CommunicationTasks.Open)
                {
                    requestTask = BusinessLogic.CommunicationTasks.Open;

                    if (!logic.IsCommAvailable)
                    {
                        logic.ClearWaitingTasks();
                        logic.EnqueueTask(BusinessLogic.CommunicationTasks.Open);
                        logic.CancelCurrentTask();
                    }
                }
                else
                {
                    if (logic.TaskState != BusinessLogic.CommunicationTasks.Open)
                    {
                        requestTask = BusinessLogic.CommunicationTasks.Nothing;
                        if (logic.IsCommAvailable)
                        {
                            answer = okText;
                        }
                        else
                        {
                            answer = failedText;
                        }
                    }
                }
                break;

            case CommandClose:
                if (requestTask == BusinessLogic.CommunicationTasks.Open)
                {
                    requestTask = BusinessLogic.CommunicationTasks.Nothing;
                }
                logic.ClearWaitingTasks();
                logic.EnqueueTask(BusinessLogic.CommunicationTasks.Close);
                logic.CancelCurrentTask();
                answer = okText;
                break;

            case CommandInitialize:
                logic.EnqueueTask(BusinessLogic.CommunicationTasks.Initialize);
                DeviceVersion = string.Empty;
                answer        = okText;
                break;

            case CommandGetVersion:
                if (string.IsNullOrEmpty(DeviceVersion))
                {
                    answer = busyText;
                }
                else
                {
                    answer = okText + " " + DeviceVersion;
                }
                break;

            case CommandRegister:
                if (AddDataSetting(parameters))
                {
                    answer = okText;
                }
                break;

            case CommandAdapt:
                RegisterLogConfigCallBack?.Invoke(viewSettingBuffer);
                viewSettingBuffer = new ViewSetting();
                answer            = okText;
                break;

            case CommandPage:
                if (logic.TaskState == BusinessLogic.CommunicationTasks.Logging)
                {
                    answer = busyText;
                }
                else
                {
                    int pageValue;
                    if (!int.TryParse(parameters, out pageValue))
                    {
                        answer = failedText;
                    }
                    else
                    {
                        if (ChangePageCallBack != null)
                        {
                            if (!ChangePageCallBack(pageValue))
                            {
                                answer = failedText;
                            }
                            else
                            {
                                ClearLogTextDataBuff();
                                answer = okText;
                            }
                        }
                    }
                }
                break;

            case CommandTimeStep:
                int timeStep;
                if (int.TryParse(parameters, out timeStep))
                {
                    if (timeStep < BusinessLogic.TimeStepMin)
                    {
                        timeStep = BusinessLogic.TimeStepMin;
                    }
                    else if (timeStep > BusinessLogic.TimeStepMax)
                    {
                        timeStep = BusinessLogic.TimeStepMax;
                    }

                    ChangeTimeStepCallBack?.Invoke(timeStep);

                    logic.LogTimeStep = (uint)timeStep;
                    answer            = okText;
                }

                break;

            case CommandLogStart:
                logic.EnqueueTask(BusinessLogic.CommunicationTasks.Config);
                logic.EnqueueTask(BusinessLogic.CommunicationTasks.Logging);
                ClearLogTextDataBuff();
                answer = okText;
                break;

            case CommandLogStop:
                if (logic.TaskState == BusinessLogic.CommunicationTasks.Logging)
                {
                    logic.ClearWaitingTasks();
                    logic.CancelCurrentTask();
                }

                answer = okText;

                break;

            case CommandLogGet:
                string data;
                if (!logTextDataBuff.TryDequeue(out data))
                {
                    if (logic.TaskState == BusinessLogic.CommunicationTasks.Logging)
                    {
                        answer = busyText;
                    }
                    else
                    {
                        answer = emptyText;
                    }
                }
                else
                {
                    answer = okText + " " + data;
                }

                break;

            case CommandLogWrite:
                if (logic.TaskState == BusinessLogic.CommunicationTasks.Logging)
                {
                    var wrParam = new BusinessLogic.DataParameter();
                    if (IsValidWriteSetting(parameters, out wrParam))
                    {
                        logic.EditValue(wrParam);
                        answer = okText;
                    }
                }

                break;

            case CommandLogSize:
                if (logic.TaskState == BusinessLogic.CommunicationTasks.Logging)
                {
                    answer = busyText;
                }
                else
                {
                    int size;
                    if (int.TryParse(parameters, out size))
                    {
                        if (size > 0)
                        {
                            ClearLogTextDataBuff();
                            logDataBuffSize = size;
                            answer          = okText;
                        }
                    }
                }

                break;

            case CommandDumpSet:
                if (requestTask == BusinessLogic.CommunicationTasks.Dump)
                {
                    answer = busyText;
                }
                else
                {
                    var param = new BusinessLogic.DataParameter();
                    if (ValidateDumpConfigrations(parameters, out param))
                    {
                        requestTask = BusinessLogic.CommunicationTasks.Dump;

                        logic.DumpConfigParameter = param;

                        logic.ClearWaitingTasks();
                        logic.EnqueueTask(BusinessLogic.CommunicationTasks.Dump);
                        logic.CancelCurrentTask();

                        answer = okText;
                    }
                }

                break;

            case CommandDumpGet:
                if (logic.TaskState == BusinessLogic.CommunicationTasks.Dump)
                {
                    answer = busyText;
                }
                else
                {
                    requestTask = BusinessLogic.CommunicationTasks.Nothing;

                    if (DumpDataBuff.Count <= 0)
                    {
                        answer = emptyText;
                    }
                    else
                    {
                        answer = okText + " ";
                        int count = 0;
                        while (count < 16)
                        {
                            byte abyte;
                            if (DumpDataBuff.TryDequeue(out abyte))
                            {
                                answer += abyte.ToString("X2");
                                count++;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
                break;

            case CommandBypassRequest:
                if (requestTask == BusinessLogic.CommunicationTasks.Bypass)
                {
                    answer = busyText;
                }
                else
                {
                    if (System.Text.RegularExpressions.Regex.IsMatch(parameters, @"\A\b[0-9a-fA-F]+\b\Z") &&
                        (parameters.Length % 2) == 0)
                    {
                        requestTask = BusinessLogic.CommunicationTasks.Bypass;

                        logic.BypassRequest.Enqueue(parameters);

                        logic.ClearWaitingTasks();
                        logic.EnqueueTask(BusinessLogic.CommunicationTasks.Bypass);
                        logic.CancelCurrentTask();

                        answer = okText;
                    }
                }
                break;

            case CommandBypassResponse:
                if (logic.TaskState == BusinessLogic.CommunicationTasks.Bypass)
                {
                    answer = busyText;
                }
                else
                {
                    requestTask = BusinessLogic.CommunicationTasks.Nothing;

                    string outputText;
                    if (logic.BypassResponse.TryDequeue(out outputText))
                    {
                        answer = okText + " " + outputText;
                    }
                    else
                    {
                        answer = emptyText;
                    }
                }

                break;

            default:
                break;
            }

            return(answer);
        }