public async Task SetPercentage_ForMultiPortDevice_TurnOn_Port1_SendsTurnOnPort1()
        {
            // Arrange
            string messageId = "Message12";
            var    device    = new DeviceDto {
                Id = "Endpoint1"
            };
            var deviceAndPort = new DeviceAndPort(device, 1);
            var request       = BuildRequest(messageId, "SetPercentage", deviceAndPort.ToString());

            List <DeviceDto> devices = new List <DeviceDto>
            {
                new DeviceDto
                {
                    DisplayName = "Device 1"
                }
            };

            var devicesClient      = new FakeDevicesClient(devices);
            var measurementsClient = new FakeMeasurementsClient();
            var statusClient       = new FakeStatusClient();

            var controller = new PercentageController(devicesClient, measurementsClient, statusClient);

            // Act
            PowerControlResponse response = (PowerControlResponse)await controller.HandleAlexaRequest(request, null);

            // Assert
            Assert.IsNotNull(response);
            Assert.AreEqual("@Switch Set percentage 110 port-1", statusClient.SentMessage);
        }
        public void EndpointWithPort_HasExpectedIdAndPort()
        {
            // Act
            DeviceAndPort deviceAndPort = new DeviceAndPort("C6CBE649-16BD-4A20-A0DB-941E2296818C#port 2#");

            // Assert
            Assert.AreEqual("C6CBE649-16BD-4A20-A0DB-941E2296818C", deviceAndPort.Id);
            Assert.AreEqual("port 2", deviceAndPort.Port);
            Assert.AreEqual("C6CBE649-16BD-4A20-A0DB-941E2296818C#port 2#", deviceAndPort.ToString());
        }
        /// <summary>
        ///  See https://github.com/alexa/alexa-smarthome/blob/master/sample_messages/StateReport/StateReport.json
        /// </summary>
        /// <param name="requestDirective"></param>
        /// <returns></returns>
        private async Task <StateReportResponse> BuildDeviceStateReport(Directive requestDirective)
        {
            string token = requestDirective.Endpoint.Scope.Token;

            if (string.IsNullOrWhiteSpace(token))
            {
                throw new ArgumentException("Missing token");
            }

            var reponse = new StateReportResponse
            {
                Context = new Context
                {
                    Properties = new List <Property>(),
                },
                Event = ConstructReponseEvent(requestDirective, "StateReport"),
            };

            // Always add endpoint health...
            reponse.Context.Properties.Add(new ValueValueProperty
            {
                Namespace = "Alexa.EndpointHealth",
                Name      = "connectivity",
                Value     = new ValuePropertyValue {
                    Value = "ON"
                },
                TimeOfSample = DateTime.UtcNow,
                UncertaintyInMilliseconds = 600,
            });

            var deviceAndPort = new DeviceAndPort(requestDirective.Endpoint.EndpointId);
            var device        = await _devicesClient.GetDeviceAsync(token, deviceAndPort.Id);

            if (device == null)
            {
                return(reponse);
            }

            foreach (var supportedInterface in SupportedInterfaces)
            {
                if (device.Tags.Contains(supportedInterface))
                {
                    // This will itterate through the appropriate controllers
                    // and get them to create the required properties for the interface
                    // they support.
                    List <Property> properties = await CreateProperties(supportedInterface, token, device, deviceAndPort.Port);

                    reponse.Context.Properties.AddRange(properties);
                }
            }

            return(reponse);
        }
        public void DeviceWithoutPort_HasExpectedProperties()
        {
            // Arrange
            DeviceDto device = new DeviceDto {
                Id = "C6CBE649-16BD-4A20-A0DB-941E2296818C"
            };

            // Act
            DeviceAndPort deviceAndPort = new DeviceAndPort(device);

            // Assert
            Assert.AreEqual("C6CBE649-16BD-4A20-A0DB-941E2296818C", deviceAndPort.Id);
            Assert.AreEqual("", deviceAndPort.Port);
            Assert.AreEqual("C6CBE649-16BD-4A20-A0DB-941E2296818C", deviceAndPort.ToString());
        }
        public void DeviceWithPort_HasExpectedId()
        {
            // Arrange
            DeviceDto device = new DeviceDto {
                Id = "C6CBE649-16BD-4A20-A0DB-941E2296818C"
            };

            // Act
            DeviceAndPort deviceAndPort = new DeviceAndPort(device, 2);

            // Assert
            Assert.AreEqual("C6CBE649-16BD-4A20-A0DB-941E2296818C", deviceAndPort.Id);
            Assert.AreEqual("port-2", deviceAndPort.Port);
            Assert.AreEqual("C6CBE649-16BD-4A20-A0DB-941E2296818C#port-2#", deviceAndPort.ToString());
        }
Пример #6
0
        private async Task <object> CreateResponse(SmartHomeRequest request, string token)
        {
            var deviceAndPort = new DeviceAndPort(request.Directive.Endpoint.EndpointId);
            var device        = await GetDevice(token, deviceAndPort);

            var properties = await CreateProperties(token, device, deviceAndPort.Port);

            return(new BrightnessControlResponse
            {
                Context = new Context
                {
                    Properties = properties,
                },
                Event = ConstructReponseEvent(request.Directive, "Response"),
            });
        }
Пример #7
0
        protected async Task SendDeviceStatusMessage(SmartHomeRequest request, string token, string messageFragment)
        {
            // Get the device we are targetting.
            DeviceAndPort deviceAndPort = request.Directive.Endpoint.GetDeviceAndPort();
            //DeviceDto device = await _devicesClient.GetDeviceAsync(token, deviceAndPort.Id);

            string username = request.Directive.Endpoint.Cookie.Username;

            // Ideall we would just send a named command "Turn On"
            // once the commands interface in inplace......

            // For now, use a status message.
            string message = string.Format("@{0} {1} {2}", username, messageFragment, deviceAndPort.Port);

            message = message.Trim();
            await _statusClient.PostStatusMessageAsync(token, message);
        }
Пример #8
0
        private Endpoint CreateDeviceEndpoint(DeviceDto device, int?port = null)
        {
            string deviceName = GetPortName(device, port);

            if (string.IsNullOrWhiteSpace(deviceName))
            {
                return(null);
            }

            DeviceAndPort deviceAndPort = new DeviceAndPort(device, port);

            var endpoint = new Endpoint
            {
                EndpointId        = deviceAndPort.ToString(),
                FriendlyName      = deviceName,
                Description       = device.Description,
                ManufacturerName  = "Tinamous",
                DisplayCategories = new List <string>(),
                Cookie            = new Cookie
                {
                    Username = device.UserName,
                    //PortNumber = port,
                    DeviceId = device.Id,
                },
                Capabilities = new List <Capability>
                {
                    new Capability
                    {
                        Type      = "AlexaInterface",
                        Interface = "Alexa",
                        Version   = "3",
                    },
                }
            };

            AddDisplayCategories(device, endpoint);

            AddDeviceCapabilities(device, endpoint, deviceAndPort.Port);

            return(endpoint);
        }
Пример #9
0
 protected Task <DeviceDto> GetDevice(string token, DeviceAndPort deviceAndPort)
 {
     return(_devicesClient.GetDeviceAsync(token, deviceAndPort.Id));
 }