static void RegisterDeviceHandler() { Console.WriteLine("请输入设备的唯一名称:"); string deviceName = Console.ReadLine(); string url = serverUrl + "/register_device?" + Constant.productKeyParam + "=" + productKey + "&" + Constant.deviceIdParam + "=" + deviceName; 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; } Console.WriteLine("注册成功"); Console.WriteLine("Iot平台设备名称: " + wrapper.VALUE.NAME); Console.WriteLine("Iot平台设备秘钥(Secret):" + wrapper.VALUE.SECRET); }
static void QuerySensorHandler() { if (!LoginUtils.Instance.LoginStatus) { Console.WriteLine("请先登录"); return; } string url = serverUrl + "/query_sensor?" + 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 <List <CableSensor> > valueWrapper = JsonConvert.DeserializeObject <ValueWrapper <List <CableSensor> > >(responsStr); if (!valueWrapper.STATUS) { Console.WriteLine("查询失败:" + valueWrapper.ErrorMsg); return; } List <CableSensor> sensorList = valueWrapper.VALUE; if (sensorList == null) { Console.WriteLine("无测量单元"); return; } for (int i = 0; i < sensorList.Count; i++) { CableSensor sensor = sensorList[i]; Console.WriteLine("测量单元Id:" + sensor.SensorId); if (sensor.AliasName != null) { Console.WriteLine("测量单元别名:" + sensor.AliasName); } if (sensor.Comment != null) { Console.WriteLine("测量单元备注:" + sensor.Comment); } Console.WriteLine("所属通信单元Id:" + sensor.BindingDeviceId); } }
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("+++++++++++++++++++++++++++++++++++"); } } }