Index: ssts-maintain/src/main/java/com/forgon/disinfectsystem/maintain/device/service/DeviceDataByCommunicationManager.java =================================================================== diff -u --- ssts-maintain/src/main/java/com/forgon/disinfectsystem/maintain/device/service/DeviceDataByCommunicationManager.java (revision 0) +++ ssts-maintain/src/main/java/com/forgon/disinfectsystem/maintain/device/service/DeviceDataByCommunicationManager.java (revision 25320) @@ -0,0 +1,22 @@ +package com.forgon.disinfectsystem.maintain.device.service; + +import com.forgon.disinfectsystem.entity.basedatamanager.deviceinterface.DeviceInterface; +import com.forgon.disinfectsystem.entity.washanddisinfectmanager.washData.WashData; + + +/** + * 根据通信协议获取设备信息 + * @author YanWeiNing + * + */ +public interface DeviceDataByCommunicationManager { + + /** + * 根据通信协议设置清洗机 + * @param devInteface + * @param washData + * @param timeoutSeconds + */ + public void readAndSaveRecord(DeviceInterface devInteface,WashData washData,int timeoutSeconds); + +} Index: ssts-web/src/main/resources/spring/applicationContext-disinfectsystem-service.xml =================================================================== diff -u -r25144 -r25320 --- ssts-web/src/main/resources/spring/applicationContext-disinfectsystem-service.xml (.../applicationContext-disinfectsystem-service.xml) (revision 25144) +++ ssts-web/src/main/resources/spring/applicationContext-disinfectsystem-service.xml (.../applicationContext-disinfectsystem-service.xml) (revision 25320) @@ -1536,6 +1536,18 @@ + + + + + + + + + + @@ -2292,6 +2304,7 @@ + Index: forgon-core/src/main/java/com/forgon/device/ModbusControllerManager.java =================================================================== diff -u --- forgon-core/src/main/java/com/forgon/device/ModbusControllerManager.java (revision 0) +++ forgon-core/src/main/java/com/forgon/device/ModbusControllerManager.java (revision 25320) @@ -0,0 +1,294 @@ +/** + * + */ +package com.forgon.device; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.net.Socket; + +import net.sf.json.JSONObject; + +import org.apache.commons.lang3.StringUtils; +import org.apache.log4j.Logger; + +import com.forgon.tools.Tools; +import com.forgon.tools.date.DateTools; + + +public class ModbusControllerManager { + private static final Logger logger = Logger.getLogger(ModbusControllerManager.class); + + //用于JSONObject对应WashData的属性 + public static final String WASHDATA_T1TEMP = "t1Temp"; + public static final String WASHDATA_T2TEMP = "t2Temp"; + public static final String WASHDATA_A0 = "A0"; + public static final String WASHDATA_TIME = "time"; + //美雅洁-modbus起始地址常量(新疆军区总医院) + public static final byte WASHDATA_T1TEMP_ADDRESS_XJJQ = (byte)0x14;//清洗温度 + public static final byte WASHDATA_T2TEMP_ADDRESS_XJJQ = (byte)0x0F;//消毒温度 + public static final byte WASHDATA_A0_ADDRESS_XJJQ = (byte)0x18;//AO + // + + private class ModbusController{ + + private Socket socket = null; + private OutputStream mOutputStream = null; + private InputStream mInputStream = null; + private int responseStrLen; + +///////////////////////////////////////////////////////////// + /** + * 发送modbus指令获得美雅洁的清洗机数据 + * @param ip ip地址 + * @param post 端口号 + * @param deviceIdStr 设备id + * @param timeout 超时时间 + * @return JSONObject 清洗机数据 + */ + public JSONObject getWashData_MeiYaJie(String ip,int post,String deviceIdStr,int timeout){ + try { + //1、连接tcp + if(reConnectIfNeeded()){ + if(!tcpConnect(ip, post, timeout)){ + throw new RuntimeException("连接modbus tcp失败"); + } + } + //2、设值解析时截取的长度 + setResponseStrLen(2); + //3、发送指令获得数据 + JSONObject reusltData = new JSONObject(); + //获取t1temp + logger.debug(String.format("开始获取数据:%s ====》",WASHDATA_T1TEMP)); + String t1temp = sendCommandAndGetData_MeiYaJie(deviceIdStr,WASHDATA_T1TEMP_ADDRESS_XJJQ); + //获取t2temp + logger.debug(String.format("开始获取数据:%s ====》",WASHDATA_T2TEMP)); + String t2temp = sendCommandAndGetData_MeiYaJie(deviceIdStr,WASHDATA_T2TEMP_ADDRESS_XJJQ); + //获取A0 + logger.debug(String.format("开始获取数据:%s ====》",WASHDATA_A0)); + String a0 = sendCommandAndGetData_MeiYaJie(deviceIdStr,WASHDATA_A0_ADDRESS_XJJQ); + //获取time:取最后一次指令获取后的时间Str + String time = DateTools.getCurrentDayByFormat(DateTools.COMMON_DATE_HMS); + //4、设置返回的JSONObject对象 + reusltData.put(WASHDATA_T1TEMP, t1temp); + reusltData.put(WASHDATA_T2TEMP, t2temp); + reusltData.put(WASHDATA_A0, a0); + reusltData.put(WASHDATA_TIME, time); + return reusltData; + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e.getMessage()); + }finally{ + //关闭连接 + close(); + } + + } + + /** + * 创建指令,放送指令,读取并解析返回数据 + * @param deviceIdStr 设备id + * @param inintialAddress 起始地址 + * @return 解析后的返回数据 + */ + private String sendCommandAndGetData_MeiYaJie(String deviceIdStr,byte inintialAddress){ + //1、生成获取美雅洁的modbus tcp指令 + byte[] command = buildSendCommand_MeiYaJie(deviceIdStr,inintialAddress); + //2、发送指令 + if(!sendCommand(command)){ + throw new RuntimeException("发送美雅洁的modbus tcp指令:"+Tools.Bytes2HexString(command, command.length)+"失败"); + }; + //3、读取并解析反馈 + String setAntReponse = getResponseData_MeiYaJie(); + if(StringUtils.isBlank(setAntReponse)){ + throw new RuntimeException("获取返回数据失败"); + } + return setAntReponse; + } + + /** + * 创建美雅洁指令 + * @param deviceId + * @return + */ + private byte[] buildSendCommand_MeiYaJie(String deviceIdStr,byte inintialAddress) { +// byte deviceId= (byte)Integer.parseInt(hexDeviceIdStr,16);//设备id + int deviceId= Integer.parseInt(deviceIdStr);//设备id + byte[] command = new byte[]{ + (byte)0x00, + (byte)0x01, + (byte)0x00, + (byte)0x00, + (byte)0x00, + (byte)0x06, + (byte)deviceId,//设备id + (byte)0x04, + (byte)0x00, + inintialAddress,//起始地址 + (byte)0x00, + (byte)0x01 + }; +// logger.debug("构造命令 ===> "+Tools.Bytes2HexString(command, command.length)); + return command; + } + + /** + * 读取美雅洁返回的信息 + * @return + */ + private String getResponseData_MeiYaJie() { + byte[] buf = new byte[1024]; + int a = 0; + while (true) { + try { + a = mInputStream.read(buf); + if (a > 0) { + String receviceString = Tools.Bytes2HexString(buf, a); + logger.debug("返回原始数据 ===> " + receviceString); + return analysisReuslt_MeiYaJie(receviceString); + } + } catch (IOException e) { + e.printStackTrace(); + close(); + return ""; + } + } + } + + /** + * 解析获得的modbus原始数据 + * @param receviceString 解析截取的长度 + * @return 解析后的数据 + */ + private String analysisReuslt_MeiYaJie(String receviceString){ + String analysisReusltStr = null; + if(StringUtils.isNotEmpty(receviceString)){ + int receviceStringLength = receviceString.length(); + if(receviceStringLength > responseStrLen){ +// logger.debug(String.format("解析返回数据: (原始数据)===> %s",receviceString)); + //1、截取最后两位获得 + String temperatureHex = receviceString.substring(receviceStringLength - responseStrLen,receviceStringLength); + //2、16进制转10进制 + analysisReusltStr = Integer.valueOf(temperatureHex,16).toString(); + logger.debug(String.format("解析返回数据:(解析后)===> %s",analysisReusltStr)); + } + } + return analysisReusltStr; + } +////////////////////////////////////// +//**********************************// + /** + * 设置解析时截取的位置 + * @param responseStrLen + */ + private void setResponseStrLen(int responseStrLen){ + this.responseStrLen = responseStrLen; + } + + /** + * 是否需要重连接 + */ + private boolean reConnectIfNeeded() { + if (socket == null || mInputStream == null || mOutputStream == null){ + return true; + }else if ((socket.isClosed() || !socket.isConnected() || socket.isOutputShutdown() || socket.isInputShutdown())) { + return true; + } + return false; + } + + /** + * 打开tcp连接 + * @param ip + * @param port + * @param timeout 超时时间毫秒 + */ + private boolean tcpConnect(String ip,int post,int timeout) { + try { + close(); + logger.debug(String.format("打开tcp连接! ==》参数:ip地址:%s;端口:%s;超时毫秒:%s",ip,post,timeout)); + socket = new Socket(); + socket.connect(new InetSocketAddress(ip, post), timeout); + socket.setKeepAlive(true); + socket.setSoTimeout(timeout); + mInputStream = socket.getInputStream(); + mOutputStream = socket.getOutputStream(); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + /** + * 关闭tcp连接 + */ + private void close() { + try { + if (mInputStream != null) { + mInputStream.close(); + mInputStream = null; + } + } catch (Exception e) { + e.printStackTrace(); + } + try { + if (mOutputStream != null) { + mOutputStream.close(); + mOutputStream = null; + } + } catch (Exception e) { + e.printStackTrace(); + } + try { + if (socket != null) { + socket.close(); + socket = null; + logger.debug(String.format("关闭tcp连接!")); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 发送指令 + * @param cmd 指令 byte[] + * @return + */ + private boolean sendCommand(byte[] cmd) { + try { + if(mOutputStream != null){ + mOutputStream.write(cmd); + mOutputStream.flush(); + logger.debug("已发送命令 ===> "+Tools.Bytes2HexString(cmd, cmd.length)); + } + return true; + } catch (IOException e) { + e.printStackTrace(); + close(); + return false; + } + } +//***************************// + } + + /** + * 获取美雅洁清洗机modbus返回信息 + * @param ip + * @param post + * @param deviceIdStr + * @return + */ + public JSONObject getWashModbusDataByMeiYaJie(String ip,int post,String deviceIdStr,int timeoutSeconds){ + logger.debug(String.format("开始获取获取美雅洁Modbus数据:参数===> ip地址:%s;端口号:%s;设备id:%s;超时:%s秒",ip,post,deviceIdStr,timeoutSeconds)); + int timeout = timeoutSeconds * 1000;//秒转毫秒 + ModbusController modbusController = new ModbusController(); + JSONObject resultData = new JSONObject(); + resultData = modbusController.getWashData_MeiYaJie(ip,post,deviceIdStr,timeout); + logger.debug("获取到的清洗机数据 ==>"+ resultData.toString()); + return resultData; + } +} Index: ssts-maintain/src/main/java/com/forgon/disinfectsystem/maintain/device/service/MeiYaJieDataManagerImpl.java =================================================================== diff -u --- ssts-maintain/src/main/java/com/forgon/disinfectsystem/maintain/device/service/MeiYaJieDataManagerImpl.java (revision 0) +++ ssts-maintain/src/main/java/com/forgon/disinfectsystem/maintain/device/service/MeiYaJieDataManagerImpl.java (revision 25320) @@ -0,0 +1,45 @@ +package com.forgon.disinfectsystem.maintain.device.service; + +import net.sf.json.JSONObject; + +import org.apache.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; + +import com.forgon.device.ModbusControllerManager; +import com.forgon.disinfectsystem.entity.basedatamanager.deviceinterface.DeviceInterface; +import com.forgon.disinfectsystem.entity.washanddisinfectmanager.washData.WashData; +/** + * 美雅洁设备处理 + * @author shuyongfu + * @since 2018-11-12 + */ +public class MeiYaJieDataManagerImpl implements DeviceDataByCommunicationManager{ + private static final Logger logger = Logger.getLogger(MeiYaJieDataManagerImpl.class); + + @Autowired + private ModbusControllerManager modbusControllerManager; + + @Override + public void readAndSaveRecord(DeviceInterface devInteface,WashData washData, int timeoutSeconds) { + String ip = devInteface.getIP();//ip地址 + int post = devInteface.getPort();//端口号 + String deviceIdStr = devInteface.getDeviceId();//设备id + try { + //1、获取美雅洁清洗机modbus返回信息 + JSONObject resultData = modbusControllerManager.getWashModbusDataByMeiYaJie(ip, post, deviceIdStr, timeoutSeconds); + //2、washData设值 + String t1Temp = (String) resultData.get(ModbusControllerManager.WASHDATA_T1TEMP);//t1temp + String t2Temp = (String) resultData.get(ModbusControllerManager.WASHDATA_T2TEMP);//t2temp + String A0 = (String) resultData.get(ModbusControllerManager.WASHDATA_A0);//A0 + String time = (String) resultData.get(ModbusControllerManager.WASHDATA_TIME);//time + washData.setA0(A0); + washData.setT1Temp(t1Temp); + washData.setT2Temp(t2Temp); + washData.setTime(time); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e.getMessage()); + } + } + +}