public temp_Hum_Hugung_OnetoOneRecieve( JTextArea showJTextArea, JTextArea showSendMsg, int port, int flag) throws SQLException { this.flag = flag; this.showSendMsg = showSendMsg; this.showMsg = showJTextArea; this.portText = port; bs = new byteandstring(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); currentDay = Calendar.getInstance().getTime(); String getP = "select * from Interface"; ResultSet rs = ds.select(getP); if (rs != null) { while (rs.next()) { this.huStartTime = rs.getString("huStartTime"); this.huEndTime = rs.getString("huEndTime"); } rs.close(); } // String getHGdeviceCount="select * from SampleAddress where Sample_IndexID is not null and // Sample_AddressH is not null and Sample_AddressL is not null and // Sample_dataL='"+portText+"'"; // rs=ds.select(getHGdeviceCount); // if(rs!=null){ // try { // if(rs.next()) // { // int HgDeviceCount=rs.getInt(1); // if(HgDeviceCount!=0){ // HgFlag = new int[HgDeviceCount+1]; // for(int i= 0;i<HgFlag.length;i++) // { // HgFlag[i]=0;//为每一台弧光设备添加一个标志位,该标志位被后面的弧光次数统计所用 // } // } // } // } catch (SQLException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } rs = null; ds.close(); }
public void run() { // 每天将前一天的temperatureHistory表中的数据更新到temperatureHistory3H中 if (!(new SimpleDateFormat("yyyy-MM-dd")) .format(currentDay) .equals((new SimpleDateFormat("yyyy-MM-dd")).format(Calendar.getInstance().getTime()))) { currentDay = Calendar.getInstance().getTime(); ds.executeUpdateTempHistory3H(); } try { dsSocket = new DatagramSocket(portText); } catch (SocketException e) { JOptionPane.showMessageDialog( null, "端口 " + portText + " 被其他程序占用!", "监听端口初始化提示", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } byte[] sendData = new byte[8]; // 获取所有的温度采样点 String getdevice = "select * from DeviceSet where Device_Addr is not null and Device_IP is not null and Device_Port='" + portText + "'"; String Device_Addr = ""; String Device_Feature = ""; String Device_StartH = ""; String Device_StartL = ""; String Device_dataL = ""; String Device_IP = ""; String Device_Port = "9004"; ResultSet rs = null; // 获取所有的弧光采样点 String getHGdevice = "select * from SampleAddress where Sample_IndexID is not null and Sample_AddressH is not null and Sample_AddressL is not null and Sample_dataL='" + portText + "'"; String Sample_ID = ""; while (true) { try { rs = ds.select(getdevice); if (rs != null) { while (rs.next()) { Device_Addr = rs.getString("Device_Addr"); Device_StartH = rs.getString("Device_StartH"); Device_StartL = rs.getString("Device_StartL"); Device_dataL = rs.getString("Device_dataL"); Device_Feature = rs.getString("Device_Feature"); Device_IP = rs.getString("Device_IP"); Device_Port = rs.getString("Device_Port"); if (Device_Addr != null && Device_Addr.equals("null") == false && Device_IP != null && Device_IP.equals("null") == false) { // Device_Addr!=null&&Device_Addr.equals("null")==false&& // ds=new DatagramSocket(); int length = 0; sendData[0] = byteandstring.hexStringTobytes(Device_Addr); sendData[1] = byteandstring.hexStringTobytes(Device_Feature); sendData[2] = bs.hexStringTobytes(Device_StartH); sendData[3] = bs.hexStringTobytes(Device_StartL); if (Device_dataL.length() > 2) { byte[] le = new byte[2]; le = byteandstring.hexStringToBytes(Device_dataL); sendData[4] = le[0]; sendData[5] = le[1]; } else { sendData[4] = 0x00; sendData[5] = bs.hexStringTobytes(Device_dataL); } byte[] data = new byte[6]; for (int j = 0; j < 6; j++) { data[j] = sendData[j]; // System.out.print(Integer.toHexString(data[j]&0xFF)+" "); } crc.update(data, 0, 6); byte d[] = crc.getCrcBytes(); sendData[6] = d[0]; sendData[7] = d[1]; crc.reset(); InetAddress address; try { address = InetAddress.getByName(Device_IP); sendDp = new DatagramPacket( sendData, sendData.length, address, Integer.parseInt(Device_Port)); } catch (UnknownHostException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // ShowSendMessage("Thread <"+flag+"> 自动采集cmd: // "+Device_IP+"<>"+Device_Port+"<>"+bs.bytesToHexString(sendData)+";"); po.writeLog( "Thread <" + flag + "> 自动采集cmd: " + Device_IP + "<>" + Device_Port + "<>" + bs.bytesToHexString(sendData) + ";"); // 写入接收日志 try { dsSocket.send(sendDp); byte[] buf = new byte[400]; DatagramPacket dp = new DatagramPacket(buf, 0, buf.length); try { dsSocket.setSoTimeout(3000); dsSocket.receive(dp); byte[] returnData = dp.getData(); int returnLenght = dp.getLength(); String showpString = ""; for (int i = 0; i < returnLenght; i++) { String dataString = Integer.toHexString(returnData[i] & 0xff); if (dataString.length() < 2) { dataString = "0" + dataString; } showpString = showpString.trim() + dataString; } showpString = showpString + "\r\n"; ShowMessage("温度数据包:" + showpString); po.writeLog("温度数据包:" + showpString); if (returnLenght > 22) { String vid = Integer.toHexString(returnData[0] & 0xff); byte productType03_08 = returnData[1]; if (productType03_08 == 0x03) { byte productType = returnData[4]; if (returnLenght == 59) { receivePoor(returnData, returnLenght); } else { ShowMessage("该条返回数据的格式不对:长度不正确!"); po.writeLog("该条返回数据的格式不对:长度不正确!"); } } else { ShowMessage("该条返回数据的格式不对:长度不正确!"); po.writeLog("该条返回数据的格式不对:长度不正确!"); } } else { ShowMessage("该条返回数据的格式不对:长度不正确!"); po.writeLog("该条返回数据的格式不对:长度不正确!"); } } catch (IOException e2) { System.out.println("dddd"); e2.printStackTrace(); } ShowSendMessage( "Thread <" + flag + "> 自动采集cmd: " + Device_IP + "<>" + Device_Port + "<>" + bs.bytesToHexString(sendData) + ";"); } catch (IOException e) { e.printStackTrace(); } } } rs.close(); } } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); ds.close(); } System.gc(); Device_Addr = ""; Device_Feature = "04"; Device_StartH = "00"; Device_StartL = "00"; Device_dataL = ""; Device_IP = ""; Device_Port = "9004"; Sample_ID = ""; sendData = new byte[8]; Date dt = new Date(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); DateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dString = df.format(dt); String nowTime = dff.format(dt); // System.out.println(nowTime); String imgStartTime_current = dString + " " + huStartTime; String imgEndTime_current = dString + " " + huEndTime; if (cd.compare_date(imgStartTime_current, imgEndTime_current) == 0) { // System.out.println("系统工作时间区间设置错误!"); ShowMessage( "弧光系统工作时间区间设置错误!<>" + cd.compare_date(imgStartTime_current, imgEndTime_current) + "<>" + "imgStartTime_current<>" + imgStartTime_current + "imgEndTime_current<>" + imgEndTime_current); } else if (cd.compare_date(imgStartTime_current, nowTime) < 1 && cd.compare_date(imgEndTime_current, nowTime) > -1) { ShowMessage("弧光系统非工作时间!<>"); } else { ShowMessage("弧光系统工作时间!<>"); try { rs = ds.select(getHGdevice); // System.out.println(getHGdevice); if (rs != null) { while (rs.next()) { Sample_ID = rs.getString("Sample_ID"); Device_Addr = rs.getString("Sample_IndexID"); Device_dataL = rs.getString("Sample_AddressH"); Device_IP = rs.getString("Sample_AddressL"); Device_Port = rs.getString("Sample_dataL"); if (Device_Addr != null && Device_Addr.equals("null") == false && Device_IP != null && Device_IP.equals("null") == false) { int length = 0; sendData[0] = byteandstring.hexStringTobytes(Device_Addr); sendData[1] = byteandstring.hexStringTobytes(Device_Feature); sendData[2] = bs.hexStringTobytes(Device_StartH); sendData[3] = bs.hexStringTobytes(Device_StartL); if (Device_dataL.length() > 2) { byte[] le = new byte[2]; le = byteandstring.hexStringToBytes(Device_dataL); sendData[4] = le[0]; sendData[5] = le[1]; } else { sendData[4] = 0x00; sendData[5] = bs.hexStringTobytes(Device_dataL); } byte[] data = new byte[6]; for (int j = 0; j < 6; j++) { data[j] = sendData[j]; } crc.update(data, 0, 6); byte d[] = crc.getCrcBytes(); sendData[6] = d[0]; sendData[7] = d[1]; crc.reset(); String sendString = ""; for (int i = 0; i < 8; i++) { String dataString = Integer.toHexString(sendData[i] & 0xff); if (dataString.length() < 2) { dataString = "0" + dataString; } sendString = sendString.trim() + dataString; } sendString = sendString + "\r\n"; ShowSendMessage("弧光cmd:" + sendString); po.writeLog("弧光cmd:" + sendString); InetAddress address; try { address = InetAddress.getByName(Device_IP); sendDp = new DatagramPacket( sendData, sendData.length, address, Integer.parseInt(Device_Port)); } catch (UnknownHostException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { dsSocket.send(sendDp); byte[] buf = new byte[400]; DatagramPacket dp = new DatagramPacket(buf, 0, buf.length); try { // System.out.println("kk"); dsSocket.setSoTimeout(2000); dsSocket.receive(dp); // System.out.println("kds"); // System.out.println("kdddk"); byte[] returnData = dp.getData(); int returnLength = dp.getLength(); String showpString = ""; for (int i = 0; i < returnLength; i++) { String dataString = Integer.toHexString(returnData[i] & 0xff); if (dataString.length() < 2) { dataString = "0" + dataString; } showpString = showpString.trim() + dataString; } showpString = showpString + "\r\n"; ShowMessage("弧光数据包:" + showpString); po.writeLog("弧光数据包:" + showpString); // System.out.println("弧光数据包:"+showpString); if (returnLength == 40) { byte productType03_08 = returnData[1]; if (productType03_08 == 0x04) { crc.update(returnData, 0, (returnLength - 2)); byte test[] = crc.getCrcBytes(); crc.reset(); if (test[0] != returnData[38] || test[1] != returnData[39]) { ShowMessage("该条返回数据的格式不对:CRC校验码不正确!"); po.writeLog("该条返回数据的格式不对:CRC校验码不正确!"); } else { receiveHg(returnData, returnLength, Sample_ID); } } } else { ShowMessage("弧光设备返回不正确,长度不是40!"); po.writeLog("弧光设备返回不正确,长度不是40!"); } System.gc(); } catch (IOException e2) { e2.printStackTrace(); } ShowSendMessage( "Thread <" + flag + "> 自动采集cmd: " + Device_IP + "<>" + Device_Port + "<>" + bs.bytesToHexString(sendData) + ";"); } catch (IOException e) { e.printStackTrace(); } } else { } } } rs.close(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); ds.close(); } } ds.close(); System.gc(); try { Thread.sleep(60000); } catch (InterruptedException e) { e.printStackTrace(); } } }