Esempio n. 1
0
        private void waitthread()
        {
            System.Threading.WaitHandle[] wh = new System.Threading.WaitHandle[inputdevices.Count + 1];
            for (int i = 0; i < inputdevices.Count; i++)
            {
                wh[i] = inputdevices[i].Eventhandle();
            }
            wh[inputdevices.Count] = stophandle;

            System.Diagnostics.Debug.WriteLine("IDL start");

            while (true)
            {
                int hhit = System.Threading.WaitHandle.WaitAny(wh);

                if (hhit == inputdevices.Count)  // stop!
                {
                    break;
                }

                List <InputDeviceEvent> list = inputdevices[hhit].GetEvents();

                if (list != null)
                {
                    //System.Diagnostics.Debug.WriteLine(Environment.TickCount + " Handle hit " + hhit + " " + inputdevices[hhit].ID().Name);

                    invokeAsyncOnUiThread(() => { OnNewEvent?.Invoke(list); }); // call in action context.
                }
            }
        }
Esempio n. 2
0
        public void SendCommand_Async(EnumCommands command, WorkCompletedCallback callback)
        {
            try
            {
                lock (syncRoot)
                {
                    if (!_waitingCommands.Contains(command))
                    {
                        OnNewEvent?.Invoke(this, "Add command:" + command + " to waiting list");

                        _commandsRetryCount[command] = 0;
                        _waitingCommands.Add(command);
                        if (callback != null)
                        {
                            _callbacks[command] = callback;
                        }
                    }
                    else
                    {
                        OnNewEvent?.Invoke(this, "Command:" + command + " already exist and will be ignored.");
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Error("Error in LEDStatusLightingUtil.SendCommand_Async:" + ex.Message);
                //MessageBox.Show("Error in SendCommand_Async. Details:" + ex.Message);
            }
        }
Esempio n. 3
0
 private void CompleteCurrentCommand(bool?result)
 {
     try
     {
         lock (syncRoot)
         {
             _waitingCommands.Remove(_currentCommand);
             if (_commandsRetryCount.ContainsKey(_currentCommand))
             {
                 _commandsRetryCount.Remove(_currentCommand);
             }
             WorkCompletedCallback callback = null;
             if (_callbacks.ContainsKey(_currentCommand))
             {
                 callback = _callbacks[_currentCommand];
                 _callbacks.Remove(_currentCommand);
             }
             if (callback != null && result != null)
             {
                 callback(result.Value);
             }
             OnNewEvent?.Invoke(this, "Complete current command " + _currentCommand + " successfully. Result:" + (result != null ? result.Value.ToString() : "null"));
             _currentCommand = EnumCommands.Unknown;
         }
     }
     catch (Exception ex)
     {
         LogManager.Error("Error in LEDStatusLightingUtil.CompleteCurrentCommand:" + ex.Message);
         //MessageBox.Show("Error in CompleteCurrentCommand. Details:" + ex.Message);
     }
 }
Esempio n. 4
0
        private void CommandsHandler()
        {
            try
            {
                // Start to process new command
                this.DataReceived += SendCommand_Async_Callback;

                OnNewEvent?.Invoke(this, "CommandsHandler is started.");
                while (IsPortOpen)
                {
                    if (_currentCommand == EnumCommands.Unknown)
                    {
                        lock (syncRoot)
                        {
                            if (_currentCommand == EnumCommands.Unknown)
                            {
                                GetNextCommand();
                                if (_currentCommand != EnumCommands.Unknown)
                                {
                                    OnNewEvent?.Invoke(this, "Start to proceed next command:" + _currentCommand);


                                    string asciiCommand = "";

                                    asciiCommand = _rs232Commands[_currentCommand];
                                    if (_commandsRetryCount.ContainsKey(_currentCommand))
                                    {
                                        _commandsRetryCount[_currentCommand]++;
                                    }
                                    else
                                    {
                                        _commandsRetryCount[_currentCommand] = 1;
                                    }
                                    OnNewEvent?.Invoke(this, "Start to send command:" + _currentCommand + ", retry:" + _commandsRetryCount[_currentCommand]);

                                    SendASCIICommand(asciiCommand);
                                }
                            }
                        }
                    }
                    Thread.Sleep(200);
                }
                this.DataReceived -= SendCommand_Async_Callback;
                OnNewEvent?.Invoke(this, "CommandsHandler is stopped.");
            }
            catch (Exception ex)
            {
                LogManager.Error("Error in LEDStatusLightingUtil.CommandsHandler:" + ex.Message);
                //MessageBox.Show("Error in CommandsHandler. Details:" + ex.Message);
                StartCommandHandler();
            }
        }
Esempio n. 5
0
 private void GetNextCommand()
 {
     _currentCommand = EnumCommands.Unknown;
     if (_waitingCommands.Count > 0)
     {
         if (_currentCommandIndex >= _waitingCommands.Count - 1)
         {
             _currentCommandIndex = 0;
         }
         else
         {
             _currentCommandIndex++;
         }
         _currentCommand = _waitingCommands[_currentCommandIndex];
         OnNewEvent?.Invoke(this, "Get next command:" + _currentCommand);
     }
 }
Esempio n. 6
0
        public void CallOnNewEvent(DateTime dt, Event.EnumSeverity s, Event.EnumType t, string text)
        {
            var l = new List <Action <DateTime, Event.EnumSeverity, Event.EnumType, string> >();

            foreach (var n in OnNewEvent)
            {
                try
                {
                    n.Invoke(dt, s, t, text);
                }
                catch (Exception)
                {
                    l.Add(n);
                }
            }
            l.ForEach(p => OnNewEvent.Remove(p));
        }
        public Logger_Mock()
        {
            _loggerMock = Substitute.For <ILogger <T> >();

            _loggerMock.When(x => x.Log(Arg.Any <LogLevel>(), Arg.Any <EventId>(), Arg.Any <object>(),
                                        Arg.Any <Exception>(), Arg.Any <Func <object, Exception, string> >()))
            .Do(x =>
            {
                var level  = x.ArgAt <LogLevel>(0);
                var format = x.ArgAt <FormattedLogValues>(2);
                var ex     = x.ArgAt <Exception>(3);

                var logEvent = new LogEvent
                {
                    Level     = level,
                    Message   = format.ToString(),
                    Exception = ex
                };

                _accumulatedLogs.Add(logEvent);
                OnNewEvent?.Invoke(logEvent);
            });
        }
Esempio n. 8
0
            internal async void Poll(object sender, ElapsedEventArgs args)
            {
                //make a web request
                using (var client = new HttpClient())
                {
                    client.BaseAddress = requestUri.Uri;
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Hoist", Configuration.ApiKey);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    // New code:
                    HttpResponseMessage response = await client.GetAsync(requestUri.Uri.PathAndQuery);
                    if (response.IsSuccessStatusCode)
                    {
                        var events = await response.Content.ReadAsAsync<IEnumerable<Events.HoistEvent>>();
                        foreach (var ev in events)
                        {
                            //fire any returned events
                            OnNewEvent.Invoke(this, ev);
                        }
                    }
                }

            }
Esempio n. 9
0
 public void RaiseEvent(EventInfo eventInfo)
 {
     OnNewEvent?.Invoke(this, eventInfo);
 }
Esempio n. 10
0
 private void HandleOnNotify(object sender, GenaNotifyResponse e)
 {
     LastEvent = new UPnPServiceEvent(e.Values);
     OnNewEvent?.Invoke(this, LastEvent);
 }
Esempio n. 11
0
        private void SendCommand_Async_Callback(object sender, string response)
        {
            try
            {
                lock (syncRoot)
                {
                    OnNewEvent?.Invoke(this, "SendCommand_Async_Callback, response:" + response + ", command:" + _currentCommand);

                    if (_currentCommand == EnumCommands.Unknown)
                    {
                        //MessageBox.Show("Tai sao lai xay ra truong hop nay");
                        return;
                    }
                    WorkCompletedCallback callback = null;
                    if (_callbacks.ContainsKey(_currentCommand))
                    {
                        callback = _callbacks[_currentCommand];
                    }
                    else
                    {
                        OnNewEvent?.Invoke(this, "Found no callback for command " + _currentCommand);

                        CompleteCurrentCommand(null);
                        //MessageBox.Show("Tai sao lai xay ra truong hop nay chu");
                        return;
                    }
                    //OnNewEvent?.Invoke(this, "SendCommand_Async_Callback, response:" + response);


                    if (_currentCommand == EnumCommands.CheckIfMUBIsPresent || _currentCommand == EnumCommands.CheckIfTTIsPresent)
                    {
                        if (response == "0")
                        {
                            CompleteCurrentCommand(false);
                            return;
                        }
                        else
                        {
                            CompleteCurrentCommand(true);
                            return;
                        }
                    }
                    else if (_currentCommand == EnumCommands.CheckIfMUBIsRemoved || _currentCommand == EnumCommands.CheckIfTTIsRemoved)
                    {
                        if (response == "0")
                        {
                            CompleteCurrentCommand(true);
                            return;
                        }
                        else
                        {
                            CompleteCurrentCommand(false);
                            return;
                        }
                    }
                    else
                    {
                        if (response == "1" || response.ToLower() == "ok" || response.ToLower() == "yes")
                        {
                            CompleteCurrentCommand(true);
                            return;
                        }
                        else
                        {
                            if (_commandsRetryCount[_currentCommand] == _maxRetryCount)
                            {
                                CompleteCurrentCommand(false);
                                return;
                            }
                            else
                            {
                                // Retry
                                OnNewEvent?.Invoke(this, "Continue retrying command " + _currentCommand + ", count:" + _commandsRetryCount[_currentCommand]);
                                _currentCommand = EnumCommands.Unknown;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CompleteCurrentCommand(false);
                Thread currentThread = Thread.CurrentThread;
                LogManager.Error("Error in SendCommand_Async_Callback. Current thread:" + currentThread.Name + ". Details:" + ex.Message);
                //MessageBox.Show("Error in SendCommand_Async_Callback. Current thread:" + currentThread.Name + ". Details:" + ex.Message);
            }
        }