public void Init_accessToken() { MyHttpClient a = MyHttpClient.getInstance(path_dir + "outgoing.CertwithKey.pkcs12", "IoM@1234", path_dir + "ca.jks", "Huawei@123"); Map paramLogin = new HashMap(); paramLogin.put("appId", appId); paramLogin.put("secret", secret); ResponseMsg b = a.sendMsg(url_login, paramLogin); HttpsUtil http_utils_test = new HttpsUtil(); string login_str = b.getContent().ToString(); System.Diagnostics.Debug.WriteLine(login_str); accessToken_str = extract_data_from_json(login_str, "\"accessToken\"", 0).Replace("\"", "");//删除双引号,这里获取的accessToken以后会用到 System.Diagnostics.Debug.WriteLine(accessToken_str); }
public void recThread() { Init_accessToken(); HttpsUtil http_utils_test = new HttpsUtil(); Map paramQueryDeviceData = new HashMap(); paramQueryDeviceData.put("pageNo", "0"); paramQueryDeviceData.put("appId", appId); paramQueryDeviceData.put("pageSize", "2048"); Map header = new HashMap(); header.put("Authorization", "Bearer " + accessToken_str); header.put("app_key", appId); while (true) { StreamClosedHttpResponse rec_http = null; dynamic json = null; try { rec_http = http_utils_test.doGetWithParasGetStatusLine(urlQueryDevices, paramQueryDeviceData, header); json = Newtonsoft.Json.Linq.JToken.Parse(rec_http.getContent()) as dynamic; } catch (Exception e) { LOG.Error("error", e);//////////////////////////////////////////////LOG记录 System.Threading.Thread.Sleep(3000); continue; } //int a = json.devices[0].services[0].data.DiZhiMa; int len_devices = 0; try { len_devices = json.totalCount;//获取本应用中共有几个节点 } catch (Exception e) { LOG.Error("error", e); ///////////////////////////////////////////LOG记录 LOG.Info("http content : " + rec_http.getContent()); ////////////////////////////////将电信平台的返回值也记录下来 Init_accessToken(); http_utils_test = new HttpsUtil(); paramQueryDeviceData = new HashMap(); paramQueryDeviceData.put("pageNo", "0"); paramQueryDeviceData.put("appId", appId); paramQueryDeviceData.put("pageSize", "2048"); header = new HashMap(); header.put("Authorization", "Bearer " + accessToken_str); header.put("app_key", appId); continue; } //返回一个需要更新的节点的列表 //遍历所有设备信息,如果发现有新的信息,就触发更新事件 for (int i = 0; i < len_devices; i++) { //判断时间新旧 DateTime temp_datetime = DateTime.ParseExact(json.devices[i].services[0].eventTime.ToString(), "yyyyMMddTHHmmssZ", null); //考虑到之后肯能多次使用这个节点的时间,所以先将其提取出来 if (DateTime.Compare(temp_datetime, DateTime_NewestData) > 0) { LOG.Info("receive new date : " + json.devices[i].services[0].data.DiZhiMa.ToString()); //如果需要更新,先将数据整理成所需格式,string数组 string[] temp_byte_array = new string[14]; temp_byte_array[0] = json.devices[i].services[0].data.DiZhiMa.ToString(); temp_byte_array[1] = json.devices[i].services[0].data.GongNengMa.ToString(); temp_byte_array[2] = json.devices[i].services[0].data.ZiJieShu.ToString(); temp_byte_array[3] = json.devices[i].services[0].data.JiQiMa.ToString(); temp_byte_array[4] = json.devices[i].services[0].data.QiTiLeiXing.ToString(); temp_byte_array[5] = json.devices[i].services[0].data.DanWei.ToString(); temp_byte_array[6] = json.devices[i].services[0].data.TangCeQiZhuangTai.ToString(); temp_byte_array[7] = json.devices[i].services[0].data.QiTiNongDu.ToString(); temp_byte_array[8] = json.devices[i].services[0].data.DiXian.ToString(); temp_byte_array[9] = json.devices[i].services[0].data.GaoXian.ToString(); temp_byte_array[10] = json.devices[i].services[0].data.DianLiang.ToString(); temp_byte_array[11] = json.devices[i].services[0].data.WenDuZhengShu.ToString(); temp_byte_array[12] = json.devices[i].services[0].data.WenDuXiaoShu.ToString(); temp_byte_array[13] = temp_datetime.ToString("yyyy-MM-dd HH:mm:ss"); //将整理后的数据传入rev_New中 rev_New(temp_byte_array); //触发事件之后,比较当前设备是否比暂存区的时间要新,如果新,就更新 if (DateTime.Compare(temp_datetime, temp_DateTime_NewestData) > 0) { temp_DateTime_NewestData = temp_datetime;//保证temp_DateTime_NewestData是当前所有节点时间和DateTime_NewestData中最新的时间 } } } //所有节点检测并更新完成之后,用暂存版的datetime更新DateTime_NewestData DateTime_NewestData = temp_DateTime_NewestData; System.Threading.Thread.Sleep(1500);//连续发送请求可能引起程序错误 } }