├── MeterCollection ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── 2017_10_24_errorLog.txt ├── bin │ └── club │ │ └── dreamtech │ │ ├── meterCommon │ │ ├── CacheData.class │ │ ├── ConfigXML.class │ │ ├── DataWrite.class │ │ ├── ErrorLog.class │ │ ├── OperationHelper.class │ │ └── SQLHelper.class │ │ ├── meterHandler │ │ ├── .DS_Store │ │ ├── ._.DS_Store │ │ ├── DTLMessageAnalysis.class │ │ ├── DTLMessageCreate.class │ │ ├── DTLMessageHandler.class │ │ ├── MeterCollectionHandler$1.class │ │ ├── MeterCollectionHandler.class │ │ └── ServerThread.class │ │ ├── meterMain │ │ ├── meterMain.class │ │ └── testMain.class │ │ └── meterModel │ │ ├── ClientModel.class │ │ └── CollectionModel.class ├── config.xml ├── lib │ ├── energy-sdk-0.0.2.jar │ ├── mysql-connector-java-5.1.6.jar │ └── mysql-write.jar └── src │ └── club │ └── dreamtech │ ├── meterCommon │ ├── CacheData.java │ ├── ConfigXML.java │ ├── DataWrite.java │ ├── ErrorLog.java │ ├── OperationHelper.java │ └── SQLHelper.java │ ├── meterHandler │ ├── .DS_Store │ ├── ._.DS_Store │ ├── DTLMessageAnalysis.java │ ├── DTLMessageCreate.java │ ├── DTLMessageHandler.java │ ├── MeterCollectionHandler.java │ └── ServerThread.java │ ├── meterMain │ ├── meterMain.java │ └── testMain.java │ └── meterModel │ ├── ClientModel.java │ └── CollectionModel.java ├── README.md ├── flow.jpg ├── 配置说明.docx └── 采集软件数据库字典及流程.docx /MeterCollection/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /MeterCollection/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | MeterCollection 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /MeterCollection/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.7 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.7 12 | -------------------------------------------------------------------------------- /MeterCollection/2017_10_24_errorLog.txt: -------------------------------------------------------------------------------- 1 | 2017_10_24 06:17:12 解析读数据报文出错:-1 2 | 2017_10_24 06:18:06 解析读数据报文出错:-1 3 | 2017_10_24 06:25:35 解析读数据报文出错:-1 4 | 2017_10_24 06:25:35 解析读数据报文出错:-1 5 | 2017_10_24 06:25:35 解析读数据报文出错:-1 6 | 2017_10_24 06:25:36 解析读数据报文出错:-1 7 | 2017_10_24 06:25:36 解析读数据报文出错:-1 8 | 2017_10_24 06:25:36 解析读数据报文出错:-1 9 | 2017_10_24 06:25:36 解析读数据报文出错:-1 10 | 2017_10_24 06:25:37 解析读数据报文出错:-1 11 | 2017_10_24 06:25:37 解析读数据报文出错:-1 12 | 2017_10_24 06:25:37 解析读数据报文出错:-1 13 | 2017_10_24 06:25:37 解析读数据报文出错:-1 14 | 2017_10_24 06:25:40 解析读数据报文出错:-1 15 | 2017_10_24 06:25:40 解析读数据报文出错:-1 16 | 2017_10_24 06:25:40 解析读数据报文出错:-1 17 | 2017_10_24 06:25:40 解析读数据报文出错:-1 18 | 2017_10_24 06:25:41 解析读数据报文出错:-1 19 | 2017_10_24 06:25:41 解析读数据报文出错:-1 20 | 2017_10_24 06:25:41 解析读数据报文出错:-1 21 | 2017_10_24 06:25:41 解析读数据报文出错:-1 22 | 2017_10_24 06:25:41 解析读数据报文出错:-1 23 | 2017_10_24 06:25:42 解析读数据报文出错:-1 24 | 2017_10_24 06:25:42 解析读数据报文出错:-1 25 | 2017_10_24 06:25:42 解析读数据报文出错:-1 26 | 2017_10_24 06:25:44 解析读数据报文出错:-1 27 | 2017_10_24 06:25:45 解析读数据报文出错:-1 28 | 2017_10_24 06:25:45 解析读数据报文出错:-1 29 | 2017_10_24 06:25:45 解析读数据报文出错:-1 30 | 2017_10_24 06:25:45 解析读数据报文出错:-1 31 | 2017_10_24 06:25:46 解析读数据报文出错:-1 32 | 2017_10_24 06:25:46 解析读数据报文出错:-1 33 | 2017_10_24 06:25:46 解析读数据报文出错:-1 34 | 2017_10_24 06:25:46 解析读数据报文出错:-1 35 | 2017_10_24 06:25:47 解析读数据报文出错:-1 36 | 2017_10_24 06:25:47 解析读数据报文出错:-1 37 | 2017_10_24 06:25:47 解析读数据报文出错:-1 38 | 2017_10_24 06:26:01 线程池错误:Software caused connection abort: recv failed 39 | -------------------------------------------------------------------------------- /MeterCollection/bin/club/dreamtech/meterCommon/CacheData.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/bin/club/dreamtech/meterCommon/CacheData.class -------------------------------------------------------------------------------- /MeterCollection/bin/club/dreamtech/meterCommon/ConfigXML.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/bin/club/dreamtech/meterCommon/ConfigXML.class -------------------------------------------------------------------------------- /MeterCollection/bin/club/dreamtech/meterCommon/DataWrite.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/bin/club/dreamtech/meterCommon/DataWrite.class -------------------------------------------------------------------------------- /MeterCollection/bin/club/dreamtech/meterCommon/ErrorLog.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/bin/club/dreamtech/meterCommon/ErrorLog.class -------------------------------------------------------------------------------- /MeterCollection/bin/club/dreamtech/meterCommon/OperationHelper.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/bin/club/dreamtech/meterCommon/OperationHelper.class -------------------------------------------------------------------------------- /MeterCollection/bin/club/dreamtech/meterCommon/SQLHelper.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/bin/club/dreamtech/meterCommon/SQLHelper.class -------------------------------------------------------------------------------- /MeterCollection/bin/club/dreamtech/meterHandler/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/bin/club/dreamtech/meterHandler/.DS_Store -------------------------------------------------------------------------------- /MeterCollection/bin/club/dreamtech/meterHandler/._.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/bin/club/dreamtech/meterHandler/._.DS_Store -------------------------------------------------------------------------------- /MeterCollection/bin/club/dreamtech/meterHandler/DTLMessageAnalysis.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/bin/club/dreamtech/meterHandler/DTLMessageAnalysis.class -------------------------------------------------------------------------------- /MeterCollection/bin/club/dreamtech/meterHandler/DTLMessageCreate.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/bin/club/dreamtech/meterHandler/DTLMessageCreate.class -------------------------------------------------------------------------------- /MeterCollection/bin/club/dreamtech/meterHandler/DTLMessageHandler.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/bin/club/dreamtech/meterHandler/DTLMessageHandler.class -------------------------------------------------------------------------------- /MeterCollection/bin/club/dreamtech/meterHandler/MeterCollectionHandler$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/bin/club/dreamtech/meterHandler/MeterCollectionHandler$1.class -------------------------------------------------------------------------------- /MeterCollection/bin/club/dreamtech/meterHandler/MeterCollectionHandler.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/bin/club/dreamtech/meterHandler/MeterCollectionHandler.class -------------------------------------------------------------------------------- /MeterCollection/bin/club/dreamtech/meterHandler/ServerThread.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/bin/club/dreamtech/meterHandler/ServerThread.class -------------------------------------------------------------------------------- /MeterCollection/bin/club/dreamtech/meterMain/meterMain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/bin/club/dreamtech/meterMain/meterMain.class -------------------------------------------------------------------------------- /MeterCollection/bin/club/dreamtech/meterMain/testMain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/bin/club/dreamtech/meterMain/testMain.class -------------------------------------------------------------------------------- /MeterCollection/bin/club/dreamtech/meterModel/ClientModel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/bin/club/dreamtech/meterModel/ClientModel.class -------------------------------------------------------------------------------- /MeterCollection/bin/club/dreamtech/meterModel/CollectionModel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/bin/club/dreamtech/meterModel/CollectionModel.class -------------------------------------------------------------------------------- /MeterCollection/config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |    5 | 39.108.180.240   6 | 3306 7 | root 8 | 123456 9 | energy 10 | 11 | 12 | 13 | 802 14 | 15 | 16 | 1 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /MeterCollection/lib/energy-sdk-0.0.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/lib/energy-sdk-0.0.2.jar -------------------------------------------------------------------------------- /MeterCollection/lib/mysql-connector-java-5.1.6.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/lib/mysql-connector-java-5.1.6.jar -------------------------------------------------------------------------------- /MeterCollection/lib/mysql-write.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/lib/mysql-write.jar -------------------------------------------------------------------------------- /MeterCollection/src/club/dreamtech/meterCommon/CacheData.java: -------------------------------------------------------------------------------- 1 | package club.dreamtech.meterCommon; 2 | 3 | 4 | import java.util.Hashtable; 5 | import java.util.List; 6 | 7 | import club.dreamtech.meterModel.ClientModel; 8 | 9 | public class CacheData { 10 | private static String[] errorMassageData=new String[]{"其他错误","无请求数据","密码错误/未授权","通讯速率不能改变","","",""}; 11 | /** 12 | * 获取错误信息字 13 | * @return 14 | */ 15 | public static String[] getErrorMassageData() { 16 | return errorMassageData; 17 | 18 | } 19 | 20 | 21 | 22 | public static Hashtable SocketClientMap=new Hashtable(); 23 | 24 | public static Hashtable> ipAndMeterMapHashtable=new Hashtable>(); 25 | 26 | // public static ConcurrentHashMap SocketClientMap=new ConcurrentHashMap(); 27 | // public static ConcurrentHashMap SocketClientMap=new ConcurrentHashMap(); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /MeterCollection/src/club/dreamtech/meterCommon/ConfigXML.java: -------------------------------------------------------------------------------- 1 | package club.dreamtech.meterCommon; 2 | 3 | import java.io.File; 4 | 5 | import javax.xml.parsers.DocumentBuilder; 6 | import javax.xml.parsers.DocumentBuilderFactory; 7 | 8 | 9 | import org.w3c.dom.Document; 10 | import org.w3c.dom.NodeList; 11 | 12 | /** 13 | * 获取配置文件信息 14 | * @author cz 15 | * 16 | */ 17 | public class ConfigXML { 18 | 19 | private static String getValueByName(String nameString) throws Exception { 20 | File file = new File("config.xml"); 21 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 22 | DocumentBuilder builder = factory.newDocumentBuilder(); 23 | Document doc = builder.parse(file); 24 | NodeList valueList = doc.getElementsByTagName(nameString); 25 | return valueList.item(0).getFirstChild().getNodeValue().trim(); 26 | } 27 | 28 | /** 29 | * 获取SQL数据库连接IP 30 | * @return 31 | * @throws Exception 32 | */ 33 | public static String getSQLIp() throws Exception { 34 | return getValueByName("SQLIP"); 35 | } 36 | 37 | /** 38 | * 获取SQL数据库连接端口 39 | * @return 40 | * @throws Exception 41 | */ 42 | public static String getSQLPort() throws Exception { 43 | return getValueByName("SQLPORT"); 44 | } 45 | 46 | /** 47 | * 获取SQL数据库连接用户名 48 | * @return 49 | * @throws Exception 50 | */ 51 | public static String getSQLUser() throws Exception { 52 | return getValueByName("USER"); 53 | } 54 | 55 | /** 56 | * 获取SQL数据库连接密码 57 | * @return 58 | * @throws Exception 59 | */ 60 | public static String getSQLPassWord() throws Exception { 61 | return getValueByName("PASSWORD"); 62 | } 63 | 64 | /** 65 | * 获取SQL数据库连接数据库名称 66 | * @return 67 | * @throws Exception 68 | */ 69 | public static String getSQLDatabase() throws Exception { 70 | return getValueByName("DATABASE"); 71 | } 72 | 73 | /** 74 | * 获取Socket端口号 75 | * @return 76 | * @throws Exception 77 | */ 78 | public static String getSocketPort() throws Exception { 79 | return getValueByName("SOCKETPORT"); 80 | } 81 | 82 | 83 | /** 84 | * 获取采集间隔时间 85 | * @return 86 | * @throws Exception 87 | */ 88 | public static int getCollectionInterval() throws Exception { 89 | return Integer.parseInt(getValueByName("INTERVAL")); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /MeterCollection/src/club/dreamtech/meterCommon/DataWrite.java: -------------------------------------------------------------------------------- 1 | package club.dreamtech.meterCommon; 2 | 3 | 4 | import java.util.Date; 5 | import java.util.List; 6 | 7 | import club.dreamtech.meterModel.CollectionModel; 8 | import cn.edu.tsinghua.sql.Connection; 9 | import cn.edu.tsinghua.sql.ConnectionFactory; 10 | import cn.edu.tsinghua.sql.DataType; 11 | import cn.edu.tsinghua.sql.IWriter; 12 | import cn.edu.tsinghua.sql.ConnectionFactory.ConnectionType; 13 | 14 | /** 15 | * 数据库写入操作类 16 | * @author cz 17 | * 18 | */ 19 | public class DataWrite { 20 | 21 | private Connection myConnection=null; 22 | private IWriter myWriter=null; 23 | 24 | public DataWrite() throws Exception { 25 | 26 | } 27 | 28 | /** 29 | * 连接,建立资源 30 | * @return 31 | */ 32 | public boolean connection() { 33 | try { 34 | myConnection=ConnectionFactory.createConnection(ConnectionType.MYSQL, new String[]{ConfigXML.getSQLIp()}, ConfigXML.getSQLPort(), ConfigXML.getSQLUser(), ConfigXML.getSQLPassWord(), ConfigXML.getSQLDatabase()); 35 | myWriter=myConnection.getWriter(); 36 | return true; 37 | } catch (Exception e) { 38 | ErrorLog.writeError("数据库连接出错:"+e.getMessage()); 39 | return false; 40 | } 41 | } 42 | 43 | /** 44 | * 释放资源 45 | * @return 46 | */ 47 | public boolean disConnection() { 48 | try { 49 | if (myWriter!=null) { 50 | myWriter.close(); 51 | } 52 | if (myConnection!=null) { 53 | myConnection.close(); 54 | } 55 | 56 | return true; 57 | } catch (Exception e) { 58 | ErrorLog.writeError("数据库关闭出错:"+e.getMessage()); 59 | return false; 60 | } 61 | } 62 | 63 | /** 64 | * 写入数据 65 | * @param colModel 采集信息类 66 | * @param type 数据类型:0 DataType.NEEDED;1 DataType.ENERGY;2 DataType.OTHER 67 | * @return 68 | */ 69 | public boolean dataWrite(CollectionModel colModel,int type) { 70 | try { 71 | if (myWriter==null) { 72 | return false; 73 | } 74 | System.out.println("数据写入"); 75 | long time=new Date().getTime(); 76 | DataType dataType[]=new DataType[]{DataType.NEEDED,DataType.ENERGY,DataType.OTHER}; 77 | myWriter.writeData(dataType[type], "DTL645-2007",colModel.getMeterAddressCode(), colModel.getIndentificationCode(), time, colModel.getValue()); 78 | System.out.println("数据写入完成"); 79 | return true; 80 | } catch (Exception e) { 81 | ErrorLog.writeError("数据写入出错:"+e.getMessage()); 82 | return false; 83 | } 84 | } 85 | 86 | /** 87 | * 批量写入数据 88 | * @param colModelList 89 | * @param type 数据类型:0 DataType.NEEDED;1 DataType.ENERGY;2 DataType.OTHER 90 | * @return 91 | */ 92 | public boolean dataListWrite(List colModelList,int type) { 93 | try { 94 | if (myWriter==null) { 95 | return false; 96 | } 97 | System.out.println("数据批量写入"); 98 | int size=colModelList.size(); 99 | if (size<=0) { 100 | return true; 101 | } 102 | String meterCodeString=colModelList.get(0).getMeterAddressCode(); 103 | String indentificationArray[]=new String[size]; 104 | double valueArray[]=new double[size]; 105 | for (int i = 0; i < size; i++) { 106 | CollectionModel collectionModel=colModelList.get(i); 107 | indentificationArray[i]=collectionModel.getIndentificationCode(); 108 | valueArray[i]=collectionModel.getValue(); 109 | } 110 | long time=new Date().getTime(); 111 | myWriter.batchWriteData(DataType.NEEDED, "DTL645-2007", meterCodeString, indentificationArray, time,valueArray); 112 | System.out.println("数据批量写入完成"); 113 | return true; 114 | } catch (Exception e) { 115 | ErrorLog.writeError("批量数据写入出错:"+e.getMessage()); 116 | return false; 117 | } 118 | } 119 | 120 | 121 | 122 | } 123 | -------------------------------------------------------------------------------- /MeterCollection/src/club/dreamtech/meterCommon/ErrorLog.java: -------------------------------------------------------------------------------- 1 | package club.dreamtech.meterCommon; 2 | 3 | import java.io.File; 4 | import java.io.FileWriter; 5 | import java.io.PrintWriter; 6 | import java.text.SimpleDateFormat; 7 | import java.util.Date; 8 | 9 | public class ErrorLog { 10 | 11 | public static boolean writeError(String errorMassage) { 12 | try { 13 | Date now=new java.util.Date(); 14 | String path=new SimpleDateFormat("yyyy_MM_dd").format(now)+"_errorLog.txt"; 15 | String time=new SimpleDateFormat("yyyy_MM_dd hh:mm:ss").format(now); 16 | File file=new File(path); 17 | if (!file.exists()) { 18 | file.createNewFile(); 19 | } 20 | PrintWriter out = new PrintWriter(new FileWriter(file,true)); 21 | out.println(time+" "+errorMassage); // \r\n即为换行 22 | out.flush(); // 把缓存区内容压入文件 23 | out.close(); // 最后记得关闭文件 24 | 25 | } catch (Exception e) { 26 | // TODO: handle exception 27 | } 28 | return true; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /MeterCollection/src/club/dreamtech/meterCommon/OperationHelper.java: -------------------------------------------------------------------------------- 1 | package club.dreamtech.meterCommon; 2 | 3 | public class OperationHelper { 4 | final protected static char[] hexArray = "0123456789ABCDEF".toCharArray(); 5 | /** 6 | * byte数组转16进制string 7 | * @param bytes 8 | * @return 9 | */ 10 | public static String bytesToHex(byte[] bytes) { 11 | char[] hexChars = new char[bytes.length * 2]; 12 | for ( int j = 0; j < bytes.length; j++ ) { 13 | int v = bytes[j] & 0xFF; 14 | hexChars[j * 2] = hexArray[v >>> 4]; 15 | hexChars[j * 2 + 1] = hexArray[v & 0x0F]; 16 | } 17 | return new String(hexChars); 18 | } 19 | 20 | /** 21 | * byte数组转值 22 | * @param bytes 23 | * @param decimalNum 数值小数位数 24 | * @return 25 | */ 26 | public static String bytesToValue(byte[] bytes,int decimalNum) { 27 | String resultString=bytesToHex(bytes); 28 | resultString=resultString.substring(0, resultString.length()-1-decimalNum)+"."+resultString.substring(resultString.length()-decimalNum); 29 | return resultString; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /MeterCollection/src/club/dreamtech/meterCommon/SQLHelper.java: -------------------------------------------------------------------------------- 1 | package club.dreamtech.meterCommon; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.PreparedStatement; 6 | import java.sql.ResultSet; 7 | import java.sql.SQLException; 8 | 9 | public class SQLHelper { 10 | private String url = "jdbc:mysql://127.0.0.1/student"; 11 | private String name = "com.mysql.jdbc.Driver"; 12 | private String user = "root"; 13 | private String password = "root"; 14 | 15 | private Connection conn = null; 16 | private PreparedStatement pst = null; 17 | 18 | public SQLHelper(String sql) { 19 | try { 20 | Class.forName(name);//指定连接类型 21 | conn = DriverManager.getConnection(url, user, password);//获取连接 22 | pst = conn.prepareStatement(sql);//准备执行语句 23 | } catch (Exception e) { 24 | e.printStackTrace(); 25 | } 26 | } 27 | 28 | public boolean GetIpAndMeter() { 29 | try { 30 | 31 | ResultSet ret = db1.pst.executeQuery();//执行语句,得到结果集 32 | while (ret.next()) { 33 | String uid = ret.getString(1); 34 | String ufname = ret.getString(2); 35 | String ulname = ret.getString(3); 36 | String udate = ret.getString(4); 37 | System.out.println(uid + "\t" + ufname + "\t" + ulname + "\t" + udate ); 38 | }//显示数据 39 | ret.close(); 40 | db1.close();//关闭连接 41 | } catch (SQLException e) { 42 | e.printStackTrace(); 43 | } 44 | } 45 | 46 | public void close() { 47 | try { 48 | this.conn.close(); 49 | this.pst.close(); 50 | } catch (SQLException e) { 51 | e.printStackTrace(); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /MeterCollection/src/club/dreamtech/meterHandler/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/src/club/dreamtech/meterHandler/.DS_Store -------------------------------------------------------------------------------- /MeterCollection/src/club/dreamtech/meterHandler/._.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/MeterCollection/src/club/dreamtech/meterHandler/._.DS_Store -------------------------------------------------------------------------------- /MeterCollection/src/club/dreamtech/meterHandler/DTLMessageAnalysis.java: -------------------------------------------------------------------------------- 1 | package club.dreamtech.meterHandler; 2 | 3 | import club.dreamtech.meterCommon.CacheData; 4 | import club.dreamtech.meterCommon.ErrorLog; 5 | import club.dreamtech.meterCommon.OperationHelper; 6 | import club.dreamtech.meterModel.*; 7 | 8 | public class DTLMessageAnalysis { 9 | public DTLMessageAnalysis() { 10 | 11 | } 12 | 13 | /** 14 | * 最大需量及发生时间解析函数 15 | * @param message 16 | * @param error 17 | * @return 18 | */ 19 | public CollectionModel maxDemandAnalysis(byte[] message,String error) 20 | { 21 | try { 22 | CollectionModel collectionModel=new CollectionModel(); 23 | if (message[10]!=(byte)0x91) {//异常数据 24 | error=errorAnalysis(message); 25 | return null; 26 | } 27 | //电表地址 28 | byte addressByte[]=new byte[6]; 29 | for (int i = 0; i < addressByte.length; i++) { 30 | addressByte[5-i]=message[i+3]; 31 | } 32 | collectionModel.setMeterAddressCode(OperationHelper.bytesToHex(addressByte)); 33 | //标识符 34 | byte indentificationByte[]=new byte[4]; 35 | for (int i = 0; i < indentificationByte.length; i++) { 36 | indentificationByte[3-i]=(byte) (message[12+i]-0x33); 37 | } 38 | collectionModel.setIndentificationCode(OperationHelper.bytesToHex(indentificationByte)); 39 | 40 | //获取值 41 | int dataLength=message[11]; 42 | if (dataLength==8) { 43 | byte valueByte[]=new byte[4]; 44 | byte timeByte[]=new byte[4]; 45 | for (int i = 0; i <4; i++) { 46 | valueByte[i]=(byte) (message[16+i]-0x33); 47 | } 48 | for (int i = 0; i < 4; i++) { 49 | timeByte[3-i]=(byte) (message[20+i]-0x33); 50 | } 51 | String timeString=OperationHelper.bytesToHex(timeByte);//格式为YYMMDDhhmm 52 | collectionModel.setValue(OperationHelper.bytesToValue(valueByte, 4)); 53 | }else { 54 | //没有数据 55 | collectionModel.setValue("0"); 56 | } 57 | return collectionModel; 58 | } catch (Exception e) { 59 | error=e.getMessage(); 60 | ErrorLog.writeError("解析读数据报文出错:"+e.getMessage()); 61 | return null; 62 | } 63 | } 64 | 65 | /** 66 | * 解析读电表数据 67 | * @param message 获取到的电能表数据报文 68 | * @param valueLength 数据长度 69 | * @param decimalNum 数据小数位长度 70 | * @param error 返回错误信息 71 | * @return 采集信息(错误返回null) 72 | */ 73 | public CollectionModel messageAnalysis(byte[] message,int valueLength,int decimalNum, String error) { 74 | try { 75 | CollectionModel collectionModel=new CollectionModel(); 76 | if (message[10]!=(byte)0x91) {//异常数据 77 | error=errorAnalysis(message); 78 | return null; 79 | } 80 | //电表地址 81 | byte addressByte[]=new byte[6]; 82 | for (int i = 0; i < addressByte.length; i++) { 83 | addressByte[5-i]=message[i+3]; 84 | } 85 | collectionModel.setMeterAddressCode(OperationHelper.bytesToHex(addressByte)); 86 | //标识符 87 | byte indentificationByte[]=new byte[4]; 88 | for (int i = 0; i < indentificationByte.length; i++) { 89 | indentificationByte[3-i]=(byte) (message[12+i]-0x33); 90 | } 91 | collectionModel.setIndentificationCode(OperationHelper.bytesToHex(indentificationByte)); 92 | 93 | //获取值 94 | int dataLength=message[11]; 95 | if (dataLength>4) { 96 | byte valueByte[]=new byte[valueLength]; 97 | for (int i = 0; i collectionModelList=new ArrayList(); 28 | for (byte i = 0; i < 0xA; i++) { 29 | inCode[2]=i; 30 | for (byte j = 0; j < 0x3F; j++) { 31 | inCode[1]=j; 32 | CollectionModel cModel=this.dtlMessageHandler.getCollectionModel(meterCode, inCode); 33 | if (cModel!=null) { 34 | System.out.println("获得采集数据"); 35 | collectionModelList.add(cModel); 36 | } 37 | } 38 | DataWrite myDataWrite=new DataWrite(); 39 | myDataWrite.connection(); 40 | myDataWrite.dataListWrite(collectionModelList, 1); 41 | myDataWrite.disConnection(); 42 | } 43 | } catch (Exception e) { 44 | CacheData.SocketClientMap.remove(OperationHelper.bytesToHex(meterCode)); 45 | ErrorLog.writeError("线程池错误:"+e.getMessage()); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /MeterCollection/src/club/dreamtech/meterMain/meterMain.java: -------------------------------------------------------------------------------- 1 | package club.dreamtech.meterMain; 2 | import java.net.ServerSocket; 3 | import java.net.Socket; 4 | import java.time.LocalDateTime; 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | import club.dreamtech.meterCommon.*; 9 | import club.dreamtech.meterHandler.DTLMessageAnalysis; 10 | import club.dreamtech.meterHandler.MeterCollectionHandler; 11 | import club.dreamtech.meterModel.CollectionModel; 12 | 13 | import java.time.ZoneId; 14 | import java.util.Map; 15 | 16 | import cn.edu.tsinghua.sql.Connection; 17 | import cn.edu.tsinghua.sql.ConnectionFactory; 18 | import cn.edu.tsinghua.sql.DataType; 19 | import cn.edu.tsinghua.sql.IWriter; 20 | import cn.edu.tsinghua.sql.ConnectionFactory.ConnectionType; 21 | import cn.edu.tsinghua.sql.mysql.MySQLWriter; 22 | import cn.edu.tsinghua.sql.IReader; 23 | import cn.edu.tsinghua.sql.IRegister; 24 | import cn.edu.tsinghua.sql.IResultSetIterator; 25 | 26 | public class meterMain { 27 | public static void main(String[] args) throws Exception { 28 | 29 | MeterCollectionHandler md=new MeterCollectionHandler(); 30 | md.startCollection(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /MeterCollection/src/club/dreamtech/meterMain/testMain.java: -------------------------------------------------------------------------------- 1 | package club.dreamtech.meterMain; 2 | 3 | import java.net.ServerSocket; 4 | import java.net.Socket; 5 | import java.sql.SQLException; 6 | import java.time.LocalDateTime; 7 | import java.time.ZoneId; 8 | import java.util.concurrent.ExecutorService; 9 | import java.util.concurrent.Executors; 10 | 11 | 12 | import java.time.ZoneId; 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | import java.util.Map; 16 | 17 | import club.dreamtech.meterCommon.OperationHelper; 18 | import club.dreamtech.meterModel.CollectionModel; 19 | import cn.edu.tsinghua.sql.Connection; 20 | import cn.edu.tsinghua.sql.ConnectionFactory; 21 | import cn.edu.tsinghua.sql.DataType; 22 | import cn.edu.tsinghua.sql.IWriter; 23 | import cn.edu.tsinghua.sql.ConnectionFactory.ConnectionType; 24 | import cn.edu.tsinghua.sql.mysql.MySQLWriter; 25 | import cn.edu.tsinghua.sql.IReader; 26 | import cn.edu.tsinghua.sql.IRegister; 27 | import cn.edu.tsinghua.sql.IResultSetIterator; 28 | 29 | public class testMain { 30 | public static void main(String[] args) throws Exception { 31 | 32 | 33 | 34 | Connection connection=ConnectionFactory.createConnection(ConnectionType.MYSQL, new String[]{"39.108.180.240"}, "3306", "root", "123456", "energy"); 35 | IReader reader=connection.getReader(); 36 | IWriter writer=connection.getWriter(); 37 | ((MySQLWriter) writer).clear(); 38 | connection.registerMeter( "041708762392","DTL645-2007"); 39 | connection.registerMeter( "041708762393","DTL645-2007"); 40 | connection.registerMeter( "041708762394", "DTL645-2007"); 41 | 42 | 43 | 44 | // connection.registerField("DTL645-2007", "00000000", "组合有功总电能"); 45 | // connection.registerField("DTL645-2007", "00000100", "组合有功费率1电能"); 46 | // connection.registerField("DTL645-2007", "00800000", "关联总电能"); 47 | // 48 | 49 | byte[] inCode=new byte[]{0x00,0x00,0x00,0x00}; 50 | 51 | for (byte i = 0; i < 0xA; i++) { 52 | inCode[2]=i; 53 | for (byte j = 0; j < 0x3F; j++) { 54 | inCode[1]=j; 55 | connection.registerField("DTL645-2007", OperationHelper.bytesToHex(inCode), "采集点"+ OperationHelper.bytesToHex(inCode)); 56 | System.out.println( "写入"+OperationHelper.bytesToHex(inCode)); 57 | } 58 | } 59 | 60 | 61 | long time=LocalDateTime.of(2017, 10, 5, 0, 30, 2).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); 62 | 63 | 64 | // writer.writeData(DataType.NEEDED, "DTL645-2007", "No.041708762392", "00800000", time, 5.0); 65 | // writer.writeData(DataType.ENERGY, "DTL645-2007", "No.041708762392", "00000100", time, 6.0); 66 | // writer.writeData(DataType.OTHER, "DTL645-2007", "No.041708762392", "00000000", time, 7.0); 67 | // 68 | // writer.batchWriteData(DataType.NEEDED, "DTL645-2007", "No.041708762393", new String[] {"00800000"}, time, new double[] {5.0}); 69 | writer.close(); 70 | 71 | // IResultSetIterator iterator=reader.getData(DataType.NEEDED, "DTL645-2007", new String[]{"No.041708762392"}, new String[]{"00800000"}, time-10, time+10, 5); 72 | // System.out.println("energy: meter\ttype\ttime\tvalue"); 73 | // while(iterator.hasNext()){ 74 | // System.out.println(iterator.getMeterShapeCode()+"\t"+iterator.getFieldShapeCode()+"\t"+iterator.getTimestamp()+"\t"+iterator.getDoubleValue()); 75 | // } 76 | reader.close(); 77 | 78 | connection.close(); 79 | 80 | 81 | 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /MeterCollection/src/club/dreamtech/meterModel/ClientModel.java: -------------------------------------------------------------------------------- 1 | package club.dreamtech.meterModel; 2 | 3 | import club.dreamtech.meterHandler.DTLMessageHandler; 4 | import club.dreamtech.meterHandler.MeterCollectionHandler; 5 | 6 | public class ClientModel { 7 | 8 | public ClientModel() { 9 | 10 | } 11 | 12 | public ClientModel(DTLMessageHandler myDTLMessageHandler,byte[] meterCode) { 13 | this.myDTLMessageHandler=myDTLMessageHandler; 14 | this.meterCode=meterCode; 15 | } 16 | 17 | private DTLMessageHandler myDTLMessageHandler; 18 | 19 | public DTLMessageHandler getMyDTLMessageHandler() { 20 | return myDTLMessageHandler; 21 | } 22 | 23 | public void setMyDTLMessageHandler(DTLMessageHandler myDTLMessageHandler) { 24 | this.myDTLMessageHandler = myDTLMessageHandler; 25 | } 26 | 27 | private byte[] meterCode; 28 | 29 | public byte[] getMeterCode() { 30 | return meterCode; 31 | } 32 | 33 | public void setMeterCode(byte[] meterCode) { 34 | this.meterCode = meterCode; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /MeterCollection/src/club/dreamtech/meterModel/CollectionModel.java: -------------------------------------------------------------------------------- 1 | package club.dreamtech.meterModel; 2 | 3 | /** 4 | * 采集实体类 5 | * @author cz 6 | * 7 | */ 8 | public class CollectionModel { 9 | private String meterAddressCode; 10 | private String indentificationCode; 11 | private String value; 12 | /** 13 | * 获取电表地址码 14 | * @return 15 | */ 16 | public String getMeterAddressCode() { 17 | return meterAddressCode; 18 | } 19 | /** 20 | * 设置电表地址码 21 | * @param meterAddressCode 22 | */ 23 | public void setMeterAddressCode(String meterAddressCode) { 24 | this.meterAddressCode = meterAddressCode; 25 | } 26 | /** 27 | * 获取标识码 28 | * @return 29 | */ 30 | public String getIndentificationCode() { 31 | return indentificationCode; 32 | } 33 | /** 34 | * 设置标识码 35 | * @param indentificationCode 36 | */ 37 | public void setIndentificationCode(String indentificationCode) { 38 | this.indentificationCode = indentificationCode; 39 | } 40 | /** 41 | * 获取采集值 42 | * @return 43 | */ 44 | public double getValue() { 45 | return Double.parseDouble(value); 46 | } 47 | /** 48 | * 设置采集值 49 | * @param value 50 | */ 51 | public void setValue(String value) { 52 | this.value = value; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 之前与清华大学一位博士后一起开发的一套智能电网平台,他负责上层用户操作及数据分析部分,我负责数据采集解析功能 2 | 使用java编写的智能电表采集系统,使用线程池进行采集,采集频率为5S,实现电表协议解析存入数据库 3 | 流程图 4 | -------------------------------------------------------------------------------- /flow.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/flow.jpg -------------------------------------------------------------------------------- /配置说明.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/配置说明.docx -------------------------------------------------------------------------------- /采集软件数据库字典及流程.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengchuanzhe/MeterCollection/96b6209eeba439c468125ceceb0e0d1afcee20cf/采集软件数据库字典及流程.docx --------------------------------------------------------------------------------