Exemplo n.º 1
0
        /// <summary>
        /// Runs the service.
        /// </summary>
        /// <param name="handler">The smarthome handler.</param>
        /// <returns>a task</returns>
        private static async Task InitBrokerConnection(SmarthomeHandler handler, string brokerAddress, string clientId, string user, string pass, string secret)
        {
            try
            {
                client             = NanomiteClient.CreateGrpcClient(brokerAddress, clientId);
                client.OnConnected = async() =>
                {
                    SubscriptionMessage subscriptionMessage = new SubscriptionMessage()
                    {
                        Topic = "LivingRoomLightOn"
                    };
                    await client.SendCommandAsync(subscriptionMessage, StaticCommandKeys.Subscribe);

                    subscriptionMessage = new SubscriptionMessage()
                    {
                        Topic = "LivingRoomLightOff"
                    };
                    await client.SendCommandAsync(subscriptionMessage, StaticCommandKeys.Subscribe);

                    subscriptionMessage = new SubscriptionMessage()
                    {
                        Topic = "SetLogLevel"
                    };
                    await client.SendCommandAsync(subscriptionMessage, StaticCommandKeys.Subscribe);
                };
                client.OnCommandReceived = (cmd, c) =>
                {
                    switch (cmd.Topic)
                    {
                    case "LivingRoomLightOn":
                        handler.TurnLightOnLivingRoom();
                        break;

                    case "LivingRoomLightOff":
                        handler.TurnLightOffLivingRoom();
                        break;

                    case "SetLogLevel":
                        var level = cmd.Data[0].CastToModel <LogLevelInfo>()?.Level;
                        LoggingLevel = (LogLevel)System.Enum.Parse(typeof(LogLevel), level);
                        break;
                    }
                };
                await client.ConnectAsync(user, pass, secret, true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// The Init
        /// </summary>
        /// <param name="brokerAddress">The brokerAddress<see cref="string"/></param>
        /// <param name="user">The user<see cref="string"/></param>
        /// <param name="pass">The pass<see cref="string"/></param>
        /// <param name="secret">The secret<see cref="string"/></param>
        /// <param name="smarthHomeUser">The smarthHomeUser<see cref="string"/></param>
        /// <param name="smartHomePass">The smartHomePass<see cref="string"/></param>
        /// <param name="livingRoomId">The livingRoomId<see cref="string"/></param>
        public static async Task Run(string brokerAddress, string user, string pass, string secret,
                                     string smarthHomeUser, string smartHomePass, string livingRoomId)
        {
            LoggingLevel  = LogLevel.Info;
            BrokerAddress = brokerAddress;
            User          = user;
            Pass          = pass;
            SmartHomeUser = smarthHomeUser;
            SmartHomePass = smartHomePass;
            LivingRoomId  = livingRoomId;
            Secret        = secret;

            // Init module to interact with smart home
            smarthomeHandler = new SmarthomeHandler(SmartHomeUser, SmartHomePass, LivingRoomId);

            /// Init connection to broker
            await InitBrokerConnection(smarthomeHandler, BrokerAddress, User, User, Pass, Secret);

            // reinit connection to smart home in the given intervall
            serviceGuard = new Timer(Connect, null, 1000, 1000 * 60 * 60);
        }