예제 #1
0
        public async Task Send(BaseDirective item, int reSendTimes = 0)
        {
            try
            {
                _waitForFeedbackDirectives.TryAdd(item.DirectiveId, new WaitForFeedBack(DateTime.Now, item, reSendTimes));

                var directiveData = _protocolProvider.GenerateDirectiveBuffer(item);
                //断线重连
                if (_serialPort.Status == SerialPortStatus.Initialled)
                {
                    await _serialPort.Open(SerialEnum.LowerComputer);
                }

                if (_waitForFeedbackDirectives.ContainsKey(item.DirectiveId) && _serialPort.Status == SerialPortStatus.Opened)
                {
                    _serialPort.Send(directiveData, _cancelTokenSource.Token);
                }
            }
            catch (CustomException)
            {
                LogFactory.Create().Info("send error");
            }
            catch (TaskCanceledException)
            {
                LogFactory.Create().Info("send cancel");
            }
            catch (Exception e)
            {
                OnErrorEvent(new CustomException(e.Message + "Send", this.GetType().FullName,
                                                 ExceptionPriority.Unrecoverable), item);
            }
        }
예제 #2
0
        public byte[] GenerateDirectiveBuffer(BaseDirective directive)
        {
            switch (directive.DirectiveType)
            {
            case DirectiveTypeEnum.Idle:
                return(GenerateDirectiveBuffer(directive as IdleDirective));

            case DirectiveTypeEnum.TryStart:
                return(GenerateDirectiveBuffer(directive as TryStartDirective));

            case DirectiveTypeEnum.TryPause:
                return(GenerateDirectiveBuffer(directive as TryPauseDirective));

            case DirectiveTypeEnum.Close:
                return(GenerateDirectiveBuffer(directive as CloseDirective));

            case DirectiveTypeEnum.Running:
                return(GenerateDirectiveBuffer(directive as RunningDirective));

            case DirectiveTypeEnum.Pausing:
                return(GenerateDirectiveBuffer(directive as PausingDirective));

            default:
                return(null);
            }
        }
예제 #3
0
        private void DeviceBase_ErrorEvent(CustomException ce, BaseDirective directive)
        {
            if (directive?.TargetDeviceId != DeviceId)
            {
                return;
            }

            OnCaptureCustomExceptionEvent(ce);
        }
예제 #4
0
 //指令排序:优先按照超时时间多少排列(比如大于3秒为超时,然后根据超过的时间量排序) 然后按照指令优先级排列
 public void PrepareDirective(BaseDirective item, int reSendTimes = 0)
 {
     lock (Locker)
     {
         if (_waitForSendDirectives.Count <= 100)
         {
             _waitForSendDirectives.Add(new WaitForSend(DateTime.Now, item, reSendTimes));
         }
     }
 }
 //指令排序:优先按照超时时间多少排列(比如大于3秒为超时,然后根据超过的时间量排序) 然后按照指令优先级排列
 public void PrepareDirective(BaseDirective item, int reSendTimes = 0)
 {
     lock (_locker)
     {
         _isSort = false;
         if (waitForSendDirectives.Count <= 100)
         {
             waitForSendDirectives.Enqueue(new WaitForSend(DateTime.Now, item, reSendTimes));
         }
     }
 }
예제 #6
0
 protected void SendDirective(BaseDirective directive, int span = 0)
 {
     loopTimer?.Dispose();
     if (span == 0)
     {
         DirectiveWorker.Instance.PrepareDirective(directive);
     }
     else
     {
         loopTimer = new Timer((obj) =>
         {
             DirectiveWorker.Instance.PrepareDirective(directive);
         }, null, span, -1);
     }
 }
        public async Task Send(BaseDirective item, int reSendTimes = 0)
        {
            try
            {
                waitForFeedbackDirectives.TryAdd(item.DirectiveId, new WaitForFeedBack(DateTime.Now, item, reSendTimes));

                var directiveData = protocolProvider.GenerateDirectiveBuffer(item);
                if (serialPort == null)
                {
                    serialPort = await SerialCreater.Instance.Create(SerialEnum.LowerComputer);

                    if (serialPort != null)
                    {
                        serialPort.ReceiveHandler += SpHelper_ReceiveHandler;
                    }
                    else
                    {
                        Debug.WriteLine("LowerComputer is null");
                    }
                }

                if (serialPort != null)
                {
                    await serialPort.Open();

                    if (waitForFeedbackDirectives.ContainsKey(item.DirectiveId))
                    {
                        await serialPort.Send(directiveData, cancelTokenSource.Token);
                    }
                }
            }
            catch (CustomException)
            {
                Debug.WriteLine("send error");
            }
            catch (TaskCanceledException)
            {
                Debug.WriteLine("send cancel");
            }
            catch (Exception e)
            {
                OnErrorEvent(new CustomException(e.Message + "Send", this.GetType().FullName,
                                                 ExceptionPriority.Unrecoverable), item);
            }
        }
예제 #8
0
        private static byte[] GetCommonBufferData(BaseDirective directive)
        {
            if (null == directive)
            {
                return(new byte[0]);
            }

            IList <byte> list = new List <byte>();

            list.Add((byte)directive.TargetDeviceId);
            list.Add((byte)directive.DirectiveType);
            list = list.Concat(DirectiveHelper.ParseNumberTo2Bytes(directive.DirectiveId)).ToList();
            list.Add((byte)directive.DeviceType);

            list = list.Concat(DirectiveHelper.GenerateCheckCode(list.ToArray())).ToList();

            return(list.ToArray());
        }
예제 #9
0
        public async Task Send(BaseDirective item, int reSendTimes = 0)
        {
            try
            {
                waitForFeedbackDirectives.TryAdd(item.DirectiveId, new WaitForFeedBack(DateTime.Now, item, reSendTimes));

                var directiveData = protocolProvider.GenerateDirectiveBuffer(item);
                //第一次加载
                if (serialPort == null)
                {
                    serialPort = await getSerialPort();
                }

                if (serialPort != null)
                {
                    // 运行过程中串口断开了
                    if (serialPort.serialPort == null)
                    {
                        serialPort.ReceiveHandler -= SpHelper_ReceiveHandler;
                        serialPort = await getSerialPort();
                    }

                    if (waitForFeedbackDirectives.ContainsKey(item.DirectiveId) && serialPort != null)
                    {
                        await serialPort.Send(directiveData, cancelTokenSource.Token);
                    }
                }
            }
            catch (CustomException e)
            {
                Debug.WriteLine("send error->" + e.Message);
            }
            catch (TaskCanceledException)
            {
                Debug.WriteLine("send cancel");
            }
            catch (Exception e)
            {
                OnErrorEvent(new CustomException(e.Message + "Send", this.GetType().FullName,
                                                 ExceptionPriority.Unrecoverable), item);
            }
        }
예제 #10
0
 public void OnErrorEvent(CustomException args, BaseDirective directive)
 {
     ErrorEvent?.Invoke(args, directive);
     Dispose();
 }
예제 #11
0
 public WaitForSend(DateTime time, BaseDirective directive, int reSendTimes = 0)
 {
     CreatedAt      = time;
     Directive      = directive;
     RetrySendTimes = reSendTimes;
 }
예제 #12
0
 protected override bool IsUserRemoteBaseMenuItemDisabledFor(BaseDirective directive) {
     switch (directive) {
         case BaseDirective.Attack:
             return !(_fleetMenuOperator as IUnitAttackable).IsAttackByAllowed(_user);
         default:
             throw new NotImplementedException(ErrorMessages.UnanticipatedSwitchValue.Inject(directive));
     }
 }
예제 #13
0
 private void Instance_ErrorEvent(CustomException arg1, BaseDirective arg2)
 {
     OnErrorEvent(arg1);
 }
예제 #14
0
 protected void OnPreCommunicationEvent(BaseDirective args)
 {
     PreCommunicationEvent?.Invoke(args);
 }
예제 #15
0
 private void PreCommunicationStatusChange(BaseDirective directive)
 {
     //发送开始命令或者暂停命令前
 }
예제 #16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BaseOrder" /> class.
 /// </summary>
 /// <param name="directive">The order directive.</param>
 /// <param name="source">The source of this order.</param>
 /// <param name="target">The target of this order. Default is null.</param>
 public BaseOrder(BaseDirective directive, OrderSource source, IUnitAttackable target = null) {
     D.AssertNotEqual(OrderSource.Captain, source);
     Directive = directive;
     Source = source;
     Target = target;
 }
예제 #17
0
 protected virtual bool IsUserRemoteBaseMenuItemDisabledFor(BaseDirective directive) { return false; }