|
@@ -0,0 +1,263 @@
|
|
|
+package org.jeecg.modules.webaccess.service;
|
|
|
+
|
|
|
+import net.sf.json.JSONArray;
|
|
|
+import net.sf.json.JSONObject;
|
|
|
+import org.apache.commons.codec.binary.Base64;
|
|
|
+import org.apache.commons.collections.map.HashedMap;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 调用webaccess的接口
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class WebAccessService {
|
|
|
+ @Autowired
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+
|
|
|
+// @Autowired
|
|
|
+// private ISysConfigService configService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 登录
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public JSONObject login() {
|
|
|
+ String paramValues = "";
|
|
|
+// String url = configService.selectConfigByKey("ems.webaccess.login");;
|
|
|
+ String url = "";
|
|
|
+ JSONObject result = httpRequest(url, null);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取得工程下的测点(Tag)信息
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public JSONObject tagList() {
|
|
|
+// String getTagValueUrl = configService.selectConfigByKey("ems.webaccess.GetTagList");
|
|
|
+ String getTagValueUrl = "";
|
|
|
+ JSONObject result = httpRequest(getTagValueUrl, null);
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取得测点(Tag)的量测值
|
|
|
+ *
|
|
|
+ * @param jsonObject
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getTagNameValues(JSONObject jsonObject) {
|
|
|
+ String paramValues = "";
|
|
|
+// String getTagValueUrl = configService.selectConfigByKey("ems.webaccess.GetTagValue");
|
|
|
+ String getTagValueUrl = "";
|
|
|
+ JSONObject result = httpRequest(getTagValueUrl, jsonObject);
|
|
|
+ JSONArray jsonArray = result.getJSONArray("Values");
|
|
|
+
|
|
|
+ for (int i = 0; i < jsonArray.size(); i++) {
|
|
|
+ if (i == 0) {
|
|
|
+ paramValues = jsonArray.getJSONObject(i).getString("Value");
|
|
|
+ } else {
|
|
|
+ paramValues = paramValues + ',' + jsonArray.getJSONObject(i).getString("Value");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return paramValues;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更改测点(Tag)的量测值
|
|
|
+ *
|
|
|
+ * @param jsonObject
|
|
|
+ * @param i 是循环开始的数字,如果是0,代表循环更改5次,防止某次更改不成功(我猜的)
|
|
|
+ * @return
|
|
|
+ * @throws InterruptedException
|
|
|
+ */
|
|
|
+ public Boolean setTagNameValues(JSONObject jsonObject, int i) throws InterruptedException {
|
|
|
+ if (i > 5) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ int cycle = i + 1;
|
|
|
+ Map<String, Double> map = new HashedMap();
|
|
|
+ JSONArray jsonArray = jsonObject.getJSONArray("Tags");
|
|
|
+
|
|
|
+ for (Object item : jsonArray) {
|
|
|
+ JSONObject itemInfor = JSONObject.fromObject(item);
|
|
|
+ map.put(itemInfor.getString("Name"), itemInfor.getDouble("Value"));
|
|
|
+ }
|
|
|
+// String getTagValueUrl = configService.selectConfigByKey("ems.webaccess.GetTagValue");
|
|
|
+// String setTagValueUrl = configService.selectConfigByKey("ems.webaccess.SetTagValue");
|
|
|
+ String getTagValueUrl = "";
|
|
|
+ String setTagValueUrl = "";
|
|
|
+
|
|
|
+ JSONObject result = httpRequest(setTagValueUrl, jsonObject);
|
|
|
+ Thread.sleep(500);
|
|
|
+
|
|
|
+ if (result.getInt("Ret") >= 0) {
|
|
|
+ result = httpRequest(getTagValueUrl, jsonObject);
|
|
|
+ JSONArray resJsonArray = result.getJSONArray("Values");
|
|
|
+ JSONArray paramJsonArry = new JSONArray();
|
|
|
+ System.out.println("rrr" + resJsonArray);
|
|
|
+
|
|
|
+ for (Object item : resJsonArray) {
|
|
|
+ JSONObject itemJson = JSONObject.fromObject(item);
|
|
|
+
|
|
|
+ if (Math.abs(itemJson.getDouble("Value") - map.get(itemJson.getString("Name"))) > 0.01) {
|
|
|
+ JSONObject paramJson = new JSONObject();
|
|
|
+ paramJson.put("Name", itemJson.getString("Name"));
|
|
|
+ paramJson.put("Value", map.get(itemJson.getString("Name")));
|
|
|
+ paramJsonArry.add(paramJson);
|
|
|
+ } else {
|
|
|
+ map.remove(itemJson.getString("Name"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (paramJsonArry.size() > 0) {
|
|
|
+ jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("Tags", paramJsonArry);
|
|
|
+ System.out.println("res" + jsonObject);
|
|
|
+
|
|
|
+ setTagNameValues(jsonObject, cycle);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更改文字测点(Tag)的量测值
|
|
|
+ *
|
|
|
+ * @param jsonObject
|
|
|
+ * @param i
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Boolean setTagNameTextValues(JSONObject jsonObject, int i) {
|
|
|
+ if (i > 5) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ int cycle = i + 1;
|
|
|
+ Map<String, String> map = new HashedMap();
|
|
|
+ JSONArray jsonArray = jsonObject.getJSONArray("Tags");
|
|
|
+ for (Object item : jsonArray) {
|
|
|
+ JSONObject itemInfor = JSONObject.fromObject(item);
|
|
|
+ map.put(itemInfor.getString("Name"), itemInfor.getString("Value"));
|
|
|
+ }
|
|
|
+// String getTagValueTextUrl = configService.selectConfigByKey("ems.webaccess.GetTagValueText");
|
|
|
+// String setTagValueTextUrl = configService.selectConfigByKey("ems.webaccess.SetTagValueText");
|
|
|
+ String getTagValueTextUrl = "";
|
|
|
+ String setTagValueTextUrl = "";
|
|
|
+
|
|
|
+ JSONObject result = httpRequest(setTagValueTextUrl, jsonObject);
|
|
|
+
|
|
|
+ if (result.getInt("Ret") >= 0) {
|
|
|
+ result = httpRequest(getTagValueTextUrl, jsonObject);
|
|
|
+ JSONArray resJsonArray = result.getJSONArray("Values");
|
|
|
+ JSONArray paramJsonArry = new JSONArray();
|
|
|
+ System.out.println(resJsonArray);
|
|
|
+
|
|
|
+ for (Object item : resJsonArray) {
|
|
|
+ JSONObject itemJson = JSONObject.fromObject(item);
|
|
|
+ if (!itemJson.getString("Value").equals(map.get(itemJson.getString("Name")))) {
|
|
|
+ JSONObject paramJson = new JSONObject();
|
|
|
+ paramJson.put("Name", itemJson.getString("Name"));
|
|
|
+ paramJson.put("Value", map.get(itemJson.getString("Name")));
|
|
|
+ paramJsonArry.add(paramJson);
|
|
|
+ } else {
|
|
|
+ map.remove(itemJson.getString("Name"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (paramJsonArry.size() > 0) {
|
|
|
+ jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("Tags", paramJsonArry);
|
|
|
+
|
|
|
+ setTagNameTextValues(jsonObject, cycle);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 调用webaccess接口
|
|
|
+ *
|
|
|
+ * @param url
|
|
|
+ * @param jsonObject
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private JSONObject httpRequest(String url, JSONObject jsonObject) {
|
|
|
+// String plainCreds = configService.selectConfigByKey("ems.webaccess.AccountPwd");
|
|
|
+ String plainCreds = "";
|
|
|
+ byte[] plainCredsBytes = plainCreds.getBytes();
|
|
|
+ byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
|
|
|
+ String base64Creds = new String(base64CredsBytes);
|
|
|
+
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
+ headers.setContentType(type);
|
|
|
+ headers.add("Authorization", "Basic " + base64Creds);
|
|
|
+ HttpEntity<String> formEntity;
|
|
|
+ JSONObject result;
|
|
|
+ if (jsonObject == null) {
|
|
|
+ formEntity = new HttpEntity<String>(null, headers);
|
|
|
+ result = restTemplate.exchange(url, HttpMethod.GET, formEntity, JSONObject.class).getBody();
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ formEntity = new HttpEntity<String>(jsonObject.toString(), headers);
|
|
|
+ result = restTemplate.postForEntity(url, formEntity, JSONObject.class).getBody();
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 组成获取参数值的信息
|
|
|
+// * @param tags 参数名
|
|
|
+// * @return
|
|
|
+// */
|
|
|
+// public JSONObject generateGetObject(List<SysWaLog> tags) {
|
|
|
+// JSONArray list = new JSONArray();
|
|
|
+// JSONObject rtn = new JSONObject();
|
|
|
+//
|
|
|
+// for (SysWaLog tag : tags) {
|
|
|
+// JSONObject sub = new JSONObject();
|
|
|
+// sub.put("Name", tag.getTagname());
|
|
|
+// list.add(sub);
|
|
|
+// }
|
|
|
+// rtn.put("Tags", list);
|
|
|
+//
|
|
|
+// return rtn;
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 组成设置参数值的信息
|
|
|
+// * @param tags 参数列表
|
|
|
+// * @return
|
|
|
+// */
|
|
|
+// public JSONObject generateSetObject(List<SysWaLog> tags) {
|
|
|
+// JSONArray list = new JSONArray();
|
|
|
+// JSONObject rtn = new JSONObject();
|
|
|
+//
|
|
|
+// if (tags != null && tags.size() > 0) {
|
|
|
+// for (SysWaLog tag : tags) {
|
|
|
+// JSONObject sub = new JSONObject();
|
|
|
+// sub.put("Name", tag.getTagname());
|
|
|
+// sub.put("Value", tag.getTagvalue());
|
|
|
+// list.add(sub);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// rtn.put("Tags", list);
|
|
|
+//
|
|
|
+// return rtn;
|
|
|
+// }
|
|
|
+}
|