diff --git a/src/ACUBL.properties b/src/ACUBL.properties index 85e2c37..fc3cb09 100644 --- a/src/ACUBL.properties +++ b/src/ACUBL.properties @@ -130,4 +130,14 @@ #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u8d77\u59cb\u5730\u5740 YXL.ACU001.ZMRT.START = 70 #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u6240\u5360\u5b57\u6570 -YXL.ACU001.ZMRT.WORDCOUNT = 16 \ No newline at end of file +YXL.ACU001.ZMRT.WORDCOUNT = 16 + + +#\u4e95\u76d6\u6570\u91cf +YXL.ACU001.JG.COUNT = 4 +#\u4e95\u76d6\u7f16\u7801\u5217\u8868 +YXL.ACU001.JG.ZC.LIST = YXL.ACU001.DJG01;YXL.ACU001.RJG01;YXL.ACU001.RJG02;YXL.ACU001.RJG03 +#\u4e95\u76d6\u72b6\u6001\u8d77\u59cb\u5730\u5740 +YXL.ACU001.JGSTAT.START = 3 +#\u4e95\u76d6\u72b6\u6001\u6240\u5360\u5b57\u6570 +YXL.ACU001.JGSTAT.WORDCOUNT = 1 \ No newline at end of file diff --git a/src/ACUBL.properties b/src/ACUBL.properties index 85e2c37..fc3cb09 100644 --- a/src/ACUBL.properties +++ b/src/ACUBL.properties @@ -130,4 +130,14 @@ #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u8d77\u59cb\u5730\u5740 YXL.ACU001.ZMRT.START = 70 #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u6240\u5360\u5b57\u6570 -YXL.ACU001.ZMRT.WORDCOUNT = 16 \ No newline at end of file +YXL.ACU001.ZMRT.WORDCOUNT = 16 + + +#\u4e95\u76d6\u6570\u91cf +YXL.ACU001.JG.COUNT = 4 +#\u4e95\u76d6\u7f16\u7801\u5217\u8868 +YXL.ACU001.JG.ZC.LIST = YXL.ACU001.DJG01;YXL.ACU001.RJG01;YXL.ACU001.RJG02;YXL.ACU001.RJG03 +#\u4e95\u76d6\u72b6\u6001\u8d77\u59cb\u5730\u5740 +YXL.ACU001.JGSTAT.START = 3 +#\u4e95\u76d6\u72b6\u6001\u6240\u5360\u5b57\u6570 +YXL.ACU001.JGSTAT.WORDCOUNT = 1 \ No newline at end of file diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index 157f261..bbcc362 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -18,6 +18,7 @@ import com.szpg.task.ReadFjStatTask; import com.szpg.task.ReadHSStatusTask; import com.szpg.task.ReadHSValueTask; +import com.szpg.task.ReadJgStatusTask; import com.szpg.task.ReadO2StatusTask; import com.szpg.task.ReadO2ValueTask; import com.szpg.task.ReadSbRtTask; @@ -74,6 +75,9 @@ // 对射报警 // dsc.testSendDSStatusCommand(); + // 井盖 + dsc.testSendJgStatusCommand(); + // 风机 // dsc.testSendFjStatCommand(); // dsc.testSendFjRtCommand(); @@ -83,8 +87,9 @@ // dsc.testSendSbStatCommand(); // 照明 - dsc.testSendZmRtCommand(); - dsc.testSendZmStatCommand(); +// dsc.testSendZmRtCommand(); +// dsc.testSendZmStatCommand(); + } /** @@ -202,6 +207,15 @@ } /** + * 发送查询井盖状态命令 + * @param client + */ + private void testSendJgStatusCommand() { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.scheduleWithFixedDelay(new ReadJgStatusTask(), 30, 300, TimeUnit.SECONDS); + } + + /** * 发送查询风机运行状态命令 * @param client */ diff --git a/src/ACUBL.properties b/src/ACUBL.properties index 85e2c37..fc3cb09 100644 --- a/src/ACUBL.properties +++ b/src/ACUBL.properties @@ -130,4 +130,14 @@ #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u8d77\u59cb\u5730\u5740 YXL.ACU001.ZMRT.START = 70 #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u6240\u5360\u5b57\u6570 -YXL.ACU001.ZMRT.WORDCOUNT = 16 \ No newline at end of file +YXL.ACU001.ZMRT.WORDCOUNT = 16 + + +#\u4e95\u76d6\u6570\u91cf +YXL.ACU001.JG.COUNT = 4 +#\u4e95\u76d6\u7f16\u7801\u5217\u8868 +YXL.ACU001.JG.ZC.LIST = YXL.ACU001.DJG01;YXL.ACU001.RJG01;YXL.ACU001.RJG02;YXL.ACU001.RJG03 +#\u4e95\u76d6\u72b6\u6001\u8d77\u59cb\u5730\u5740 +YXL.ACU001.JGSTAT.START = 3 +#\u4e95\u76d6\u72b6\u6001\u6240\u5360\u5b57\u6570 +YXL.ACU001.JGSTAT.WORDCOUNT = 1 \ No newline at end of file diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index 157f261..bbcc362 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -18,6 +18,7 @@ import com.szpg.task.ReadFjStatTask; import com.szpg.task.ReadHSStatusTask; import com.szpg.task.ReadHSValueTask; +import com.szpg.task.ReadJgStatusTask; import com.szpg.task.ReadO2StatusTask; import com.szpg.task.ReadO2ValueTask; import com.szpg.task.ReadSbRtTask; @@ -74,6 +75,9 @@ // 对射报警 // dsc.testSendDSStatusCommand(); + // 井盖 + dsc.testSendJgStatusCommand(); + // 风机 // dsc.testSendFjStatCommand(); // dsc.testSendFjRtCommand(); @@ -83,8 +87,9 @@ // dsc.testSendSbStatCommand(); // 照明 - dsc.testSendZmRtCommand(); - dsc.testSendZmStatCommand(); +// dsc.testSendZmRtCommand(); +// dsc.testSendZmStatCommand(); + } /** @@ -202,6 +207,15 @@ } /** + * 发送查询井盖状态命令 + * @param client + */ + private void testSendJgStatusCommand() { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.scheduleWithFixedDelay(new ReadJgStatusTask(), 30, 300, TimeUnit.SECONDS); + } + + /** * 发送查询风机运行状态命令 * @param client */ diff --git a/src/com/szpg/db/dao/PgJgDao.java b/src/com/szpg/db/dao/PgJgDao.java new file mode 100644 index 0000000..86a4a66 --- /dev/null +++ b/src/com/szpg/db/dao/PgJgDao.java @@ -0,0 +1,24 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgJgStat; + +/** + * 井盖相关参数读写的数据库接口 + * @author admin + * + */ +public interface PgJgDao { + + /******** 井盖状态 ********/ + public List findAllStat(); + public PgJgStat findStatById(Integer id); + + public List findStatByDevice(Integer deviceid); + public List findStatByDeviceAndTm(Integer deviceid, String start, String end); + public PgJgStat findLatestStatByDevice(Integer deviceid); + + public int addSbjzRecord(PgJgStat jgstat); + public int addSbjzRecord(boolean open, boolean lock, String tm, int deviceid); +} diff --git a/src/ACUBL.properties b/src/ACUBL.properties index 85e2c37..fc3cb09 100644 --- a/src/ACUBL.properties +++ b/src/ACUBL.properties @@ -130,4 +130,14 @@ #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u8d77\u59cb\u5730\u5740 YXL.ACU001.ZMRT.START = 70 #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u6240\u5360\u5b57\u6570 -YXL.ACU001.ZMRT.WORDCOUNT = 16 \ No newline at end of file +YXL.ACU001.ZMRT.WORDCOUNT = 16 + + +#\u4e95\u76d6\u6570\u91cf +YXL.ACU001.JG.COUNT = 4 +#\u4e95\u76d6\u7f16\u7801\u5217\u8868 +YXL.ACU001.JG.ZC.LIST = YXL.ACU001.DJG01;YXL.ACU001.RJG01;YXL.ACU001.RJG02;YXL.ACU001.RJG03 +#\u4e95\u76d6\u72b6\u6001\u8d77\u59cb\u5730\u5740 +YXL.ACU001.JGSTAT.START = 3 +#\u4e95\u76d6\u72b6\u6001\u6240\u5360\u5b57\u6570 +YXL.ACU001.JGSTAT.WORDCOUNT = 1 \ No newline at end of file diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index 157f261..bbcc362 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -18,6 +18,7 @@ import com.szpg.task.ReadFjStatTask; import com.szpg.task.ReadHSStatusTask; import com.szpg.task.ReadHSValueTask; +import com.szpg.task.ReadJgStatusTask; import com.szpg.task.ReadO2StatusTask; import com.szpg.task.ReadO2ValueTask; import com.szpg.task.ReadSbRtTask; @@ -74,6 +75,9 @@ // 对射报警 // dsc.testSendDSStatusCommand(); + // 井盖 + dsc.testSendJgStatusCommand(); + // 风机 // dsc.testSendFjStatCommand(); // dsc.testSendFjRtCommand(); @@ -83,8 +87,9 @@ // dsc.testSendSbStatCommand(); // 照明 - dsc.testSendZmRtCommand(); - dsc.testSendZmStatCommand(); +// dsc.testSendZmRtCommand(); +// dsc.testSendZmStatCommand(); + } /** @@ -202,6 +207,15 @@ } /** + * 发送查询井盖状态命令 + * @param client + */ + private void testSendJgStatusCommand() { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.scheduleWithFixedDelay(new ReadJgStatusTask(), 30, 300, TimeUnit.SECONDS); + } + + /** * 发送查询风机运行状态命令 * @param client */ diff --git a/src/com/szpg/db/dao/PgJgDao.java b/src/com/szpg/db/dao/PgJgDao.java new file mode 100644 index 0000000..86a4a66 --- /dev/null +++ b/src/com/szpg/db/dao/PgJgDao.java @@ -0,0 +1,24 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgJgStat; + +/** + * 井盖相关参数读写的数据库接口 + * @author admin + * + */ +public interface PgJgDao { + + /******** 井盖状态 ********/ + public List findAllStat(); + public PgJgStat findStatById(Integer id); + + public List findStatByDevice(Integer deviceid); + public List findStatByDeviceAndTm(Integer deviceid, String start, String end); + public PgJgStat findLatestStatByDevice(Integer deviceid); + + public int addSbjzRecord(PgJgStat jgstat); + public int addSbjzRecord(boolean open, boolean lock, String tm, int deviceid); +} diff --git a/src/com/szpg/db/dao/impl/PgJgDaoImpl.java b/src/com/szpg/db/dao/impl/PgJgDaoImpl.java new file mode 100644 index 0000000..557e73e --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgJgDaoImpl.java @@ -0,0 +1,314 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.ArrayListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgJgDao; +import com.szpg.db.data.PgJgStat; +import com.szpg.db.util.ConnectionManager; + +public class PgJgDaoImpl implements PgJgDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAllStat() { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("查询所有井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("查询所有井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgJgStat findStatById(Integer id) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler(), param); + + if (null != tempList && tempList.size() == 1) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(0); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + logger.debug("根据ID查询井盖状态成功" + stat + "]"); + + return stat; + } + else + return null; + } catch (Exception ex) { + logger.error("根据ID查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public List findStatByDevice(Integer deviceid) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ?"; + Object[] param = new Object[1]; + param[0] = deviceid; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("根据设备ID查询井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("根据设备ID查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public List findStatByDeviceAndTm(Integer deviceid, String start, String end) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ? AND LOGTIME >= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') AND LOGTIME <= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')"; + Object[] params = new Object[3]; + params[0] = deviceid; + params[1] = start; + params[2] = end; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("根据设备ID和时间查询井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("根据设备ID和时间查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgJgStat findLatestStatByDevice(Integer deviceid) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ? ORDER BY LOGTIME DESC"; + Object[] param = new Object[1]; + param[0] = deviceid; + try { + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(0); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + logger.debug("根据设备ID查询最新的井盖状态成功" + stat); + return stat; + } + else + return null; + } catch (Exception ex) { + logger.error("根据ID查询最新井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addSbjzRecord(PgJgStat jgstat) { + return addSbjzRecord(jgstat.getJgopen(), jgstat.getJglock(), jgstat.getTmStr(), jgstat.getPgdeviceid()); + } + + @Override + public int addSbjzRecord(boolean open, boolean lock, String tm, int deviceid) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_JG_STAT " + + "(JGOPEN, JGLOCK, LOGTIME, UPTIME, PGDEVICEID) " + + "VALUES (?, ?, TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), ?)"; + Object[] params = new Object[5]; + params[0] = open; + params[1] = lock; + params[2] = tm; + params[3] = tm; + params[4] = deviceid; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入井盖状态成功[tm=" + tm + ", open=" + open + ", lock=" + lock + ", deviceid=" + deviceid + "]"); + else + logger.error("插入井盖状态失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + +} diff --git a/src/ACUBL.properties b/src/ACUBL.properties index 85e2c37..fc3cb09 100644 --- a/src/ACUBL.properties +++ b/src/ACUBL.properties @@ -130,4 +130,14 @@ #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u8d77\u59cb\u5730\u5740 YXL.ACU001.ZMRT.START = 70 #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u6240\u5360\u5b57\u6570 -YXL.ACU001.ZMRT.WORDCOUNT = 16 \ No newline at end of file +YXL.ACU001.ZMRT.WORDCOUNT = 16 + + +#\u4e95\u76d6\u6570\u91cf +YXL.ACU001.JG.COUNT = 4 +#\u4e95\u76d6\u7f16\u7801\u5217\u8868 +YXL.ACU001.JG.ZC.LIST = YXL.ACU001.DJG01;YXL.ACU001.RJG01;YXL.ACU001.RJG02;YXL.ACU001.RJG03 +#\u4e95\u76d6\u72b6\u6001\u8d77\u59cb\u5730\u5740 +YXL.ACU001.JGSTAT.START = 3 +#\u4e95\u76d6\u72b6\u6001\u6240\u5360\u5b57\u6570 +YXL.ACU001.JGSTAT.WORDCOUNT = 1 \ No newline at end of file diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index 157f261..bbcc362 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -18,6 +18,7 @@ import com.szpg.task.ReadFjStatTask; import com.szpg.task.ReadHSStatusTask; import com.szpg.task.ReadHSValueTask; +import com.szpg.task.ReadJgStatusTask; import com.szpg.task.ReadO2StatusTask; import com.szpg.task.ReadO2ValueTask; import com.szpg.task.ReadSbRtTask; @@ -74,6 +75,9 @@ // 对射报警 // dsc.testSendDSStatusCommand(); + // 井盖 + dsc.testSendJgStatusCommand(); + // 风机 // dsc.testSendFjStatCommand(); // dsc.testSendFjRtCommand(); @@ -83,8 +87,9 @@ // dsc.testSendSbStatCommand(); // 照明 - dsc.testSendZmRtCommand(); - dsc.testSendZmStatCommand(); +// dsc.testSendZmRtCommand(); +// dsc.testSendZmStatCommand(); + } /** @@ -202,6 +207,15 @@ } /** + * 发送查询井盖状态命令 + * @param client + */ + private void testSendJgStatusCommand() { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.scheduleWithFixedDelay(new ReadJgStatusTask(), 30, 300, TimeUnit.SECONDS); + } + + /** * 发送查询风机运行状态命令 * @param client */ diff --git a/src/com/szpg/db/dao/PgJgDao.java b/src/com/szpg/db/dao/PgJgDao.java new file mode 100644 index 0000000..86a4a66 --- /dev/null +++ b/src/com/szpg/db/dao/PgJgDao.java @@ -0,0 +1,24 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgJgStat; + +/** + * 井盖相关参数读写的数据库接口 + * @author admin + * + */ +public interface PgJgDao { + + /******** 井盖状态 ********/ + public List findAllStat(); + public PgJgStat findStatById(Integer id); + + public List findStatByDevice(Integer deviceid); + public List findStatByDeviceAndTm(Integer deviceid, String start, String end); + public PgJgStat findLatestStatByDevice(Integer deviceid); + + public int addSbjzRecord(PgJgStat jgstat); + public int addSbjzRecord(boolean open, boolean lock, String tm, int deviceid); +} diff --git a/src/com/szpg/db/dao/impl/PgJgDaoImpl.java b/src/com/szpg/db/dao/impl/PgJgDaoImpl.java new file mode 100644 index 0000000..557e73e --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgJgDaoImpl.java @@ -0,0 +1,314 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.ArrayListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgJgDao; +import com.szpg.db.data.PgJgStat; +import com.szpg.db.util.ConnectionManager; + +public class PgJgDaoImpl implements PgJgDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAllStat() { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("查询所有井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("查询所有井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgJgStat findStatById(Integer id) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler(), param); + + if (null != tempList && tempList.size() == 1) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(0); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + logger.debug("根据ID查询井盖状态成功" + stat + "]"); + + return stat; + } + else + return null; + } catch (Exception ex) { + logger.error("根据ID查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public List findStatByDevice(Integer deviceid) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ?"; + Object[] param = new Object[1]; + param[0] = deviceid; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("根据设备ID查询井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("根据设备ID查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public List findStatByDeviceAndTm(Integer deviceid, String start, String end) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ? AND LOGTIME >= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') AND LOGTIME <= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')"; + Object[] params = new Object[3]; + params[0] = deviceid; + params[1] = start; + params[2] = end; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("根据设备ID和时间查询井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("根据设备ID和时间查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgJgStat findLatestStatByDevice(Integer deviceid) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ? ORDER BY LOGTIME DESC"; + Object[] param = new Object[1]; + param[0] = deviceid; + try { + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(0); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + logger.debug("根据设备ID查询最新的井盖状态成功" + stat); + return stat; + } + else + return null; + } catch (Exception ex) { + logger.error("根据ID查询最新井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addSbjzRecord(PgJgStat jgstat) { + return addSbjzRecord(jgstat.getJgopen(), jgstat.getJglock(), jgstat.getTmStr(), jgstat.getPgdeviceid()); + } + + @Override + public int addSbjzRecord(boolean open, boolean lock, String tm, int deviceid) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_JG_STAT " + + "(JGOPEN, JGLOCK, LOGTIME, UPTIME, PGDEVICEID) " + + "VALUES (?, ?, TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), ?)"; + Object[] params = new Object[5]; + params[0] = open; + params[1] = lock; + params[2] = tm; + params[3] = tm; + params[4] = deviceid; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入井盖状态成功[tm=" + tm + ", open=" + open + ", lock=" + lock + ", deviceid=" + deviceid + "]"); + else + logger.error("插入井盖状态失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + +} diff --git a/src/com/szpg/db/data/PgJgStat.java b/src/com/szpg/db/data/PgJgStat.java new file mode 100644 index 0000000..5d1425a --- /dev/null +++ b/src/com/szpg/db/data/PgJgStat.java @@ -0,0 +1,100 @@ +package com.szpg.db.data; + +import java.util.Date; + +import com.szpg.util.TimeFormat; + +/** + * 井盖状态数据表 + * + * @author admin + * + */ +public class PgJgStat implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -3706226324665928352L; + + private Integer id; + private Date logtime; + private Date uptime; + private Integer pgdeviceid; + private Boolean jgopen; + private Boolean jglock; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Date getLogtime() { + return logtime; + } + + public void setLogtime(Date logtime) { + this.logtime = logtime; + } + + public Date getUptime() { + return uptime; + } + + public void setUptime(Date uptime) { + this.uptime = uptime; + } + + public Integer getPgdeviceid() { + return pgdeviceid; + } + + public void setPgdeviceid(Integer pgdeviceid) { + this.pgdeviceid = pgdeviceid; + } + + public Boolean getJgopen() { + return jgopen; + } + + public void setJgopen(Boolean jgopen) { + this.jgopen = jgopen; + } + + public Boolean getJglock() { + return jglock; + } + + public void setJglock(Boolean jglock) { + this.jglock = jglock; + } + + public void setTm(Date tm) { + this.logtime = tm; + this.uptime = tm; + } + + public String getTmStr() { + if (null != this.logtime) { + return TimeFormat.formatTimestamp(logtime); + } else { + return ""; + } + } + + public String getTmStrGB() { + if (null != this.logtime) { + return TimeFormat.format(logtime, "yyyyMMddHHmmss"); + } else { + return ""; + } + } + + @Override + public String toString() { + return "PgJgStat[open=" + jgopen + ", lock=" + jglock + ", logtime=" + TimeFormat.formatTimestamp(logtime) + "]"; + } +} diff --git a/src/ACUBL.properties b/src/ACUBL.properties index 85e2c37..fc3cb09 100644 --- a/src/ACUBL.properties +++ b/src/ACUBL.properties @@ -130,4 +130,14 @@ #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u8d77\u59cb\u5730\u5740 YXL.ACU001.ZMRT.START = 70 #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u6240\u5360\u5b57\u6570 -YXL.ACU001.ZMRT.WORDCOUNT = 16 \ No newline at end of file +YXL.ACU001.ZMRT.WORDCOUNT = 16 + + +#\u4e95\u76d6\u6570\u91cf +YXL.ACU001.JG.COUNT = 4 +#\u4e95\u76d6\u7f16\u7801\u5217\u8868 +YXL.ACU001.JG.ZC.LIST = YXL.ACU001.DJG01;YXL.ACU001.RJG01;YXL.ACU001.RJG02;YXL.ACU001.RJG03 +#\u4e95\u76d6\u72b6\u6001\u8d77\u59cb\u5730\u5740 +YXL.ACU001.JGSTAT.START = 3 +#\u4e95\u76d6\u72b6\u6001\u6240\u5360\u5b57\u6570 +YXL.ACU001.JGSTAT.WORDCOUNT = 1 \ No newline at end of file diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index 157f261..bbcc362 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -18,6 +18,7 @@ import com.szpg.task.ReadFjStatTask; import com.szpg.task.ReadHSStatusTask; import com.szpg.task.ReadHSValueTask; +import com.szpg.task.ReadJgStatusTask; import com.szpg.task.ReadO2StatusTask; import com.szpg.task.ReadO2ValueTask; import com.szpg.task.ReadSbRtTask; @@ -74,6 +75,9 @@ // 对射报警 // dsc.testSendDSStatusCommand(); + // 井盖 + dsc.testSendJgStatusCommand(); + // 风机 // dsc.testSendFjStatCommand(); // dsc.testSendFjRtCommand(); @@ -83,8 +87,9 @@ // dsc.testSendSbStatCommand(); // 照明 - dsc.testSendZmRtCommand(); - dsc.testSendZmStatCommand(); +// dsc.testSendZmRtCommand(); +// dsc.testSendZmStatCommand(); + } /** @@ -202,6 +207,15 @@ } /** + * 发送查询井盖状态命令 + * @param client + */ + private void testSendJgStatusCommand() { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.scheduleWithFixedDelay(new ReadJgStatusTask(), 30, 300, TimeUnit.SECONDS); + } + + /** * 发送查询风机运行状态命令 * @param client */ diff --git a/src/com/szpg/db/dao/PgJgDao.java b/src/com/szpg/db/dao/PgJgDao.java new file mode 100644 index 0000000..86a4a66 --- /dev/null +++ b/src/com/szpg/db/dao/PgJgDao.java @@ -0,0 +1,24 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgJgStat; + +/** + * 井盖相关参数读写的数据库接口 + * @author admin + * + */ +public interface PgJgDao { + + /******** 井盖状态 ********/ + public List findAllStat(); + public PgJgStat findStatById(Integer id); + + public List findStatByDevice(Integer deviceid); + public List findStatByDeviceAndTm(Integer deviceid, String start, String end); + public PgJgStat findLatestStatByDevice(Integer deviceid); + + public int addSbjzRecord(PgJgStat jgstat); + public int addSbjzRecord(boolean open, boolean lock, String tm, int deviceid); +} diff --git a/src/com/szpg/db/dao/impl/PgJgDaoImpl.java b/src/com/szpg/db/dao/impl/PgJgDaoImpl.java new file mode 100644 index 0000000..557e73e --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgJgDaoImpl.java @@ -0,0 +1,314 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.ArrayListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgJgDao; +import com.szpg.db.data.PgJgStat; +import com.szpg.db.util.ConnectionManager; + +public class PgJgDaoImpl implements PgJgDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAllStat() { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("查询所有井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("查询所有井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgJgStat findStatById(Integer id) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler(), param); + + if (null != tempList && tempList.size() == 1) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(0); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + logger.debug("根据ID查询井盖状态成功" + stat + "]"); + + return stat; + } + else + return null; + } catch (Exception ex) { + logger.error("根据ID查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public List findStatByDevice(Integer deviceid) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ?"; + Object[] param = new Object[1]; + param[0] = deviceid; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("根据设备ID查询井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("根据设备ID查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public List findStatByDeviceAndTm(Integer deviceid, String start, String end) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ? AND LOGTIME >= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') AND LOGTIME <= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')"; + Object[] params = new Object[3]; + params[0] = deviceid; + params[1] = start; + params[2] = end; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("根据设备ID和时间查询井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("根据设备ID和时间查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgJgStat findLatestStatByDevice(Integer deviceid) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ? ORDER BY LOGTIME DESC"; + Object[] param = new Object[1]; + param[0] = deviceid; + try { + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(0); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + logger.debug("根据设备ID查询最新的井盖状态成功" + stat); + return stat; + } + else + return null; + } catch (Exception ex) { + logger.error("根据ID查询最新井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addSbjzRecord(PgJgStat jgstat) { + return addSbjzRecord(jgstat.getJgopen(), jgstat.getJglock(), jgstat.getTmStr(), jgstat.getPgdeviceid()); + } + + @Override + public int addSbjzRecord(boolean open, boolean lock, String tm, int deviceid) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_JG_STAT " + + "(JGOPEN, JGLOCK, LOGTIME, UPTIME, PGDEVICEID) " + + "VALUES (?, ?, TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), ?)"; + Object[] params = new Object[5]; + params[0] = open; + params[1] = lock; + params[2] = tm; + params[3] = tm; + params[4] = deviceid; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入井盖状态成功[tm=" + tm + ", open=" + open + ", lock=" + lock + ", deviceid=" + deviceid + "]"); + else + logger.error("插入井盖状态失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + +} diff --git a/src/com/szpg/db/data/PgJgStat.java b/src/com/szpg/db/data/PgJgStat.java new file mode 100644 index 0000000..5d1425a --- /dev/null +++ b/src/com/szpg/db/data/PgJgStat.java @@ -0,0 +1,100 @@ +package com.szpg.db.data; + +import java.util.Date; + +import com.szpg.util.TimeFormat; + +/** + * 井盖状态数据表 + * + * @author admin + * + */ +public class PgJgStat implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -3706226324665928352L; + + private Integer id; + private Date logtime; + private Date uptime; + private Integer pgdeviceid; + private Boolean jgopen; + private Boolean jglock; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Date getLogtime() { + return logtime; + } + + public void setLogtime(Date logtime) { + this.logtime = logtime; + } + + public Date getUptime() { + return uptime; + } + + public void setUptime(Date uptime) { + this.uptime = uptime; + } + + public Integer getPgdeviceid() { + return pgdeviceid; + } + + public void setPgdeviceid(Integer pgdeviceid) { + this.pgdeviceid = pgdeviceid; + } + + public Boolean getJgopen() { + return jgopen; + } + + public void setJgopen(Boolean jgopen) { + this.jgopen = jgopen; + } + + public Boolean getJglock() { + return jglock; + } + + public void setJglock(Boolean jglock) { + this.jglock = jglock; + } + + public void setTm(Date tm) { + this.logtime = tm; + this.uptime = tm; + } + + public String getTmStr() { + if (null != this.logtime) { + return TimeFormat.formatTimestamp(logtime); + } else { + return ""; + } + } + + public String getTmStrGB() { + if (null != this.logtime) { + return TimeFormat.format(logtime, "yyyyMMddHHmmss"); + } else { + return ""; + } + } + + @Override + public String toString() { + return "PgJgStat[open=" + jgopen + ", lock=" + jglock + ", logtime=" + TimeFormat.formatTimestamp(logtime) + "]"; + } +} diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 171b537..3107ff1 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -22,4 +22,5 @@ public final static String CMD_TYPE_READSBRUNTIME = "26"; //读取水泵运行时长命令 public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 + public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 } diff --git a/src/ACUBL.properties b/src/ACUBL.properties index 85e2c37..fc3cb09 100644 --- a/src/ACUBL.properties +++ b/src/ACUBL.properties @@ -130,4 +130,14 @@ #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u8d77\u59cb\u5730\u5740 YXL.ACU001.ZMRT.START = 70 #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u6240\u5360\u5b57\u6570 -YXL.ACU001.ZMRT.WORDCOUNT = 16 \ No newline at end of file +YXL.ACU001.ZMRT.WORDCOUNT = 16 + + +#\u4e95\u76d6\u6570\u91cf +YXL.ACU001.JG.COUNT = 4 +#\u4e95\u76d6\u7f16\u7801\u5217\u8868 +YXL.ACU001.JG.ZC.LIST = YXL.ACU001.DJG01;YXL.ACU001.RJG01;YXL.ACU001.RJG02;YXL.ACU001.RJG03 +#\u4e95\u76d6\u72b6\u6001\u8d77\u59cb\u5730\u5740 +YXL.ACU001.JGSTAT.START = 3 +#\u4e95\u76d6\u72b6\u6001\u6240\u5360\u5b57\u6570 +YXL.ACU001.JGSTAT.WORDCOUNT = 1 \ No newline at end of file diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index 157f261..bbcc362 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -18,6 +18,7 @@ import com.szpg.task.ReadFjStatTask; import com.szpg.task.ReadHSStatusTask; import com.szpg.task.ReadHSValueTask; +import com.szpg.task.ReadJgStatusTask; import com.szpg.task.ReadO2StatusTask; import com.szpg.task.ReadO2ValueTask; import com.szpg.task.ReadSbRtTask; @@ -74,6 +75,9 @@ // 对射报警 // dsc.testSendDSStatusCommand(); + // 井盖 + dsc.testSendJgStatusCommand(); + // 风机 // dsc.testSendFjStatCommand(); // dsc.testSendFjRtCommand(); @@ -83,8 +87,9 @@ // dsc.testSendSbStatCommand(); // 照明 - dsc.testSendZmRtCommand(); - dsc.testSendZmStatCommand(); +// dsc.testSendZmRtCommand(); +// dsc.testSendZmStatCommand(); + } /** @@ -202,6 +207,15 @@ } /** + * 发送查询井盖状态命令 + * @param client + */ + private void testSendJgStatusCommand() { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.scheduleWithFixedDelay(new ReadJgStatusTask(), 30, 300, TimeUnit.SECONDS); + } + + /** * 发送查询风机运行状态命令 * @param client */ diff --git a/src/com/szpg/db/dao/PgJgDao.java b/src/com/szpg/db/dao/PgJgDao.java new file mode 100644 index 0000000..86a4a66 --- /dev/null +++ b/src/com/szpg/db/dao/PgJgDao.java @@ -0,0 +1,24 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgJgStat; + +/** + * 井盖相关参数读写的数据库接口 + * @author admin + * + */ +public interface PgJgDao { + + /******** 井盖状态 ********/ + public List findAllStat(); + public PgJgStat findStatById(Integer id); + + public List findStatByDevice(Integer deviceid); + public List findStatByDeviceAndTm(Integer deviceid, String start, String end); + public PgJgStat findLatestStatByDevice(Integer deviceid); + + public int addSbjzRecord(PgJgStat jgstat); + public int addSbjzRecord(boolean open, boolean lock, String tm, int deviceid); +} diff --git a/src/com/szpg/db/dao/impl/PgJgDaoImpl.java b/src/com/szpg/db/dao/impl/PgJgDaoImpl.java new file mode 100644 index 0000000..557e73e --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgJgDaoImpl.java @@ -0,0 +1,314 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.ArrayListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgJgDao; +import com.szpg.db.data.PgJgStat; +import com.szpg.db.util.ConnectionManager; + +public class PgJgDaoImpl implements PgJgDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAllStat() { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("查询所有井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("查询所有井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgJgStat findStatById(Integer id) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler(), param); + + if (null != tempList && tempList.size() == 1) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(0); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + logger.debug("根据ID查询井盖状态成功" + stat + "]"); + + return stat; + } + else + return null; + } catch (Exception ex) { + logger.error("根据ID查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public List findStatByDevice(Integer deviceid) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ?"; + Object[] param = new Object[1]; + param[0] = deviceid; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("根据设备ID查询井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("根据设备ID查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public List findStatByDeviceAndTm(Integer deviceid, String start, String end) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ? AND LOGTIME >= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') AND LOGTIME <= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')"; + Object[] params = new Object[3]; + params[0] = deviceid; + params[1] = start; + params[2] = end; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("根据设备ID和时间查询井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("根据设备ID和时间查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgJgStat findLatestStatByDevice(Integer deviceid) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ? ORDER BY LOGTIME DESC"; + Object[] param = new Object[1]; + param[0] = deviceid; + try { + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(0); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + logger.debug("根据设备ID查询最新的井盖状态成功" + stat); + return stat; + } + else + return null; + } catch (Exception ex) { + logger.error("根据ID查询最新井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addSbjzRecord(PgJgStat jgstat) { + return addSbjzRecord(jgstat.getJgopen(), jgstat.getJglock(), jgstat.getTmStr(), jgstat.getPgdeviceid()); + } + + @Override + public int addSbjzRecord(boolean open, boolean lock, String tm, int deviceid) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_JG_STAT " + + "(JGOPEN, JGLOCK, LOGTIME, UPTIME, PGDEVICEID) " + + "VALUES (?, ?, TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), ?)"; + Object[] params = new Object[5]; + params[0] = open; + params[1] = lock; + params[2] = tm; + params[3] = tm; + params[4] = deviceid; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入井盖状态成功[tm=" + tm + ", open=" + open + ", lock=" + lock + ", deviceid=" + deviceid + "]"); + else + logger.error("插入井盖状态失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + +} diff --git a/src/com/szpg/db/data/PgJgStat.java b/src/com/szpg/db/data/PgJgStat.java new file mode 100644 index 0000000..5d1425a --- /dev/null +++ b/src/com/szpg/db/data/PgJgStat.java @@ -0,0 +1,100 @@ +package com.szpg.db.data; + +import java.util.Date; + +import com.szpg.util.TimeFormat; + +/** + * 井盖状态数据表 + * + * @author admin + * + */ +public class PgJgStat implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -3706226324665928352L; + + private Integer id; + private Date logtime; + private Date uptime; + private Integer pgdeviceid; + private Boolean jgopen; + private Boolean jglock; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Date getLogtime() { + return logtime; + } + + public void setLogtime(Date logtime) { + this.logtime = logtime; + } + + public Date getUptime() { + return uptime; + } + + public void setUptime(Date uptime) { + this.uptime = uptime; + } + + public Integer getPgdeviceid() { + return pgdeviceid; + } + + public void setPgdeviceid(Integer pgdeviceid) { + this.pgdeviceid = pgdeviceid; + } + + public Boolean getJgopen() { + return jgopen; + } + + public void setJgopen(Boolean jgopen) { + this.jgopen = jgopen; + } + + public Boolean getJglock() { + return jglock; + } + + public void setJglock(Boolean jglock) { + this.jglock = jglock; + } + + public void setTm(Date tm) { + this.logtime = tm; + this.uptime = tm; + } + + public String getTmStr() { + if (null != this.logtime) { + return TimeFormat.formatTimestamp(logtime); + } else { + return ""; + } + } + + public String getTmStrGB() { + if (null != this.logtime) { + return TimeFormat.format(logtime, "yyyyMMddHHmmss"); + } else { + return ""; + } + } + + @Override + public String toString() { + return "PgJgStat[open=" + jgopen + ", lock=" + jglock + ", logtime=" + TimeFormat.formatTimestamp(logtime) + "]"; + } +} diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 171b537..3107ff1 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -22,4 +22,5 @@ public final static String CMD_TYPE_READSBRUNTIME = "26"; //读取水泵运行时长命令 public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 + public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index 4608bdd..ca1f453 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -10,6 +10,7 @@ import com.szpg.plc.message.command.read.ReadFjStatCommand; import com.szpg.plc.message.command.read.ReadHSStatusCommand; import com.szpg.plc.message.command.read.ReadHSValueCommand; +import com.szpg.plc.message.command.read.ReadJgStatusCommand; import com.szpg.plc.message.command.read.ReadO2StatusCommand; import com.szpg.plc.message.command.read.ReadO2ValueCommand; import com.szpg.plc.message.command.read.ReadSbRtCommand; @@ -98,6 +99,9 @@ case AppMessageConstants.CMD_TYPE_READDSSTATUS: return new ReadDSStatusCommand(); + case AppMessageConstants.CMD_TYPE_READJGSTATUS: + return new ReadJgStatusCommand(); + case AppMessageConstants.CMD_TYPE_READFJSTAT: return new ReadFjStatCommand(); case AppMessageConstants.CMD_TYPE_READFJRUNTIME: diff --git a/src/ACUBL.properties b/src/ACUBL.properties index 85e2c37..fc3cb09 100644 --- a/src/ACUBL.properties +++ b/src/ACUBL.properties @@ -130,4 +130,14 @@ #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u8d77\u59cb\u5730\u5740 YXL.ACU001.ZMRT.START = 70 #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u6240\u5360\u5b57\u6570 -YXL.ACU001.ZMRT.WORDCOUNT = 16 \ No newline at end of file +YXL.ACU001.ZMRT.WORDCOUNT = 16 + + +#\u4e95\u76d6\u6570\u91cf +YXL.ACU001.JG.COUNT = 4 +#\u4e95\u76d6\u7f16\u7801\u5217\u8868 +YXL.ACU001.JG.ZC.LIST = YXL.ACU001.DJG01;YXL.ACU001.RJG01;YXL.ACU001.RJG02;YXL.ACU001.RJG03 +#\u4e95\u76d6\u72b6\u6001\u8d77\u59cb\u5730\u5740 +YXL.ACU001.JGSTAT.START = 3 +#\u4e95\u76d6\u72b6\u6001\u6240\u5360\u5b57\u6570 +YXL.ACU001.JGSTAT.WORDCOUNT = 1 \ No newline at end of file diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index 157f261..bbcc362 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -18,6 +18,7 @@ import com.szpg.task.ReadFjStatTask; import com.szpg.task.ReadHSStatusTask; import com.szpg.task.ReadHSValueTask; +import com.szpg.task.ReadJgStatusTask; import com.szpg.task.ReadO2StatusTask; import com.szpg.task.ReadO2ValueTask; import com.szpg.task.ReadSbRtTask; @@ -74,6 +75,9 @@ // 对射报警 // dsc.testSendDSStatusCommand(); + // 井盖 + dsc.testSendJgStatusCommand(); + // 风机 // dsc.testSendFjStatCommand(); // dsc.testSendFjRtCommand(); @@ -83,8 +87,9 @@ // dsc.testSendSbStatCommand(); // 照明 - dsc.testSendZmRtCommand(); - dsc.testSendZmStatCommand(); +// dsc.testSendZmRtCommand(); +// dsc.testSendZmStatCommand(); + } /** @@ -202,6 +207,15 @@ } /** + * 发送查询井盖状态命令 + * @param client + */ + private void testSendJgStatusCommand() { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.scheduleWithFixedDelay(new ReadJgStatusTask(), 30, 300, TimeUnit.SECONDS); + } + + /** * 发送查询风机运行状态命令 * @param client */ diff --git a/src/com/szpg/db/dao/PgJgDao.java b/src/com/szpg/db/dao/PgJgDao.java new file mode 100644 index 0000000..86a4a66 --- /dev/null +++ b/src/com/szpg/db/dao/PgJgDao.java @@ -0,0 +1,24 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgJgStat; + +/** + * 井盖相关参数读写的数据库接口 + * @author admin + * + */ +public interface PgJgDao { + + /******** 井盖状态 ********/ + public List findAllStat(); + public PgJgStat findStatById(Integer id); + + public List findStatByDevice(Integer deviceid); + public List findStatByDeviceAndTm(Integer deviceid, String start, String end); + public PgJgStat findLatestStatByDevice(Integer deviceid); + + public int addSbjzRecord(PgJgStat jgstat); + public int addSbjzRecord(boolean open, boolean lock, String tm, int deviceid); +} diff --git a/src/com/szpg/db/dao/impl/PgJgDaoImpl.java b/src/com/szpg/db/dao/impl/PgJgDaoImpl.java new file mode 100644 index 0000000..557e73e --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgJgDaoImpl.java @@ -0,0 +1,314 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.ArrayListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgJgDao; +import com.szpg.db.data.PgJgStat; +import com.szpg.db.util.ConnectionManager; + +public class PgJgDaoImpl implements PgJgDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAllStat() { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("查询所有井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("查询所有井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgJgStat findStatById(Integer id) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler(), param); + + if (null != tempList && tempList.size() == 1) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(0); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + logger.debug("根据ID查询井盖状态成功" + stat + "]"); + + return stat; + } + else + return null; + } catch (Exception ex) { + logger.error("根据ID查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public List findStatByDevice(Integer deviceid) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ?"; + Object[] param = new Object[1]; + param[0] = deviceid; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("根据设备ID查询井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("根据设备ID查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public List findStatByDeviceAndTm(Integer deviceid, String start, String end) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ? AND LOGTIME >= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') AND LOGTIME <= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')"; + Object[] params = new Object[3]; + params[0] = deviceid; + params[1] = start; + params[2] = end; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("根据设备ID和时间查询井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("根据设备ID和时间查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgJgStat findLatestStatByDevice(Integer deviceid) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ? ORDER BY LOGTIME DESC"; + Object[] param = new Object[1]; + param[0] = deviceid; + try { + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(0); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + logger.debug("根据设备ID查询最新的井盖状态成功" + stat); + return stat; + } + else + return null; + } catch (Exception ex) { + logger.error("根据ID查询最新井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addSbjzRecord(PgJgStat jgstat) { + return addSbjzRecord(jgstat.getJgopen(), jgstat.getJglock(), jgstat.getTmStr(), jgstat.getPgdeviceid()); + } + + @Override + public int addSbjzRecord(boolean open, boolean lock, String tm, int deviceid) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_JG_STAT " + + "(JGOPEN, JGLOCK, LOGTIME, UPTIME, PGDEVICEID) " + + "VALUES (?, ?, TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), ?)"; + Object[] params = new Object[5]; + params[0] = open; + params[1] = lock; + params[2] = tm; + params[3] = tm; + params[4] = deviceid; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入井盖状态成功[tm=" + tm + ", open=" + open + ", lock=" + lock + ", deviceid=" + deviceid + "]"); + else + logger.error("插入井盖状态失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + +} diff --git a/src/com/szpg/db/data/PgJgStat.java b/src/com/szpg/db/data/PgJgStat.java new file mode 100644 index 0000000..5d1425a --- /dev/null +++ b/src/com/szpg/db/data/PgJgStat.java @@ -0,0 +1,100 @@ +package com.szpg.db.data; + +import java.util.Date; + +import com.szpg.util.TimeFormat; + +/** + * 井盖状态数据表 + * + * @author admin + * + */ +public class PgJgStat implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -3706226324665928352L; + + private Integer id; + private Date logtime; + private Date uptime; + private Integer pgdeviceid; + private Boolean jgopen; + private Boolean jglock; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Date getLogtime() { + return logtime; + } + + public void setLogtime(Date logtime) { + this.logtime = logtime; + } + + public Date getUptime() { + return uptime; + } + + public void setUptime(Date uptime) { + this.uptime = uptime; + } + + public Integer getPgdeviceid() { + return pgdeviceid; + } + + public void setPgdeviceid(Integer pgdeviceid) { + this.pgdeviceid = pgdeviceid; + } + + public Boolean getJgopen() { + return jgopen; + } + + public void setJgopen(Boolean jgopen) { + this.jgopen = jgopen; + } + + public Boolean getJglock() { + return jglock; + } + + public void setJglock(Boolean jglock) { + this.jglock = jglock; + } + + public void setTm(Date tm) { + this.logtime = tm; + this.uptime = tm; + } + + public String getTmStr() { + if (null != this.logtime) { + return TimeFormat.formatTimestamp(logtime); + } else { + return ""; + } + } + + public String getTmStrGB() { + if (null != this.logtime) { + return TimeFormat.format(logtime, "yyyyMMddHHmmss"); + } else { + return ""; + } + } + + @Override + public String toString() { + return "PgJgStat[open=" + jgopen + ", lock=" + jglock + ", logtime=" + TimeFormat.formatTimestamp(logtime) + "]"; + } +} diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 171b537..3107ff1 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -22,4 +22,5 @@ public final static String CMD_TYPE_READSBRUNTIME = "26"; //读取水泵运行时长命令 public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 + public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index 4608bdd..ca1f453 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -10,6 +10,7 @@ import com.szpg.plc.message.command.read.ReadFjStatCommand; import com.szpg.plc.message.command.read.ReadHSStatusCommand; import com.szpg.plc.message.command.read.ReadHSValueCommand; +import com.szpg.plc.message.command.read.ReadJgStatusCommand; import com.szpg.plc.message.command.read.ReadO2StatusCommand; import com.szpg.plc.message.command.read.ReadO2ValueCommand; import com.szpg.plc.message.command.read.ReadSbRtCommand; @@ -98,6 +99,9 @@ case AppMessageConstants.CMD_TYPE_READDSSTATUS: return new ReadDSStatusCommand(); + case AppMessageConstants.CMD_TYPE_READJGSTATUS: + return new ReadJgStatusCommand(); + case AppMessageConstants.CMD_TYPE_READFJSTAT: return new ReadFjStatCommand(); case AppMessageConstants.CMD_TYPE_READFJRUNTIME: diff --git a/src/com/szpg/plc/message/command/read/ReadJgStatusCommand.java b/src/com/szpg/plc/message/command/read/ReadJgStatusCommand.java new file mode 100644 index 0000000..d78dcfd --- /dev/null +++ b/src/com/szpg/plc/message/command/read/ReadJgStatusCommand.java @@ -0,0 +1,29 @@ +package com.szpg.plc.message.command.read; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.response.read.ReadJgStatusCommandResponse; + +public class ReadJgStatusCommand extends ReadMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -695129650895569926L; + + @Override + public Class getResponseClass() { + return ReadJgStatusCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送查询井盖状态命令"; + } + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_READJGSTATUS; + } + +} diff --git a/src/ACUBL.properties b/src/ACUBL.properties index 85e2c37..fc3cb09 100644 --- a/src/ACUBL.properties +++ b/src/ACUBL.properties @@ -130,4 +130,14 @@ #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u8d77\u59cb\u5730\u5740 YXL.ACU001.ZMRT.START = 70 #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u6240\u5360\u5b57\u6570 -YXL.ACU001.ZMRT.WORDCOUNT = 16 \ No newline at end of file +YXL.ACU001.ZMRT.WORDCOUNT = 16 + + +#\u4e95\u76d6\u6570\u91cf +YXL.ACU001.JG.COUNT = 4 +#\u4e95\u76d6\u7f16\u7801\u5217\u8868 +YXL.ACU001.JG.ZC.LIST = YXL.ACU001.DJG01;YXL.ACU001.RJG01;YXL.ACU001.RJG02;YXL.ACU001.RJG03 +#\u4e95\u76d6\u72b6\u6001\u8d77\u59cb\u5730\u5740 +YXL.ACU001.JGSTAT.START = 3 +#\u4e95\u76d6\u72b6\u6001\u6240\u5360\u5b57\u6570 +YXL.ACU001.JGSTAT.WORDCOUNT = 1 \ No newline at end of file diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index 157f261..bbcc362 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -18,6 +18,7 @@ import com.szpg.task.ReadFjStatTask; import com.szpg.task.ReadHSStatusTask; import com.szpg.task.ReadHSValueTask; +import com.szpg.task.ReadJgStatusTask; import com.szpg.task.ReadO2StatusTask; import com.szpg.task.ReadO2ValueTask; import com.szpg.task.ReadSbRtTask; @@ -74,6 +75,9 @@ // 对射报警 // dsc.testSendDSStatusCommand(); + // 井盖 + dsc.testSendJgStatusCommand(); + // 风机 // dsc.testSendFjStatCommand(); // dsc.testSendFjRtCommand(); @@ -83,8 +87,9 @@ // dsc.testSendSbStatCommand(); // 照明 - dsc.testSendZmRtCommand(); - dsc.testSendZmStatCommand(); +// dsc.testSendZmRtCommand(); +// dsc.testSendZmStatCommand(); + } /** @@ -202,6 +207,15 @@ } /** + * 发送查询井盖状态命令 + * @param client + */ + private void testSendJgStatusCommand() { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.scheduleWithFixedDelay(new ReadJgStatusTask(), 30, 300, TimeUnit.SECONDS); + } + + /** * 发送查询风机运行状态命令 * @param client */ diff --git a/src/com/szpg/db/dao/PgJgDao.java b/src/com/szpg/db/dao/PgJgDao.java new file mode 100644 index 0000000..86a4a66 --- /dev/null +++ b/src/com/szpg/db/dao/PgJgDao.java @@ -0,0 +1,24 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgJgStat; + +/** + * 井盖相关参数读写的数据库接口 + * @author admin + * + */ +public interface PgJgDao { + + /******** 井盖状态 ********/ + public List findAllStat(); + public PgJgStat findStatById(Integer id); + + public List findStatByDevice(Integer deviceid); + public List findStatByDeviceAndTm(Integer deviceid, String start, String end); + public PgJgStat findLatestStatByDevice(Integer deviceid); + + public int addSbjzRecord(PgJgStat jgstat); + public int addSbjzRecord(boolean open, boolean lock, String tm, int deviceid); +} diff --git a/src/com/szpg/db/dao/impl/PgJgDaoImpl.java b/src/com/szpg/db/dao/impl/PgJgDaoImpl.java new file mode 100644 index 0000000..557e73e --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgJgDaoImpl.java @@ -0,0 +1,314 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.ArrayListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgJgDao; +import com.szpg.db.data.PgJgStat; +import com.szpg.db.util.ConnectionManager; + +public class PgJgDaoImpl implements PgJgDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAllStat() { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("查询所有井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("查询所有井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgJgStat findStatById(Integer id) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler(), param); + + if (null != tempList && tempList.size() == 1) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(0); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + logger.debug("根据ID查询井盖状态成功" + stat + "]"); + + return stat; + } + else + return null; + } catch (Exception ex) { + logger.error("根据ID查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public List findStatByDevice(Integer deviceid) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ?"; + Object[] param = new Object[1]; + param[0] = deviceid; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("根据设备ID查询井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("根据设备ID查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public List findStatByDeviceAndTm(Integer deviceid, String start, String end) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ? AND LOGTIME >= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') AND LOGTIME <= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')"; + Object[] params = new Object[3]; + params[0] = deviceid; + params[1] = start; + params[2] = end; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("根据设备ID和时间查询井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("根据设备ID和时间查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgJgStat findLatestStatByDevice(Integer deviceid) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ? ORDER BY LOGTIME DESC"; + Object[] param = new Object[1]; + param[0] = deviceid; + try { + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(0); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + logger.debug("根据设备ID查询最新的井盖状态成功" + stat); + return stat; + } + else + return null; + } catch (Exception ex) { + logger.error("根据ID查询最新井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addSbjzRecord(PgJgStat jgstat) { + return addSbjzRecord(jgstat.getJgopen(), jgstat.getJglock(), jgstat.getTmStr(), jgstat.getPgdeviceid()); + } + + @Override + public int addSbjzRecord(boolean open, boolean lock, String tm, int deviceid) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_JG_STAT " + + "(JGOPEN, JGLOCK, LOGTIME, UPTIME, PGDEVICEID) " + + "VALUES (?, ?, TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), ?)"; + Object[] params = new Object[5]; + params[0] = open; + params[1] = lock; + params[2] = tm; + params[3] = tm; + params[4] = deviceid; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入井盖状态成功[tm=" + tm + ", open=" + open + ", lock=" + lock + ", deviceid=" + deviceid + "]"); + else + logger.error("插入井盖状态失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + +} diff --git a/src/com/szpg/db/data/PgJgStat.java b/src/com/szpg/db/data/PgJgStat.java new file mode 100644 index 0000000..5d1425a --- /dev/null +++ b/src/com/szpg/db/data/PgJgStat.java @@ -0,0 +1,100 @@ +package com.szpg.db.data; + +import java.util.Date; + +import com.szpg.util.TimeFormat; + +/** + * 井盖状态数据表 + * + * @author admin + * + */ +public class PgJgStat implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -3706226324665928352L; + + private Integer id; + private Date logtime; + private Date uptime; + private Integer pgdeviceid; + private Boolean jgopen; + private Boolean jglock; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Date getLogtime() { + return logtime; + } + + public void setLogtime(Date logtime) { + this.logtime = logtime; + } + + public Date getUptime() { + return uptime; + } + + public void setUptime(Date uptime) { + this.uptime = uptime; + } + + public Integer getPgdeviceid() { + return pgdeviceid; + } + + public void setPgdeviceid(Integer pgdeviceid) { + this.pgdeviceid = pgdeviceid; + } + + public Boolean getJgopen() { + return jgopen; + } + + public void setJgopen(Boolean jgopen) { + this.jgopen = jgopen; + } + + public Boolean getJglock() { + return jglock; + } + + public void setJglock(Boolean jglock) { + this.jglock = jglock; + } + + public void setTm(Date tm) { + this.logtime = tm; + this.uptime = tm; + } + + public String getTmStr() { + if (null != this.logtime) { + return TimeFormat.formatTimestamp(logtime); + } else { + return ""; + } + } + + public String getTmStrGB() { + if (null != this.logtime) { + return TimeFormat.format(logtime, "yyyyMMddHHmmss"); + } else { + return ""; + } + } + + @Override + public String toString() { + return "PgJgStat[open=" + jgopen + ", lock=" + jglock + ", logtime=" + TimeFormat.formatTimestamp(logtime) + "]"; + } +} diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 171b537..3107ff1 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -22,4 +22,5 @@ public final static String CMD_TYPE_READSBRUNTIME = "26"; //读取水泵运行时长命令 public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 + public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index 4608bdd..ca1f453 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -10,6 +10,7 @@ import com.szpg.plc.message.command.read.ReadFjStatCommand; import com.szpg.plc.message.command.read.ReadHSStatusCommand; import com.szpg.plc.message.command.read.ReadHSValueCommand; +import com.szpg.plc.message.command.read.ReadJgStatusCommand; import com.szpg.plc.message.command.read.ReadO2StatusCommand; import com.szpg.plc.message.command.read.ReadO2ValueCommand; import com.szpg.plc.message.command.read.ReadSbRtCommand; @@ -98,6 +99,9 @@ case AppMessageConstants.CMD_TYPE_READDSSTATUS: return new ReadDSStatusCommand(); + case AppMessageConstants.CMD_TYPE_READJGSTATUS: + return new ReadJgStatusCommand(); + case AppMessageConstants.CMD_TYPE_READFJSTAT: return new ReadFjStatCommand(); case AppMessageConstants.CMD_TYPE_READFJRUNTIME: diff --git a/src/com/szpg/plc/message/command/read/ReadJgStatusCommand.java b/src/com/szpg/plc/message/command/read/ReadJgStatusCommand.java new file mode 100644 index 0000000..d78dcfd --- /dev/null +++ b/src/com/szpg/plc/message/command/read/ReadJgStatusCommand.java @@ -0,0 +1,29 @@ +package com.szpg.plc.message.command.read; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.response.read.ReadJgStatusCommandResponse; + +public class ReadJgStatusCommand extends ReadMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -695129650895569926L; + + @Override + public Class getResponseClass() { + return ReadJgStatusCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送查询井盖状态命令"; + } + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_READJGSTATUS; + } + +} diff --git a/src/com/szpg/plc/message/response/read/ReadJgStatusCommandResponse.java b/src/com/szpg/plc/message/response/read/ReadJgStatusCommandResponse.java new file mode 100644 index 0000000..dca5f0b --- /dev/null +++ b/src/com/szpg/plc/message/response/read/ReadJgStatusCommandResponse.java @@ -0,0 +1,146 @@ +package com.szpg.plc.message.response.read; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgJgDao; +import com.szpg.db.dao.PgDeviceDao; +import com.szpg.db.dao.PgHjsbblDao; +import com.szpg.db.dao.impl.PgJgDaoImpl; +import com.szpg.db.data.PgHjsbbl; +import com.szpg.db.dao.impl.PgDeviceDaoImpl; +import com.szpg.db.dao.impl.PgHjsbblDaoImpl; +import com.szpg.plc.message.response.ReadMemoryCommandResponse; +import com.szpg.plc.util.ByteUtil; +import com.szpg.util.Configure; +import com.szpg.util.TimeFormat; + +public class ReadJgStatusCommandResponse extends ReadMemoryCommandResponse { + + /** + * + */ + private static final long serialVersionUID = -8618969419223150064L; + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + private List jgdk; // 井盖打开/关闭 + private List jgsd; // 井盖锁定/解锁 + private String[] zcList; //井盖的资产列表,从配置文件中获取 + + public ReadJgStatusCommandResponse() { + jgdk = new ArrayList(); + jgsd = new ArrayList(); + } + + public List getJgdk() { + return jgdk; + } + + public void setJgdk(List jgdk) { + this.jgdk = jgdk; + } + + public List getJgsd() { + return jgsd; + } + + public void setJgsd(List jgsd) { + this.jgsd = jgsd; + } + + public String[] getZcList() { + return zcList; + } + + public void setZcList(String[] zcList) { + this.zcList = zcList; + } + + @Override + public void afterAction() { + // 1将井盖状态数据存入数据库 + PgJgDao jgDao = new PgJgDaoImpl(); + PgDeviceDao deviceDao = new PgDeviceDaoImpl(); + + // 遍历资产列表,将井盖状态存入数据库 + for (int i = 0; i < zcList.length; i++) { + String zcbh = zcList[i]; + + int id = deviceDao.findDeviceIdByCode(zcbh); + if (id > 0) { + jgDao.addSbjzRecord(jgdk.get(i), jgsd.get(i), TimeFormat.formatTimestamp(this.getTime().getTime()), id); + } + } + } + + @Override + public void parseData(byte[] messageData) { + // 获取目标ACU的代码 + String acucode = this.getAcucode(); + + // 判断数据的长度是否满足要求 + if (messageData.length != Integer.parseInt(Configure.getProperty("acubl", acucode + ".JGSTAT.WORDCOUNT")) * 2) { + logger.error("返回的数据长度与读取的不一致!"); + this.setValid(false); + return; + } + + // 将数据转换为字符串 + // 按照高字在前,低字在后;高字节在前,低字节在后的顺序 + String dataStr = ""; + for (int c = 0; c < messageData.length; c = c + 2) { + dataStr = ByteUtil.binToBinString(new byte[] {messageData[c], messageData[c + 1]}) + dataStr; + } + + PgHjsbblDao blDao = new PgHjsbblDaoImpl(); + + // 获取目标ACU井盖状态内存区域的起始字地址 + int start = Integer.parseInt(Configure.getProperty("acubl", acucode + ".JGSTAT.START")); + + // 获取目标ACU井盖相关的资产列表,即井盖设备列表 + String zcListStr = Configure.getProperty("acubl", acucode + ".JG.ZC.LIST"); + if (StringUtils.isEmpty(zcListStr) == true) { + this.setValid(false); + return; + } + zcList = zcListStr.split(";"); + + // 解析井盖状态 + for (int i = 0; i < zcList.length; i++) { + String zcbh = zcList[i]; + + // 解析井盖打开/关闭状态 + PgHjsbbl openBlObj = blDao.findBlByBh(zcbh + ".Open"); + if (null == openBlObj) { + continue; + } + + int okszdz = openBlObj.getKszdz(); //开始字地址 + int ooffset = okszdz - start; //与开始字的偏移量 + int obit = openBlObj.getSzw(); //所在位 + + char openBt = dataStr.charAt(dataStr.length() - 1 - ooffset * 16 - obit); // 井盖打开/关闭标志位 + + getJgdk().add(openBt == '0' ? true : false); //0-未关闭;1-关闭 + + // 解析井盖锁定/解锁状态 + PgHjsbbl lockBlObj = blDao.findBlByBh(zcbh + ".Lock"); + if (null == lockBlObj) { + continue; + } + + int lkszdz = lockBlObj.getKszdz(); //开始字地址 + int loffset = lkszdz - start; //与开始字的偏移量 + int lbit = lockBlObj.getSzw(); //所在位 + + char lockBt = dataStr.charAt(dataStr.length() - 1 - loffset * 16 - lbit); // 井盖解锁/锁定标志位 + + getJgsd().add(lockBt == '0' ? true : false); //0-锁定;1-解锁 + } + } + +} diff --git a/src/ACUBL.properties b/src/ACUBL.properties index 85e2c37..fc3cb09 100644 --- a/src/ACUBL.properties +++ b/src/ACUBL.properties @@ -130,4 +130,14 @@ #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u8d77\u59cb\u5730\u5740 YXL.ACU001.ZMRT.START = 70 #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u6240\u5360\u5b57\u6570 -YXL.ACU001.ZMRT.WORDCOUNT = 16 \ No newline at end of file +YXL.ACU001.ZMRT.WORDCOUNT = 16 + + +#\u4e95\u76d6\u6570\u91cf +YXL.ACU001.JG.COUNT = 4 +#\u4e95\u76d6\u7f16\u7801\u5217\u8868 +YXL.ACU001.JG.ZC.LIST = YXL.ACU001.DJG01;YXL.ACU001.RJG01;YXL.ACU001.RJG02;YXL.ACU001.RJG03 +#\u4e95\u76d6\u72b6\u6001\u8d77\u59cb\u5730\u5740 +YXL.ACU001.JGSTAT.START = 3 +#\u4e95\u76d6\u72b6\u6001\u6240\u5360\u5b57\u6570 +YXL.ACU001.JGSTAT.WORDCOUNT = 1 \ No newline at end of file diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index 157f261..bbcc362 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -18,6 +18,7 @@ import com.szpg.task.ReadFjStatTask; import com.szpg.task.ReadHSStatusTask; import com.szpg.task.ReadHSValueTask; +import com.szpg.task.ReadJgStatusTask; import com.szpg.task.ReadO2StatusTask; import com.szpg.task.ReadO2ValueTask; import com.szpg.task.ReadSbRtTask; @@ -74,6 +75,9 @@ // 对射报警 // dsc.testSendDSStatusCommand(); + // 井盖 + dsc.testSendJgStatusCommand(); + // 风机 // dsc.testSendFjStatCommand(); // dsc.testSendFjRtCommand(); @@ -83,8 +87,9 @@ // dsc.testSendSbStatCommand(); // 照明 - dsc.testSendZmRtCommand(); - dsc.testSendZmStatCommand(); +// dsc.testSendZmRtCommand(); +// dsc.testSendZmStatCommand(); + } /** @@ -202,6 +207,15 @@ } /** + * 发送查询井盖状态命令 + * @param client + */ + private void testSendJgStatusCommand() { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.scheduleWithFixedDelay(new ReadJgStatusTask(), 30, 300, TimeUnit.SECONDS); + } + + /** * 发送查询风机运行状态命令 * @param client */ diff --git a/src/com/szpg/db/dao/PgJgDao.java b/src/com/szpg/db/dao/PgJgDao.java new file mode 100644 index 0000000..86a4a66 --- /dev/null +++ b/src/com/szpg/db/dao/PgJgDao.java @@ -0,0 +1,24 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgJgStat; + +/** + * 井盖相关参数读写的数据库接口 + * @author admin + * + */ +public interface PgJgDao { + + /******** 井盖状态 ********/ + public List findAllStat(); + public PgJgStat findStatById(Integer id); + + public List findStatByDevice(Integer deviceid); + public List findStatByDeviceAndTm(Integer deviceid, String start, String end); + public PgJgStat findLatestStatByDevice(Integer deviceid); + + public int addSbjzRecord(PgJgStat jgstat); + public int addSbjzRecord(boolean open, boolean lock, String tm, int deviceid); +} diff --git a/src/com/szpg/db/dao/impl/PgJgDaoImpl.java b/src/com/szpg/db/dao/impl/PgJgDaoImpl.java new file mode 100644 index 0000000..557e73e --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgJgDaoImpl.java @@ -0,0 +1,314 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.ArrayListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgJgDao; +import com.szpg.db.data.PgJgStat; +import com.szpg.db.util.ConnectionManager; + +public class PgJgDaoImpl implements PgJgDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAllStat() { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("查询所有井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("查询所有井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgJgStat findStatById(Integer id) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler(), param); + + if (null != tempList && tempList.size() == 1) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(0); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + logger.debug("根据ID查询井盖状态成功" + stat + "]"); + + return stat; + } + else + return null; + } catch (Exception ex) { + logger.error("根据ID查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public List findStatByDevice(Integer deviceid) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ?"; + Object[] param = new Object[1]; + param[0] = deviceid; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("根据设备ID查询井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("根据设备ID查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public List findStatByDeviceAndTm(Integer deviceid, String start, String end) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ? AND LOGTIME >= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') AND LOGTIME <= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')"; + Object[] params = new Object[3]; + params[0] = deviceid; + params[1] = start; + params[2] = end; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("根据设备ID和时间查询井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("根据设备ID和时间查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgJgStat findLatestStatByDevice(Integer deviceid) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ? ORDER BY LOGTIME DESC"; + Object[] param = new Object[1]; + param[0] = deviceid; + try { + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(0); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + logger.debug("根据设备ID查询最新的井盖状态成功" + stat); + return stat; + } + else + return null; + } catch (Exception ex) { + logger.error("根据ID查询最新井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addSbjzRecord(PgJgStat jgstat) { + return addSbjzRecord(jgstat.getJgopen(), jgstat.getJglock(), jgstat.getTmStr(), jgstat.getPgdeviceid()); + } + + @Override + public int addSbjzRecord(boolean open, boolean lock, String tm, int deviceid) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_JG_STAT " + + "(JGOPEN, JGLOCK, LOGTIME, UPTIME, PGDEVICEID) " + + "VALUES (?, ?, TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), ?)"; + Object[] params = new Object[5]; + params[0] = open; + params[1] = lock; + params[2] = tm; + params[3] = tm; + params[4] = deviceid; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入井盖状态成功[tm=" + tm + ", open=" + open + ", lock=" + lock + ", deviceid=" + deviceid + "]"); + else + logger.error("插入井盖状态失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + +} diff --git a/src/com/szpg/db/data/PgJgStat.java b/src/com/szpg/db/data/PgJgStat.java new file mode 100644 index 0000000..5d1425a --- /dev/null +++ b/src/com/szpg/db/data/PgJgStat.java @@ -0,0 +1,100 @@ +package com.szpg.db.data; + +import java.util.Date; + +import com.szpg.util.TimeFormat; + +/** + * 井盖状态数据表 + * + * @author admin + * + */ +public class PgJgStat implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -3706226324665928352L; + + private Integer id; + private Date logtime; + private Date uptime; + private Integer pgdeviceid; + private Boolean jgopen; + private Boolean jglock; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Date getLogtime() { + return logtime; + } + + public void setLogtime(Date logtime) { + this.logtime = logtime; + } + + public Date getUptime() { + return uptime; + } + + public void setUptime(Date uptime) { + this.uptime = uptime; + } + + public Integer getPgdeviceid() { + return pgdeviceid; + } + + public void setPgdeviceid(Integer pgdeviceid) { + this.pgdeviceid = pgdeviceid; + } + + public Boolean getJgopen() { + return jgopen; + } + + public void setJgopen(Boolean jgopen) { + this.jgopen = jgopen; + } + + public Boolean getJglock() { + return jglock; + } + + public void setJglock(Boolean jglock) { + this.jglock = jglock; + } + + public void setTm(Date tm) { + this.logtime = tm; + this.uptime = tm; + } + + public String getTmStr() { + if (null != this.logtime) { + return TimeFormat.formatTimestamp(logtime); + } else { + return ""; + } + } + + public String getTmStrGB() { + if (null != this.logtime) { + return TimeFormat.format(logtime, "yyyyMMddHHmmss"); + } else { + return ""; + } + } + + @Override + public String toString() { + return "PgJgStat[open=" + jgopen + ", lock=" + jglock + ", logtime=" + TimeFormat.formatTimestamp(logtime) + "]"; + } +} diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 171b537..3107ff1 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -22,4 +22,5 @@ public final static String CMD_TYPE_READSBRUNTIME = "26"; //读取水泵运行时长命令 public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 + public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index 4608bdd..ca1f453 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -10,6 +10,7 @@ import com.szpg.plc.message.command.read.ReadFjStatCommand; import com.szpg.plc.message.command.read.ReadHSStatusCommand; import com.szpg.plc.message.command.read.ReadHSValueCommand; +import com.szpg.plc.message.command.read.ReadJgStatusCommand; import com.szpg.plc.message.command.read.ReadO2StatusCommand; import com.szpg.plc.message.command.read.ReadO2ValueCommand; import com.szpg.plc.message.command.read.ReadSbRtCommand; @@ -98,6 +99,9 @@ case AppMessageConstants.CMD_TYPE_READDSSTATUS: return new ReadDSStatusCommand(); + case AppMessageConstants.CMD_TYPE_READJGSTATUS: + return new ReadJgStatusCommand(); + case AppMessageConstants.CMD_TYPE_READFJSTAT: return new ReadFjStatCommand(); case AppMessageConstants.CMD_TYPE_READFJRUNTIME: diff --git a/src/com/szpg/plc/message/command/read/ReadJgStatusCommand.java b/src/com/szpg/plc/message/command/read/ReadJgStatusCommand.java new file mode 100644 index 0000000..d78dcfd --- /dev/null +++ b/src/com/szpg/plc/message/command/read/ReadJgStatusCommand.java @@ -0,0 +1,29 @@ +package com.szpg.plc.message.command.read; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.response.read.ReadJgStatusCommandResponse; + +public class ReadJgStatusCommand extends ReadMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -695129650895569926L; + + @Override + public Class getResponseClass() { + return ReadJgStatusCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送查询井盖状态命令"; + } + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_READJGSTATUS; + } + +} diff --git a/src/com/szpg/plc/message/response/read/ReadJgStatusCommandResponse.java b/src/com/szpg/plc/message/response/read/ReadJgStatusCommandResponse.java new file mode 100644 index 0000000..dca5f0b --- /dev/null +++ b/src/com/szpg/plc/message/response/read/ReadJgStatusCommandResponse.java @@ -0,0 +1,146 @@ +package com.szpg.plc.message.response.read; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgJgDao; +import com.szpg.db.dao.PgDeviceDao; +import com.szpg.db.dao.PgHjsbblDao; +import com.szpg.db.dao.impl.PgJgDaoImpl; +import com.szpg.db.data.PgHjsbbl; +import com.szpg.db.dao.impl.PgDeviceDaoImpl; +import com.szpg.db.dao.impl.PgHjsbblDaoImpl; +import com.szpg.plc.message.response.ReadMemoryCommandResponse; +import com.szpg.plc.util.ByteUtil; +import com.szpg.util.Configure; +import com.szpg.util.TimeFormat; + +public class ReadJgStatusCommandResponse extends ReadMemoryCommandResponse { + + /** + * + */ + private static final long serialVersionUID = -8618969419223150064L; + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + private List jgdk; // 井盖打开/关闭 + private List jgsd; // 井盖锁定/解锁 + private String[] zcList; //井盖的资产列表,从配置文件中获取 + + public ReadJgStatusCommandResponse() { + jgdk = new ArrayList(); + jgsd = new ArrayList(); + } + + public List getJgdk() { + return jgdk; + } + + public void setJgdk(List jgdk) { + this.jgdk = jgdk; + } + + public List getJgsd() { + return jgsd; + } + + public void setJgsd(List jgsd) { + this.jgsd = jgsd; + } + + public String[] getZcList() { + return zcList; + } + + public void setZcList(String[] zcList) { + this.zcList = zcList; + } + + @Override + public void afterAction() { + // 1将井盖状态数据存入数据库 + PgJgDao jgDao = new PgJgDaoImpl(); + PgDeviceDao deviceDao = new PgDeviceDaoImpl(); + + // 遍历资产列表,将井盖状态存入数据库 + for (int i = 0; i < zcList.length; i++) { + String zcbh = zcList[i]; + + int id = deviceDao.findDeviceIdByCode(zcbh); + if (id > 0) { + jgDao.addSbjzRecord(jgdk.get(i), jgsd.get(i), TimeFormat.formatTimestamp(this.getTime().getTime()), id); + } + } + } + + @Override + public void parseData(byte[] messageData) { + // 获取目标ACU的代码 + String acucode = this.getAcucode(); + + // 判断数据的长度是否满足要求 + if (messageData.length != Integer.parseInt(Configure.getProperty("acubl", acucode + ".JGSTAT.WORDCOUNT")) * 2) { + logger.error("返回的数据长度与读取的不一致!"); + this.setValid(false); + return; + } + + // 将数据转换为字符串 + // 按照高字在前,低字在后;高字节在前,低字节在后的顺序 + String dataStr = ""; + for (int c = 0; c < messageData.length; c = c + 2) { + dataStr = ByteUtil.binToBinString(new byte[] {messageData[c], messageData[c + 1]}) + dataStr; + } + + PgHjsbblDao blDao = new PgHjsbblDaoImpl(); + + // 获取目标ACU井盖状态内存区域的起始字地址 + int start = Integer.parseInt(Configure.getProperty("acubl", acucode + ".JGSTAT.START")); + + // 获取目标ACU井盖相关的资产列表,即井盖设备列表 + String zcListStr = Configure.getProperty("acubl", acucode + ".JG.ZC.LIST"); + if (StringUtils.isEmpty(zcListStr) == true) { + this.setValid(false); + return; + } + zcList = zcListStr.split(";"); + + // 解析井盖状态 + for (int i = 0; i < zcList.length; i++) { + String zcbh = zcList[i]; + + // 解析井盖打开/关闭状态 + PgHjsbbl openBlObj = blDao.findBlByBh(zcbh + ".Open"); + if (null == openBlObj) { + continue; + } + + int okszdz = openBlObj.getKszdz(); //开始字地址 + int ooffset = okszdz - start; //与开始字的偏移量 + int obit = openBlObj.getSzw(); //所在位 + + char openBt = dataStr.charAt(dataStr.length() - 1 - ooffset * 16 - obit); // 井盖打开/关闭标志位 + + getJgdk().add(openBt == '0' ? true : false); //0-未关闭;1-关闭 + + // 解析井盖锁定/解锁状态 + PgHjsbbl lockBlObj = blDao.findBlByBh(zcbh + ".Lock"); + if (null == lockBlObj) { + continue; + } + + int lkszdz = lockBlObj.getKszdz(); //开始字地址 + int loffset = lkszdz - start; //与开始字的偏移量 + int lbit = lockBlObj.getSzw(); //所在位 + + char lockBt = dataStr.charAt(dataStr.length() - 1 - loffset * 16 - lbit); // 井盖解锁/锁定标志位 + + getJgsd().add(lockBt == '0' ? true : false); //0-锁定;1-解锁 + } + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index ee2e41f..bdba274 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -22,6 +22,7 @@ import com.szpg.plc.message.response.read.ReadFjStatCommandResponse; import com.szpg.plc.message.response.read.ReadHSStatusCommandResponse; import com.szpg.plc.message.response.read.ReadHSValueCommandResponse; +import com.szpg.plc.message.response.read.ReadJgStatusCommandResponse; import com.szpg.plc.message.response.read.ReadO2StatusCommandResponse; import com.szpg.plc.message.response.read.ReadO2ValueCommandResponse; import com.szpg.plc.message.response.read.ReadSbRtCommandResponse; @@ -197,6 +198,10 @@ case AppMessageConstants.CMD_TYPE_READZMRUNTIME: received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); break; + + case AppMessageConstants.CMD_TYPE_READJGSTATUS: + received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + break; } // 4将已响应的命令删除 @@ -715,6 +720,34 @@ return rsrcr; } + + + /** + * 将查询井盖状态响应消息字节数组转换为消息对象 + * + * @param byteMessage + * @return + */ + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); + + // 设置ACU代码 + rjscr.setAcucode(cmd.getDest_acu_code()); + + byte[] body = finsFrame.TEXT_DATA_BODY; + rjscr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + if (body[2] == 0x00 && body[3] == 0x00) { + // 正常返回 + byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存 + rjscr.parseData(data); + + // 设置响应对应的命令ID + rjscr.setCmdId(cmd.getId()); + } + + return rjscr; + } + /** * 将消息对象解析为字节数组 diff --git a/src/ACUBL.properties b/src/ACUBL.properties index 85e2c37..fc3cb09 100644 --- a/src/ACUBL.properties +++ b/src/ACUBL.properties @@ -130,4 +130,14 @@ #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u8d77\u59cb\u5730\u5740 YXL.ACU001.ZMRT.START = 70 #\u7167\u660e\u8fd0\u884c\u65f6\u957f\u6240\u5360\u5b57\u6570 -YXL.ACU001.ZMRT.WORDCOUNT = 16 \ No newline at end of file +YXL.ACU001.ZMRT.WORDCOUNT = 16 + + +#\u4e95\u76d6\u6570\u91cf +YXL.ACU001.JG.COUNT = 4 +#\u4e95\u76d6\u7f16\u7801\u5217\u8868 +YXL.ACU001.JG.ZC.LIST = YXL.ACU001.DJG01;YXL.ACU001.RJG01;YXL.ACU001.RJG02;YXL.ACU001.RJG03 +#\u4e95\u76d6\u72b6\u6001\u8d77\u59cb\u5730\u5740 +YXL.ACU001.JGSTAT.START = 3 +#\u4e95\u76d6\u72b6\u6001\u6240\u5360\u5b57\u6570 +YXL.ACU001.JGSTAT.WORDCOUNT = 1 \ No newline at end of file diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index 157f261..bbcc362 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -18,6 +18,7 @@ import com.szpg.task.ReadFjStatTask; import com.szpg.task.ReadHSStatusTask; import com.szpg.task.ReadHSValueTask; +import com.szpg.task.ReadJgStatusTask; import com.szpg.task.ReadO2StatusTask; import com.szpg.task.ReadO2ValueTask; import com.szpg.task.ReadSbRtTask; @@ -74,6 +75,9 @@ // 对射报警 // dsc.testSendDSStatusCommand(); + // 井盖 + dsc.testSendJgStatusCommand(); + // 风机 // dsc.testSendFjStatCommand(); // dsc.testSendFjRtCommand(); @@ -83,8 +87,9 @@ // dsc.testSendSbStatCommand(); // 照明 - dsc.testSendZmRtCommand(); - dsc.testSendZmStatCommand(); +// dsc.testSendZmRtCommand(); +// dsc.testSendZmStatCommand(); + } /** @@ -202,6 +207,15 @@ } /** + * 发送查询井盖状态命令 + * @param client + */ + private void testSendJgStatusCommand() { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.scheduleWithFixedDelay(new ReadJgStatusTask(), 30, 300, TimeUnit.SECONDS); + } + + /** * 发送查询风机运行状态命令 * @param client */ diff --git a/src/com/szpg/db/dao/PgJgDao.java b/src/com/szpg/db/dao/PgJgDao.java new file mode 100644 index 0000000..86a4a66 --- /dev/null +++ b/src/com/szpg/db/dao/PgJgDao.java @@ -0,0 +1,24 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgJgStat; + +/** + * 井盖相关参数读写的数据库接口 + * @author admin + * + */ +public interface PgJgDao { + + /******** 井盖状态 ********/ + public List findAllStat(); + public PgJgStat findStatById(Integer id); + + public List findStatByDevice(Integer deviceid); + public List findStatByDeviceAndTm(Integer deviceid, String start, String end); + public PgJgStat findLatestStatByDevice(Integer deviceid); + + public int addSbjzRecord(PgJgStat jgstat); + public int addSbjzRecord(boolean open, boolean lock, String tm, int deviceid); +} diff --git a/src/com/szpg/db/dao/impl/PgJgDaoImpl.java b/src/com/szpg/db/dao/impl/PgJgDaoImpl.java new file mode 100644 index 0000000..557e73e --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgJgDaoImpl.java @@ -0,0 +1,314 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.ArrayListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgJgDao; +import com.szpg.db.data.PgJgStat; +import com.szpg.db.util.ConnectionManager; + +public class PgJgDaoImpl implements PgJgDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAllStat() { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("查询所有井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("查询所有井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgJgStat findStatById(Integer id) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler(), param); + + if (null != tempList && tempList.size() == 1) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(0); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + logger.debug("根据ID查询井盖状态成功" + stat + "]"); + + return stat; + } + else + return null; + } catch (Exception ex) { + logger.error("根据ID查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public List findStatByDevice(Integer deviceid) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ?"; + Object[] param = new Object[1]; + param[0] = deviceid; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("根据设备ID查询井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("根据设备ID查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public List findStatByDeviceAndTm(Integer deviceid, String start, String end) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ? AND LOGTIME >= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') AND LOGTIME <= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')"; + Object[] params = new Object[3]; + params[0] = deviceid; + params[1] = start; + params[2] = end; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + logger.debug("根据设备ID和时间查询井盖状态成功[" + tempList.size() + "]"); + + List list = new ArrayList(); + for (int i = 0; i < tempList.size(); i++) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(i); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + list.add(stat); + } + return list; + } + else + return null; + } catch (Exception ex) { + logger.error("根据设备ID和时间查询井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgJgStat findLatestStatByDevice(Integer deviceid) { + Connection conn = null; + + String queryStr = "SELECT ID, LOGTIME, PGDEVICEID, JGOPEN, JGLOCK FROM PG_JG_STAT WHERE PGDEVICEID = ? ORDER BY LOGTIME DESC"; + Object[] param = new Object[1]; + param[0] = deviceid; + try { + QueryRunner runner = new QueryRunner(); + List tempList = (List) runner.query(conn, queryStr, new ArrayListHandler()); + + if (null != tempList && tempList.isEmpty() == false) { + PgJgStat stat = new PgJgStat(); + Object[] item = tempList.get(0); + + stat.setId(((Number) item[0]).intValue()); + stat.setPgdeviceid(((Number) item[2]).intValue()); + + long tmValue = ((java.sql.Timestamp) ((oracle.sql.TIMESTAMP) item[1]).toJdbc()).getTime(); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(tmValue); + stat.setLogtime(cal.getTime()); + stat.setUptime(cal.getTime()); + + stat.setJgopen(Boolean.parseBoolean((String) item[3])); + stat.setJglock(Boolean.parseBoolean((String) item[4])); + + logger.debug("根据设备ID查询最新的井盖状态成功" + stat); + return stat; + } + else + return null; + } catch (Exception ex) { + logger.error("根据ID查询最新井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addSbjzRecord(PgJgStat jgstat) { + return addSbjzRecord(jgstat.getJgopen(), jgstat.getJglock(), jgstat.getTmStr(), jgstat.getPgdeviceid()); + } + + @Override + public int addSbjzRecord(boolean open, boolean lock, String tm, int deviceid) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_JG_STAT " + + "(JGOPEN, JGLOCK, LOGTIME, UPTIME, PGDEVICEID) " + + "VALUES (?, ?, TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), ?)"; + Object[] params = new Object[5]; + params[0] = open; + params[1] = lock; + params[2] = tm; + params[3] = tm; + params[4] = deviceid; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入井盖状态成功[tm=" + tm + ", open=" + open + ", lock=" + lock + ", deviceid=" + deviceid + "]"); + else + logger.error("插入井盖状态失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入井盖状态异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + +} diff --git a/src/com/szpg/db/data/PgJgStat.java b/src/com/szpg/db/data/PgJgStat.java new file mode 100644 index 0000000..5d1425a --- /dev/null +++ b/src/com/szpg/db/data/PgJgStat.java @@ -0,0 +1,100 @@ +package com.szpg.db.data; + +import java.util.Date; + +import com.szpg.util.TimeFormat; + +/** + * 井盖状态数据表 + * + * @author admin + * + */ +public class PgJgStat implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -3706226324665928352L; + + private Integer id; + private Date logtime; + private Date uptime; + private Integer pgdeviceid; + private Boolean jgopen; + private Boolean jglock; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Date getLogtime() { + return logtime; + } + + public void setLogtime(Date logtime) { + this.logtime = logtime; + } + + public Date getUptime() { + return uptime; + } + + public void setUptime(Date uptime) { + this.uptime = uptime; + } + + public Integer getPgdeviceid() { + return pgdeviceid; + } + + public void setPgdeviceid(Integer pgdeviceid) { + this.pgdeviceid = pgdeviceid; + } + + public Boolean getJgopen() { + return jgopen; + } + + public void setJgopen(Boolean jgopen) { + this.jgopen = jgopen; + } + + public Boolean getJglock() { + return jglock; + } + + public void setJglock(Boolean jglock) { + this.jglock = jglock; + } + + public void setTm(Date tm) { + this.logtime = tm; + this.uptime = tm; + } + + public String getTmStr() { + if (null != this.logtime) { + return TimeFormat.formatTimestamp(logtime); + } else { + return ""; + } + } + + public String getTmStrGB() { + if (null != this.logtime) { + return TimeFormat.format(logtime, "yyyyMMddHHmmss"); + } else { + return ""; + } + } + + @Override + public String toString() { + return "PgJgStat[open=" + jgopen + ", lock=" + jglock + ", logtime=" + TimeFormat.formatTimestamp(logtime) + "]"; + } +} diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 171b537..3107ff1 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -22,4 +22,5 @@ public final static String CMD_TYPE_READSBRUNTIME = "26"; //读取水泵运行时长命令 public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 + public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index 4608bdd..ca1f453 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -10,6 +10,7 @@ import com.szpg.plc.message.command.read.ReadFjStatCommand; import com.szpg.plc.message.command.read.ReadHSStatusCommand; import com.szpg.plc.message.command.read.ReadHSValueCommand; +import com.szpg.plc.message.command.read.ReadJgStatusCommand; import com.szpg.plc.message.command.read.ReadO2StatusCommand; import com.szpg.plc.message.command.read.ReadO2ValueCommand; import com.szpg.plc.message.command.read.ReadSbRtCommand; @@ -98,6 +99,9 @@ case AppMessageConstants.CMD_TYPE_READDSSTATUS: return new ReadDSStatusCommand(); + case AppMessageConstants.CMD_TYPE_READJGSTATUS: + return new ReadJgStatusCommand(); + case AppMessageConstants.CMD_TYPE_READFJSTAT: return new ReadFjStatCommand(); case AppMessageConstants.CMD_TYPE_READFJRUNTIME: diff --git a/src/com/szpg/plc/message/command/read/ReadJgStatusCommand.java b/src/com/szpg/plc/message/command/read/ReadJgStatusCommand.java new file mode 100644 index 0000000..d78dcfd --- /dev/null +++ b/src/com/szpg/plc/message/command/read/ReadJgStatusCommand.java @@ -0,0 +1,29 @@ +package com.szpg.plc.message.command.read; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.response.read.ReadJgStatusCommandResponse; + +public class ReadJgStatusCommand extends ReadMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -695129650895569926L; + + @Override + public Class getResponseClass() { + return ReadJgStatusCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送查询井盖状态命令"; + } + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_READJGSTATUS; + } + +} diff --git a/src/com/szpg/plc/message/response/read/ReadJgStatusCommandResponse.java b/src/com/szpg/plc/message/response/read/ReadJgStatusCommandResponse.java new file mode 100644 index 0000000..dca5f0b --- /dev/null +++ b/src/com/szpg/plc/message/response/read/ReadJgStatusCommandResponse.java @@ -0,0 +1,146 @@ +package com.szpg.plc.message.response.read; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgJgDao; +import com.szpg.db.dao.PgDeviceDao; +import com.szpg.db.dao.PgHjsbblDao; +import com.szpg.db.dao.impl.PgJgDaoImpl; +import com.szpg.db.data.PgHjsbbl; +import com.szpg.db.dao.impl.PgDeviceDaoImpl; +import com.szpg.db.dao.impl.PgHjsbblDaoImpl; +import com.szpg.plc.message.response.ReadMemoryCommandResponse; +import com.szpg.plc.util.ByteUtil; +import com.szpg.util.Configure; +import com.szpg.util.TimeFormat; + +public class ReadJgStatusCommandResponse extends ReadMemoryCommandResponse { + + /** + * + */ + private static final long serialVersionUID = -8618969419223150064L; + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + private List jgdk; // 井盖打开/关闭 + private List jgsd; // 井盖锁定/解锁 + private String[] zcList; //井盖的资产列表,从配置文件中获取 + + public ReadJgStatusCommandResponse() { + jgdk = new ArrayList(); + jgsd = new ArrayList(); + } + + public List getJgdk() { + return jgdk; + } + + public void setJgdk(List jgdk) { + this.jgdk = jgdk; + } + + public List getJgsd() { + return jgsd; + } + + public void setJgsd(List jgsd) { + this.jgsd = jgsd; + } + + public String[] getZcList() { + return zcList; + } + + public void setZcList(String[] zcList) { + this.zcList = zcList; + } + + @Override + public void afterAction() { + // 1将井盖状态数据存入数据库 + PgJgDao jgDao = new PgJgDaoImpl(); + PgDeviceDao deviceDao = new PgDeviceDaoImpl(); + + // 遍历资产列表,将井盖状态存入数据库 + for (int i = 0; i < zcList.length; i++) { + String zcbh = zcList[i]; + + int id = deviceDao.findDeviceIdByCode(zcbh); + if (id > 0) { + jgDao.addSbjzRecord(jgdk.get(i), jgsd.get(i), TimeFormat.formatTimestamp(this.getTime().getTime()), id); + } + } + } + + @Override + public void parseData(byte[] messageData) { + // 获取目标ACU的代码 + String acucode = this.getAcucode(); + + // 判断数据的长度是否满足要求 + if (messageData.length != Integer.parseInt(Configure.getProperty("acubl", acucode + ".JGSTAT.WORDCOUNT")) * 2) { + logger.error("返回的数据长度与读取的不一致!"); + this.setValid(false); + return; + } + + // 将数据转换为字符串 + // 按照高字在前,低字在后;高字节在前,低字节在后的顺序 + String dataStr = ""; + for (int c = 0; c < messageData.length; c = c + 2) { + dataStr = ByteUtil.binToBinString(new byte[] {messageData[c], messageData[c + 1]}) + dataStr; + } + + PgHjsbblDao blDao = new PgHjsbblDaoImpl(); + + // 获取目标ACU井盖状态内存区域的起始字地址 + int start = Integer.parseInt(Configure.getProperty("acubl", acucode + ".JGSTAT.START")); + + // 获取目标ACU井盖相关的资产列表,即井盖设备列表 + String zcListStr = Configure.getProperty("acubl", acucode + ".JG.ZC.LIST"); + if (StringUtils.isEmpty(zcListStr) == true) { + this.setValid(false); + return; + } + zcList = zcListStr.split(";"); + + // 解析井盖状态 + for (int i = 0; i < zcList.length; i++) { + String zcbh = zcList[i]; + + // 解析井盖打开/关闭状态 + PgHjsbbl openBlObj = blDao.findBlByBh(zcbh + ".Open"); + if (null == openBlObj) { + continue; + } + + int okszdz = openBlObj.getKszdz(); //开始字地址 + int ooffset = okszdz - start; //与开始字的偏移量 + int obit = openBlObj.getSzw(); //所在位 + + char openBt = dataStr.charAt(dataStr.length() - 1 - ooffset * 16 - obit); // 井盖打开/关闭标志位 + + getJgdk().add(openBt == '0' ? true : false); //0-未关闭;1-关闭 + + // 解析井盖锁定/解锁状态 + PgHjsbbl lockBlObj = blDao.findBlByBh(zcbh + ".Lock"); + if (null == lockBlObj) { + continue; + } + + int lkszdz = lockBlObj.getKszdz(); //开始字地址 + int loffset = lkszdz - start; //与开始字的偏移量 + int lbit = lockBlObj.getSzw(); //所在位 + + char lockBt = dataStr.charAt(dataStr.length() - 1 - loffset * 16 - lbit); // 井盖解锁/锁定标志位 + + getJgsd().add(lockBt == '0' ? true : false); //0-锁定;1-解锁 + } + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index ee2e41f..bdba274 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -22,6 +22,7 @@ import com.szpg.plc.message.response.read.ReadFjStatCommandResponse; import com.szpg.plc.message.response.read.ReadHSStatusCommandResponse; import com.szpg.plc.message.response.read.ReadHSValueCommandResponse; +import com.szpg.plc.message.response.read.ReadJgStatusCommandResponse; import com.szpg.plc.message.response.read.ReadO2StatusCommandResponse; import com.szpg.plc.message.response.read.ReadO2ValueCommandResponse; import com.szpg.plc.message.response.read.ReadSbRtCommandResponse; @@ -197,6 +198,10 @@ case AppMessageConstants.CMD_TYPE_READZMRUNTIME: received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); break; + + case AppMessageConstants.CMD_TYPE_READJGSTATUS: + received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + break; } // 4将已响应的命令删除 @@ -715,6 +720,34 @@ return rsrcr; } + + + /** + * 将查询井盖状态响应消息字节数组转换为消息对象 + * + * @param byteMessage + * @return + */ + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); + + // 设置ACU代码 + rjscr.setAcucode(cmd.getDest_acu_code()); + + byte[] body = finsFrame.TEXT_DATA_BODY; + rjscr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + if (body[2] == 0x00 && body[3] == 0x00) { + // 正常返回 + byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存 + rjscr.parseData(data); + + // 设置响应对应的命令ID + rjscr.setCmdId(cmd.getId()); + } + + return rjscr; + } + /** * 将消息对象解析为字节数组 diff --git a/src/com/szpg/task/ReadJgStatusTask.java b/src/com/szpg/task/ReadJgStatusTask.java new file mode 100644 index 0000000..ea69b97 --- /dev/null +++ b/src/com/szpg/task/ReadJgStatusTask.java @@ -0,0 +1,50 @@ +package com.szpg.task; + +import java.util.Map; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.protocol.fins.FINSConstants; +import com.szpg.plc.server.ACUClient; +import com.szpg.plc.server.ACUClientUtil; +import com.szpg.plc.util.ByteUtil; +import com.szpg.service.ReadControllerStatusService; +import com.szpg.util.Configure; + +public class ReadJgStatusTask implements Runnable { + + private ReadControllerStatusService service; + + public ReadJgStatusTask() { + service = new ReadControllerStatusService(); + } + + @Override + public void run() { + // 查找所有的client + Map clients = ACUClientUtil.getInstance().getClients(); + + for (String key : clients.keySet()) { + ACUClient client = clients.get(key); + + // 构造命令对象 + ReadMemoryCommand command = ReadMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_READJGSTATUS); + String sour = Configure.getProperty("sys", "LOCALHOST.NET") + + Configure.getProperty("sys", "LOCALHOST.NODE") + + Configure.getProperty("sys", "LOCALHOST.UNIT"); + command.setMessageProducerId(sour); + + String dest = client.getNet() + client.getNode() + client.getUnit(); + command.setDestinationId(dest); + + command.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_WORD); + + command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".JGSTAT.START")), 2)) + "00"); + command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".JGSTAT.WORDCOUNT"))); + command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".JG.COUNT"))); + + service.executeService(client, command); + } + } + +}