public void mqttConnect(string clientId, string productKey, AliIotDeviceInfo deviceInfo) { string brokerAddress = productKey + "." + aliIotAddress; try { mqttClient = new MqttClient(brokerAddress); mqttClient.MqttMsgPublishReceived += mqttMsgReceived; mqttClient.ConnectionClosed += mqttConnectionClosed; string mqttClientId = clientId + "|securemode=3,signmethod=hmacsha1|"; string mqttUsername = deviceInfo.NAME + "&" + productKey; Dictionary <string, string> signParams = new Dictionary <string, string>(); signParams.Add("productKey", productKey); signParams.Add("deviceName", deviceInfo.NAME); signParams.Add("clientId", clientId); string mqttPassword = SignUtil.sign(signParams, deviceInfo.SECRET); mqttClient.Connect(mqttClientId, mqttUsername, mqttPassword); string subTopic = "/" + productKey + "/" + deviceInfo.NAME + "/user/get"; mqttClient.Subscribe(new string[] { subTopic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }); } catch (Exception ex) { throw ex; } }
static void LoginHandler() { Console.WriteLine("请输入用户名"); string userName = Console.ReadLine(); Console.WriteLine("请输入密码"); string password = Console.ReadLine(); string url = serverUrl + "/login?" + Constant.productKeyParam + "=" + productKey + "&" + Constant.userNameParam + "=" + userName + "&" + Constant.passwordParam + "=" + password; string statusCode; string responsStr = HttpUtils.Instance.CallGet(url, out statusCode); if (!statusCode.Equals("OK")) { Console.WriteLine("登录失败,请确认网络连接"); return; } ValueWrapper <AliIotDeviceInfo> wrapper = JsonConvert.DeserializeObject <ValueWrapper <AliIotDeviceInfo> >(responsStr); if (!wrapper.STATUS) { Console.WriteLine("登录失败: " + wrapper.ErrorMsg); return; } AliIotDeviceInfo deviceInfo = wrapper.VALUE; try { mqttService.mqttConnect(userName, productKey, deviceInfo); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine("连接Iot平台失败"); return; } LoginUtils.Instance.LoginStatus = true; LoginUtils.Instance.DeviceInfo = deviceInfo; LoginUtils.Instance.UserName = userName; Console.WriteLine("登录成功"); }
static void QueryBoundDeviceHandler() { if (!LoginUtils.Instance.LoginStatus) { Console.WriteLine("请先登录"); return; } string url = serverUrl + "/query_bound_device?" + Constant.productKeyParam + "=" + productKey + "&" + Constant.userNameParam + "=" + LoginUtils.Instance.UserName; string statusCode; string responsStr = HttpUtils.Instance.CallGet(url, out statusCode); if (!statusCode.Equals("OK")) { Console.WriteLine("查询失败,请确认网络连接"); return; } ValueWrapper <Dictionary <string, CableDevice> > valueWrapper = JsonConvert.DeserializeObject <ValueWrapper <Dictionary <string, CableDevice> > >(responsStr); if (!valueWrapper.STATUS) { Console.WriteLine("查询失败: " + valueWrapper.ErrorMsg); return; } Dictionary <string, CableDevice> deviceMap = valueWrapper.VALUE; if (deviceMap == null || deviceMap.Count == 0) { Console.WriteLine("该用户暂未绑定设备"); return; } string[] deviceIdArray = deviceMap.Keys.ToArray <string>(); foreach (string deviceId in deviceIdArray) { CableDevice cableDevice = null; deviceMap.TryGetValue(deviceId, out cableDevice); if (cableDevice != null) { Console.WriteLine("+++++++++++++++++++++++++++++++++++"); Console.WriteLine("通信设备ID号: " + deviceId); string aliasName = cableDevice.AliasName; string comment = cableDevice.Comment; if (aliasName == null) { aliasName = ""; } if (comment == null) { comment = ""; } Console.WriteLine("设备别名:" + aliasName); Console.WriteLine("设备备注:" + comment); AliIotDeviceInfo iotDeviceInfo = cableDevice.IotDeviceInfo; if (iotDeviceInfo != null) { Console.WriteLine("设备在线状态:" + iotDeviceInfo.STATUS); } Console.WriteLine("+++++++++++++++++++++++++++++++++++"); } } }