//------------------------------------------------------------------------------------------------------------------------ public void SendMessage(API.ApiMsg message) { var msg = new MqttMsg() { Payload = message.ToJSON(HtmlEncode: false) }; this.MqttClient.PublishMqttMsg(msg, this.mqttCloudBrokerPubTopicPrefix + PlegmaAPI.ApiMsgNames[message.GetType()]); }
//------------------------------------------------------------------------------------------------------------------------ public void SendResponse(API.ApiMsg response, int syncId) { var msg = new MqttMsg() { IsResponse = true, SyncId = syncId, Payload = response.ToJSON(HtmlEncode: false) }; this.MqttClient.PublishMqttMsg(msg, this.mqttCloudBrokerPubTopicPrefix + PlegmaAPI.ApiMsgNames[response.GetType()]); }
//------------------------------------------------------------------------------------------------------------------------ public Trsp SendRequest <Trsp>(API.ApiMsg request, TimeSpan?timeout = null) { //check if (request == null) { DebugEx.Assert("Request msg cannot be null"); return(default(Trsp)); } //set default timeout if (timeout == null) { timeout = TimeSpan.FromSeconds(15); } //generate ID int syncId = GetNewSyncId(); //create msg MqttMsg msg; try { msg = new MqttMsg() { IsRequest = true, SyncId = syncId, Payload = request.ToJSON(HtmlEncode: false) }; } catch (Exception ex) { DebugEx.Assert(ex, "Could not create request msg"); return(default(Trsp)); } //create waiter var w = new RpcBlocker(); lock (RpcPending) RpcPending.Add(syncId, w); //wait for response lock (w) { //publish msg if (request is Yodiwo.API.Warlock.WarlockApiMsg) { this.MqttClient.PublishMqttMsg(msg, this.mqttCloudBrokerPubTopicPrefix + Yodiwo.API.Warlock.WarlockAPI.ApiMsgNames[request.GetType()]); } else if (request is Yodiwo.API.Plegma.PlegmaApiMsg) { this.MqttClient.PublishMqttMsg(msg, this.mqttCloudBrokerPubTopicPrefix + PlegmaAPI.ApiMsgNames[request.GetType()]); } if (timeout == null) { Monitor.Wait(w); } else { Monitor.Wait(w, timeout.Value); } } //remove if found lock (RpcPending) RpcPending.Remove(syncId); //give response back if (w.Response == null) { return(default(Trsp)); } else { return((Trsp)w.Response); } }