示例#1
0
        private async Task <MqttResult> SendMqttMessageAsync(String id, String username, String sendMsg, String cmdId)
        {
            TwainCloudServer tcs = null;

            if (map.ContainsKey(username))
            {
                tcs = map[username];
            }

            if (null == tcs || !tcs.isConnected())
            {
                tcs           = new TwainCloudServer(StringUtil.GetTopic_RecieveMsgFromDevice(username));
                map[username] = tcs;

                await tcs.Connect();
            }

            if (cmdId == null || cmdId.Length == 0)
            {
                tcs.ClearLastMsg();
            }

            DateTime startSend   = DateTime.Now;
            TimeSpan timeoutSpan = new TimeSpan(0, 0, 30);              // 30s

            if (sendMsg.IndexOf("x-privet-token:\\\"\\\"") > 0)
            {
                timeoutSpan = new TimeSpan(0, 0, 5);                  // 5s
            }

            await tcs.Send(StringUtil.GetTopic_SendMsgToDevice(id), sendMsg);

            MqttResult ret = new MqttResult();

            ret.bTimeout = false;
            while (tcs.GetMessage(cmdId) == null)
            {
                if (DateTime.Now - startSend > timeoutSpan)
                {
                    // timeout
                    ret.bTimeout    = true;
                    ret.errorString = "timed out.";
                    break;
                }
                await Task.Delay(100);
            }

            ret.result = tcs.GetMessage(cmdId);
            Logger.LogDebug("Get Message:" + ret);

            return(ret);
        }
示例#2
0
        public MqttResult SendMqttMessage(String id, String username, String sendMsg, String cmdId)
        {
            MqttResult ret = null;

            this.Logger.LogDebug("start sendMsg :" + sendMsg);
            Task.Run(async() =>
            {
                try
                {
                    ret = await SendMqttMessageAsync(id, username, sendMsg, cmdId);
                } catch (Exception e) {
                    this.Logger.LogError(e.Message);
                }
            }).Wait();

            this.Logger.LogDebug("sendMsg ok:" + sendMsg);

            return(ret);
        }