|
|
@@ -0,0 +1,131 @@
|
|
|
+package com.yunfei.adapter.tcpclient;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.yunfei.adapter.tcp.DefPacket;
|
|
|
+import org.tio.client.intf.ClientAioHandler;
|
|
|
+import org.tio.core.ChannelContext;
|
|
|
+import org.tio.core.Tio;
|
|
|
+import org.tio.core.TioConfig;
|
|
|
+import org.tio.core.exception.AioDecodeException;
|
|
|
+import org.tio.core.intf.Packet;
|
|
|
+
|
|
|
+import java.nio.ByteBuffer;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+public class JSSFClientHandle implements ClientAioHandler {
|
|
|
+ private String userId;
|
|
|
+
|
|
|
+ private String pwd;
|
|
|
+
|
|
|
+ public JSSFClientHandle(String userId, String pwd) {
|
|
|
+ this.userId = userId;
|
|
|
+ this.pwd = pwd;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Packet heartbeatPacket(ChannelContext channelContext) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Packet decode(ByteBuffer byteBuffer, int i, int i1, int i2, ChannelContext channelContext) throws AioDecodeException {
|
|
|
+ byte[] array = new byte[byteBuffer.remaining()];
|
|
|
+ String s = new String(array, StandardCharsets.UTF_8);
|
|
|
+ byteBuffer.get(array);
|
|
|
+ DefPacket defPackage = new DefPacket();
|
|
|
+ String message = new String(array, StandardCharsets.UTF_8);
|
|
|
+ defPackage.setBody(message.replace("/n/n", "").getBytes(StandardCharsets.UTF_8));
|
|
|
+ return defPackage;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ByteBuffer encode(Packet packet, TioConfig tioConfig, ChannelContext channelContext) {
|
|
|
+ DefPacket defaultPackage = (DefPacket) packet;
|
|
|
+ byte[] body = defaultPackage.getBody();
|
|
|
+ String s = new String(body, StandardCharsets.UTF_8);
|
|
|
+ s += "\n\n";
|
|
|
+ body = s.getBytes(StandardCharsets.UTF_8);
|
|
|
+ ByteBuffer buffer = ByteBuffer.allocate(body.length);
|
|
|
+ buffer.put(body);
|
|
|
+ return buffer;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handler(Packet packet, ChannelContext channelContext) throws Exception {
|
|
|
+ DefPacket defPackage = (DefPacket) packet;
|
|
|
+ byte[] body = defPackage.getBody();
|
|
|
+ String s = new String(body, StandardCharsets.UTF_8);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(s);
|
|
|
+ if (!jsonObject.containsKey("Function")) {
|
|
|
+ if (jsonObject.containsKey("MsgType")) {
|
|
|
+ if (jsonObject.getString("MsgType").equals("LogOn")) {
|
|
|
+ getDevices(channelContext);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ switch (jsonObject.getString("Function")) {
|
|
|
+ case "LoadInteractiveAndElement":
|
|
|
+ this.loadInteractiveAndElement(jsonObject, channelContext);
|
|
|
+ return;
|
|
|
+ case "LoadInteractiveValue":
|
|
|
+ this.loadInteractiveValue(jsonObject, channelContext);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+// bean.publish("/JSSF/" + channelContext.userid + "/property/report", paramsToInputs(jsonObject).toJSONString());
|
|
|
+ }
|
|
|
+
|
|
|
+ private void loadInteractiveValue(JSONObject jsonObject, ChannelContext channelContext) {
|
|
|
+ JSSF jssf = JSSFScanner.currentDevice.get(channelContext.userid);
|
|
|
+ jssf.getSfElement().forEach(x -> {
|
|
|
+ if (x.getInteractiveNumber().equals(jsonObject.getString("InteractiveNumber"))) {
|
|
|
+ JSONObject jsonObject1 = jsonObject.getJSONObject("Values");
|
|
|
+ Set<String> keySet = jsonObject1.keySet();
|
|
|
+ for (String key : keySet) {
|
|
|
+ if (jsonObject1.get(key) instanceof Boolean) {
|
|
|
+ x.setBooleanValue(jsonObject1.getBooleanValue(key));
|
|
|
+ } else if (jsonObject1.get(key) instanceof Integer) {
|
|
|
+ x.setIntValue(jsonObject1.getIntValue(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getDevices(ChannelContext channelContext) {
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+ obj.put("MsgType", "APPProgram");
|
|
|
+ obj.put("UserID", channelContext.userid);
|
|
|
+ obj.put("ProjectID", 1);
|
|
|
+ obj.put("Function", "LoadInteractiveAndElement");
|
|
|
+ DefPacket defPacket = new DefPacket();
|
|
|
+ System.err.println(JSON.toJSONString(obj));
|
|
|
+ defPacket.setBody(JSON.toJSONString(obj).getBytes(StandardCharsets.UTF_8));
|
|
|
+ Tio.send(channelContext, defPacket);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void loadInteractiveAndElement(JSONObject object, ChannelContext channelContext) {
|
|
|
+ JSONArray jsonArray = object.getJSONObject("DataTable").getJSONArray("Rows");
|
|
|
+ JSSF jssf = new JSSF();
|
|
|
+ jssf.setSfElement(new ArrayList<>());
|
|
|
+ jssf.setPassword(pwd);
|
|
|
+ jssf.setUserId(userId);
|
|
|
+ for (int i = 0; i < jsonArray.size(); i++) {
|
|
|
+ JSONArray jsonArray1 = jsonArray.getJSONObject(i).getJSONArray("Value");
|
|
|
+ if (jsonArray1.getString(5).equals("电磁阀")) {
|
|
|
+ if (jssf.getSfElement() == null || jssf.getSfElement().size() == 0) {
|
|
|
+ jssf.setSfElement(new ArrayList<>());
|
|
|
+ }
|
|
|
+ JSSF.Element element = new JSSF.Element();
|
|
|
+ element.setInteractiveNumber(jsonArray1.getString(2));
|
|
|
+ element.setIntValue(0);
|
|
|
+ element.setBooleanValue(false);
|
|
|
+ jssf.getSfElement().add(element);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ JSSFScanner.currentDevice.put(channelContext.userid, jssf);
|
|
|
+ }
|
|
|
+}
|