Beispiel #1
0
        public void SendToAndReceiveTest()
        {
            RegisterDevice(true);

            var persistentConnection = new PersistentConnectionClient(PlatformWebsocketApi);

            persistentConnection.Login(_deviceId, _apiKey);

            persistentConnection.SendMessageTo(_otherDeviceId, "{\"Temperature\": 24, \"Time\":" + DateTime.UtcNow.Ticks + "}");

            persistentConnection.Close();

            var persistentConnection2 = new PersistentConnectionClient(PlatformWebsocketApi);

            PushedMessage    received = null;
            ManualResetEvent mre      = new ManualResetEvent(false);

            persistentConnection2.Login(_otherDeviceId, _apiKey);
            persistentConnection2.Subscribe(SubscriptionType.ReceiveAndForget, msg => { received = msg;
                                                                                        mre.Set(); });

            mre.WaitOne(TimeSpan.FromSeconds(2));

            Assert.IsNotNull(received);
            Assert.AreEqual(_deviceId, received.SenderDeviceId);
            Assert.IsTrue(received.Payload.StartsWith("{\"Temperature\": 24, \"Time\":"));
        }
Beispiel #2
0
        public void SendToTest()
        {
            RegisterDevice(true);

            var persistentConnection = new PersistentConnectionClient(PlatformWebsocketApi);

            persistentConnection.Login(_deviceId, _apiKey);

            persistentConnection.SendMessageTo(_otherDeviceId, "{\"Temperature\": 24, \"Time\":" + DateTime.UtcNow.Ticks + "}");

            persistentConnection.Close();
        }
Beispiel #3
0
        private static void PersistentSend(string[] lines, int deviceOrder, int sleep, int maxSend)
        {
            var line = lines[deviceOrder];

            var parts = line.Split(' ');

            var deviceId = parts[0];
            var apiKey   = parts[1];

            Log(deviceId);


            int cnt = 0;
            var rnd = new Random();

            var persistentConnectionClient = new PersistentConnectionClient(Config.PlatformApiWS);

            while (true)
            {
                RetryableLogin(persistentConnectionClient, deviceId, apiKey);

                while (true)
                {
                    var target         = lines[rnd.Next(maxSend)];
                    var targetDeviceId = target.Split(' ')[0];
                    try
                    {
                        persistentConnectionClient.SendMessageTo(targetDeviceId,
                                                                 "{\"Temperature:\": 24, \"Humidity\": 60, \"Time\":" + DateTime.UtcNow.Ticks + "}");
                        cnt++;
                    }
                    catch (Exception ex)
                    {
                        Log(ex);
                    }
                    persistentConnectionClient.Spin();
                    Thread.Sleep(sleep);
                    Log("To " + targetDeviceId + " Cnt: " + cnt);
                }
            }
        }