Ejemplo n.º 1
0
        private static void DestroyTestResources()
        {
            if ((_devices != null && _devices.Any()) || (_distributions != null && _distributions.Any()))
            {
                using (var client = new M2XClient(_masterKey))
                {
                    if (_distributions != null && _distributions.Any())
                    {
                        for (var i = _distributions.Count(); i > 0; i--)
                        {
                            var _distribution = _distributions.ElementAt(i - 1);
                            var distribution  = client.Distribution(_distribution.Value.id);
                            var result        = distribution.Delete().Result;
                            System.Threading.Thread.Sleep(250);
                            _distributions.Remove(_distribution.Key);
                        }
                    }

                    if (_devices != null && _devices.Any())
                    {
                        for (var i = _devices.Count(); i > 0; i--)
                        {
                            var _device = _devices.ElementAt(i - 1);
                            var device  = client.Device(_device.Value.id);
                            var result  = device.Delete().Result;
                            System.Threading.Thread.Sleep(250);
                            _devices.Remove(_device.Key);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static void InitializeTestSpecs(TestContext testContext)
        {
            _devices          = new Dictionary <string, Device>();
            _testDeviceSerial = $"td-{DateTime.UtcNow.Ticks}";

            using (var client = new M2XClient(_masterKey))
            {
                var createDeviceParms  = $"{{ \"base_device\": \"d781ab7460136af9db496c97172a6e6c\", \"name\": \"{Constants.TestDeviceNamePrefix} {DateTime.UtcNow.Ticks}\", \"description\": \"{Constants.TestDeviceDescription}\", \"serial\": \"{_testDeviceSerial}\", \"visibility\": \"private\" }}";
                var createDeviceResult = client.CreateDevice(createDeviceParms).Result;
                var device             = JsonConvert.DeserializeObject <Device>(createDeviceResult.Raw);
                _devices.Add("primary", device);
                _testDeviceId = device.id;
                var testDevice          = client.Device(device.id);
                var updateTagsParms     = "{ \"tags\": \"test only\" }";
                var resultTags          = testDevice.Update(updateTagsParms).Result;
                var updateLocationParms = $"{{ \"name\": \"Test Device Location\", \"latitude\": {Constants.TestDeviceLatitude}, \"longitude\": {Constants.TestDeviceLongitude} }}";
                var resultLocation      = testDevice.UpdateLocation(updateLocationParms).Result;
                var updateMetadataParms = "{ \"owner\": \"The Testing Guy\" } ";
                var resultMetadata      = testDevice.UpdateMetadata(updateMetadataParms);

                var stream01UpdateParms = $"{{ \"values\": [ {{ \"timestamp\": \"{DateTime.Now.AddSeconds(-10).ToString("yyyy-MM-ddTHH:mm:ssZ")}\", \"value\": 98.6 }}, {{ \"timestamp\": \"{DateTime.Now.AddSeconds(-5).ToString("yyyy-MM-ddTHH:mm:ssZ")}\", \"value\": 98.7 }} ] }}";
                var stream01            = testDevice.Stream(Constants.TestStreamName001);
                var resultStream01Post  = stream01.PostValues(stream01UpdateParms).Result;
                System.Threading.Thread.Sleep(500);

                var stream02UpdateParms = $"{{ \"values\": [ {{ \"timestamp\": \"{DateTime.Now.AddSeconds(-10).ToString("yyyy-MM-ddTHH:mm:ssZ")}\", \"value\": \"normal\" }}, {{ \"timestamp\": \"{DateTime.Now.AddSeconds(-5).ToString("yyyy-MM-ddTHH:mm:ssZ")}\", \"value\": \"normal\" }} ] }}";
                var stream02            = testDevice.Stream(Constants.TestStreamName002);
                var resultStream02Post  = stream02.PostValues(stream02UpdateParms).Result;
                System.Threading.Thread.Sleep(500);
            }
        }
Ejemplo n.º 3
0
        public static void InitializeTestSpecs(TestContext testContext)
        {
            _devices          = new Dictionary <string, Device>();
            _distributions    = new Dictionary <string, Distribution>();
            _testDeviceSerial = $"td-{DateTime.UtcNow.Ticks}";

            using (var client = new M2XClient(_masterKey))
            {
                var createDeviceParms  = $"{{ \"base_device\": \"d781ab7460136af9db496c97172a6e6c\", \"name\": \"{Constants.TestDeviceNamePrefix} {DateTime.UtcNow.Ticks}\", \"description\": \"{Constants.TestDeviceDescription}\", \"serial\": \"{_testDeviceSerial}\", \"visibility\": \"private\" }}";
                var createDeviceResult = client.CreateDevice(createDeviceParms).Result;
                var device             = JsonConvert.DeserializeObject <Device>(createDeviceResult.Raw);
                _devices.Add("primary", device);
                _testDeviceId = device.id;

                var testDevice      = client.Device(device.id);
                var updateTagsParms = "{ \"tags\": \"test only\" }";
                var resultTags      = testDevice.Update(updateTagsParms).Result;
                for (var i = 0; i < 5; i++)
                {
                    var updateLocationParms = $"{{ \"name\": \"Test Device Location{i + 1}\", \"latitude\": {(Constants.TestDeviceLatitude + i)}, \"longitude\": {(Constants.TestDeviceLongitude + i)} }}";
                    var resultLocation      = testDevice.UpdateLocation(updateLocationParms).Result;
                }
                var updateMetadataParms = $"{{ \"{Constants.TestMetadataDefaultFieldName}\": \"{Constants.TestMetadataDefaultFieldValue}\" }} ";
                var resultMetadata      = testDevice.UpdateMetadata(updateMetadataParms);

                var stream01UpdateParms = $"{{ \"values\": [ {{ \"timestamp\": \"{DateTime.Now.AddSeconds(-10).ToString("yyyy-MM-ddTHH:mm:ssZ")}\", \"value\": 98.6 }}, {{ \"timestamp\": \"{DateTime.Now.AddSeconds(-5).ToString("yyyy-MM-ddTHH:mm:ssZ")}\", \"value\": 98.7 }} ] }}";
                var stream01            = testDevice.Stream(Constants.TestStreamName001);
                var resultStream01Post  = stream01.PostValues(stream01UpdateParms).Result;
                System.Threading.Thread.Sleep(250);

                var stream02UpdateParms = $"{{ \"values\": [ {{ \"timestamp\": \"{DateTime.Now.AddSeconds(-10).ToString("yyyy-MM-ddTHH:mm:ssZ")}\", \"value\": \"normal\" }}, {{ \"timestamp\": \"{DateTime.Now.AddSeconds(-5).ToString("yyyy-MM-ddTHH:mm:ssZ")}\", \"value\": \"normal\" }} ] }}";
                var stream02            = testDevice.Stream(Constants.TestStreamName002);
                var resultStream02Post  = stream02.PostValues(stream02UpdateParms).Result;
                System.Threading.Thread.Sleep(250);

                var testDistributionCreateParms  = $"{{ \"name\": \"Test Distribution {DateTime.Now.Ticks}\", \"description\": \"This is just a test!\", \"visibility\": \"private\", \"base_device\": \"{_testDeviceId}\", \"metadata\": {{ \"{Constants.TestMetadataDefaultFieldName}\": \"{Constants.TestMetadataDefaultFieldValue}\" }} }}";
                var testDistributionCreateResult = client.CreateDistribution(testDistributionCreateParms).Result;
                _accountIsNotPro = testDistributionCreateResult.Error;

                if (_accountIsNotPro)
                {
                    return;
                }

                var testDistribution           = JsonConvert.DeserializeObject <Distribution>(testDistributionCreateResult.Raw);
                var distributionAddDeviceParms = $"{{ \"serial\": \"td-{(DateTime.UtcNow.Ticks + 50)}\" }}";
                var distributionDeviceData     = client.Distribution(testDistribution.id).AddDevice(distributionAddDeviceParms).Result;
                var distributionDevice         = JsonConvert.DeserializeObject <Device>(distributionDeviceData.Raw);
                _devices.Add(distributionDevice.serial, distributionDevice);
                _distributions.Add("primary", testDistribution);
                System.Threading.Thread.Sleep(250);
            }
        }
Ejemplo n.º 4
0
        private static void Main()
        {
            //list initialisation
            for (int i = 0; i < 8; i++)
            {
                emgList.Add(new List <int>());
            }
            for (int i = 0; i < 3; i++)
            {
                deltaList.Add(new List <double>());
            }
            //M2X initialisation
            M2XDevice device = m2x.Device("7787671fb86b9e2bc3ccaa23ad934bf6");

            movement     = device.Stream("patientMove");
            streamFall   = device.Stream("hasFallen");
            patientState = device.Stream("patientState");
            // Myo initialisation from SDK
            using (var channel = Channel.Create(
                       ChannelDriver.Create(ChannelBridge.Create(),
                                            MyoErrorHandlerDriver.Create(MyoErrorHandlerBridge.Create()))))
                using (var hub = Hub.Create(channel))
                {
                    // listenin and register event handlers
                    hub.MyoConnected += (sender, e) =>
                    {
                        //e.Myo.Lock();
                        Console.WriteLine("Myo has been connected!", e.Myo.Handle);
                        //e.Myo.OrientationDataAcquired += Myo_OrientationDataAcquired;
                        e.Myo.AccelerometerDataAcquired += Myo_Accelerometer;
                        e.Myo.EmgDataAcquired           += MyoEmgDataHandler;
                        e.Myo.SetEmgStreaming(true);
                        e.Myo.Lock();
                    };

                    // disabling myo listening and handlers
                    hub.MyoDisconnected += (sender, e) =>
                    {
                        Console.WriteLine("Myo was disconnected, data logging wont work.", e.Myo.Arm);
                        e.Myo.AccelerometerDataAcquired -= Myo_Accelerometer;
                        e.Myo.EmgDataAcquired           -= MyoEmgDataHandler;
                    };

                    // start listening for Myo data
                    channel.StartListening();

                    // wait on user input
                    ConsoleHelper.UserInputLoop(hub);
                }
        }
Ejemplo n.º 5
0
 private static void DestroyTestDevice()
 {
     if (_testDeviceExists)
     {
         using (var client = new M2XClient(_masterKey))
         {
             var device = client.Device(_testDevice.id);
             device.Delete();
             System.Threading.Thread.Sleep(500);
             _testDevice       = null;
             _testDeviceExists = false;
         }
     }
 }
Ejemplo n.º 6
0
 private static void DestroyTestDevices()
 {
     if (_devices != null && _devices.Any())
     {
         for (var i = _devices.Count(); i > 0; i--)
         {
             var _device = _devices.ElementAt(i - 1);
             using (var client = new M2XClient(_masterKey))
             {
                 var device = client.Device(_device.Value.id);
                 device.Delete();
                 System.Threading.Thread.Sleep(500);
                 _devices.Remove(_device.Key);
             }
         }
     }
 }
Ejemplo n.º 7
0
        public async Task CanAccess_MasterApiKey_AndSendCommands_ToSingleDevice_AndList_DeviceCommands()
        {
            try
            {
                using (var client = new M2XClient(_masterKey))
                {
                    var sendCommandParms = new StringBuilder($"{{ ");
                    sendCommandParms.Append($"\"name\": \"PHONE_HOME\"");
                    sendCommandParms.Append($", ");
                    sendCommandParms.Append($"\"data\": {{ \"server_url\": \"https://m2x.att.com\" }}");
                    sendCommandParms.Append($", ");
                    sendCommandParms.Append($"\"targets\": {{ \"devices\": [\"{_testDevice.id}\"] }}");
                    sendCommandParms.Append($" }}");

                    var result = await client.SendCommand(sendCommandParms.ToString());

                    Assert.IsNotNull(result);
                    Assert.IsFalse(result.Error);
                    Assert.IsFalse(result.ServerError);
                    Assert.IsNull(result.WebError);
                    Assert.IsFalse(string.IsNullOrWhiteSpace(result.Raw));
                    Assert.AreEqual(Constants.M2X_Response_Success_Accepted, result.Raw);
                }

                using (var client = new M2XClient(_masterKey))
                {
                    var device = client.Device(_testDevice.id);
                    var result = await device.Commands();

                    Assert.IsNotNull(result);
                    Assert.IsFalse(result.Error);
                    Assert.IsFalse(result.ServerError);
                    Assert.IsNull(result.WebError);
                    Assert.IsFalse(string.IsNullOrWhiteSpace(result.Raw));
                    var commands = JsonConvert.DeserializeObject <ApiResponseForCommandSearch>(result.Raw);
                    Assert.IsNotNull(commands);
                }
            }
            catch (Exception)
            {
                DestroyTestDevice();
                throw;
            }
        }
Ejemplo n.º 8
0
        public void CommandsApiTest()
        {
            response = m2x.CreateDevice(new DeviceParams
            {
                name       = "TestDevice-" + this.TestId,
                visibility = M2XVisibility.Private,
            }).Result;
            Assert.AreEqual(HttpStatusCode.Created, response.Status, response.Raw);
            var deviceDetails = response.Json <DeviceDetails>();

            Assert.IsNotNull(deviceDetails.id);
            this.device = m2x.Device(deviceDetails.id);
            Thread.Sleep(1000);

            string commandName = "TestCommand-" + this.TestId;

            response = m2x.SendCommand(new SendCommandParams
            {
                name    = commandName,
                targets = new CommandTargets {
                    devices = new string[] { deviceDetails.id }
                },
            }).Result;
            Assert.AreEqual(HttpStatusCode.Accepted, response.Status, response.Raw);
            Thread.Sleep(1000);

            response = m2x.Commands(new CommandParams {
                name = commandName
            }).Result;
            Assert.AreEqual(HttpStatusCode.OK, response.Status, response.Raw);
            var commands = response.Json <CommandList <CommandDetails> >();

            Assert.IsNotNull(commands, response.Raw);
            Assert.IsNotNull(commands.commands, response.Raw);
            Assert.AreEqual(1, commands.commands.Length, response.Raw);
            Assert.AreEqual(commandName, commands.commands[0].name, response.Raw);
            string commandId = commands.commands[0].id;

            Assert.IsNotNull(commandId, response.Raw);

            response = m2x.CommandDetails(commandId).Result;
            Assert.AreEqual(HttpStatusCode.OK, response.Status, response.Raw);
            var command = response.Json <CommandDetails>();

            Assert.IsNotNull(command, response.Raw);
            Assert.AreEqual(commandName, command.name, response.Raw);

            response = m2x.Keys(new KeyListParams {
                device = deviceDetails.id
            }).Result;
            Assert.AreEqual(HttpStatusCode.OK, response.Status, response.Raw);
            var keys = response.Json <KeyList>();

            Assert.IsNotNull(keys, response.Raw);
            Assert.IsNotNull(keys.keys, response.Raw);
            Assert.AreEqual(1, keys.keys.Length, response.Raw);
            Assert.IsTrue(keys.keys[0].device.Contains(deviceDetails.id), response.Raw);
            string key = keys.keys[0].key;

            Assert.AreEqual(deviceDetails.key, key, response.Raw);
            Assert.IsNotNull(key, response.Raw);

            using (var deviceClient = new M2XClient(deviceDetails.key))
            {
                var device_ = deviceClient.Device(deviceDetails.id);

                response = device_.Commands(new CommandParams {
                    status = M2XCommandStatus.Sent
                }).Result;
                Assert.AreEqual(HttpStatusCode.OK, response.Status, response.Raw);
                commands = response.Json <CommandList <CommandDetails> >();
                Assert.IsNotNull(commands, response.Raw);
                Assert.IsNotNull(commands.commands, response.Raw);
                Assert.AreEqual(1, commands.commands.Length, response.Raw);
                Assert.AreEqual(commandName, commands.commands[0].name, response.Raw);
                Assert.AreEqual(commandId, commands.commands[0].id, response.Raw);
                Assert.AreEqual(M2XCommandStatus.Sent, commands.commands[0].status, response.Raw);

                response = device_.CommandDetails(commandId).Result;
                Assert.AreEqual(HttpStatusCode.OK, response.Status, response.Raw);
                command = response.Json <CommandDetails>();
                Assert.IsNotNull(command, response.Raw);
                Assert.AreEqual(commandName, command.name, response.Raw);
                Assert.AreEqual(M2XCommandStatus.Sent, command.status, response.Raw);

                response = device_.ProcessCommand(commandId).Result;
                Assert.AreEqual(HttpStatusCode.NoContent, response.Status, response.Raw);
                Thread.Sleep(1000);
                response = device_.CommandDetails(commandId).Result;
                Assert.AreEqual(HttpStatusCode.OK, response.Status, response.Raw);
                command = response.Json <CommandDetails>();
                Assert.IsNotNull(command, response.Raw);
                Assert.AreEqual(commandName, command.name, response.Raw);
                Assert.AreEqual(M2XCommandStatus.Processed, command.status, response.Raw);

                commandName = "TestCommand2-" + this.TestId;
                response    = m2x.SendCommand(new SendCommandParams
                {
                    name    = commandName,
                    targets = new CommandTargets {
                        devices = new string[] { deviceDetails.id }
                    },
                }).Result;
                Assert.AreEqual(HttpStatusCode.Accepted, response.Status, response.Raw);
                Thread.Sleep(1000);

                response = device_.Commands(new CommandParams {
                    status = M2XCommandStatus.Sent
                }).Result;
                Assert.AreEqual(HttpStatusCode.OK, response.Status, response.Raw);
                commands = response.Json <CommandList <CommandDetails> >();
                Assert.IsNotNull(commands, response.Raw);
                Assert.IsNotNull(commands.commands, response.Raw);
                Assert.AreEqual(1, commands.commands.Length, response.Raw);
                Assert.AreEqual(commandName, commands.commands[0].name, response.Raw);
                commandId = commands.commands[0].id;

                response = device_.RejectCommand(commandId).Result;
                Assert.AreEqual(HttpStatusCode.NoContent, response.Status, response.Raw);
                Thread.Sleep(1000);
                response = device_.CommandDetails(commandId).Result;
                Assert.AreEqual(HttpStatusCode.OK, response.Status, response.Raw);
                command = response.Json <CommandDetails>();
                Assert.IsNotNull(command, response.Raw);
                Assert.AreEqual(commandName, command.name, response.Raw);
                Assert.AreEqual(M2XCommandStatus.Rejected, command.status, response.Raw);
            }
        }
Ejemplo n.º 9
0
        public async Task CanCreate_Collection_And_PrivateDevice_ThenAdd_DeviceToCollection_TheRemove_DeviceFromCollection_AndDelete_Both()
        {
            using (var client = new M2XClient(_masterKey))
            {
                var collectionParameters   = $"{{ \"name\": \"*** PLEASE DELETE ME *** Test Auto Created Collection {DateTime.UtcNow.Ticks}\" }}";
                var createCollectionResult = await client.CreateCollection(collectionParameters);

                Assert.IsNotNull(createCollectionResult);
                Assert.IsFalse(createCollectionResult.Error);
                Assert.IsFalse(createCollectionResult.ServerError);
                Assert.IsNull(createCollectionResult.WebError);
                Assert.IsFalse(string.IsNullOrWhiteSpace(createCollectionResult.Raw));

                var collection = JsonConvert.DeserializeObject <Collection>(createCollectionResult.Raw);
                Assert.IsNotNull(collection);

                var collectionWrapper = client.Collection(collection.id);
                Assert.IsNotNull(collectionWrapper);

                var deviceParameters = $"{{ \"name\": \"*** PLEASE DELETE ME *** Test Auto Created Device {DateTime.UtcNow.Ticks}\", \"visibility\": \"private\" }}";
                var result           = await client.CreateDevice(deviceParameters);

                Assert.IsNotNull(result);
                Assert.IsFalse(result.Error);
                Assert.IsFalse(result.ServerError);
                Assert.IsNull(result.WebError);
                Assert.IsFalse(string.IsNullOrWhiteSpace(result.Raw));

                var device = JsonConvert.DeserializeObject <Device>(result.Raw);
                Assert.IsNotNull(device);

                var addCollectionDeviceResult = await collectionWrapper.AddDevice(device.id);

                Assert.IsNotNull(addCollectionDeviceResult);
                Assert.IsTrue(string.IsNullOrWhiteSpace(addCollectionDeviceResult.Raw));


                var checkCollectionDevices0 = await collectionWrapper.Details();

                Assert.IsNotNull(checkCollectionDevices0);
                Assert.IsFalse(string.IsNullOrWhiteSpace(checkCollectionDevices0.Raw));
                var checkCollection0 = JsonConvert.DeserializeObject <Collection>(checkCollectionDevices0.Raw);
                Assert.IsNotNull(checkCollection0);
                Assert.IsTrue(checkCollection0.devices > 0);

                var removeCollectionDeviceResult = await collectionWrapper.RemoveDevice(device.id);

                Assert.IsNotNull(removeCollectionDeviceResult);
                Assert.IsTrue(string.IsNullOrWhiteSpace(removeCollectionDeviceResult.Raw));


                var checkCollectionDevices1 = await collectionWrapper.Details();

                Assert.IsNotNull(checkCollectionDevices1);
                Assert.IsFalse(string.IsNullOrWhiteSpace(checkCollectionDevices1.Raw));
                var checkCollection1 = JsonConvert.DeserializeObject <Collection>(checkCollectionDevices1.Raw);
                Assert.IsNotNull(checkCollection1);
                Assert.IsTrue(checkCollection1.devices == 0);

                var deviceWrapper = client.Device(device.id, device.serial != null ? device.serial.ToString() : null);
                Assert.IsNotNull(deviceWrapper);
                await deviceWrapper.Delete();

                var devicesCheck = await client.SearchDevices(new { ids = device.id });

                Assert.IsNotNull(devicesCheck);
                Assert.IsFalse(devicesCheck.Error);
                Assert.IsFalse(devicesCheck.ServerError);
                Assert.IsNull(devicesCheck.WebError);
                Assert.IsFalse(string.IsNullOrWhiteSpace(devicesCheck.Raw));
                ProcessDeviceSearchResult(devicesCheck.Raw, false);

                await collectionWrapper.Delete();

                var collectionCheck       = client.Collection(collection.id);
                var collectionCheckResult = await collectionCheck.Details();

                Assert.IsNotNull(collectionCheck);
                Assert.IsTrue(collectionCheckResult.Error);
                Assert.IsFalse(collectionCheckResult.ServerError);
                Assert.IsNull(collectionCheckResult.WebError);
                Assert.IsFalse(string.IsNullOrWhiteSpace(collectionCheckResult.Raw));
                ProcessCollectionsSearchResult(collectionCheckResult.Raw, false);
            }
        }
Ejemplo n.º 10
0
        public async Task CanAccess_MasterApiKey_AndSendCommand_ToSingleDevice_ThenList_CommandsSent_WithFilters()
        {
            try
            {
                var commandName = "PHONE_HOME";
                using (var client = new M2XClient(_masterKey))
                {
                    var sendCommandParms = new StringBuilder($"{{ ");
                    sendCommandParms.Append($"\"name\": \"{commandName}\"");
                    sendCommandParms.Append($", ");
                    sendCommandParms.Append($"\"data\": {{ \"server_url\": \"https://m2x.att.com\" }}");
                    sendCommandParms.Append($", ");
                    sendCommandParms.Append($"\"targets\": {{ \"devices\": [\"{_testDevice.id}\"] }}");
                    sendCommandParms.Append($" }}");

                    var result = await client.SendCommand(sendCommandParms.ToString());

                    Assert.IsNotNull(result);
                    Assert.IsFalse(result.Error);
                    Assert.IsFalse(result.ServerError);
                    Assert.IsNull(result.WebError);
                    Assert.IsFalse(string.IsNullOrWhiteSpace(result.Raw));
                    Assert.AreEqual(Constants.M2X_Response_Success_Accepted, result.Raw);
                }

                System.Threading.Thread.Sleep(1000);
                using (var client = new M2XClient(_masterKey))
                {
                    var device = client.Device(_testDevice.id);
                    foreach (var filter in new[] { "limit", "page", "dir|desc", "dir|asc", "start", "end", "name", "status|pending", "status|processed", "status|rejected" })
                    {
                        M2XResponse result = null;
                        switch (filter)
                        {
                        case "limit":
                            result = await device.Commands(new { limit = 2 });

                            break;

                        case "page":
                            result = await device.Commands(new { page = 1 });

                            break;

                        case "dir|desc":
                            result = await device.Commands(new { dir = "desc" });

                            break;

                        case "dir|asc":
                            result = await device.Commands(new { dir = "asc" });

                            break;

                        case "start":
                            result = await device.Commands(new { start = DateTime.UtcNow.AddMinutes(-60).ToString(Constants.ISO8601_DateStartFormat) });

                            break;

                        case "end":
                            result = await device.Commands(new { end = DateTime.UtcNow.AddMinutes(-10).ToString(Constants.ISO8601_DateStartFormat) });

                            break;

                        case "name":
                            result = await device.Commands(new { name = "PHONE_HOME" });

                            break;

                        case "status|pending":
                            result = await device.Commands(new { status = "pending" });

                            break;

                        case "status|processed":
                            result = await device.Commands(new { status = "processed" });

                            break;

                        case "status|rejected":
                            result = await device.Commands(new { status = "rejected" });

                            break;
                        }

                        Assert.IsNotNull(result);
                        Assert.IsFalse(result.Error);
                        Assert.IsFalse(result.ServerError);
                        Assert.IsNull(result.WebError);
                        Assert.IsFalse(string.IsNullOrWhiteSpace(result.Raw));
                        ProcessCommandSearchResult(result.Raw, null);
                    }
                }
            }
            catch (Exception ex)
            {
                DestroyTestDevice();
                throw;
            }
        }
Ejemplo n.º 11
0
        public async Task CanAccess_MasterApiKey_AndSendCommands_ToSingleDevice_AndDevice_MarksCommand_AsRejected()
        {
            try
            {
                var commandName = "PHONE_HOME";
                using (var client = new M2XClient(_masterKey))
                {
                    var sendCommandParms = new StringBuilder($"{{ ");
                    sendCommandParms.Append($"\"name\": \"{commandName}\"");
                    sendCommandParms.Append($", ");
                    sendCommandParms.Append($"\"data\": {{ \"server_url\": \"https://m2x.att.com\" }}");
                    sendCommandParms.Append($", ");
                    sendCommandParms.Append($"\"targets\": {{ \"devices\": [\"{_testDevice.id}\"] }}");
                    sendCommandParms.Append($" }}");

                    var result = await client.SendCommand(sendCommandParms.ToString());

                    Assert.IsNotNull(result);
                    Assert.IsFalse(result.Error);
                    Assert.IsFalse(result.ServerError);
                    Assert.IsNull(result.WebError);
                    Assert.IsFalse(string.IsNullOrWhiteSpace(result.Raw));
                    Assert.AreEqual(Constants.M2X_Response_Success_Accepted, result.Raw);
                }

                System.Threading.Thread.Sleep(1000);
                var targetCommandId = string.Empty;
                using (var client = new M2XClient(_masterKey))
                {
                    var device = client.Device(_testDevice.id);
                    var result = await device.Commands();

                    Assert.IsNotNull(result);
                    Assert.IsFalse(result.Error);
                    Assert.IsFalse(result.ServerError);
                    Assert.IsNull(result.WebError);
                    Assert.IsFalse(string.IsNullOrWhiteSpace(result.Raw));
                    var commandData = JsonConvert.DeserializeObject <ApiResponseForCommandSearch>(result.Raw);
                    Assert.IsNotNull(commandData);
                    targetCommandId = commandData.commands.All(a => a.status_counts == null)
                                                ? commandData.commands.First().id
                                                : commandData.commands.First(f => f.status_counts.processed == 0 && f.status_counts.rejected == 0 && f.status_counts.pending > 0).id;
                }

                System.Threading.Thread.Sleep(500);
                using (var client = new M2XClient(_masterKey))
                {
                    var device       = client.Device(_testDevice.id);
                    var processParms = $"{{ \"reason\": \"Because I Can!\" }}";
                    var result       = await device.RejectCommand(targetCommandId, processParms);

                    Assert.IsNotNull(result);
                    Assert.IsFalse(result.Error);
                    Assert.IsFalse(result.ServerError);
                    Assert.IsNull(result.WebError);
                    Assert.IsTrue(string.IsNullOrWhiteSpace(result.Raw));
                }

                System.Threading.Thread.Sleep(500);
                using (var client = new M2XClient(_masterKey))
                {
                    var result = await client.Commands();

                    Assert.IsNotNull(result);
                    Assert.IsFalse(result.Error);
                    Assert.IsFalse(result.ServerError);
                    Assert.IsNull(result.WebError);
                    Assert.IsFalse(string.IsNullOrWhiteSpace(result.Raw));
                    ProcessCommandSearchResultFindTargetCommandAndCheckStatus(result.Raw, _testDevice.id, targetCommandId, "rejected");
                }
            }
            catch (Exception ex)
            {
                DestroyTestDevice();
                throw;
            }
        }
Ejemplo n.º 12
0
        public async Task CanAccess_MasterApiKey_AndSendCommands_ToSingleDevice_AndView_Single_DeviceCommand()
        {
            try
            {
                var commandName = "PHONE_HOME";
                using (var client = new M2XClient(_masterKey))
                {
                    var sendCommandParms = new StringBuilder($"{{ ");
                    sendCommandParms.Append($"\"name\": \"{commandName}\"");
                    sendCommandParms.Append($", ");
                    sendCommandParms.Append($"\"data\": {{ \"server_url\": \"https://m2x.att.com\" }}");
                    sendCommandParms.Append($", ");
                    sendCommandParms.Append($"\"targets\": {{ \"devices\": [\"{_testDevice.id}\"] }}");
                    sendCommandParms.Append($" }}");

                    var result = await client.SendCommand(sendCommandParms.ToString());

                    Assert.IsNotNull(result);
                    Assert.IsFalse(result.Error);
                    Assert.IsFalse(result.ServerError);
                    Assert.IsNull(result.WebError);
                    Assert.IsFalse(string.IsNullOrWhiteSpace(result.Raw));
                    Assert.AreEqual(Constants.M2X_Response_Success_Accepted, result.Raw);
                }

                System.Threading.Thread.Sleep(500);
                var targetCommandId = string.Empty;
                using (var client = new M2XClient(_masterKey))
                {
                    var device = client.Device(_testDevice.id);
                    var result = await device.Commands();

                    Assert.IsNotNull(result);
                    Assert.IsFalse(result.Error);
                    Assert.IsFalse(result.ServerError);
                    Assert.IsNull(result.WebError);
                    Assert.IsFalse(string.IsNullOrWhiteSpace(result.Raw));
                    var commandData = JsonConvert.DeserializeObject <ApiResponseForCommandSearch>(result.Raw);
                    Assert.IsNotNull(commandData);
                    targetCommandId = commandData.commands.First().id;
                }

                System.Threading.Thread.Sleep(500);
                using (var client = new M2XClient(_masterKey))
                {
                    var device = client.Device(_testDevice.id);
                    var result = await device.CommandDetails(targetCommandId);

                    Assert.IsNotNull(result);
                    Assert.IsFalse(result.Error);
                    Assert.IsFalse(result.ServerError);
                    Assert.IsNull(result.WebError);
                    Assert.IsFalse(string.IsNullOrWhiteSpace(result.Raw));

                    var commandDetail = JsonConvert.DeserializeObject <ApiResponseForCommandDetail>(result.Raw);
                    Assert.IsNotNull(commandDetail);
                    Assert.IsNotNull(commandDetail.data);
                    Assert.AreNotEqual(default(DateTime), commandDetail.sent_at);
                    Assert.AreEqual(commandName, commandDetail.name);
                }
            }
            catch (Exception ex)
            {
                DestroyTestDevice();
                throw;
            }
        }