private static void Process()
        {
            const int SLEEP_TIME_ON_EXCEPTION_MS = 5000;

            for (; ;)
            {
                try
                {
                    if (_Readers != null)
                    {
                        foreach (var reader in _Readers)
                        {
                            IEnumerable <string> dataEnumerable = reader.GetData();
                            foreach (string newDataJson in dataEnumerable)
                            {
                                if (_Gateway != null && newDataJson != null)
                                {
                                    _Gateway.Enqueue(newDataJson);
                                }
                            }
                        }
                    }

                    Thread.Sleep(_Config.SleepTimeMs);
                }
                catch (Exception ex)
                {
                    _Logger.LogError(ex.Message);
                    Thread.Sleep(SLEEP_TIME_ON_EXCEPTION_MS);
                }
            }
        }
예제 #2
0
 public void Enqueue(string deviceId, string jsonData)
 {
     try
     {
         service.Enqueue(deviceId, jsonData);
     }
     catch (Exception ex)
     {
         _logger.LogError("exception caught: " + ex.StackTrace);
     }
 }
예제 #3
0
        public void TestRepeatSend( )
        {
            try
            {
                GatewayService service = PrepareGatewayService( );

                // Send a flurry of messages, repeat a few times

                // script message sequence
                int[] sequence = new int[TEST_ITERATIONS];
                for (int iteration = 0; iteration < TEST_ITERATIONS; ++iteration)
                {
                    int count = _rand.Next(MAX_TEST_MESSAGES);

                    sequence[iteration] = count;

                    _totalMessagesToSend += count;
                }

                const float mean  = 39.6001f;
                const int   range = 10;

                Random rand = new Random(( int )(DateTime.Now.Ticks >> 32));

                // send the messages
                for (int iteration = 0; iteration < TEST_ITERATIONS; ++iteration)
                {
                    int count = sequence[iteration];

                    while (--count >= 0)
                    {
                        //
                        // Build a message.
                        // It will look something like this:
                        // "{\"unitofmeasure\":\"%\",\"location\":\"Olivier's office\",\"measurename\":\"Humidity\",\"timecreated\":\"2/26/2015 12:50:29 AM\",\"organization\":\"MSOpenTech\",\"guid\":\"00000000-0000-0000-0000-000000000000\",\"value\":39.600000000000001,\"displayname\":\"NETMF\"}"
                        //

                        bool  add     = (rand.Next( ) % 2) == 0;
                        int   variant = rand.Next( ) % range;
                        float value   = mean;

                        StringBuilder sb = new StringBuilder( );
                        sb.Append("{\"unitofmeasure\":\"%\",\"location\":\"Olivier's office\",\"measurename\":\"Humidity\",");
                        sb.Append("\"timecreated\":\"");
                        sb.Append(DateTime.UtcNow.ToString( ));   // this should look like "2015-02-25T23:07:47.159Z"
                        sb.Append("\",\"organization\":\"MSOpenTech\",\"guid\":\"");
                        sb.Append(new Guid( ).ToString( ));
                        sb.Append("\",\"value\":");
                        sb.Append((value += add ? variant : -variant).ToString( ));
                        sb.Append(",\"displayname\":\"NETMF\"}");

                        string message = sb.ToString( );

                        service.Enqueue(message);

                        DataArrived(message);
                    }
                }

                Debug.Assert(_totalMessagesSent == _totalMessagesToSend);

                _completed.WaitOne( );

                _batchSenderThread.Stop(STOP_TIMEOUT_MS);
            }
            catch (Exception ex)
            {
                _logger.LogError("exception caught: " + ex.StackTrace);
            }
            finally
            {
                _batchSenderThread.Stop(STOP_TIMEOUT_MS);
                _sender.Close( );
            }
        }