コード例 #1
0
        public void SetDirectionTest()
        {
            FakeInvokeDeviceMethod serviceClient = new FakeInvokeDeviceMethod();

            // define parameters
            string deviceId     = "mydevice";
            string direction    = GateDirection.In.ToString();
            string deviceMethod = "ReceiveCommandGateChange";

            SetGateDirectionAction action = new SetGateDirectionAction(serviceClient, this.logger);

            var result = action.Run(deviceId, direction).Result;

            // Assert that serviceClient.invocations count is 1
            Assert.AreEqual(1, serviceClient.invocations.Count);

            cmdGateDirectionUpdate expectedResponse = new cmdGateDirectionUpdate()
            {
                Direction = GateDirection.In
            };

            // Assert that the method name details from the invocation is as expected
            StringAssert.AreEqualIgnoringCase(deviceMethod, serviceClient.invocations[0].method.MethodName);

            // get response to object
            cmdGateDirectionUpdate actualResponse = JsonConvert.DeserializeObject <cmdGateDirectionUpdate>(serviceClient.invocations[0].method.GetPayloadAsJson());

            // check the expected against the actuals
            Assert.AreEqual(deviceId, serviceClient.invocations[0].device, "Device IDs did not match");
            Assert.AreEqual(expectedResponse.Direction, actualResponse.Direction, "Direction did not match");
        }
コード例 #2
0
        /// <summary>
        /// This method is called by Azure IOT Hub and represents a direct method call to the device
        /// This is the response to 'SimulatedTicketSwipeOccured' request for a ticket validation
        /// </summary>
        private Task <MethodResponse> ReceiveCommandGateChange(MethodRequest methodRequest, object userContext)
        {
            var data = Encoding.UTF8.GetString(methodRequest.Data);
            cmdGateDirectionUpdate directionCommand = JsonConvert.DeserializeObject <cmdGateDirectionUpdate>(data);

            var json = JObject.Parse(data);

            Console.WriteLine("Executed direct method: " + methodRequest.Name);
            Console.WriteLine($"Transaction Id: {directionCommand.TransactionId}");
            Console.WriteLine($"MessageType: {directionCommand.MessageType} = Direction: {directionCommand.Direction}");
            Console.WriteLine();

            // update the gate direction
            this.SetDirectionAsync(directionCommand.Direction).Wait();

            // Acknowlege the direct method call with a 200 success message
            string result = "{\"result\":\"Executed direct method: " + methodRequest.Name + "\"}";

            return(Task.FromResult(new MethodResponse(Encoding.UTF8.GetBytes(result), 200)));
        }
コード例 #3
0
        public void TestGateReaderGateDirection()
        {
            FakeDeviceClient   fakeDeviceClient = new FakeDeviceClient(fakeTwin);
            FakeEventScheduler fakeScheduler    = new FakeEventScheduler();

            TestContext.WriteLine(">> Testing the Device's Gate Direction initialization ..");

            // create our test device
            GateReaderDevice device = new GateReaderDevice(deviceconfig, fakeDeviceClient, fakeScheduler);

            device.InitializeAsync().Wait();

            // gate direction should be "Out" as fakeTwin properties should override device config
            Assert.AreEqual(GateDirection.Out, device.Direction, $"Device gate direction is not correct, expected 'In', found {device.Direction}");

            TestContext.WriteLine(">> Testing the Device's Gate Direction change commands..");

            cmdGateDirectionUpdate commandGateDirectionUpdate = new cmdGateDirectionUpdate()
            {
                Direction = GateDirection.Out
            };

            // call the gate change direction
            TestContext.WriteLine(string.Empty);
            TestContext.WriteLine(">> Sending command to change gate direction when its already pointed that way, shouldn't log event");
            string        requestString = JsonConvert.SerializeObject(commandGateDirectionUpdate);
            MethodRequest methodRequest = new MethodRequest("ReceiveCommandGateChange", Encoding.UTF8.GetBytes(requestString));

            MethodResponse myresult = fakeDeviceClient.directMethods[1](methodRequest, null).Result;

            // gate direction should be "In" now
            Assert.AreEqual(GateDirection.Out, device.Direction, $"Device gate direction is not correct, expected 'Out', found {device.Direction}");

            // check the device twin properties
            // ensures that the device is using the device client SetDigitalTwinPropertyAsync method for twin property updates
            GateDirection twinDirection = (GateDirection)Enum.Parse(typeof(GateDirection), fakeTwin.Properties.Reported["GateDirection"].ToString());

            Assert.AreEqual(GateDirection.Out, twinDirection, $"Device gate direction in the device twin is not correct, expected 'Out', found {twinDirection}");
        }
コード例 #4
0
        public async Task <IActionResult> Run(string deviceId, string direction)
        {
            // create command message
            CloudToDeviceMethod methodInvocation = new CloudToDeviceMethod("ReceiveCommandGateChange")
            {
                ResponseTimeout = TimeSpan.FromSeconds(30)
            };

            try
            {
                cmdGateDirectionUpdate command = new cmdGateDirectionUpdate()
                {
                    Direction = (GateDirection)Enum.Parse(typeof(GateDirection), direction)
                };
                methodInvocation.SetPayloadJson(JsonConvert.SerializeObject(command));
            }
            catch (System.ArgumentException)
            {
                return(new BadRequestObjectResult("value of 'direction' must be 'In' or 'Out'"));
            }

            try
            {
                // Invoke the direct method and get the response from the simulated device.
                var response = await serviceClient.InvokeDeviceMethodAsync(deviceId, methodInvocation);

                Console.WriteLine("Response status: {0}, payload:", response.Status);
                Console.WriteLine(response.GetPayloadAsJson());
            }
            catch (Microsoft.Azure.Devices.Common.Exceptions.DeviceNotFoundException ex)
            {
                return(new BadRequestObjectResult($"Device '{deviceId}' either does not exist or is not responding: {ex.Message}"));
            }

            return(new OkObjectResult($"Device {deviceId} updated to new direction: {direction}"));
        }