package com.zbooksoft.gdmis.common; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.nio.charset.StandardCharsets; public class ShelveOpForAndroid { public static String IMS_START_SYMBOL = "START";// 操作起始标识 public static String IMS_OP_VERSION = "01";// 接口版本 public static String IMS_OP_COMM_LEFTOPEN = "LEFTOPEN";// 操作命令-左开 public static String IMS_OP_COMM_RIGHTOPEN = "RIGHTOPEN";// 操作命令-右开 public static String IMS_OP_COMM_VENTILATION = "VENTILATION";// 操作命令-通风 public static String IMS_OP_COMM_OPENSTATUS = "OPENSTATUS";// 查询命令-查询开架是否成功 public static String IMS_OP_COMM_CLOSED = "CLOSED";// 操作命令-合拢 public static String IMS_OP_COMM_LIGHT = "LIGHT";// 操作命令-亮灯,XY定位灯 public static String IMS_OP_COMM_STOP = "STOP";// 操作命令-停止 public static String IMS_END_SYMBOL = "END";// 操作结束标识 public static String IMS_SEPARATE_SYMBOL = "*";// 操作结束标识 private static int socketConnTimeout = 2000;// socket连接时间 private static int socketSoTimeout = 2000;// 连接时长 /*只用于八益样品间-密集架电源通电断电命令 on 2018.1.2 by chenb */ //密集架通电 public static byte[] IMS_OP_COMM_TD = {(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x06, (byte) 0x01, (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0xff, (byte) 0x00}; //密集架断电 public static byte[] IMS_OP_COMM_DD = {(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x06, (byte) 0x01, (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00}; /** * @param ip 安卓屏IP * @param port 端口 10086 * @param column 列号 * @param actionType 动作 * @return */ public static int shelveOperate(String ip, int port, int column, String actionType) { if (ip != null && port != -1 && column != -1) { try { if (!"3".equals(actionType)) { ShelveOpForAndroid.stop(ip, port); try { Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); } } switch (actionType) { case "3":// 停止 ShelveOpForAndroid.stop(ip, port); return 1; case "4":// 左移 ShelveOpForAndroid.rightOpen(ip, port, column); return 1; case "5":// 右移 ShelveOpForAndroid.leftOpen(ip, port, column); return 1; case "6":// 通风 ShelveOpForAndroid.vent(ip, port); return 1; case "7":// 合拢 ShelveOpForAndroid.close(ip, port); return 1; default: return 1; } } catch (Exception e) { e.printStackTrace(); return 1; } } else { return 1; } } /** * 密集架列左开 * * @param ip 固定列ip * @param port 固定列端口 * @param channel 列号 */ public static boolean leftOpen(String ip, int port, int channel) { System.out.println("leftopen"); Socket socketForIms = new Socket(); SocketAddress address = new InetSocketAddress(ip, port); OutputStream os = null; InputStream is = null; String str = ""; try { try { socketForIms.connect(address, socketConnTimeout); socketForIms.setSoTimeout(socketSoTimeout); os = socketForIms.getOutputStream(); } catch (Exception e) { //log.error("设备连接失败", e); e.printStackTrace(); throw e; // return false; } StringBuffer comm = new StringBuffer(); comm.append(IMS_START_SYMBOL); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_OP_VERSION); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_OP_COMM_LEFTOPEN); comm.append(IMS_SEPARATE_SYMBOL); String strCol = "01"; if (channel < 1 || channel > 99) { // log.error("密集架左开-列号不对"); return false; } else if (channel < 10) { strCol = "0" + String.valueOf(channel); } else { strCol = String.valueOf(channel); } comm.append(strCol); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_END_SYMBOL); //log.info("密集架操作命令:" + comm.toString()); os.write(ShelveOpForAndroid.stringToAscii(comm.toString())); os.flush(); is = socketForIms.getInputStream(); try { byte[] buf = new byte[1024]; int numReadedBytes = is.read(buf, 0, buf.length); str = new String(buf, 0, numReadedBytes, StandardCharsets.US_ASCII); } catch (Exception e) { //log.error("获取密集架左开的返回值失败:", e1); e.printStackTrace(); } //log.info("密集架左开成功"); } catch (Exception e) { //log.error("密集架左开失败", e); e.printStackTrace(); return false; } finally { if (os != null) { try { os.close(); } catch (Exception e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (Exception e) { e.printStackTrace(); } } if (socketForIms != null) { try { socketForIms.close(); socketForIms = null; } catch (IOException e) { e.printStackTrace(); } } } //log.info("密集架左开返回值:" + str); return true; } /** * 密集架列右开 * * @param ip 固定列ip * @param port 固定列端口 * @param channel 列号 * @throws */ public static boolean rightOpen(String ip, int port, int channel) { Socket socketForIms = new Socket(); SocketAddress address = new InetSocketAddress(ip, port); OutputStream os = null; InputStream is = null; String str = ""; try { try { socketForIms.connect(address, socketConnTimeout); socketForIms.setSoTimeout(socketSoTimeout); os = socketForIms.getOutputStream(); } catch (Exception e) { e.printStackTrace(); throw e; // return false; } StringBuffer comm = new StringBuffer(); comm.append(IMS_START_SYMBOL); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_OP_VERSION); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_OP_COMM_RIGHTOPEN); comm.append(IMS_SEPARATE_SYMBOL); String strCol = "01"; if (channel < 1 || channel > 99) { //log.error("密集架右开-列号不对"); return false; } else if (channel < 10) { strCol = "0" + String.valueOf(channel); } else { strCol = String.valueOf(channel); } comm.append(strCol); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_END_SYMBOL); //log.info("密集架操作命令:" + comm.toString()); os.write(ShelveOpForAndroid.stringToAscii(comm.toString())); os.flush(); is = socketForIms.getInputStream(); try { byte[] buf = new byte[1024]; int numReadedBytes = is.read(buf, 0, buf.length); str = new String(buf, 0, numReadedBytes, StandardCharsets.US_ASCII); } catch (Exception e) { e.printStackTrace(); } //log.info("密集架右开成功"); } catch (Exception e) { e.printStackTrace(); return false; } finally { if (os != null) { try { os.close(); } catch (Exception e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (Exception e) { e.printStackTrace(); } } if (socketForIms != null) { try { socketForIms.close(); } catch (IOException e) { e.printStackTrace(); } } } //log.info("密集架右开返回值:" + str); return true; } /** * 密集架通风 * * @param ip 固定列ip * @param port 固定列端口 */ public static boolean vent(String ip, int port) { Socket socketForIms = new Socket(); SocketAddress address = new InetSocketAddress(ip, port); OutputStream os = null; InputStream is = null; String str = ""; try { try { socketForIms.connect(address, socketConnTimeout); socketForIms.setSoTimeout(socketSoTimeout); os = socketForIms.getOutputStream(); } catch (Exception e) { e.printStackTrace(); throw e; // return false; } StringBuffer comm = new StringBuffer(); comm.append(IMS_START_SYMBOL); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_OP_VERSION); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_OP_COMM_VENTILATION); comm.append(IMS_SEPARATE_SYMBOL); comm.append("00"); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_END_SYMBOL); //log.info("密集架操作命令:" + comm.toString()); os.write(ShelveOpForAndroid.stringToAscii(comm.toString())); os.flush(); is = socketForIms.getInputStream(); try { byte[] buf = new byte[1024]; int numReadedBytes = is.read(buf, 0, buf.length); str = new String(buf, 0, numReadedBytes, StandardCharsets.US_ASCII); } catch (Exception e) { e.printStackTrace(); } //log.info("密集架通风成功"); } catch (Exception e) { e.printStackTrace(); return false; } finally { if (os != null) { try { os.close(); } catch (Exception e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (Exception e) { e.printStackTrace(); } } if (socketForIms != null) { try { socketForIms.close(); } catch (IOException e) { e.printStackTrace(); } } } //log.info("密集架通风返回值:" + str); return true; } /** * 密集架开架状态查询 * * @param ip * @param port * @return 返回 true 代表开架成功,反之开架中或者开架失败 */ public static boolean openstatus(String ip, int port) { Socket socketForIms = new Socket(); SocketAddress address = new InetSocketAddress(ip, port); OutputStream os = null; InputStream is = null; String str = ""; try { try { socketForIms.connect(address, socketConnTimeout); socketForIms.setSoTimeout(socketSoTimeout); os = socketForIms.getOutputStream(); } catch (Exception e) { e.printStackTrace(); //log.error("设备连接失败", e); throw e; // return false; } StringBuffer comm = new StringBuffer(); comm.append(IMS_START_SYMBOL); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_OP_VERSION); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_OP_COMM_OPENSTATUS); comm.append(IMS_SEPARATE_SYMBOL); comm.append("00"); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_END_SYMBOL); //log.info("密集架操作命令:" + comm.toString()); os.write(ShelveOpForAndroid.stringToAscii(comm.toString())); os.flush(); is = socketForIms.getInputStream(); try { byte[] buf = new byte[1024]; int numReadedBytes = is.read(buf, 0, buf.length); str = new String(buf, 0, numReadedBytes, StandardCharsets.US_ASCII); } catch (Exception e1) { //log.error("获取密集架开架是否成功的返回值失败:", e1); e1.printStackTrace(); } } catch (Exception e) { //log.error("密集架开架状态查询失败", e); e.printStackTrace(); return false; } finally { if (os != null) { try { os.close(); } catch (Exception e) { //log.error("密集架开架状态接口OutputStream关闭失败", e); e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (Exception e) { //log.error("密集架开架状态接口InputStream关闭失败", e); e.printStackTrace(); } } if (socketForIms != null) { try { socketForIms.close(); } catch (IOException e) { //log.error("密集架开架状态接口Socket关闭失败", e); e.printStackTrace(); } } } //log.info("密集架开架状态返回值:" + str); if ("ok".equals(str)) { return true; } else { return false; } } /** * 密集架合拢 * * @param ip 固定列ip * @param port 固定列端口 */ public static boolean close(String ip, int port) { Socket socketForIms = new Socket(); SocketAddress address = new InetSocketAddress(ip, port); OutputStream os = null; InputStream is = null; String str = ""; try { try { socketForIms.connect(address, socketConnTimeout); socketForIms.setSoTimeout(socketSoTimeout); os = socketForIms.getOutputStream(); } catch (Exception e) { e.printStackTrace(); throw e; // return false; } StringBuffer comm = new StringBuffer(); comm.append(IMS_START_SYMBOL); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_OP_VERSION); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_OP_COMM_CLOSED); comm.append(IMS_SEPARATE_SYMBOL); comm.append("00"); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_END_SYMBOL); //log.info("密集架操作命令:" + comm.toString()); os.write(ShelveOpForAndroid.stringToAscii(comm.toString())); os.flush(); is = socketForIms.getInputStream(); try { byte[] buf = new byte[1024]; int numReadedBytes = is.read(buf, 0, buf.length); str = new String(buf, 0, numReadedBytes, StandardCharsets.US_ASCII); } catch (Exception e) { e.printStackTrace(); } //log.info("密集架合拢成功"); } catch (Exception e) { e.printStackTrace(); return false; } finally { if (os != null) { try { os.close(); } catch (Exception e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (Exception e) { e.printStackTrace(); } } if (socketForIms != null) { try { socketForIms.close(); } catch (IOException e) { e.printStackTrace(); } } } //log.info("密集架合拢返回值:" + str); return true; } /** * xy灯 * * @param ip 安卓屏ip * @param port 安卓屏端口 * @param col 列 * @param face 面 0左面 1右面 * @param jie 节 * @param ceng 层 * @param open 0关1开 * @return * @throws IOException * @throws InterruptedException */ public static boolean light(String ip, int port, int col, int face, int jie, int ceng, int open) throws IOException, InterruptedException { Socket socketForIms = new Socket(); SocketAddress address = new InetSocketAddress(ip, port); OutputStream os = null; InputStream is = null; String str = ""; try { try { socketForIms.connect(address, socketConnTimeout); socketForIms.setSoTimeout(socketSoTimeout); os = socketForIms.getOutputStream(); } catch (Exception e) { e.printStackTrace(); throw e; // return false; } StringBuffer comm = new StringBuffer(); comm.append(IMS_START_SYMBOL); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_OP_VERSION); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_OP_COMM_LIGHT); comm.append(IMS_SEPARATE_SYMBOL); String strCol = "01"; if (col < 1 || col > 99) { //log.error("密集架右开-列号不对"); return false; } else if (col < 10) { strCol = "0" + String.valueOf(col); } else { strCol = String.valueOf(col); } comm.append(strCol); comm.append(IMS_SEPARATE_SYMBOL); comm.append("0" + face); comm.append(IMS_SEPARATE_SYMBOL); String strjie = "01"; if (jie < 1 || jie > 99) { return false; } else if (jie < 10) { strjie = "0" + String.valueOf(jie); } else { strjie = String.valueOf(jie); } comm.append(strjie); comm.append(IMS_SEPARATE_SYMBOL); String strceng = "01"; if (ceng < 1 || ceng > 99) { return false; } else if (ceng < 10) { strceng = "0" + String.valueOf(ceng); } else { strceng = String.valueOf(ceng); } comm.append(strceng); comm.append(IMS_SEPARATE_SYMBOL); comm.append("0" + open); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_END_SYMBOL); //log.info("密集架操作命令:" + comm.toString()); System.out.println(comm.toString()); os.write(ShelveOpForAndroid.stringToAscii(comm.toString())); os.flush(); is = socketForIms.getInputStream(); try { byte[] buf = new byte[1024]; int numReadedBytes = is.read(buf, 0, buf.length); str = new String(buf, 0, numReadedBytes, StandardCharsets.US_ASCII); } catch (Exception e) { e.printStackTrace(); } //log.info("密集架右开成功"); } catch (Exception e) { e.printStackTrace(); return false; } finally { if (os != null) { try { os.close(); } catch (Exception e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (Exception e) { e.printStackTrace(); } } if (socketForIms != null) { try { socketForIms.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; } /** * 密集架停止 * * @param ip 固定列ip * @param port 固定列端口 */ public static boolean stop(String ip, int port) { Socket socketForIms = new Socket(); SocketAddress address = new InetSocketAddress(ip, port); OutputStream os = null; InputStream is = null; String str = ""; try { try { socketForIms.connect(address, socketConnTimeout); socketForIms.setSoTimeout(socketSoTimeout); os = socketForIms.getOutputStream(); } catch (Exception e) { e.printStackTrace(); throw e; // return false; } StringBuffer comm = new StringBuffer(); comm.append(IMS_START_SYMBOL); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_OP_VERSION); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_OP_COMM_STOP); comm.append(IMS_SEPARATE_SYMBOL); comm.append("00"); comm.append(IMS_SEPARATE_SYMBOL); comm.append(IMS_END_SYMBOL); //log.info("密集架操作命令:" + comm.toString()); os.write(ShelveOpForAndroid.stringToAscii(comm.toString())); os.flush(); is = socketForIms.getInputStream(); try { byte[] buf = new byte[1024]; int numReadedBytes = is.read(buf, 0, buf.length); str = new String(buf, 0, numReadedBytes, StandardCharsets.US_ASCII); } catch (Exception e) { e.printStackTrace(); } // log.info("密集架停止成功"); } catch (Exception e) { e.printStackTrace(); return false; } finally { if (os != null) { try { os.close(); } catch (Exception e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (Exception e) { e.printStackTrace(); } } if (socketForIms != null) { try { socketForIms.close(); } catch (IOException e) { e.printStackTrace(); } } } //log.info("密集架停止返回值:" + str); return true; } public static byte[] stringToAscii(String value) { try { return value.getBytes("US-ASCII"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } /** * 密集架通电 * * @param ip 固定列ip * @param port 固定列端口 */ public static boolean openPws(String ip, int port) { Socket socketForIms = new Socket(); SocketAddress address = new InetSocketAddress(ip, port); OutputStream os = null; InputStream is = null; String str = ""; try { try { socketForIms.connect(address, socketConnTimeout); socketForIms.setSoTimeout(socketSoTimeout); os = socketForIms.getOutputStream(); } catch (Exception e) { e.printStackTrace(); throw e; // return false; } //log.info("密集架操作命令:" + IMS_OP_COMM_TD.toString()); os.write(IMS_OP_COMM_TD); os.flush(); is = socketForIms.getInputStream(); try { byte[] buf = new byte[1024]; int numReadedBytes = is.read(buf, 0, buf.length); str = new String(buf, 0, numReadedBytes, StandardCharsets.US_ASCII); } catch (Exception e) { e.printStackTrace(); } //log.info("密集架通电成功"); } catch (Exception e) { e.printStackTrace(); return false; } finally { if (os != null) { try { os.close(); } catch (Exception e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (Exception e) { e.printStackTrace(); } } if (socketForIms != null) { try { socketForIms.close(); } catch (IOException e) { e.printStackTrace(); } } } //log.info("密集架通电返回值:" + str); return true; } /** * 密集架通电 * * @param ip 固定列ip * @param port 固定列端口 */ public static boolean closePws(String ip, int port) { Socket socketForIms = new Socket(); SocketAddress address = new InetSocketAddress(ip, port); OutputStream os = null; InputStream is = null; String str = ""; try { try { socketForIms.connect(address, socketConnTimeout); socketForIms.setSoTimeout(socketSoTimeout); os = socketForIms.getOutputStream(); } catch (Exception e) { //log.error("设备连接失败", e); throw e; // return false; } //log.info("密集架操作命令:" + IMS_OP_COMM_TD.toString()); os.write(IMS_OP_COMM_DD); os.flush(); is = socketForIms.getInputStream(); try { byte[] buf = new byte[1024]; int numReadedBytes = is.read(buf, 0, buf.length); str = new String(buf, 0, numReadedBytes, StandardCharsets.US_ASCII); } catch (Exception e1) { //log.error("获取密集架断电的返回值失败:", e1); } //log.info("密集架断电成功"); } catch (Exception e) { //log.error("密集架断电失败", e); return false; } finally { if (os != null) { try { os.close(); } catch (Exception e) { //log.error("密集架断电接口OutputStream关闭失败", e); } } if (is != null) { try { is.close(); } catch (Exception e) { // log.error("密集架断电接口InputStream关闭失败", e); } } if (socketForIms != null) { try { socketForIms.close(); } catch (IOException e) { //log.error("密集架断电接口Socket关闭失败", e); } } } //log.info("密集架断电返回值:" + str); return true; } /** * 根据档案id打开对应密集架 * @param ip 固定列ip * @param archivesId 档案id * @param port 固定列端口 *//* public static boolean openByArchivesId(String ip, int port,String colnum,Ams_archives doc) { // 先停止密集架 stop(ip, port); try { Thread.sleep(1500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (XUtil.isNotEmpty(colnum) &&doc!=null) { if ("0".equals(doc.store_lr)) { System.out.println("left open"); //如果所在密集架在L面,左开 return leftOpen(ip, port, Integer.valueOf(colnum)); }else { //如果所在密集架在R面,右开 return rightOpen(ip, port, Integer.valueOf(colnum)); } }else { return false; } }*/ /** * @Title: main @Description: @param args @return void 返回类型 @throws */ public static void main(String[] args) { try { ShelveOpForAndroid.light("127.0.0.1", 10086, 2, 1, 4, 2, 1); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }