/// <summary> /// Publishes the specified topic. /// </summary> /// <param name="topic">The topic.</param> private static async void Publish(string topic) { var cmd = new Command() { Type = CommandType.Action, Topic = topic }; await client.SendCommandAsync(cmd); }
private async void InitBrokerConnection(string brokerAddress, string clientId, string user, string pass, string secret) { try { client = NanomiteClient.CreateGrpcClient(brokerAddress, clientId); client.OnConnected = async() => { SubscriptionMessage subscriptionMessage = new SubscriptionMessage() { Topic = LogLevel.Debug.ToString() }; await client.SendCommandAsync(subscriptionMessage, StaticCommandKeys.Subscribe); subscriptionMessage = new SubscriptionMessage() { Topic = LogLevel.Info.ToString() }; await client.SendCommandAsync(subscriptionMessage, StaticCommandKeys.Subscribe); subscriptionMessage = new SubscriptionMessage() { Topic = LogLevel.Warning.ToString() }; await client.SendCommandAsync(subscriptionMessage, StaticCommandKeys.Subscribe); subscriptionMessage = new SubscriptionMessage() { Topic = LogLevel.Error.ToString() }; await client.SendCommandAsync(subscriptionMessage, StaticCommandKeys.Subscribe); }; client.OnCommandReceived = (cmd, c) => { switch (cmd.Topic) { case "Info": case "Debug": case "Warning": case "Error": Device.BeginInvokeOnMainThread(() => { var message = cmd.Data[0].CastToModel <LogMessage>(); LogEditor.Text += message.Message + Environment.NewLine; }); break; } }; await client.ConnectAsync(user, pass, secret, true); } catch (Exception ex) { throw ex; } }
/// <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; } }
private async void LoggingOnClicked(object sender, EventArgs e) { var cmd = new Nanomite.Core.Network.Common.Command() { Type = CommandType.Action, Topic = "SetLogLevel" }; LogLevelInfo logMessage = new LogLevelInfo() { Level = LogLevel.Debug.ToString(), }; cmd.Data.Add(Any.Pack(logMessage)); await client.SendCommandAsync(cmd); }
/// <summary> /// Logs a message to the server /// </summary> /// <param name="level">The level<see cref="LoggingLevel"/></param> /// <param name="message">The message<see cref="string"/></param> private static async void Log(LogLevel level, string message) { Debug.WriteLine(message); if (level >= LoggingLevel) { var cmd = new Command() { Type = CommandType.Action, Topic = level.ToString() }; LogMessage logMessage = new LogMessage() { Level = level.ToString(), Message = message }; cmd.Data.Add(Any.Pack(logMessage)); await client.SendCommandAsync(cmd); } }