diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjRtTask.java b/src/com/szpg/task/ReadFjRtTask.java index 8127b09..690d2a9 100644 --- a/src/com/szpg/task/ReadFjRtTask.java +++ b/src/com/szpg/task/ReadFjRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJ.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjRtTask.java b/src/com/szpg/task/ReadFjRtTask.java index 8127b09..690d2a9 100644 --- a/src/com/szpg/task/ReadFjRtTask.java +++ b/src/com/szpg/task/ReadFjRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJ.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjStatTask.java b/src/com/szpg/task/ReadFjStatTask.java index 9558a0a..bd0f5c5 100644 --- a/src/com/szpg/task/ReadFjStatTask.java +++ b/src/com/szpg/task/ReadFjStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjRtTask.java b/src/com/szpg/task/ReadFjRtTask.java index 8127b09..690d2a9 100644 --- a/src/com/szpg/task/ReadFjRtTask.java +++ b/src/com/szpg/task/ReadFjRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJ.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjStatTask.java b/src/com/szpg/task/ReadFjStatTask.java index 9558a0a..bd0f5c5 100644 --- a/src/com/szpg/task/ReadFjStatTask.java +++ b/src/com/szpg/task/ReadFjStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadHSStatusTask.java b/src/com/szpg/task/ReadHSStatusTask.java index 94629cc..15a9e82 100644 --- a/src/com/szpg/task/ReadHSStatusTask.java +++ b/src/com/szpg/task/ReadHSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjRtTask.java b/src/com/szpg/task/ReadFjRtTask.java index 8127b09..690d2a9 100644 --- a/src/com/szpg/task/ReadFjRtTask.java +++ b/src/com/szpg/task/ReadFjRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJ.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjStatTask.java b/src/com/szpg/task/ReadFjStatTask.java index 9558a0a..bd0f5c5 100644 --- a/src/com/szpg/task/ReadFjStatTask.java +++ b/src/com/szpg/task/ReadFjStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadHSStatusTask.java b/src/com/szpg/task/ReadHSStatusTask.java index 94629cc..15a9e82 100644 --- a/src/com/szpg/task/ReadHSStatusTask.java +++ b/src/com/szpg/task/ReadHSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadHSValueTask.java b/src/com/szpg/task/ReadHSValueTask.java index c7f1149..5881156 100644 --- a/src/com/szpg/task/ReadHSValueTask.java +++ b/src/com/szpg/task/ReadHSValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjRtTask.java b/src/com/szpg/task/ReadFjRtTask.java index 8127b09..690d2a9 100644 --- a/src/com/szpg/task/ReadFjRtTask.java +++ b/src/com/szpg/task/ReadFjRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJ.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjStatTask.java b/src/com/szpg/task/ReadFjStatTask.java index 9558a0a..bd0f5c5 100644 --- a/src/com/szpg/task/ReadFjStatTask.java +++ b/src/com/szpg/task/ReadFjStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadHSStatusTask.java b/src/com/szpg/task/ReadHSStatusTask.java index 94629cc..15a9e82 100644 --- a/src/com/szpg/task/ReadHSStatusTask.java +++ b/src/com/szpg/task/ReadHSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadHSValueTask.java b/src/com/szpg/task/ReadHSValueTask.java index c7f1149..5881156 100644 --- a/src/com/szpg/task/ReadHSValueTask.java +++ b/src/com/szpg/task/ReadHSValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadJgStatusTask.java b/src/com/szpg/task/ReadJgStatusTask.java index ea69b97..d86f95b 100644 --- a/src/com/szpg/task/ReadJgStatusTask.java +++ b/src/com/szpg/task/ReadJgStatusTask.java @@ -41,7 +41,6 @@ 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); } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjRtTask.java b/src/com/szpg/task/ReadFjRtTask.java index 8127b09..690d2a9 100644 --- a/src/com/szpg/task/ReadFjRtTask.java +++ b/src/com/szpg/task/ReadFjRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJ.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjStatTask.java b/src/com/szpg/task/ReadFjStatTask.java index 9558a0a..bd0f5c5 100644 --- a/src/com/szpg/task/ReadFjStatTask.java +++ b/src/com/szpg/task/ReadFjStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadHSStatusTask.java b/src/com/szpg/task/ReadHSStatusTask.java index 94629cc..15a9e82 100644 --- a/src/com/szpg/task/ReadHSStatusTask.java +++ b/src/com/szpg/task/ReadHSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadHSValueTask.java b/src/com/szpg/task/ReadHSValueTask.java index c7f1149..5881156 100644 --- a/src/com/szpg/task/ReadHSValueTask.java +++ b/src/com/szpg/task/ReadHSValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadJgStatusTask.java b/src/com/szpg/task/ReadJgStatusTask.java index ea69b97..d86f95b 100644 --- a/src/com/szpg/task/ReadJgStatusTask.java +++ b/src/com/szpg/task/ReadJgStatusTask.java @@ -41,7 +41,6 @@ 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); } diff --git a/src/com/szpg/task/ReadO2StatusTask.java b/src/com/szpg/task/ReadO2StatusTask.java index 951cf10..cf68811 100644 --- a/src/com/szpg/task/ReadO2StatusTask.java +++ b/src/com/szpg/task/ReadO2StatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjRtTask.java b/src/com/szpg/task/ReadFjRtTask.java index 8127b09..690d2a9 100644 --- a/src/com/szpg/task/ReadFjRtTask.java +++ b/src/com/szpg/task/ReadFjRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJ.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjStatTask.java b/src/com/szpg/task/ReadFjStatTask.java index 9558a0a..bd0f5c5 100644 --- a/src/com/szpg/task/ReadFjStatTask.java +++ b/src/com/szpg/task/ReadFjStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadHSStatusTask.java b/src/com/szpg/task/ReadHSStatusTask.java index 94629cc..15a9e82 100644 --- a/src/com/szpg/task/ReadHSStatusTask.java +++ b/src/com/szpg/task/ReadHSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadHSValueTask.java b/src/com/szpg/task/ReadHSValueTask.java index c7f1149..5881156 100644 --- a/src/com/szpg/task/ReadHSValueTask.java +++ b/src/com/szpg/task/ReadHSValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadJgStatusTask.java b/src/com/szpg/task/ReadJgStatusTask.java index ea69b97..d86f95b 100644 --- a/src/com/szpg/task/ReadJgStatusTask.java +++ b/src/com/szpg/task/ReadJgStatusTask.java @@ -41,7 +41,6 @@ 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); } diff --git a/src/com/szpg/task/ReadO2StatusTask.java b/src/com/szpg/task/ReadO2StatusTask.java index 951cf10..cf68811 100644 --- a/src/com/szpg/task/ReadO2StatusTask.java +++ b/src/com/szpg/task/ReadO2StatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadO2ValueTask.java b/src/com/szpg/task/ReadO2ValueTask.java index 0da31f2..5e2e21f 100644 --- a/src/com/szpg/task/ReadO2ValueTask.java +++ b/src/com/szpg/task/ReadO2ValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjRtTask.java b/src/com/szpg/task/ReadFjRtTask.java index 8127b09..690d2a9 100644 --- a/src/com/szpg/task/ReadFjRtTask.java +++ b/src/com/szpg/task/ReadFjRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJ.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjStatTask.java b/src/com/szpg/task/ReadFjStatTask.java index 9558a0a..bd0f5c5 100644 --- a/src/com/szpg/task/ReadFjStatTask.java +++ b/src/com/szpg/task/ReadFjStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadHSStatusTask.java b/src/com/szpg/task/ReadHSStatusTask.java index 94629cc..15a9e82 100644 --- a/src/com/szpg/task/ReadHSStatusTask.java +++ b/src/com/szpg/task/ReadHSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadHSValueTask.java b/src/com/szpg/task/ReadHSValueTask.java index c7f1149..5881156 100644 --- a/src/com/szpg/task/ReadHSValueTask.java +++ b/src/com/szpg/task/ReadHSValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadJgStatusTask.java b/src/com/szpg/task/ReadJgStatusTask.java index ea69b97..d86f95b 100644 --- a/src/com/szpg/task/ReadJgStatusTask.java +++ b/src/com/szpg/task/ReadJgStatusTask.java @@ -41,7 +41,6 @@ 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); } diff --git a/src/com/szpg/task/ReadO2StatusTask.java b/src/com/szpg/task/ReadO2StatusTask.java index 951cf10..cf68811 100644 --- a/src/com/szpg/task/ReadO2StatusTask.java +++ b/src/com/szpg/task/ReadO2StatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadO2ValueTask.java b/src/com/szpg/task/ReadO2ValueTask.java index 0da31f2..5e2e21f 100644 --- a/src/com/szpg/task/ReadO2ValueTask.java +++ b/src/com/szpg/task/ReadO2ValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadSbRtTask.java b/src/com/szpg/task/ReadSbRtTask.java index 6dd46cc..9083b72 100644 --- a/src/com/szpg/task/ReadSbRtTask.java +++ b/src/com/szpg/task/ReadSbRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjRtTask.java b/src/com/szpg/task/ReadFjRtTask.java index 8127b09..690d2a9 100644 --- a/src/com/szpg/task/ReadFjRtTask.java +++ b/src/com/szpg/task/ReadFjRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJ.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjStatTask.java b/src/com/szpg/task/ReadFjStatTask.java index 9558a0a..bd0f5c5 100644 --- a/src/com/szpg/task/ReadFjStatTask.java +++ b/src/com/szpg/task/ReadFjStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadHSStatusTask.java b/src/com/szpg/task/ReadHSStatusTask.java index 94629cc..15a9e82 100644 --- a/src/com/szpg/task/ReadHSStatusTask.java +++ b/src/com/szpg/task/ReadHSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadHSValueTask.java b/src/com/szpg/task/ReadHSValueTask.java index c7f1149..5881156 100644 --- a/src/com/szpg/task/ReadHSValueTask.java +++ b/src/com/szpg/task/ReadHSValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadJgStatusTask.java b/src/com/szpg/task/ReadJgStatusTask.java index ea69b97..d86f95b 100644 --- a/src/com/szpg/task/ReadJgStatusTask.java +++ b/src/com/szpg/task/ReadJgStatusTask.java @@ -41,7 +41,6 @@ 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); } diff --git a/src/com/szpg/task/ReadO2StatusTask.java b/src/com/szpg/task/ReadO2StatusTask.java index 951cf10..cf68811 100644 --- a/src/com/szpg/task/ReadO2StatusTask.java +++ b/src/com/szpg/task/ReadO2StatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadO2ValueTask.java b/src/com/szpg/task/ReadO2ValueTask.java index 0da31f2..5e2e21f 100644 --- a/src/com/szpg/task/ReadO2ValueTask.java +++ b/src/com/szpg/task/ReadO2ValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadSbRtTask.java b/src/com/szpg/task/ReadSbRtTask.java index 6dd46cc..9083b72 100644 --- a/src/com/szpg/task/ReadSbRtTask.java +++ b/src/com/szpg/task/ReadSbRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadSbStatTask.java b/src/com/szpg/task/ReadSbStatTask.java index 5c7c518..1ca23f4 100644 --- a/src/com/szpg/task/ReadSbStatTask.java +++ b/src/com/szpg/task/ReadSbStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjRtTask.java b/src/com/szpg/task/ReadFjRtTask.java index 8127b09..690d2a9 100644 --- a/src/com/szpg/task/ReadFjRtTask.java +++ b/src/com/szpg/task/ReadFjRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJ.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjStatTask.java b/src/com/szpg/task/ReadFjStatTask.java index 9558a0a..bd0f5c5 100644 --- a/src/com/szpg/task/ReadFjStatTask.java +++ b/src/com/szpg/task/ReadFjStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadHSStatusTask.java b/src/com/szpg/task/ReadHSStatusTask.java index 94629cc..15a9e82 100644 --- a/src/com/szpg/task/ReadHSStatusTask.java +++ b/src/com/szpg/task/ReadHSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadHSValueTask.java b/src/com/szpg/task/ReadHSValueTask.java index c7f1149..5881156 100644 --- a/src/com/szpg/task/ReadHSValueTask.java +++ b/src/com/szpg/task/ReadHSValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadJgStatusTask.java b/src/com/szpg/task/ReadJgStatusTask.java index ea69b97..d86f95b 100644 --- a/src/com/szpg/task/ReadJgStatusTask.java +++ b/src/com/szpg/task/ReadJgStatusTask.java @@ -41,7 +41,6 @@ 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); } diff --git a/src/com/szpg/task/ReadO2StatusTask.java b/src/com/szpg/task/ReadO2StatusTask.java index 951cf10..cf68811 100644 --- a/src/com/szpg/task/ReadO2StatusTask.java +++ b/src/com/szpg/task/ReadO2StatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadO2ValueTask.java b/src/com/szpg/task/ReadO2ValueTask.java index 0da31f2..5e2e21f 100644 --- a/src/com/szpg/task/ReadO2ValueTask.java +++ b/src/com/szpg/task/ReadO2ValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadSbRtTask.java b/src/com/szpg/task/ReadSbRtTask.java index 6dd46cc..9083b72 100644 --- a/src/com/szpg/task/ReadSbRtTask.java +++ b/src/com/szpg/task/ReadSbRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadSbStatTask.java b/src/com/szpg/task/ReadSbStatTask.java index 5c7c518..1ca23f4 100644 --- a/src/com/szpg/task/ReadSbStatTask.java +++ b/src/com/szpg/task/ReadSbStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadWSStatusTask.java b/src/com/szpg/task/ReadWSStatusTask.java index 608c978..bfa6809 100644 --- a/src/com/szpg/task/ReadWSStatusTask.java +++ b/src/com/szpg/task/ReadWSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjRtTask.java b/src/com/szpg/task/ReadFjRtTask.java index 8127b09..690d2a9 100644 --- a/src/com/szpg/task/ReadFjRtTask.java +++ b/src/com/szpg/task/ReadFjRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJ.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjStatTask.java b/src/com/szpg/task/ReadFjStatTask.java index 9558a0a..bd0f5c5 100644 --- a/src/com/szpg/task/ReadFjStatTask.java +++ b/src/com/szpg/task/ReadFjStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadHSStatusTask.java b/src/com/szpg/task/ReadHSStatusTask.java index 94629cc..15a9e82 100644 --- a/src/com/szpg/task/ReadHSStatusTask.java +++ b/src/com/szpg/task/ReadHSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadHSValueTask.java b/src/com/szpg/task/ReadHSValueTask.java index c7f1149..5881156 100644 --- a/src/com/szpg/task/ReadHSValueTask.java +++ b/src/com/szpg/task/ReadHSValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadJgStatusTask.java b/src/com/szpg/task/ReadJgStatusTask.java index ea69b97..d86f95b 100644 --- a/src/com/szpg/task/ReadJgStatusTask.java +++ b/src/com/szpg/task/ReadJgStatusTask.java @@ -41,7 +41,6 @@ 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); } diff --git a/src/com/szpg/task/ReadO2StatusTask.java b/src/com/szpg/task/ReadO2StatusTask.java index 951cf10..cf68811 100644 --- a/src/com/szpg/task/ReadO2StatusTask.java +++ b/src/com/szpg/task/ReadO2StatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadO2ValueTask.java b/src/com/szpg/task/ReadO2ValueTask.java index 0da31f2..5e2e21f 100644 --- a/src/com/szpg/task/ReadO2ValueTask.java +++ b/src/com/szpg/task/ReadO2ValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadSbRtTask.java b/src/com/szpg/task/ReadSbRtTask.java index 6dd46cc..9083b72 100644 --- a/src/com/szpg/task/ReadSbRtTask.java +++ b/src/com/szpg/task/ReadSbRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadSbStatTask.java b/src/com/szpg/task/ReadSbStatTask.java index 5c7c518..1ca23f4 100644 --- a/src/com/szpg/task/ReadSbStatTask.java +++ b/src/com/szpg/task/ReadSbStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadWSStatusTask.java b/src/com/szpg/task/ReadWSStatusTask.java index 608c978..bfa6809 100644 --- a/src/com/szpg/task/ReadWSStatusTask.java +++ b/src/com/szpg/task/ReadWSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadWSValueTask.java b/src/com/szpg/task/ReadWSValueTask.java index 3a34dec..a354e7a 100644 --- a/src/com/szpg/task/ReadWSValueTask.java +++ b/src/com/szpg/task/ReadWSValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjRtTask.java b/src/com/szpg/task/ReadFjRtTask.java index 8127b09..690d2a9 100644 --- a/src/com/szpg/task/ReadFjRtTask.java +++ b/src/com/szpg/task/ReadFjRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJ.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjStatTask.java b/src/com/szpg/task/ReadFjStatTask.java index 9558a0a..bd0f5c5 100644 --- a/src/com/szpg/task/ReadFjStatTask.java +++ b/src/com/szpg/task/ReadFjStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadHSStatusTask.java b/src/com/szpg/task/ReadHSStatusTask.java index 94629cc..15a9e82 100644 --- a/src/com/szpg/task/ReadHSStatusTask.java +++ b/src/com/szpg/task/ReadHSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadHSValueTask.java b/src/com/szpg/task/ReadHSValueTask.java index c7f1149..5881156 100644 --- a/src/com/szpg/task/ReadHSValueTask.java +++ b/src/com/szpg/task/ReadHSValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadJgStatusTask.java b/src/com/szpg/task/ReadJgStatusTask.java index ea69b97..d86f95b 100644 --- a/src/com/szpg/task/ReadJgStatusTask.java +++ b/src/com/szpg/task/ReadJgStatusTask.java @@ -41,7 +41,6 @@ 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); } diff --git a/src/com/szpg/task/ReadO2StatusTask.java b/src/com/szpg/task/ReadO2StatusTask.java index 951cf10..cf68811 100644 --- a/src/com/szpg/task/ReadO2StatusTask.java +++ b/src/com/szpg/task/ReadO2StatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadO2ValueTask.java b/src/com/szpg/task/ReadO2ValueTask.java index 0da31f2..5e2e21f 100644 --- a/src/com/szpg/task/ReadO2ValueTask.java +++ b/src/com/szpg/task/ReadO2ValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadSbRtTask.java b/src/com/szpg/task/ReadSbRtTask.java index 6dd46cc..9083b72 100644 --- a/src/com/szpg/task/ReadSbRtTask.java +++ b/src/com/szpg/task/ReadSbRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadSbStatTask.java b/src/com/szpg/task/ReadSbStatTask.java index 5c7c518..1ca23f4 100644 --- a/src/com/szpg/task/ReadSbStatTask.java +++ b/src/com/szpg/task/ReadSbStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadWSStatusTask.java b/src/com/szpg/task/ReadWSStatusTask.java index 608c978..bfa6809 100644 --- a/src/com/szpg/task/ReadWSStatusTask.java +++ b/src/com/szpg/task/ReadWSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadWSValueTask.java b/src/com/szpg/task/ReadWSValueTask.java index 3a34dec..a354e7a 100644 --- a/src/com/szpg/task/ReadWSValueTask.java +++ b/src/com/szpg/task/ReadWSValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadYWStatusTask.java b/src/com/szpg/task/ReadYWStatusTask.java index 36b2952..6df45a8 100644 --- a/src/com/szpg/task/ReadYWStatusTask.java +++ b/src/com/szpg/task/ReadYWStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YWALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YWALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YW.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjRtTask.java b/src/com/szpg/task/ReadFjRtTask.java index 8127b09..690d2a9 100644 --- a/src/com/szpg/task/ReadFjRtTask.java +++ b/src/com/szpg/task/ReadFjRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJ.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjStatTask.java b/src/com/szpg/task/ReadFjStatTask.java index 9558a0a..bd0f5c5 100644 --- a/src/com/szpg/task/ReadFjStatTask.java +++ b/src/com/szpg/task/ReadFjStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadHSStatusTask.java b/src/com/szpg/task/ReadHSStatusTask.java index 94629cc..15a9e82 100644 --- a/src/com/szpg/task/ReadHSStatusTask.java +++ b/src/com/szpg/task/ReadHSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadHSValueTask.java b/src/com/szpg/task/ReadHSValueTask.java index c7f1149..5881156 100644 --- a/src/com/szpg/task/ReadHSValueTask.java +++ b/src/com/szpg/task/ReadHSValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadJgStatusTask.java b/src/com/szpg/task/ReadJgStatusTask.java index ea69b97..d86f95b 100644 --- a/src/com/szpg/task/ReadJgStatusTask.java +++ b/src/com/szpg/task/ReadJgStatusTask.java @@ -41,7 +41,6 @@ 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); } diff --git a/src/com/szpg/task/ReadO2StatusTask.java b/src/com/szpg/task/ReadO2StatusTask.java index 951cf10..cf68811 100644 --- a/src/com/szpg/task/ReadO2StatusTask.java +++ b/src/com/szpg/task/ReadO2StatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadO2ValueTask.java b/src/com/szpg/task/ReadO2ValueTask.java index 0da31f2..5e2e21f 100644 --- a/src/com/szpg/task/ReadO2ValueTask.java +++ b/src/com/szpg/task/ReadO2ValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadSbRtTask.java b/src/com/szpg/task/ReadSbRtTask.java index 6dd46cc..9083b72 100644 --- a/src/com/szpg/task/ReadSbRtTask.java +++ b/src/com/szpg/task/ReadSbRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadSbStatTask.java b/src/com/szpg/task/ReadSbStatTask.java index 5c7c518..1ca23f4 100644 --- a/src/com/szpg/task/ReadSbStatTask.java +++ b/src/com/szpg/task/ReadSbStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadWSStatusTask.java b/src/com/szpg/task/ReadWSStatusTask.java index 608c978..bfa6809 100644 --- a/src/com/szpg/task/ReadWSStatusTask.java +++ b/src/com/szpg/task/ReadWSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadWSValueTask.java b/src/com/szpg/task/ReadWSValueTask.java index 3a34dec..a354e7a 100644 --- a/src/com/szpg/task/ReadWSValueTask.java +++ b/src/com/szpg/task/ReadWSValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadYWStatusTask.java b/src/com/szpg/task/ReadYWStatusTask.java index 36b2952..6df45a8 100644 --- a/src/com/szpg/task/ReadYWStatusTask.java +++ b/src/com/szpg/task/ReadYWStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YWALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YWALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YW.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadZmRtTask.java b/src/com/szpg/task/ReadZmRtTask.java index 881b7cb..30c5835 100644 --- a/src/com/szpg/task/ReadZmRtTask.java +++ b/src/com/szpg/task/ReadZmRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZM.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjRtTask.java b/src/com/szpg/task/ReadFjRtTask.java index 8127b09..690d2a9 100644 --- a/src/com/szpg/task/ReadFjRtTask.java +++ b/src/com/szpg/task/ReadFjRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJ.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjStatTask.java b/src/com/szpg/task/ReadFjStatTask.java index 9558a0a..bd0f5c5 100644 --- a/src/com/szpg/task/ReadFjStatTask.java +++ b/src/com/szpg/task/ReadFjStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadHSStatusTask.java b/src/com/szpg/task/ReadHSStatusTask.java index 94629cc..15a9e82 100644 --- a/src/com/szpg/task/ReadHSStatusTask.java +++ b/src/com/szpg/task/ReadHSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadHSValueTask.java b/src/com/szpg/task/ReadHSValueTask.java index c7f1149..5881156 100644 --- a/src/com/szpg/task/ReadHSValueTask.java +++ b/src/com/szpg/task/ReadHSValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadJgStatusTask.java b/src/com/szpg/task/ReadJgStatusTask.java index ea69b97..d86f95b 100644 --- a/src/com/szpg/task/ReadJgStatusTask.java +++ b/src/com/szpg/task/ReadJgStatusTask.java @@ -41,7 +41,6 @@ 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); } diff --git a/src/com/szpg/task/ReadO2StatusTask.java b/src/com/szpg/task/ReadO2StatusTask.java index 951cf10..cf68811 100644 --- a/src/com/szpg/task/ReadO2StatusTask.java +++ b/src/com/szpg/task/ReadO2StatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadO2ValueTask.java b/src/com/szpg/task/ReadO2ValueTask.java index 0da31f2..5e2e21f 100644 --- a/src/com/szpg/task/ReadO2ValueTask.java +++ b/src/com/szpg/task/ReadO2ValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadSbRtTask.java b/src/com/szpg/task/ReadSbRtTask.java index 6dd46cc..9083b72 100644 --- a/src/com/szpg/task/ReadSbRtTask.java +++ b/src/com/szpg/task/ReadSbRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadSbStatTask.java b/src/com/szpg/task/ReadSbStatTask.java index 5c7c518..1ca23f4 100644 --- a/src/com/szpg/task/ReadSbStatTask.java +++ b/src/com/szpg/task/ReadSbStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadWSStatusTask.java b/src/com/szpg/task/ReadWSStatusTask.java index 608c978..bfa6809 100644 --- a/src/com/szpg/task/ReadWSStatusTask.java +++ b/src/com/szpg/task/ReadWSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadWSValueTask.java b/src/com/szpg/task/ReadWSValueTask.java index 3a34dec..a354e7a 100644 --- a/src/com/szpg/task/ReadWSValueTask.java +++ b/src/com/szpg/task/ReadWSValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadYWStatusTask.java b/src/com/szpg/task/ReadYWStatusTask.java index 36b2952..6df45a8 100644 --- a/src/com/szpg/task/ReadYWStatusTask.java +++ b/src/com/szpg/task/ReadYWStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YWALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YWALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YW.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadZmRtTask.java b/src/com/szpg/task/ReadZmRtTask.java index 881b7cb..30c5835 100644 --- a/src/com/szpg/task/ReadZmRtTask.java +++ b/src/com/szpg/task/ReadZmRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZM.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadZmStatTask.java b/src/com/szpg/task/ReadZmStatTask.java index 540d3b8..c81c1eb 100644 --- a/src/com/szpg/task/ReadZmStatTask.java +++ b/src/com/szpg/task/ReadZmStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZM.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjRtTask.java b/src/com/szpg/task/ReadFjRtTask.java index 8127b09..690d2a9 100644 --- a/src/com/szpg/task/ReadFjRtTask.java +++ b/src/com/szpg/task/ReadFjRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJ.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjStatTask.java b/src/com/szpg/task/ReadFjStatTask.java index 9558a0a..bd0f5c5 100644 --- a/src/com/szpg/task/ReadFjStatTask.java +++ b/src/com/szpg/task/ReadFjStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadHSStatusTask.java b/src/com/szpg/task/ReadHSStatusTask.java index 94629cc..15a9e82 100644 --- a/src/com/szpg/task/ReadHSStatusTask.java +++ b/src/com/szpg/task/ReadHSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadHSValueTask.java b/src/com/szpg/task/ReadHSValueTask.java index c7f1149..5881156 100644 --- a/src/com/szpg/task/ReadHSValueTask.java +++ b/src/com/szpg/task/ReadHSValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadJgStatusTask.java b/src/com/szpg/task/ReadJgStatusTask.java index ea69b97..d86f95b 100644 --- a/src/com/szpg/task/ReadJgStatusTask.java +++ b/src/com/szpg/task/ReadJgStatusTask.java @@ -41,7 +41,6 @@ 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); } diff --git a/src/com/szpg/task/ReadO2StatusTask.java b/src/com/szpg/task/ReadO2StatusTask.java index 951cf10..cf68811 100644 --- a/src/com/szpg/task/ReadO2StatusTask.java +++ b/src/com/szpg/task/ReadO2StatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadO2ValueTask.java b/src/com/szpg/task/ReadO2ValueTask.java index 0da31f2..5e2e21f 100644 --- a/src/com/szpg/task/ReadO2ValueTask.java +++ b/src/com/szpg/task/ReadO2ValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadSbRtTask.java b/src/com/szpg/task/ReadSbRtTask.java index 6dd46cc..9083b72 100644 --- a/src/com/szpg/task/ReadSbRtTask.java +++ b/src/com/szpg/task/ReadSbRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadSbStatTask.java b/src/com/szpg/task/ReadSbStatTask.java index 5c7c518..1ca23f4 100644 --- a/src/com/szpg/task/ReadSbStatTask.java +++ b/src/com/szpg/task/ReadSbStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadWSStatusTask.java b/src/com/szpg/task/ReadWSStatusTask.java index 608c978..bfa6809 100644 --- a/src/com/szpg/task/ReadWSStatusTask.java +++ b/src/com/szpg/task/ReadWSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadWSValueTask.java b/src/com/szpg/task/ReadWSValueTask.java index 3a34dec..a354e7a 100644 --- a/src/com/szpg/task/ReadWSValueTask.java +++ b/src/com/szpg/task/ReadWSValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadYWStatusTask.java b/src/com/szpg/task/ReadYWStatusTask.java index 36b2952..6df45a8 100644 --- a/src/com/szpg/task/ReadYWStatusTask.java +++ b/src/com/szpg/task/ReadYWStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YWALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YWALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YW.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadZmRtTask.java b/src/com/szpg/task/ReadZmRtTask.java index 881b7cb..30c5835 100644 --- a/src/com/szpg/task/ReadZmRtTask.java +++ b/src/com/szpg/task/ReadZmRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZM.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadZmStatTask.java b/src/com/szpg/task/ReadZmStatTask.java index 540d3b8..c81c1eb 100644 --- a/src/com/szpg/task/ReadZmStatTask.java +++ b/src/com/szpg/task/ReadZmStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZM.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/TurnOffFjTask.java b/src/com/szpg/task/TurnOffFjTask.java new file mode 100644 index 0000000..6280063 --- /dev/null +++ b/src/com/szpg/task/TurnOffFjTask.java @@ -0,0 +1,178 @@ +package com.szpg.task; + +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.PgAcuDao; +import com.szpg.db.dao.PgDeviceDao; +import com.szpg.db.dao.PgHjsbblDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuDaoImpl; +import com.szpg.db.dao.impl.PgDeviceDaoImpl; +import com.szpg.db.dao.impl.PgHjsbblDaoImpl; +import com.szpg.db.data.PgAcu; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.data.PgHjsbbl; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.CommandResponse; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.protocol.DTProtocolInterface; +import com.szpg.plc.protocol.ProtocolFactory; +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.util.Configure; + +public class TurnOffFjTask implements Runnable { + + private Logger logger = Logger.getLogger(this.getClass().getName()); + + private String deviceCode; + private PgHjsbblDao blDao = new PgHjsbblDaoImpl(); + + public TurnOffFjTask(String deviceCode) { + this.deviceCode = deviceCode; + } + + @Override + public void run() { + // 查找ACU的信息 + PgAcuDao acuDao = new PgAcuDaoImpl(); + PgDeviceDao deviceDao = new PgDeviceDaoImpl(); + String acucode = deviceDao.findAcuCodeByCode(deviceCode); + PgAcu acu = acuDao.findACUByCode(acucode); + ACUClient client = ACUClientUtil.getInstance().getClients().get(acu.getAcu_host() + ":" + acu.getAcu_port()); + if (null != client) { + // 源地址 + String sour = Configure.getProperty("sys", "LOCALHOST.NET") + + Configure.getProperty("sys", "LOCALHOST.NODE") + + Configure.getProperty("sys", "LOCALHOST.UNIT"); + + // 目标地址 + String dest = client.getNet() + client.getNode() + client.getUnit(); + + DTProtocolInterface finspi = ProtocolFactory.getDefaultDTProtocol(); + + // 1首先将启动位置0 + WriteMemoryCommand clearOnCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETFJON); + PgHjsbbl onBlObj = blDao.findBlByBh(deviceCode + ".ON"); + if (null != onBlObj) { + clearOnCmd.setMessageProducerId(sour); + clearOnCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + clearOnCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = onBlObj.getKszdz(); + int end = onBlObj.getJszdz(); + int bit = onBlObj.getSzw(); + + // 开始字地址 + clearOnCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + clearOnCmd.setBit(bit); + + // 位数 + clearOnCmd.setCount(end - start + 1); + + // 位内容 + clearOnCmd.setValue(new byte[] {0x00} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(clearOnCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志 + logger.error("清除启动位后的阻塞等待线程被打断", e); + } + + // 2 发送设置启动位的命令 + WriteMemoryCommand setOffCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETFJOFF); + PgHjsbbl offBlObj = blDao.findBlByBh(deviceCode + ".OFF"); + if (null != offBlObj) { + setOffCmd.setMessageProducerId(sour); + setOffCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + setOffCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = offBlObj.getKszdz(); + int end = offBlObj.getJszdz(); + int bit = offBlObj.getSzw(); + + // 开始字地址 + setOffCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + setOffCmd.setBit(bit); + + // 位数 + setOffCmd.setCount(end - start + 1); + + // 位内容 + setOffCmd.setValue(new byte[] {0x01} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(setOffCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + // 3将命令存入数据库 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = new PgAcuCmd(); + cmd.setCmd_type(setOffCmd.getCommandType()); + cmd.setDest_acu_code(acucode); + cmd.setTm(setOffCmd.getTime().getTime()); + cmdDao.addCmdRecord(cmd); + + // 4阻塞,循环查找响应消息池,找到对应的响应消息 + boolean flag = false; + int times = 0; + CommandResponse response = null; + while (flag == false && times < 240) { + response = ACUClientUtil.getInstance().responsePool.getResponse(cmd.getId()); + + if (null != response && response.equals("") == false) { + flag = true; + } + + times++; + try { + Thread.sleep(500); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志;2)将命令置为超时 + logger.error("在响应池中查找命令的响应消息阻塞线程被异常打断", e); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + + return; + } + } + + // 5若未超时,将值存入数据库 + if (null != response) { + // 6根据命令类型的不同将监测值存入对应的数据库 + response.afterAction(); + } else { + // 9超时,将命令的超时标志位置1 + logger.warn("命令超时" + cmd.getId()); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + } + } + } +} diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjRtTask.java b/src/com/szpg/task/ReadFjRtTask.java index 8127b09..690d2a9 100644 --- a/src/com/szpg/task/ReadFjRtTask.java +++ b/src/com/szpg/task/ReadFjRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJ.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjStatTask.java b/src/com/szpg/task/ReadFjStatTask.java index 9558a0a..bd0f5c5 100644 --- a/src/com/szpg/task/ReadFjStatTask.java +++ b/src/com/szpg/task/ReadFjStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadHSStatusTask.java b/src/com/szpg/task/ReadHSStatusTask.java index 94629cc..15a9e82 100644 --- a/src/com/szpg/task/ReadHSStatusTask.java +++ b/src/com/szpg/task/ReadHSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadHSValueTask.java b/src/com/szpg/task/ReadHSValueTask.java index c7f1149..5881156 100644 --- a/src/com/szpg/task/ReadHSValueTask.java +++ b/src/com/szpg/task/ReadHSValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadJgStatusTask.java b/src/com/szpg/task/ReadJgStatusTask.java index ea69b97..d86f95b 100644 --- a/src/com/szpg/task/ReadJgStatusTask.java +++ b/src/com/szpg/task/ReadJgStatusTask.java @@ -41,7 +41,6 @@ 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); } diff --git a/src/com/szpg/task/ReadO2StatusTask.java b/src/com/szpg/task/ReadO2StatusTask.java index 951cf10..cf68811 100644 --- a/src/com/szpg/task/ReadO2StatusTask.java +++ b/src/com/szpg/task/ReadO2StatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadO2ValueTask.java b/src/com/szpg/task/ReadO2ValueTask.java index 0da31f2..5e2e21f 100644 --- a/src/com/szpg/task/ReadO2ValueTask.java +++ b/src/com/szpg/task/ReadO2ValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadSbRtTask.java b/src/com/szpg/task/ReadSbRtTask.java index 6dd46cc..9083b72 100644 --- a/src/com/szpg/task/ReadSbRtTask.java +++ b/src/com/szpg/task/ReadSbRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadSbStatTask.java b/src/com/szpg/task/ReadSbStatTask.java index 5c7c518..1ca23f4 100644 --- a/src/com/szpg/task/ReadSbStatTask.java +++ b/src/com/szpg/task/ReadSbStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadWSStatusTask.java b/src/com/szpg/task/ReadWSStatusTask.java index 608c978..bfa6809 100644 --- a/src/com/szpg/task/ReadWSStatusTask.java +++ b/src/com/szpg/task/ReadWSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadWSValueTask.java b/src/com/szpg/task/ReadWSValueTask.java index 3a34dec..a354e7a 100644 --- a/src/com/szpg/task/ReadWSValueTask.java +++ b/src/com/szpg/task/ReadWSValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadYWStatusTask.java b/src/com/szpg/task/ReadYWStatusTask.java index 36b2952..6df45a8 100644 --- a/src/com/szpg/task/ReadYWStatusTask.java +++ b/src/com/szpg/task/ReadYWStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YWALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YWALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YW.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadZmRtTask.java b/src/com/szpg/task/ReadZmRtTask.java index 881b7cb..30c5835 100644 --- a/src/com/szpg/task/ReadZmRtTask.java +++ b/src/com/szpg/task/ReadZmRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZM.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadZmStatTask.java b/src/com/szpg/task/ReadZmStatTask.java index 540d3b8..c81c1eb 100644 --- a/src/com/szpg/task/ReadZmStatTask.java +++ b/src/com/szpg/task/ReadZmStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZM.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/TurnOffFjTask.java b/src/com/szpg/task/TurnOffFjTask.java new file mode 100644 index 0000000..6280063 --- /dev/null +++ b/src/com/szpg/task/TurnOffFjTask.java @@ -0,0 +1,178 @@ +package com.szpg.task; + +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.PgAcuDao; +import com.szpg.db.dao.PgDeviceDao; +import com.szpg.db.dao.PgHjsbblDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuDaoImpl; +import com.szpg.db.dao.impl.PgDeviceDaoImpl; +import com.szpg.db.dao.impl.PgHjsbblDaoImpl; +import com.szpg.db.data.PgAcu; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.data.PgHjsbbl; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.CommandResponse; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.protocol.DTProtocolInterface; +import com.szpg.plc.protocol.ProtocolFactory; +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.util.Configure; + +public class TurnOffFjTask implements Runnable { + + private Logger logger = Logger.getLogger(this.getClass().getName()); + + private String deviceCode; + private PgHjsbblDao blDao = new PgHjsbblDaoImpl(); + + public TurnOffFjTask(String deviceCode) { + this.deviceCode = deviceCode; + } + + @Override + public void run() { + // 查找ACU的信息 + PgAcuDao acuDao = new PgAcuDaoImpl(); + PgDeviceDao deviceDao = new PgDeviceDaoImpl(); + String acucode = deviceDao.findAcuCodeByCode(deviceCode); + PgAcu acu = acuDao.findACUByCode(acucode); + ACUClient client = ACUClientUtil.getInstance().getClients().get(acu.getAcu_host() + ":" + acu.getAcu_port()); + if (null != client) { + // 源地址 + String sour = Configure.getProperty("sys", "LOCALHOST.NET") + + Configure.getProperty("sys", "LOCALHOST.NODE") + + Configure.getProperty("sys", "LOCALHOST.UNIT"); + + // 目标地址 + String dest = client.getNet() + client.getNode() + client.getUnit(); + + DTProtocolInterface finspi = ProtocolFactory.getDefaultDTProtocol(); + + // 1首先将启动位置0 + WriteMemoryCommand clearOnCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETFJON); + PgHjsbbl onBlObj = blDao.findBlByBh(deviceCode + ".ON"); + if (null != onBlObj) { + clearOnCmd.setMessageProducerId(sour); + clearOnCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + clearOnCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = onBlObj.getKszdz(); + int end = onBlObj.getJszdz(); + int bit = onBlObj.getSzw(); + + // 开始字地址 + clearOnCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + clearOnCmd.setBit(bit); + + // 位数 + clearOnCmd.setCount(end - start + 1); + + // 位内容 + clearOnCmd.setValue(new byte[] {0x00} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(clearOnCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志 + logger.error("清除启动位后的阻塞等待线程被打断", e); + } + + // 2 发送设置启动位的命令 + WriteMemoryCommand setOffCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETFJOFF); + PgHjsbbl offBlObj = blDao.findBlByBh(deviceCode + ".OFF"); + if (null != offBlObj) { + setOffCmd.setMessageProducerId(sour); + setOffCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + setOffCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = offBlObj.getKszdz(); + int end = offBlObj.getJszdz(); + int bit = offBlObj.getSzw(); + + // 开始字地址 + setOffCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + setOffCmd.setBit(bit); + + // 位数 + setOffCmd.setCount(end - start + 1); + + // 位内容 + setOffCmd.setValue(new byte[] {0x01} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(setOffCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + // 3将命令存入数据库 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = new PgAcuCmd(); + cmd.setCmd_type(setOffCmd.getCommandType()); + cmd.setDest_acu_code(acucode); + cmd.setTm(setOffCmd.getTime().getTime()); + cmdDao.addCmdRecord(cmd); + + // 4阻塞,循环查找响应消息池,找到对应的响应消息 + boolean flag = false; + int times = 0; + CommandResponse response = null; + while (flag == false && times < 240) { + response = ACUClientUtil.getInstance().responsePool.getResponse(cmd.getId()); + + if (null != response && response.equals("") == false) { + flag = true; + } + + times++; + try { + Thread.sleep(500); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志;2)将命令置为超时 + logger.error("在响应池中查找命令的响应消息阻塞线程被异常打断", e); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + + return; + } + } + + // 5若未超时,将值存入数据库 + if (null != response) { + // 6根据命令类型的不同将监测值存入对应的数据库 + response.afterAction(); + } else { + // 9超时,将命令的超时标志位置1 + logger.warn("命令超时" + cmd.getId()); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + } + } + } +} diff --git a/src/com/szpg/task/TurnOffZmTask.java b/src/com/szpg/task/TurnOffZmTask.java new file mode 100644 index 0000000..d3a1ff3 --- /dev/null +++ b/src/com/szpg/task/TurnOffZmTask.java @@ -0,0 +1,178 @@ +package com.szpg.task; + +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.PgAcuDao; +import com.szpg.db.dao.PgDeviceDao; +import com.szpg.db.dao.PgHjsbblDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuDaoImpl; +import com.szpg.db.dao.impl.PgDeviceDaoImpl; +import com.szpg.db.dao.impl.PgHjsbblDaoImpl; +import com.szpg.db.data.PgAcu; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.data.PgHjsbbl; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.CommandResponse; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.protocol.DTProtocolInterface; +import com.szpg.plc.protocol.ProtocolFactory; +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.util.Configure; + +public class TurnOffZmTask implements Runnable { + + private Logger logger = Logger.getLogger(this.getClass().getName()); + + private String deviceCode; + private PgHjsbblDao blDao = new PgHjsbblDaoImpl(); + + public TurnOffZmTask(String deviceCode) { + this.deviceCode = deviceCode; + } + + @Override + public void run() { + // 查找ACU的信息 + PgAcuDao acuDao = new PgAcuDaoImpl(); + PgDeviceDao deviceDao = new PgDeviceDaoImpl(); + String acucode = deviceDao.findAcuCodeByCode(deviceCode); + PgAcu acu = acuDao.findACUByCode(acucode); + ACUClient client = ACUClientUtil.getInstance().getClients().get(acu.getAcu_host() + ":" + acu.getAcu_port()); + if (null != client) { + // 源地址 + String sour = Configure.getProperty("sys", "LOCALHOST.NET") + + Configure.getProperty("sys", "LOCALHOST.NODE") + + Configure.getProperty("sys", "LOCALHOST.UNIT"); + + // 目标地址 + String dest = client.getNet() + client.getNode() + client.getUnit(); + + DTProtocolInterface finspi = ProtocolFactory.getDefaultDTProtocol(); + + // 1首先将启动位置0 + WriteMemoryCommand clearOnCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETZMON); + PgHjsbbl onBlObj = blDao.findBlByBh(deviceCode + ".ON"); + if (null != onBlObj) { + clearOnCmd.setMessageProducerId(sour); + clearOnCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + clearOnCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = onBlObj.getKszdz(); + int end = onBlObj.getJszdz(); + int bit = onBlObj.getSzw(); + + // 开始字地址 + clearOnCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + clearOnCmd.setBit(bit); + + // 位数 + clearOnCmd.setCount(end - start + 1); + + // 位内容 + clearOnCmd.setValue(new byte[] {0x00} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(clearOnCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志 + logger.error("清除启动位后的阻塞等待线程被打断", e); + } + + // 2 发送设置启动位的命令 + WriteMemoryCommand setOffCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETZMOFF); + PgHjsbbl offBlObj = blDao.findBlByBh(deviceCode + ".OFF"); + if (null != offBlObj) { + setOffCmd.setMessageProducerId(sour); + setOffCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + setOffCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = offBlObj.getKszdz(); + int end = offBlObj.getJszdz(); + int bit = offBlObj.getSzw(); + + // 开始字地址 + setOffCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + setOffCmd.setBit(bit); + + // 位数 + setOffCmd.setCount(end - start + 1); + + // 位内容 + setOffCmd.setValue(new byte[] {0x01} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(setOffCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + // 3将命令存入数据库 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = new PgAcuCmd(); + cmd.setCmd_type(setOffCmd.getCommandType()); + cmd.setDest_acu_code(acucode); + cmd.setTm(setOffCmd.getTime().getTime()); + cmdDao.addCmdRecord(cmd); + + // 4阻塞,循环查找响应消息池,找到对应的响应消息 + boolean flag = false; + int times = 0; + CommandResponse response = null; + while (flag == false && times < 240) { + response = ACUClientUtil.getInstance().responsePool.getResponse(cmd.getId()); + + if (null != response && response.equals("") == false) { + flag = true; + } + + times++; + try { + Thread.sleep(500); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志;2)将命令置为超时 + logger.error("在响应池中查找命令的响应消息阻塞线程被异常打断", e); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + + return; + } + } + + // 5若未超时,将值存入数据库 + if (null != response) { + // 6根据命令类型的不同将监测值存入对应的数据库 + response.afterAction(); + } else { + // 9超时,将命令的超时标志位置1 + logger.warn("命令超时" + cmd.getId()); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + } + } + } +} diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjRtTask.java b/src/com/szpg/task/ReadFjRtTask.java index 8127b09..690d2a9 100644 --- a/src/com/szpg/task/ReadFjRtTask.java +++ b/src/com/szpg/task/ReadFjRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJ.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjStatTask.java b/src/com/szpg/task/ReadFjStatTask.java index 9558a0a..bd0f5c5 100644 --- a/src/com/szpg/task/ReadFjStatTask.java +++ b/src/com/szpg/task/ReadFjStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadHSStatusTask.java b/src/com/szpg/task/ReadHSStatusTask.java index 94629cc..15a9e82 100644 --- a/src/com/szpg/task/ReadHSStatusTask.java +++ b/src/com/szpg/task/ReadHSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadHSValueTask.java b/src/com/szpg/task/ReadHSValueTask.java index c7f1149..5881156 100644 --- a/src/com/szpg/task/ReadHSValueTask.java +++ b/src/com/szpg/task/ReadHSValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadJgStatusTask.java b/src/com/szpg/task/ReadJgStatusTask.java index ea69b97..d86f95b 100644 --- a/src/com/szpg/task/ReadJgStatusTask.java +++ b/src/com/szpg/task/ReadJgStatusTask.java @@ -41,7 +41,6 @@ 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); } diff --git a/src/com/szpg/task/ReadO2StatusTask.java b/src/com/szpg/task/ReadO2StatusTask.java index 951cf10..cf68811 100644 --- a/src/com/szpg/task/ReadO2StatusTask.java +++ b/src/com/szpg/task/ReadO2StatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadO2ValueTask.java b/src/com/szpg/task/ReadO2ValueTask.java index 0da31f2..5e2e21f 100644 --- a/src/com/szpg/task/ReadO2ValueTask.java +++ b/src/com/szpg/task/ReadO2ValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadSbRtTask.java b/src/com/szpg/task/ReadSbRtTask.java index 6dd46cc..9083b72 100644 --- a/src/com/szpg/task/ReadSbRtTask.java +++ b/src/com/szpg/task/ReadSbRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadSbStatTask.java b/src/com/szpg/task/ReadSbStatTask.java index 5c7c518..1ca23f4 100644 --- a/src/com/szpg/task/ReadSbStatTask.java +++ b/src/com/szpg/task/ReadSbStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadWSStatusTask.java b/src/com/szpg/task/ReadWSStatusTask.java index 608c978..bfa6809 100644 --- a/src/com/szpg/task/ReadWSStatusTask.java +++ b/src/com/szpg/task/ReadWSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadWSValueTask.java b/src/com/szpg/task/ReadWSValueTask.java index 3a34dec..a354e7a 100644 --- a/src/com/szpg/task/ReadWSValueTask.java +++ b/src/com/szpg/task/ReadWSValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadYWStatusTask.java b/src/com/szpg/task/ReadYWStatusTask.java index 36b2952..6df45a8 100644 --- a/src/com/szpg/task/ReadYWStatusTask.java +++ b/src/com/szpg/task/ReadYWStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YWALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YWALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YW.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadZmRtTask.java b/src/com/szpg/task/ReadZmRtTask.java index 881b7cb..30c5835 100644 --- a/src/com/szpg/task/ReadZmRtTask.java +++ b/src/com/szpg/task/ReadZmRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZM.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadZmStatTask.java b/src/com/szpg/task/ReadZmStatTask.java index 540d3b8..c81c1eb 100644 --- a/src/com/szpg/task/ReadZmStatTask.java +++ b/src/com/szpg/task/ReadZmStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZM.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/TurnOffFjTask.java b/src/com/szpg/task/TurnOffFjTask.java new file mode 100644 index 0000000..6280063 --- /dev/null +++ b/src/com/szpg/task/TurnOffFjTask.java @@ -0,0 +1,178 @@ +package com.szpg.task; + +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.PgAcuDao; +import com.szpg.db.dao.PgDeviceDao; +import com.szpg.db.dao.PgHjsbblDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuDaoImpl; +import com.szpg.db.dao.impl.PgDeviceDaoImpl; +import com.szpg.db.dao.impl.PgHjsbblDaoImpl; +import com.szpg.db.data.PgAcu; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.data.PgHjsbbl; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.CommandResponse; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.protocol.DTProtocolInterface; +import com.szpg.plc.protocol.ProtocolFactory; +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.util.Configure; + +public class TurnOffFjTask implements Runnable { + + private Logger logger = Logger.getLogger(this.getClass().getName()); + + private String deviceCode; + private PgHjsbblDao blDao = new PgHjsbblDaoImpl(); + + public TurnOffFjTask(String deviceCode) { + this.deviceCode = deviceCode; + } + + @Override + public void run() { + // 查找ACU的信息 + PgAcuDao acuDao = new PgAcuDaoImpl(); + PgDeviceDao deviceDao = new PgDeviceDaoImpl(); + String acucode = deviceDao.findAcuCodeByCode(deviceCode); + PgAcu acu = acuDao.findACUByCode(acucode); + ACUClient client = ACUClientUtil.getInstance().getClients().get(acu.getAcu_host() + ":" + acu.getAcu_port()); + if (null != client) { + // 源地址 + String sour = Configure.getProperty("sys", "LOCALHOST.NET") + + Configure.getProperty("sys", "LOCALHOST.NODE") + + Configure.getProperty("sys", "LOCALHOST.UNIT"); + + // 目标地址 + String dest = client.getNet() + client.getNode() + client.getUnit(); + + DTProtocolInterface finspi = ProtocolFactory.getDefaultDTProtocol(); + + // 1首先将启动位置0 + WriteMemoryCommand clearOnCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETFJON); + PgHjsbbl onBlObj = blDao.findBlByBh(deviceCode + ".ON"); + if (null != onBlObj) { + clearOnCmd.setMessageProducerId(sour); + clearOnCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + clearOnCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = onBlObj.getKszdz(); + int end = onBlObj.getJszdz(); + int bit = onBlObj.getSzw(); + + // 开始字地址 + clearOnCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + clearOnCmd.setBit(bit); + + // 位数 + clearOnCmd.setCount(end - start + 1); + + // 位内容 + clearOnCmd.setValue(new byte[] {0x00} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(clearOnCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志 + logger.error("清除启动位后的阻塞等待线程被打断", e); + } + + // 2 发送设置启动位的命令 + WriteMemoryCommand setOffCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETFJOFF); + PgHjsbbl offBlObj = blDao.findBlByBh(deviceCode + ".OFF"); + if (null != offBlObj) { + setOffCmd.setMessageProducerId(sour); + setOffCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + setOffCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = offBlObj.getKszdz(); + int end = offBlObj.getJszdz(); + int bit = offBlObj.getSzw(); + + // 开始字地址 + setOffCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + setOffCmd.setBit(bit); + + // 位数 + setOffCmd.setCount(end - start + 1); + + // 位内容 + setOffCmd.setValue(new byte[] {0x01} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(setOffCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + // 3将命令存入数据库 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = new PgAcuCmd(); + cmd.setCmd_type(setOffCmd.getCommandType()); + cmd.setDest_acu_code(acucode); + cmd.setTm(setOffCmd.getTime().getTime()); + cmdDao.addCmdRecord(cmd); + + // 4阻塞,循环查找响应消息池,找到对应的响应消息 + boolean flag = false; + int times = 0; + CommandResponse response = null; + while (flag == false && times < 240) { + response = ACUClientUtil.getInstance().responsePool.getResponse(cmd.getId()); + + if (null != response && response.equals("") == false) { + flag = true; + } + + times++; + try { + Thread.sleep(500); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志;2)将命令置为超时 + logger.error("在响应池中查找命令的响应消息阻塞线程被异常打断", e); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + + return; + } + } + + // 5若未超时,将值存入数据库 + if (null != response) { + // 6根据命令类型的不同将监测值存入对应的数据库 + response.afterAction(); + } else { + // 9超时,将命令的超时标志位置1 + logger.warn("命令超时" + cmd.getId()); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + } + } + } +} diff --git a/src/com/szpg/task/TurnOffZmTask.java b/src/com/szpg/task/TurnOffZmTask.java new file mode 100644 index 0000000..d3a1ff3 --- /dev/null +++ b/src/com/szpg/task/TurnOffZmTask.java @@ -0,0 +1,178 @@ +package com.szpg.task; + +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.PgAcuDao; +import com.szpg.db.dao.PgDeviceDao; +import com.szpg.db.dao.PgHjsbblDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuDaoImpl; +import com.szpg.db.dao.impl.PgDeviceDaoImpl; +import com.szpg.db.dao.impl.PgHjsbblDaoImpl; +import com.szpg.db.data.PgAcu; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.data.PgHjsbbl; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.CommandResponse; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.protocol.DTProtocolInterface; +import com.szpg.plc.protocol.ProtocolFactory; +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.util.Configure; + +public class TurnOffZmTask implements Runnable { + + private Logger logger = Logger.getLogger(this.getClass().getName()); + + private String deviceCode; + private PgHjsbblDao blDao = new PgHjsbblDaoImpl(); + + public TurnOffZmTask(String deviceCode) { + this.deviceCode = deviceCode; + } + + @Override + public void run() { + // 查找ACU的信息 + PgAcuDao acuDao = new PgAcuDaoImpl(); + PgDeviceDao deviceDao = new PgDeviceDaoImpl(); + String acucode = deviceDao.findAcuCodeByCode(deviceCode); + PgAcu acu = acuDao.findACUByCode(acucode); + ACUClient client = ACUClientUtil.getInstance().getClients().get(acu.getAcu_host() + ":" + acu.getAcu_port()); + if (null != client) { + // 源地址 + String sour = Configure.getProperty("sys", "LOCALHOST.NET") + + Configure.getProperty("sys", "LOCALHOST.NODE") + + Configure.getProperty("sys", "LOCALHOST.UNIT"); + + // 目标地址 + String dest = client.getNet() + client.getNode() + client.getUnit(); + + DTProtocolInterface finspi = ProtocolFactory.getDefaultDTProtocol(); + + // 1首先将启动位置0 + WriteMemoryCommand clearOnCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETZMON); + PgHjsbbl onBlObj = blDao.findBlByBh(deviceCode + ".ON"); + if (null != onBlObj) { + clearOnCmd.setMessageProducerId(sour); + clearOnCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + clearOnCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = onBlObj.getKszdz(); + int end = onBlObj.getJszdz(); + int bit = onBlObj.getSzw(); + + // 开始字地址 + clearOnCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + clearOnCmd.setBit(bit); + + // 位数 + clearOnCmd.setCount(end - start + 1); + + // 位内容 + clearOnCmd.setValue(new byte[] {0x00} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(clearOnCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志 + logger.error("清除启动位后的阻塞等待线程被打断", e); + } + + // 2 发送设置启动位的命令 + WriteMemoryCommand setOffCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETZMOFF); + PgHjsbbl offBlObj = blDao.findBlByBh(deviceCode + ".OFF"); + if (null != offBlObj) { + setOffCmd.setMessageProducerId(sour); + setOffCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + setOffCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = offBlObj.getKszdz(); + int end = offBlObj.getJszdz(); + int bit = offBlObj.getSzw(); + + // 开始字地址 + setOffCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + setOffCmd.setBit(bit); + + // 位数 + setOffCmd.setCount(end - start + 1); + + // 位内容 + setOffCmd.setValue(new byte[] {0x01} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(setOffCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + // 3将命令存入数据库 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = new PgAcuCmd(); + cmd.setCmd_type(setOffCmd.getCommandType()); + cmd.setDest_acu_code(acucode); + cmd.setTm(setOffCmd.getTime().getTime()); + cmdDao.addCmdRecord(cmd); + + // 4阻塞,循环查找响应消息池,找到对应的响应消息 + boolean flag = false; + int times = 0; + CommandResponse response = null; + while (flag == false && times < 240) { + response = ACUClientUtil.getInstance().responsePool.getResponse(cmd.getId()); + + if (null != response && response.equals("") == false) { + flag = true; + } + + times++; + try { + Thread.sleep(500); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志;2)将命令置为超时 + logger.error("在响应池中查找命令的响应消息阻塞线程被异常打断", e); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + + return; + } + } + + // 5若未超时,将值存入数据库 + if (null != response) { + // 6根据命令类型的不同将监测值存入对应的数据库 + response.afterAction(); + } else { + // 9超时,将命令的超时标志位置1 + logger.warn("命令超时" + cmd.getId()); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + } + } + } +} diff --git a/src/com/szpg/task/TurnOnFjTask.java b/src/com/szpg/task/TurnOnFjTask.java new file mode 100644 index 0000000..5be904a --- /dev/null +++ b/src/com/szpg/task/TurnOnFjTask.java @@ -0,0 +1,179 @@ +package com.szpg.task; + +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.PgAcuDao; +import com.szpg.db.dao.PgDeviceDao; +import com.szpg.db.dao.PgHjsbblDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuDaoImpl; +import com.szpg.db.dao.impl.PgDeviceDaoImpl; +import com.szpg.db.dao.impl.PgHjsbblDaoImpl; +import com.szpg.db.data.PgAcu; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.data.PgHjsbbl; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.CommandResponse; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.protocol.DTProtocolInterface; +import com.szpg.plc.protocol.ProtocolFactory; +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.util.Configure; + +public class TurnOnFjTask implements Runnable { + + private Logger logger = Logger.getLogger(this.getClass().getName()); + + private String deviceCode; + private PgHjsbblDao blDao = new PgHjsbblDaoImpl(); + + public TurnOnFjTask(String deviceCode) { + this.deviceCode = deviceCode; + } + + @Override + public void run() { + // 查找ACU的信息 + PgAcuDao acuDao = new PgAcuDaoImpl(); + PgDeviceDao deviceDao = new PgDeviceDaoImpl(); + String acucode = deviceDao.findAcuCodeByCode(deviceCode); + PgAcu acu = acuDao.findACUByCode(acucode); + ACUClient client = ACUClientUtil.getInstance().getClients().get(acu.getAcu_host() + ":" + acu.getAcu_port()); + if (null != client) { + // 源地址 + String sour = Configure.getProperty("sys", "LOCALHOST.NET") + + Configure.getProperty("sys", "LOCALHOST.NODE") + + Configure.getProperty("sys", "LOCALHOST.UNIT"); + + // 目标地址 + String dest = client.getNet() + client.getNode() + client.getUnit(); + + DTProtocolInterface finspi = ProtocolFactory.getDefaultDTProtocol(); + + // 1首先将停止位置0 + WriteMemoryCommand clearOffCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETFJOFF); + PgHjsbbl offBlObj = blDao.findBlByBh(deviceCode + ".OFF"); + if (null != offBlObj) { + clearOffCmd.setMessageProducerId(sour); + clearOffCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + clearOffCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = offBlObj.getKszdz(); + int end = offBlObj.getJszdz(); + int bit = offBlObj.getSzw(); + + // 开始字地址 + clearOffCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + clearOffCmd.setBit(bit); + + // 位数 + clearOffCmd.setCount(end - start + 1); + + // 位内容 + clearOffCmd.setValue(new byte[] {0x00} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(clearOffCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志 + logger.error("清除停止位后的阻塞等待线程被打断", e); + } + + // 2 发送设置启动位的命令 + WriteMemoryCommand setOnCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETFJON); + PgHjsbbl onBlObj = blDao.findBlByBh(deviceCode + ".ON"); + if (null != onBlObj) { + setOnCmd.setMessageProducerId(sour); + setOnCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + setOnCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = onBlObj.getKszdz(); + int end = onBlObj.getJszdz(); + int bit = onBlObj.getSzw(); + + // 开始字地址 + setOnCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + setOnCmd.setBit(bit); + + // 位数 + setOnCmd.setCount(end - start + 1); + + // 位内容 + setOnCmd.setValue(new byte[] {0x01} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(setOnCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + // 3将命令存入数据库 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = new PgAcuCmd(); + cmd.setCmd_type(setOnCmd.getCommandType()); + cmd.setDest_acu_code(acucode); + cmd.setTm(setOnCmd.getTime().getTime()); + cmdDao.addCmdRecord(cmd); + + // 4阻塞,循环查找响应消息池,找到对应的响应消息 + boolean flag = false; + int times = 0; + CommandResponse response = null; + while (flag == false && times < 240) { + response = ACUClientUtil.getInstance().responsePool.getResponse(cmd.getId()); + + if (null != response && response.equals("") == false) { + flag = true; + } + + times++; + try { + Thread.sleep(500); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志;2)将命令置为超时 + logger.error("在响应池中查找命令的响应消息阻塞线程被异常打断", e); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + + return; + } + } + + // 5若未超时,将值存入数据库 + if (null != response) { + // 6根据命令类型的不同将监测值存入对应的数据库 + response.afterAction(); + } else { + // 9超时,将命令的超时标志位置1 + logger.warn("命令超时" + cmd.getId()); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + } + } + } + +} diff --git a/src/com/szpg/DSCTest.java b/src/com/szpg/DSCTest.java index bbcc362..edef399 100644 --- a/src/com/szpg/DSCTest.java +++ b/src/com/szpg/DSCTest.java @@ -28,6 +28,10 @@ import com.szpg.task.ReadYWStatusTask; import com.szpg.task.ReadZmRtTask; import com.szpg.task.ReadZmStatTask; +import com.szpg.task.TurnOffFjTask; +import com.szpg.task.TurnOffZmTask; +import com.szpg.task.TurnOnFjTask; +import com.szpg.task.TurnOnZmTask; public class DSCTest { @@ -76,7 +80,7 @@ // dsc.testSendDSStatusCommand(); // 井盖 - dsc.testSendJgStatusCommand(); +// dsc.testSendJgStatusCommand(); // 风机 // dsc.testSendFjStatCommand(); @@ -90,6 +94,13 @@ // dsc.testSendZmRtCommand(); // dsc.testSendZmStatCommand(); + // 照明开关 +// dsc.testSendTurnOnZmCommand("YXL.ACU001.RZM01"); +// dsc.testSendTurnOffZmCommand("YXL.ACU001.RZM01"); + + // 风机开关 + dsc.testSendTurnOnFjCommand("YXL.ACU001.RFJ01"); + dsc.testSendTurnOffFjCommand("YXL.ACU001.RFJ01"); } /** @@ -268,4 +279,43 @@ ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); sche.scheduleWithFixedDelay(new ReadZmRtTask(), 40, 300, TimeUnit.SECONDS); } + + /** + * 发送打开照明 + */ + private void testSendTurnOnZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnZmTask(deviceCode), 15, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭照明 + */ + private void testSendTurnOffZmCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffZmTask(deviceCode), 20, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送打开风机 + */ + private void testSendTurnOnFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOnFjTask(deviceCode), 25, TimeUnit.SECONDS); + sche.shutdown(); + } + + + /** + * 发送关闭风机 + */ + private void testSendTurnOffFjCommand(String deviceCode) { + ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1); + sche.schedule(new TurnOffFjTask(deviceCode), 30, TimeUnit.SECONDS); + sche.shutdown(); + } } diff --git a/src/com/szpg/db/dao/PgAcuCmdDao.java b/src/com/szpg/db/dao/PgAcuCmdDao.java new file mode 100644 index 0000000..73070a5 --- /dev/null +++ b/src/com/szpg/db/dao/PgAcuCmdDao.java @@ -0,0 +1,19 @@ +package com.szpg.db.dao; + +import java.util.List; + +import com.szpg.db.data.PgAcuCmd; + +public interface PgAcuCmdDao { + + public List findAll(); + public PgAcuCmd findById(String id); + + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type); + + public int addCmdRecord(PgAcuCmd cmd); + public int updateCmdRecordTimeout(String cmdId); + public int deleteCmdRecord(String cmdId); + + public int deleteTimeoutCmd(); +} diff --git a/src/com/szpg/db/dao/PgAcuDao.java b/src/com/szpg/db/dao/PgAcuDao.java index 5dc85d2..f9142b8 100644 --- a/src/com/szpg/db/dao/PgAcuDao.java +++ b/src/com/szpg/db/dao/PgAcuDao.java @@ -10,4 +10,5 @@ public PgAcu findACUById(Integer id); public PgAcu findACUByDest(String dest); + public PgAcu findACUByCode(String acucode); } diff --git a/src/com/szpg/db/dao/PgAcuRdcmdDao.java b/src/com/szpg/db/dao/PgAcuRdcmdDao.java deleted file mode 100644 index 42f38f6..0000000 --- a/src/com/szpg/db/dao/PgAcuRdcmdDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.szpg.db.dao; - -import java.util.List; - -import com.szpg.db.data.PgAcuRdcmd; - -public interface PgAcuRdcmdDao { - - public List findAll(); - public PgAcuRdcmd findById(String id); - - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type); - - public int addCmdRecord(PgAcuRdcmd cmd); - public int updateCmdRecordTimeout(String cmdId); - public int deleteCmdRecord(String cmdId); - - public int deleteTimeoutCmd(); -} diff --git a/src/com/szpg/db/dao/PgDeviceDao.java b/src/com/szpg/db/dao/PgDeviceDao.java index 8eb4bd8..c3759bf 100644 --- a/src/com/szpg/db/dao/PgDeviceDao.java +++ b/src/com/szpg/db/dao/PgDeviceDao.java @@ -1,9 +1,7 @@ package com.szpg.db.dao; -import java.util.List; - public interface PgDeviceDao { - public List findDeviceIdByDestAndType(String dest, String type); + public String findAcuCodeByCode(String code); public Integer findDeviceIdByCode(String code); } diff --git a/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java new file mode 100644 index 0000000..16bc85f --- /dev/null +++ b/src/com/szpg/db/dao/impl/PgAcuCmdDaoImpl.java @@ -0,0 +1,253 @@ +package com.szpg.db.dao.impl; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import org.apache.commons.dbutils.DbUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.util.ConnectionManager; +import com.szpg.util.TimeFormat; + +public class PgAcuCmdDaoImpl implements PgAcuCmdDao { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + @Override + public List findAll() { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD"; + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class)); + + if (null != list && list.isEmpty() == false) { + logger.debug("查询所有内存命令成功[" + list.size() + "]"); + + 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 PgAcuCmd findById(String id) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = id; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据ID查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据ID查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public PgAcuCmd findLatestCmdByDestAndType(String dest, String type) { + Connection conn = null; + + String queryStr = "SELECT C.* FROM PG_ACU_CMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; + Object[] params = new Object[2]; + params[0] = dest; + params[1] = type; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuCmd.class), params); + + if (null != list && list.isEmpty() == false) { + logger.debug("根据目的地址和命令类型查询内存命令成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据目的地址和命令类型查询内存命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + + @Override + public int addCmdRecord(PgAcuCmd cmd) { + Connection conn = null; + + String insertStr = "INSERT INTO PG_ACU_CMD " + + "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + + "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; + Object[] params = new Object[4]; + params[0] = cmd.getId(); + params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); + params[2] = cmd.getCmd_type(); + params[3] = cmd.getDest_acu_code(); + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, insertStr, params); + + if (count > 0) + logger.debug("插入命令数据成功" + cmd); + else + logger.error("插入命令数据失败!"); + + return count; + } catch (Exception ex) { + logger.error("插入命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int updateCmdRecordTimeout(String cmdId) { + Connection conn = null; + + String updateStr = "UPDATE PG_ACU_CMD SET TIMEOUT = '1' WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) { + logger.debug("更新命令已超时字段成功" + cmdId); + } else { + logger.error("更新命令已超时字段成功" + cmdId); + } + + return count; + } catch (Exception ex) { + logger.error("更新命令已超时字段异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteCmdRecord(String cmdId) { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE ID = ?"; + Object[] param = new Object[1]; + param[0] = cmdId; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr, param); + + if (count > 0) + logger.debug("删除命令成功" + cmdId); + else + logger.error("删除命令失败!"); + + return count; + } catch (Exception ex) { + logger.error("删除命令异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return 0; + } + + @Override + public int deleteTimeoutCmd() { + Connection conn = null; + + String updateStr = "DELETE FROM PG_ACU_CMD WHERE TIMEOUT = '1'"; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + int count = runner.update(conn, updateStr); + + if (count > 0) { + logger.debug("删除超时的命令成功"); + } + + 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/dao/impl/PgAcuDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java index 751c2ce..30d7822 100644 --- a/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgAcuDaoImpl.java @@ -116,4 +116,37 @@ return null; } + @Override + public PgAcu findACUByCode(String acucode) { + Connection conn = null; + + String queryStr = "SELECT * FROM PG_ACU WHERE ACU_CODE = ?"; + Object[] param = new Object[1]; + param[0] = acucode; + + try { + conn = ConnectionManager.getConnectionFromC3P0(); + conn.setAutoCommit(false); + + QueryRunner runner = new QueryRunner(); + List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcu.class), param); + + if (null != list && list.size() == 1) { + logger.debug("根据代码查询ACU成功[" + list.size() + "]"); + + return list.get(0); + } else + return null; + } catch (Exception ex) { + logger.error("根据代码查询ACU异常", ex); + } finally { + try { + DbUtils.commitAndClose(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java b/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java deleted file mode 100644 index 6317a39..0000000 --- a/src/com/szpg/db/dao/impl/PgAcuRdcmdDaoImpl.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.szpg.db.dao.impl; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import org.apache.commons.dbutils.DbUtils; -import org.apache.commons.dbutils.QueryRunner; -import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.log4j.Logger; - -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.data.PgAcuRdcmd; -import com.szpg.db.util.ConnectionManager; -import com.szpg.util.TimeFormat; - -public class PgAcuRdcmdDaoImpl implements PgAcuRdcmdDao { - - private final Logger logger = Logger.getLogger(this.getClass().getName()); - - @Override - public List findAll() { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD"; - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class)); - - if (null != list && list.isEmpty() == false) { - logger.debug("查询所有内存读取命令成功[" + list.size() + "]"); - - 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 PgAcuRdcmd findById(String id) { - Connection conn = null; - - String queryStr = "SELECT * FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = id; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), param); - - if (null != list && list.size() == 1) { - logger.debug("根据ID查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据ID查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public PgAcuRdcmd findLatestCmdByDestAndType(String dest, String type) { - Connection conn = null; - - String queryStr = "SELECT C.* FROM PG_ACU_RDCMD C, PG_ACU A WHERE TIMEOUT='0' AND A.ACU_CODE=C.DEST_ACU_CODE AND A.ACU_DEST=? AND CMD_TYPE=? ORDER BY TM DESC"; - Object[] params = new Object[2]; - params[0] = dest; - params[1] = type; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - List list = (List) runner.query(conn, queryStr, new BeanListHandler(PgAcuRdcmd.class), params); - - if (null != list && list.isEmpty() == false) { - logger.debug("根据目的地址和命令类型查询内存读取命令成功[" + list.size() + "]"); - - return list.get(0); - } else - return null; - } catch (Exception ex) { - logger.error("根据目的地址和命令类型查询内存读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - } - - @Override - public int addCmdRecord(PgAcuRdcmd cmd) { - Connection conn = null; - - String insertStr = "INSERT INTO PG_ACU_RDCMD " + - "(ID, TM, CMD_TYPE, DEST_ACU_CODE, TIMEOUT) " + - "VALUES (?, TO_DATE(?, 'YYYYMMDDHH24MISS'), ?, ?, '0')"; - Object[] params = new Object[4]; - params[0] = cmd.getId(); - params[1] = TimeFormat.format(cmd.getTm(), "yyyyMMddHHmmss"); - params[2] = cmd.getCmd_type(); - params[3] = cmd.getDest_acu_code(); - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, insertStr, params); - - if (count > 0) - logger.debug("插入读取命令数据成功" + cmd); - else - logger.error("插入读取命令数据失败!"); - - return count; - } catch (Exception ex) { - logger.error("插入读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int updateCmdRecordTimeout(String cmdId) { - Connection conn = null; - - String updateStr = "UPDATE PG_ACU_RDCMD SET TIMEOUT = '1' WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) { - logger.debug("更新读取命令已超时字段成功" + cmdId); - } else { - logger.error("更新读取命令已超时字段成功" + cmdId); - } - - return count; - } catch (Exception ex) { - logger.error("更新读取命令已超时字段异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteCmdRecord(String cmdId) { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE ID = ?"; - Object[] param = new Object[1]; - param[0] = cmdId; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr, param); - - if (count > 0) - logger.debug("删除读取命令成功" + cmdId); - else - logger.error("删除读取命令失败!"); - - return count; - } catch (Exception ex) { - logger.error("删除读取命令异常", ex); - } finally { - try { - DbUtils.commitAndClose(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return 0; - } - - @Override - public int deleteTimeoutCmd() { - Connection conn = null; - - String updateStr = "DELETE FROM PG_ACU_RDCMD WHERE TIMEOUT = '1'"; - - try { - conn = ConnectionManager.getConnectionFromC3P0(); - conn.setAutoCommit(false); - - QueryRunner runner = new QueryRunner(); - int count = runner.update(conn, updateStr); - - if (count > 0) { - logger.debug("删除超时的命令成功"); - } - - 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/dao/impl/PgDeviceDaoImpl.java b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java index b63352a..c55bed9 100644 --- a/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java +++ b/src/com/szpg/db/dao/impl/PgDeviceDaoImpl.java @@ -2,7 +2,6 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; @@ -18,62 +17,29 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override - public List findDeviceIdByDestAndType(String dest, String type) { + public String findAcuCodeByCode(String code) { Connection conn = null; - String key = ""; - - switch (type.toUpperCase()) { - case "CH": - key = "%甲烷%"; - break; - case "WS": - key = "%温湿度%"; - break; - case "CO": - key = "%一氧化碳%"; - break; - case "O2": - key = "%氧气%"; - break; - case "HS": - key = "%硫化氢%"; - break; - case "YW": - key = "%液位%"; - break; - case "DS": - key = "%对射%"; - break; - case "FJ": - key = "%风机%"; - break; - } - - String queryStr = "SELECT D.ID FROM PG_DEVICE D, PG_ACU A WHERE D.PARTITION = A.ACU_CODE AND A.ACU_DEST = ? AND D.DEVCODE LIKE '" + key + "' ORDER BY D.ID"; + String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; - param[0] = dest; + param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); - List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); + List tempList = (List) runner.query(conn, queryStr, new ColumnListHandler(), param); - if (null != tempList && tempList.isEmpty() == false) { - logger.debug("根据ACU的目的地址和类型查询设备ID成功[" + tempList.size() + "]"); + if (null != tempList && tempList.size() == 1) { + String partition = tempList.get(0); - List list = new ArrayList(); - for (int i = 0; i < tempList.size(); i++) { - list.add(((Number) tempList.get(i)).intValue()); - } - - return list; + logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); + return partition; } else return null; } catch (Exception ex) { - logger.error("根据ACU的目的地址和类型查询设备ID异常", ex); + logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); diff --git a/src/com/szpg/db/data/PgAcuCmd.java b/src/com/szpg/db/data/PgAcuCmd.java new file mode 100644 index 0000000..9e57628 --- /dev/null +++ b/src/com/szpg/db/data/PgAcuCmd.java @@ -0,0 +1,85 @@ +package com.szpg.db.data; + +import java.util.Calendar; +import java.util.Date; +import java.util.UUID; + +import com.szpg.util.TimeFormat; + +/** + * 读取内存命令对应数据表 + * + * @author admin + * + */ +public class PgAcuCmd implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -4164184831926735991L; + + private String id; // ID + private Date tm; // 命令发送时间 + private String cmd_type; // 命令类型 + private String dest_acu_code; // 目标ACU代码 + private boolean timeout; // 是否超时 + + public PgAcuCmd() { + this.setId(UUID.randomUUID().toString().replace("-", "")); + this.setTm(Calendar.getInstance().getTime()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getTm() { + return tm; + } + + public void setTm(Date tm) { + this.tm = tm; + } + + public String getCmd_type() { + return cmd_type; + } + + public void setCmd_type(String cmd_type) { + this.cmd_type = cmd_type; + } + + public String getDest_acu_code() { + return dest_acu_code; + } + + public void setDest_acu_code(String dest_acu_code) { + this.dest_acu_code = dest_acu_code; + } + + public boolean isTimeout() { + return timeout; + } + + public void setTimeout(boolean timeout) { + this.timeout = timeout; + } + + @Override + public String toString() { + return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; + } + + public String getTmStr() { + if (null != this.getTm()) + return TimeFormat.formatTimestamp(getTm()); + else + return ""; + } + +} diff --git a/src/com/szpg/db/data/PgAcuRdcmd.java b/src/com/szpg/db/data/PgAcuRdcmd.java deleted file mode 100644 index 65158b0..0000000 --- a/src/com/szpg/db/data/PgAcuRdcmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.szpg.db.data; - -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; - -import com.szpg.util.TimeFormat; - -/** - * 读取内存命令对应数据表 - * - * @author admin - * - */ -public class PgAcuRdcmd implements java.io.Serializable { - - /** - * - */ - private static final long serialVersionUID = -4164184831926735991L; - - private String id; // ID - private Date tm; // 命令发送时间 - private String cmd_type; // 命令类型 - private String dest_acu_code; // 目标ACU代码 - private boolean timeout; // 是否超时 - - public PgAcuRdcmd() { - this.setId(UUID.randomUUID().toString().replace("-", "")); - this.setTm(Calendar.getInstance().getTime()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getTm() { - return tm; - } - - public void setTm(Date tm) { - this.tm = tm; - } - - public String getCmd_type() { - return cmd_type; - } - - public void setCmd_type(String cmd_type) { - this.cmd_type = cmd_type; - } - - public String getDest_acu_code() { - return dest_acu_code; - } - - public void setDest_acu_code(String dest_acu_code) { - this.dest_acu_code = dest_acu_code; - } - - public boolean isTimeout() { - return timeout; - } - - public void setTimeout(boolean timeout) { - this.timeout = timeout; - } - - @Override - public String toString() { - return "PgAcuRdcmd [tm=" + getTmStr() + ", cmd_type=" + cmd_type + ", acu=" + dest_acu_code + "]"; - } - - public String getTmStr() { - if (null != this.getTm()) - return TimeFormat.formatTimestamp(getTm()); - else - return ""; - } - -} diff --git a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java index e6ded41..810a976 100644 --- a/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java +++ b/src/com/szpg/db/test/PgAcuRdcmdDaoTest.java @@ -5,23 +5,23 @@ import org.junit.Before; import org.junit.Test; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessageConstants; public class PgAcuRdcmdDaoTest { - private PgAcuRdcmdDao dao; + private PgAcuCmdDao dao; @Before public void init() { - dao = new PgAcuRdcmdDaoImpl(); + dao = new PgAcuCmdDaoImpl(); } // @Test public void testAdd() { - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(AppMessageConstants.CMD_TYPE_READCH4VALUE); cmd.setDest_acu_code("YXL.ACU001"); diff --git a/src/com/szpg/plc/message/AppMessageConstants.java b/src/com/szpg/plc/message/AppMessageConstants.java index 3107ff1..5bbda5f 100644 --- a/src/com/szpg/plc/message/AppMessageConstants.java +++ b/src/com/szpg/plc/message/AppMessageConstants.java @@ -23,4 +23,10 @@ public final static String CMD_TYPE_READZMSTAT = "27"; //读取照明运行状态命令 public final static String CMD_TYPE_READZMRUNTIME = "28"; //读取照明运行时长命令 public final static String CMD_TYPE_READJGSTATUS = "29"; //读取井盖状态命令 + + + public final static String CMD_TYPE_SETZMON = "50"; // 设置照明启动位命令 + public final static String CMD_TYPE_SETZMOFF = "51"; // 设置照明停止位命令 + public final static String CMD_TYPE_SETFJON = "52"; // 设置风机启动位命令 + public final static String CMD_TYPE_SETFJOFF = "53"; // 设置风机停止位命令 } diff --git a/src/com/szpg/plc/message/command/ReadMemoryCommand.java b/src/com/szpg/plc/message/command/ReadMemoryCommand.java index ca1f453..6ffb780 100644 --- a/src/com/szpg/plc/message/command/ReadMemoryCommand.java +++ b/src/com/szpg/plc/message/command/ReadMemoryCommand.java @@ -32,7 +32,6 @@ private byte memoryArea; //读取的内存区域代码 private String startAddress; //起始地址 private int countWord; //读取的字数量/1WORD=2BYTE - private int countSensor; //传感器数量 public byte getMemoryArea() { return memoryArea; @@ -58,14 +57,6 @@ this.countWord = countWord; } - public int getCountSensor() { - return countSensor; - } - - public void setCountSensor(int countSensor) { - this.countSensor = countSensor; - } - public static ReadMemoryCommand getInstance(String type) { switch (type) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: diff --git a/src/com/szpg/plc/message/command/WriteMemoryCommand.java b/src/com/szpg/plc/message/command/WriteMemoryCommand.java new file mode 100644 index 0000000..5df5f9c --- /dev/null +++ b/src/com/szpg/plc/message/command/WriteMemoryCommand.java @@ -0,0 +1,81 @@ +package com.szpg.plc.message.command; + +import com.szpg.plc.message.AppCommand; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.write.SetFjOffBitCommand; +import com.szpg.plc.message.command.write.SetFjOnBitCommand; +import com.szpg.plc.message.command.write.SetZmOffBitCommand; +import com.szpg.plc.message.command.write.SetZmOnBitCommand; + +public abstract class WriteMemoryCommand extends AppCommand { + + /** + * + */ + private static final long serialVersionUID = -1083680357338083535L; + + private byte memoryArea; //写的内存区域代码 + private String startAddress; //起始地址 + private int count; //写的字数量/1WORD=2BYTE + private int bit; // 要写的位 + private byte[] value; //要写入的内容 + + public byte getMemoryArea() { + return memoryArea; + } + + public void setMemoryArea(byte memoryArea) { + this.memoryArea = memoryArea; + } + + public String getStartAddress() { + return startAddress; + } + + public void setStartAddress(String startAddress) { + this.startAddress = startAddress; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getBit() { + return bit; + } + + public void setBit(int bit) { + this.bit = bit; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + + public static WriteMemoryCommand getInstance(String type) { + switch (type) { + case AppMessageConstants.CMD_TYPE_SETZMON: + return new SetZmOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETZMOFF: + return new SetZmOffBitCommand(); + + case AppMessageConstants.CMD_TYPE_SETFJON: + return new SetFjOnBitCommand(); + case AppMessageConstants.CMD_TYPE_SETFJOFF: + return new SetFjOffBitCommand(); + + default: + return null; + } + } + + public abstract String getCommandType(); +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java new file mode 100644 index 0000000..2e116ab --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -1263699293153077982L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java new file mode 100644 index 0000000..b2a4532 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetFjOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetFjOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4282052929122659440L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETFJON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置风机启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java new file mode 100644 index 0000000..b96f742 --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOffBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOffBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = -4397813530861878200L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMOFF; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明停止位命令"; + } +} diff --git a/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java new file mode 100644 index 0000000..5a60c8f --- /dev/null +++ b/src/com/szpg/plc/message/command/write/SetZmOnBitCommand.java @@ -0,0 +1,28 @@ +package com.szpg.plc.message.command.write; + +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; + +public class SetZmOnBitCommand extends WriteMemoryCommand { + + /** + * + */ + private static final long serialVersionUID = 7634275732105602031L; + + @Override + public String getCommandType() { + return AppMessageConstants.CMD_TYPE_SETZMON; + } + + @Override + public Class getResponseClass() { + return WriteMemoryCommandResponse.class; + } + + @Override + public String toString() { + return "向终端[" + getDestinationId() + "]发送设置照明启动位命令"; + } +} diff --git a/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java new file mode 100644 index 0000000..dafd82d --- /dev/null +++ b/src/com/szpg/plc/message/response/WriteMemoryCommandResponse.java @@ -0,0 +1,41 @@ +package com.szpg.plc.message.response; + +import com.szpg.plc.message.CommandResponse; + +public class WriteMemoryCommandResponse extends CommandResponse { + + /** + * + */ + private static final long serialVersionUID = 2712983116469366743L; + + private boolean success; + private String commandType; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getCommandType() { + return commandType; + } + + public void setCommandType(String commandType) { + this.commandType = commandType; + } + + @Override + public void afterAction() { + + } + + @Override + public void parseData(byte[] messageData) { + + } + +} diff --git a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java index bdba274..9d0837a 100644 --- a/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java +++ b/src/com/szpg/plc/protocol/fins/FINSDTProtocolImp.java @@ -4,15 +4,17 @@ import java.util.Calendar; import java.util.List; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.AppMessage; import com.szpg.plc.message.AppMessageConstants; import com.szpg.plc.message.UnKnownMessage; import com.szpg.plc.message.command.LinkCommand; import com.szpg.plc.message.command.ReadMemoryCommand; +import com.szpg.plc.message.command.WriteMemoryCommand; import com.szpg.plc.message.response.LinkCommandResponse; +import com.szpg.plc.message.response.WriteMemoryCommandResponse; import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse; import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse; import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse; @@ -123,103 +125,126 @@ if (commandStr.equalsIgnoreCase("00000002")) { String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage); String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型,与APPMessageConstants中的命令类型值保持一致 + + // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 + String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 + + // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType); + if (commandCode.equalsIgnoreCase("0101")) { // 读内存命令响应的解析 - - // 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数 - String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址 - - // 2查询数据中最近的有效的读内存命令,获取其读取的参数类型 - PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl(); - PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType); - if (null != readCmd) { + if (null != cmd) { // 3根据参数类型调用相应的方法进行解析 switch(commandType) { case AppMessageConstants.CMD_TYPE_READCH4VALUE: - received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCH4STATUS: - received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSVALUE: - received = bytesToReadWSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadWSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READWSSTATUS: - received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadWSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOVALUE: - received = bytesToReadCOValueCommandResponse(finsFrame, readCmd); + received = bytesToReadCOValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READCOSTATUS: - received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadCOStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2VALUE: - received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd); + received = bytesToReadO2ValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READO2STATUS: - received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd); + received = bytesToReadO2StatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSVALUE: - received = bytesToReadHSValueCommandResponse(finsFrame, readCmd); + received = bytesToReadHSValueCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READHSSTATUS: - received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadHSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READYWSTATUS: - received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadYWStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READDSSTATUS: - received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadDSStatusCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJSTAT: - received = bytesToReadFjStatCommandResponse(finsFrame, readCmd); + received = bytesToReadFjStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READFJRUNTIME: - received = bytesToReadFjRtCommandResponse(finsFrame, readCmd); + received = bytesToReadFjRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBSTAT: - received = bytesToReadSbStatCommandResponse(finsFrame, readCmd); + received = bytesToReadSbStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READSBRUNTIME: - received = bytesToReadSbRtCommandResponse(finsFrame, readCmd); + received = bytesToReadSbRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMSTAT: - received = bytesToReadZmStatCommandResponse(finsFrame, readCmd); + received = bytesToReadZmStatCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READZMRUNTIME: - received = bytesToReadZmRtCommandResponse(finsFrame, readCmd); + received = bytesToReadZmRtCommandResponse(finsFrame, cmd); break; case AppMessageConstants.CMD_TYPE_READJGSTATUS: - received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd); + received = bytesToReadJgStatusCommandResponse(finsFrame, cmd); break; } // 4将已响应的命令删除 - readCmdDao.deleteCmdRecord(readCmd.getId()); + cmdDao.deleteCmdRecord(cmd.getId()); } } else if (commandCode.equalsIgnoreCase("0102")) { + WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse(); + + // 设置ACU代码 + wmcr.setAcucode(cmd.getDest_acu_code()); + // 写内存命令响应 + byte[] body = finsFrame.TEXT_DATA_BODY; + wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame)); + + if (body.length == 4) { + if (body[2] == 0x00 && body[3] == 0x00) { + // 写入成功 + wmcr.setSuccess(true); + } else { + wmcr.setSuccess(false); + } + wmcr.setValid(true); + } else { + wmcr.setValid(false); + } + + wmcr.setCmdId(cmd.getId()); + wmcr.setCommandType(commandType); + + // 4将已响应的命令删除 + cmdDao.deleteCmdRecord(cmd.getId()); + + received = wmcr; } + } else { + received = new UnKnownMessage(byteMessage); + received.setTime(Calendar.getInstance()); } - -// -// -// -// default: -// received = new UnKnownMessage(byteMessage); -// received.setTime(Calendar.getInstance()); -// } return received; } @@ -247,7 +272,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse(); // 设置ACU代码 @@ -276,7 +301,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse(); // 设置ACU代码 @@ -304,7 +329,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse(); // 设置ACU代码 @@ -325,7 +350,7 @@ } - private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse(); // 设置ACU代码 @@ -352,7 +377,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse(); // 设置ACU代码 @@ -379,7 +404,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse(); // 设置ACU代码 @@ -406,7 +431,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse(); // 设置ACU代码 @@ -433,7 +458,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse(); // 设置ACU代码 @@ -460,7 +485,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse(); // 设置ACU代码 @@ -487,7 +512,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse(); // 设置ACU代码 @@ -513,7 +538,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse(); // 设置ACU代码 @@ -539,7 +564,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse(); // 设置ACU代码 @@ -566,7 +591,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse(); // 设置ACU代码 @@ -593,7 +618,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse(); // 设置ACU代码 @@ -620,7 +645,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse(); // 设置ACU代码 @@ -647,7 +672,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse(); // 设置ACU代码 @@ -674,7 +699,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse(); // 设置ACU代码 @@ -701,7 +726,7 @@ * @param finsFrame * @return */ - private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse(); // 设置ACU代码 @@ -728,7 +753,7 @@ * @param byteMessage * @return */ - private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) { + private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuCmd cmd) { ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse(); // 设置ACU代码 @@ -767,11 +792,11 @@ if (message instanceof ReadMemoryCommand) { frame = readMemoryCommandToBytes((ReadMemoryCommand) message); } -// -// // 写内存命令 -// if (message instanceof QueryRealTimeValueCommand) { -// frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message); -// } + + // 写内存命令 + if (message instanceof WriteMemoryCommand) { + frame = writeMemoryCommandToBytes((WriteMemoryCommand) message); + } return frame; } @@ -810,73 +835,40 @@ } /** - * 将读取甲烷监测值命令转换为字节数组 + * 将写PLC内存命令转换为字节数组 * @param message * @return */ -// private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取甲烷报警状态命令转换为字节数组 - * @param message - * @return - */ -// private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度监测值命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - - /** - * 将读取温湿度报警状态命令转换为字节数组 - * - * @param message - * @return - */ -// private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) { -// byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); -// -// FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), -// message.getMessageProducerId(), -// message.getMemoryArea(), -// start, -// message.getCountWord()); -// -// return finsFrame.toBytes(); -// } - + private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) { + if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) { + // 按位操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getBit(), + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) { + // 按字操作 + byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3); + + FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), + message.getMessageProducerId(), + message.getCommandType(), + message.getMemoryArea(), + start, + message.getCount(), + message.getValue()); + + return finsFrame.toBytes(); + } else { + return null; + } + } } diff --git a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java index c06c8d6..6225746 100644 --- a/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java +++ b/src/com/szpg/plc/protocol/fins/frame/FINSByteFrame.java @@ -138,7 +138,7 @@ * @param sid 服务号 * @param memArea 内存区域代码 * @param start 读取的起始地址 - * @param count 读取的长度(以word计,长度为2个字节) + * @param count 读取的字长度(以word计,长度为2个字节) */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 @@ -165,18 +165,24 @@ /** * 构造方法 - * 适用于写内存命令 + * 适用于按字写D区内存命令 * - * @param dest - * @param sour - * @param sid - * @param memArea - * @param start - * @param count - * @param dataBody + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 起始地址 + * @param count 写入的字长度 + * @param dataBody 写入的内容 */ public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int count, byte[] dataBody) { this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; // 构造用户数据域的内容 Bytes data = new Bytes(); @@ -193,6 +199,44 @@ this.LENGTH = ByteUtil.intToBins(length(), 4); } + /** + * 构造方法 + * 适用于按位写W区的内存 + * + * @param dest 目标地址 + * @param sour 源地址 + * @param sid 服务号 + * @param memArea 内存区域代码 + * @param start 字地址 + * @param bit 位地址 + * @param bitCount 写入的位数 + * @param bitValue 写入的位内容 + */ + public FINSByteFrame(String dest, String sour, String sid, byte memArea, byte[] start, int bit, int bitCount, byte[] bitValue) { + this.TEXT_CONTROL_BODY = FINSByteFrameTool.buildFrameControl(dest, sour, sid); // 构造控制域 + + // 设置读取内存命令的发送命令功能码00000002 + this.COMMAND = FINSConstants.COMMAND_COMMAND; + + // 设置错误码为00000000 + this.ERROR_CODE = FINSConstants.ERROR_NORMAL; + + // 构造用户数据域的内容 + Bytes data = new Bytes(); + data.append(FINSConstants.COMMAND_MEMORY_WRITE); //写入命令代码 + data.append(memArea); // 写入内存区域代码 + data.append(start); // 写入的字地址 + data.append(ByteUtil.intToBins(bit, 1)); // 写入的位地址 + data.append(ByteUtil.intToBins(bitCount, 2)); // 写入的位数 + data.append(bitValue); + + // 为用户数据域赋值 + this.TEXT_DATA_BODY = data.toBytes(); + + // 计算帧长度并赋值 + this.LENGTH = ByteUtil.intToBins(length(), 4); + } + /** * 判断帧起始字符 diff --git a/src/com/szpg/plc/server/ACUCommandResponsePool.java b/src/com/szpg/plc/server/ACUCommandResponsePool.java index 478aedd..37e0646 100644 --- a/src/com/szpg/plc/server/ACUCommandResponsePool.java +++ b/src/com/szpg/plc/server/ACUCommandResponsePool.java @@ -7,8 +7,8 @@ import org.apache.log4j.Logger; -import com.szpg.db.dao.PgAcuRdcmdDao; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.plc.message.CommandResponse; import com.szpg.util.Configure; @@ -47,7 +47,7 @@ class RefreshPoolTask implements Runnable { - PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); @Override public synchronized void run() { diff --git a/src/com/szpg/service/ReadControllerRuntimeService.java b/src/com/szpg/service/ReadControllerRuntimeService.java index d216167..07ee8c1 100644 --- a/src/com/szpg/service/ReadControllerRuntimeService.java +++ b/src/com/szpg/service/ReadControllerRuntimeService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadControllerStatusService.java b/src/com/szpg/service/ReadControllerStatusService.java index 3223bfd..2f022a4 100644 --- a/src/com/szpg/service/ReadControllerStatusService.java +++ b/src/com/szpg/service/ReadControllerStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadDsAlmService.java b/src/com/szpg/service/ReadDsAlmService.java index d6b4252..af45121 100644 --- a/src/com/szpg/service/ReadDsAlmService.java +++ b/src/com/szpg/service/ReadDsAlmService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorStatusService.java b/src/com/szpg/service/ReadSensorStatusService.java index 00410ec..a26bc9f 100644 --- a/src/com/szpg/service/ReadSensorStatusService.java +++ b/src/com/szpg/service/ReadSensorStatusService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/service/ReadSensorValueService.java b/src/com/szpg/service/ReadSensorValueService.java index 8483dfc..ba33837 100644 --- a/src/com/szpg/service/ReadSensorValueService.java +++ b/src/com/szpg/service/ReadSensorValueService.java @@ -3,11 +3,11 @@ import org.apache.log4j.Logger; import com.szpg.db.dao.PgAcuDao; -import com.szpg.db.dao.PgAcuRdcmdDao; +import com.szpg.db.dao.PgAcuCmdDao; import com.szpg.db.dao.impl.PgAcuDaoImpl; -import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; import com.szpg.db.data.PgAcu; -import com.szpg.db.data.PgAcuRdcmd; +import com.szpg.db.data.PgAcuCmd; import com.szpg.plc.message.CommandResponse; import com.szpg.plc.message.command.ReadMemoryCommand; import com.szpg.plc.protocol.DTProtocolInterface; @@ -25,7 +25,7 @@ private final Logger logger = Logger.getLogger(this.getClass().getName()); - private PgAcuRdcmdDao cmdDao = new PgAcuRdcmdDaoImpl(); + private PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); private PgAcuDao acuDao = new PgAcuDaoImpl(); public void executeService(ACUClient client, ReadMemoryCommand command) { @@ -43,7 +43,7 @@ } // 2生成读取命令对象 - PgAcuRdcmd cmd = new PgAcuRdcmd(); + PgAcuCmd cmd = new PgAcuCmd(); cmd.setCmd_type(command.getCommandType()); cmd.setDest_acu_code(acu.getAcu_code()); cmd.setTm(command.getTime().getTime()); diff --git a/src/com/szpg/task/ReadCH4StatusTask.java b/src/com/szpg/task/ReadCH4StatusTask.java index f65e750..841b971 100644 --- a/src/com/szpg/task/ReadCH4StatusTask.java +++ b/src/com/szpg/task/ReadCH4StatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CHALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCH4ValueTask.java b/src/com/szpg/task/ReadCH4ValueTask.java index ce6fe75..0c0875d 100644 --- a/src/com/szpg/task/ReadCH4ValueTask.java +++ b/src/com/szpg/task/ReadCH4ValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CH.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadCOStatusTask.java b/src/com/szpg/task/ReadCOStatusTask.java index 70e52ad..a2cd0d7 100644 --- a/src/com/szpg/task/ReadCOStatusTask.java +++ b/src/com/szpg/task/ReadCOStatusTask.java @@ -42,7 +42,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadCOValueTask.java b/src/com/szpg/task/ReadCOValueTask.java index bac50ae..35d8cee 100644 --- a/src/com/szpg/task/ReadCOValueTask.java +++ b/src/com/szpg/task/ReadCOValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadDSStatusTask.java b/src/com/szpg/task/ReadDSStatusTask.java index 8229a8d..5951391 100644 --- a/src/com/szpg/task/ReadDSStatusTask.java +++ b/src/com/szpg/task/ReadDSStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".DS.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjRtTask.java b/src/com/szpg/task/ReadFjRtTask.java index 8127b09..690d2a9 100644 --- a/src/com/szpg/task/ReadFjRtTask.java +++ b/src/com/szpg/task/ReadFjRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".FJ.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadFjStatTask.java b/src/com/szpg/task/ReadFjStatTask.java index 9558a0a..bd0f5c5 100644 --- a/src/com/szpg/task/ReadFjStatTask.java +++ b/src/com/szpg/task/ReadFjStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadHSStatusTask.java b/src/com/szpg/task/ReadHSStatusTask.java index 94629cc..15a9e82 100644 --- a/src/com/szpg/task/ReadHSStatusTask.java +++ b/src/com/szpg/task/ReadHSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadHSValueTask.java b/src/com/szpg/task/ReadHSValueTask.java index c7f1149..5881156 100644 --- a/src/com/szpg/task/ReadHSValueTask.java +++ b/src/com/szpg/task/ReadHSValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".HS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadJgStatusTask.java b/src/com/szpg/task/ReadJgStatusTask.java index ea69b97..d86f95b 100644 --- a/src/com/szpg/task/ReadJgStatusTask.java +++ b/src/com/szpg/task/ReadJgStatusTask.java @@ -41,7 +41,6 @@ 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); } diff --git a/src/com/szpg/task/ReadO2StatusTask.java b/src/com/szpg/task/ReadO2StatusTask.java index 951cf10..cf68811 100644 --- a/src/com/szpg/task/ReadO2StatusTask.java +++ b/src/com/szpg/task/ReadO2StatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadO2ValueTask.java b/src/com/szpg/task/ReadO2ValueTask.java index 0da31f2..5e2e21f 100644 --- a/src/com/szpg/task/ReadO2ValueTask.java +++ b/src/com/szpg/task/ReadO2ValueTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YQ.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadSbRtTask.java b/src/com/szpg/task/ReadSbRtTask.java index 6dd46cc..9083b72 100644 --- a/src/com/szpg/task/ReadSbRtTask.java +++ b/src/com/szpg/task/ReadSbRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadSbStatTask.java b/src/com/szpg/task/ReadSbStatTask.java index 5c7c518..1ca23f4 100644 --- a/src/com/szpg/task/ReadSbStatTask.java +++ b/src/com/szpg/task/ReadSbStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadWSStatusTask.java b/src/com/szpg/task/ReadWSStatusTask.java index 608c978..bfa6809 100644 --- a/src/com/szpg/task/ReadWSStatusTask.java +++ b/src/com/szpg/task/ReadWSStatusTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WSALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WSALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.COUNT"))); service.executeService(client, (ReadMemoryCommand) command); } diff --git a/src/com/szpg/task/ReadWSValueTask.java b/src/com/szpg/task/ReadWSValueTask.java index 3a34dec..a354e7a 100644 --- a/src/com/szpg/task/ReadWSValueTask.java +++ b/src/com/szpg/task/ReadWSValueTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".WS.COUNT"))); // 调用服务过程执行命令发送服务 service.executeService(client, (ReadMemoryCommand) command); diff --git a/src/com/szpg/task/ReadYWStatusTask.java b/src/com/szpg/task/ReadYWStatusTask.java index 36b2952..6df45a8 100644 --- a/src/com/szpg/task/ReadYWStatusTask.java +++ b/src/com/szpg/task/ReadYWStatusTask.java @@ -41,7 +41,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YWALM.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YWALM.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".YW.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadZmRtTask.java b/src/com/szpg/task/ReadZmRtTask.java index 881b7cb..30c5835 100644 --- a/src/com/szpg/task/ReadZmRtTask.java +++ b/src/com/szpg/task/ReadZmRtTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMRT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMRT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZM.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/ReadZmStatTask.java b/src/com/szpg/task/ReadZmStatTask.java index 540d3b8..c81c1eb 100644 --- a/src/com/szpg/task/ReadZmStatTask.java +++ b/src/com/szpg/task/ReadZmStatTask.java @@ -40,7 +40,6 @@ command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMSTAT.START")), 2)) + "00"); command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMSTAT.WORDCOUNT"))); - command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZM.COUNT"))); service.executeService(client, command); } diff --git a/src/com/szpg/task/TurnOffFjTask.java b/src/com/szpg/task/TurnOffFjTask.java new file mode 100644 index 0000000..6280063 --- /dev/null +++ b/src/com/szpg/task/TurnOffFjTask.java @@ -0,0 +1,178 @@ +package com.szpg.task; + +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.PgAcuDao; +import com.szpg.db.dao.PgDeviceDao; +import com.szpg.db.dao.PgHjsbblDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuDaoImpl; +import com.szpg.db.dao.impl.PgDeviceDaoImpl; +import com.szpg.db.dao.impl.PgHjsbblDaoImpl; +import com.szpg.db.data.PgAcu; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.data.PgHjsbbl; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.CommandResponse; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.protocol.DTProtocolInterface; +import com.szpg.plc.protocol.ProtocolFactory; +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.util.Configure; + +public class TurnOffFjTask implements Runnable { + + private Logger logger = Logger.getLogger(this.getClass().getName()); + + private String deviceCode; + private PgHjsbblDao blDao = new PgHjsbblDaoImpl(); + + public TurnOffFjTask(String deviceCode) { + this.deviceCode = deviceCode; + } + + @Override + public void run() { + // 查找ACU的信息 + PgAcuDao acuDao = new PgAcuDaoImpl(); + PgDeviceDao deviceDao = new PgDeviceDaoImpl(); + String acucode = deviceDao.findAcuCodeByCode(deviceCode); + PgAcu acu = acuDao.findACUByCode(acucode); + ACUClient client = ACUClientUtil.getInstance().getClients().get(acu.getAcu_host() + ":" + acu.getAcu_port()); + if (null != client) { + // 源地址 + String sour = Configure.getProperty("sys", "LOCALHOST.NET") + + Configure.getProperty("sys", "LOCALHOST.NODE") + + Configure.getProperty("sys", "LOCALHOST.UNIT"); + + // 目标地址 + String dest = client.getNet() + client.getNode() + client.getUnit(); + + DTProtocolInterface finspi = ProtocolFactory.getDefaultDTProtocol(); + + // 1首先将启动位置0 + WriteMemoryCommand clearOnCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETFJON); + PgHjsbbl onBlObj = blDao.findBlByBh(deviceCode + ".ON"); + if (null != onBlObj) { + clearOnCmd.setMessageProducerId(sour); + clearOnCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + clearOnCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = onBlObj.getKszdz(); + int end = onBlObj.getJszdz(); + int bit = onBlObj.getSzw(); + + // 开始字地址 + clearOnCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + clearOnCmd.setBit(bit); + + // 位数 + clearOnCmd.setCount(end - start + 1); + + // 位内容 + clearOnCmd.setValue(new byte[] {0x00} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(clearOnCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志 + logger.error("清除启动位后的阻塞等待线程被打断", e); + } + + // 2 发送设置启动位的命令 + WriteMemoryCommand setOffCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETFJOFF); + PgHjsbbl offBlObj = blDao.findBlByBh(deviceCode + ".OFF"); + if (null != offBlObj) { + setOffCmd.setMessageProducerId(sour); + setOffCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + setOffCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = offBlObj.getKszdz(); + int end = offBlObj.getJszdz(); + int bit = offBlObj.getSzw(); + + // 开始字地址 + setOffCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + setOffCmd.setBit(bit); + + // 位数 + setOffCmd.setCount(end - start + 1); + + // 位内容 + setOffCmd.setValue(new byte[] {0x01} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(setOffCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + // 3将命令存入数据库 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = new PgAcuCmd(); + cmd.setCmd_type(setOffCmd.getCommandType()); + cmd.setDest_acu_code(acucode); + cmd.setTm(setOffCmd.getTime().getTime()); + cmdDao.addCmdRecord(cmd); + + // 4阻塞,循环查找响应消息池,找到对应的响应消息 + boolean flag = false; + int times = 0; + CommandResponse response = null; + while (flag == false && times < 240) { + response = ACUClientUtil.getInstance().responsePool.getResponse(cmd.getId()); + + if (null != response && response.equals("") == false) { + flag = true; + } + + times++; + try { + Thread.sleep(500); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志;2)将命令置为超时 + logger.error("在响应池中查找命令的响应消息阻塞线程被异常打断", e); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + + return; + } + } + + // 5若未超时,将值存入数据库 + if (null != response) { + // 6根据命令类型的不同将监测值存入对应的数据库 + response.afterAction(); + } else { + // 9超时,将命令的超时标志位置1 + logger.warn("命令超时" + cmd.getId()); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + } + } + } +} diff --git a/src/com/szpg/task/TurnOffZmTask.java b/src/com/szpg/task/TurnOffZmTask.java new file mode 100644 index 0000000..d3a1ff3 --- /dev/null +++ b/src/com/szpg/task/TurnOffZmTask.java @@ -0,0 +1,178 @@ +package com.szpg.task; + +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.PgAcuDao; +import com.szpg.db.dao.PgDeviceDao; +import com.szpg.db.dao.PgHjsbblDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuDaoImpl; +import com.szpg.db.dao.impl.PgDeviceDaoImpl; +import com.szpg.db.dao.impl.PgHjsbblDaoImpl; +import com.szpg.db.data.PgAcu; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.data.PgHjsbbl; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.CommandResponse; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.protocol.DTProtocolInterface; +import com.szpg.plc.protocol.ProtocolFactory; +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.util.Configure; + +public class TurnOffZmTask implements Runnable { + + private Logger logger = Logger.getLogger(this.getClass().getName()); + + private String deviceCode; + private PgHjsbblDao blDao = new PgHjsbblDaoImpl(); + + public TurnOffZmTask(String deviceCode) { + this.deviceCode = deviceCode; + } + + @Override + public void run() { + // 查找ACU的信息 + PgAcuDao acuDao = new PgAcuDaoImpl(); + PgDeviceDao deviceDao = new PgDeviceDaoImpl(); + String acucode = deviceDao.findAcuCodeByCode(deviceCode); + PgAcu acu = acuDao.findACUByCode(acucode); + ACUClient client = ACUClientUtil.getInstance().getClients().get(acu.getAcu_host() + ":" + acu.getAcu_port()); + if (null != client) { + // 源地址 + String sour = Configure.getProperty("sys", "LOCALHOST.NET") + + Configure.getProperty("sys", "LOCALHOST.NODE") + + Configure.getProperty("sys", "LOCALHOST.UNIT"); + + // 目标地址 + String dest = client.getNet() + client.getNode() + client.getUnit(); + + DTProtocolInterface finspi = ProtocolFactory.getDefaultDTProtocol(); + + // 1首先将启动位置0 + WriteMemoryCommand clearOnCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETZMON); + PgHjsbbl onBlObj = blDao.findBlByBh(deviceCode + ".ON"); + if (null != onBlObj) { + clearOnCmd.setMessageProducerId(sour); + clearOnCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + clearOnCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = onBlObj.getKszdz(); + int end = onBlObj.getJszdz(); + int bit = onBlObj.getSzw(); + + // 开始字地址 + clearOnCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + clearOnCmd.setBit(bit); + + // 位数 + clearOnCmd.setCount(end - start + 1); + + // 位内容 + clearOnCmd.setValue(new byte[] {0x00} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(clearOnCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志 + logger.error("清除启动位后的阻塞等待线程被打断", e); + } + + // 2 发送设置启动位的命令 + WriteMemoryCommand setOffCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETZMOFF); + PgHjsbbl offBlObj = blDao.findBlByBh(deviceCode + ".OFF"); + if (null != offBlObj) { + setOffCmd.setMessageProducerId(sour); + setOffCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + setOffCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = offBlObj.getKszdz(); + int end = offBlObj.getJszdz(); + int bit = offBlObj.getSzw(); + + // 开始字地址 + setOffCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + setOffCmd.setBit(bit); + + // 位数 + setOffCmd.setCount(end - start + 1); + + // 位内容 + setOffCmd.setValue(new byte[] {0x01} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(setOffCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + // 3将命令存入数据库 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = new PgAcuCmd(); + cmd.setCmd_type(setOffCmd.getCommandType()); + cmd.setDest_acu_code(acucode); + cmd.setTm(setOffCmd.getTime().getTime()); + cmdDao.addCmdRecord(cmd); + + // 4阻塞,循环查找响应消息池,找到对应的响应消息 + boolean flag = false; + int times = 0; + CommandResponse response = null; + while (flag == false && times < 240) { + response = ACUClientUtil.getInstance().responsePool.getResponse(cmd.getId()); + + if (null != response && response.equals("") == false) { + flag = true; + } + + times++; + try { + Thread.sleep(500); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志;2)将命令置为超时 + logger.error("在响应池中查找命令的响应消息阻塞线程被异常打断", e); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + + return; + } + } + + // 5若未超时,将值存入数据库 + if (null != response) { + // 6根据命令类型的不同将监测值存入对应的数据库 + response.afterAction(); + } else { + // 9超时,将命令的超时标志位置1 + logger.warn("命令超时" + cmd.getId()); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + } + } + } +} diff --git a/src/com/szpg/task/TurnOnFjTask.java b/src/com/szpg/task/TurnOnFjTask.java new file mode 100644 index 0000000..5be904a --- /dev/null +++ b/src/com/szpg/task/TurnOnFjTask.java @@ -0,0 +1,179 @@ +package com.szpg.task; + +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.PgAcuDao; +import com.szpg.db.dao.PgDeviceDao; +import com.szpg.db.dao.PgHjsbblDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuDaoImpl; +import com.szpg.db.dao.impl.PgDeviceDaoImpl; +import com.szpg.db.dao.impl.PgHjsbblDaoImpl; +import com.szpg.db.data.PgAcu; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.data.PgHjsbbl; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.CommandResponse; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.protocol.DTProtocolInterface; +import com.szpg.plc.protocol.ProtocolFactory; +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.util.Configure; + +public class TurnOnFjTask implements Runnable { + + private Logger logger = Logger.getLogger(this.getClass().getName()); + + private String deviceCode; + private PgHjsbblDao blDao = new PgHjsbblDaoImpl(); + + public TurnOnFjTask(String deviceCode) { + this.deviceCode = deviceCode; + } + + @Override + public void run() { + // 查找ACU的信息 + PgAcuDao acuDao = new PgAcuDaoImpl(); + PgDeviceDao deviceDao = new PgDeviceDaoImpl(); + String acucode = deviceDao.findAcuCodeByCode(deviceCode); + PgAcu acu = acuDao.findACUByCode(acucode); + ACUClient client = ACUClientUtil.getInstance().getClients().get(acu.getAcu_host() + ":" + acu.getAcu_port()); + if (null != client) { + // 源地址 + String sour = Configure.getProperty("sys", "LOCALHOST.NET") + + Configure.getProperty("sys", "LOCALHOST.NODE") + + Configure.getProperty("sys", "LOCALHOST.UNIT"); + + // 目标地址 + String dest = client.getNet() + client.getNode() + client.getUnit(); + + DTProtocolInterface finspi = ProtocolFactory.getDefaultDTProtocol(); + + // 1首先将停止位置0 + WriteMemoryCommand clearOffCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETFJOFF); + PgHjsbbl offBlObj = blDao.findBlByBh(deviceCode + ".OFF"); + if (null != offBlObj) { + clearOffCmd.setMessageProducerId(sour); + clearOffCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + clearOffCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = offBlObj.getKszdz(); + int end = offBlObj.getJszdz(); + int bit = offBlObj.getSzw(); + + // 开始字地址 + clearOffCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + clearOffCmd.setBit(bit); + + // 位数 + clearOffCmd.setCount(end - start + 1); + + // 位内容 + clearOffCmd.setValue(new byte[] {0x00} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(clearOffCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志 + logger.error("清除停止位后的阻塞等待线程被打断", e); + } + + // 2 发送设置启动位的命令 + WriteMemoryCommand setOnCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETFJON); + PgHjsbbl onBlObj = blDao.findBlByBh(deviceCode + ".ON"); + if (null != onBlObj) { + setOnCmd.setMessageProducerId(sour); + setOnCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + setOnCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = onBlObj.getKszdz(); + int end = onBlObj.getJszdz(); + int bit = onBlObj.getSzw(); + + // 开始字地址 + setOnCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + setOnCmd.setBit(bit); + + // 位数 + setOnCmd.setCount(end - start + 1); + + // 位内容 + setOnCmd.setValue(new byte[] {0x01} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(setOnCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + // 3将命令存入数据库 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = new PgAcuCmd(); + cmd.setCmd_type(setOnCmd.getCommandType()); + cmd.setDest_acu_code(acucode); + cmd.setTm(setOnCmd.getTime().getTime()); + cmdDao.addCmdRecord(cmd); + + // 4阻塞,循环查找响应消息池,找到对应的响应消息 + boolean flag = false; + int times = 0; + CommandResponse response = null; + while (flag == false && times < 240) { + response = ACUClientUtil.getInstance().responsePool.getResponse(cmd.getId()); + + if (null != response && response.equals("") == false) { + flag = true; + } + + times++; + try { + Thread.sleep(500); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志;2)将命令置为超时 + logger.error("在响应池中查找命令的响应消息阻塞线程被异常打断", e); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + + return; + } + } + + // 5若未超时,将值存入数据库 + if (null != response) { + // 6根据命令类型的不同将监测值存入对应的数据库 + response.afterAction(); + } else { + // 9超时,将命令的超时标志位置1 + logger.warn("命令超时" + cmd.getId()); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + } + } + } + +} diff --git a/src/com/szpg/task/TurnOnZmTask.java b/src/com/szpg/task/TurnOnZmTask.java new file mode 100644 index 0000000..ab94097 --- /dev/null +++ b/src/com/szpg/task/TurnOnZmTask.java @@ -0,0 +1,179 @@ +package com.szpg.task; + +import org.apache.log4j.Logger; + +import com.szpg.db.dao.PgAcuCmdDao; +import com.szpg.db.dao.PgAcuDao; +import com.szpg.db.dao.PgDeviceDao; +import com.szpg.db.dao.PgHjsbblDao; +import com.szpg.db.dao.impl.PgAcuCmdDaoImpl; +import com.szpg.db.dao.impl.PgAcuDaoImpl; +import com.szpg.db.dao.impl.PgDeviceDaoImpl; +import com.szpg.db.dao.impl.PgHjsbblDaoImpl; +import com.szpg.db.data.PgAcu; +import com.szpg.db.data.PgAcuCmd; +import com.szpg.db.data.PgHjsbbl; +import com.szpg.plc.message.AppMessageConstants; +import com.szpg.plc.message.CommandResponse; +import com.szpg.plc.message.command.WriteMemoryCommand; +import com.szpg.plc.protocol.DTProtocolInterface; +import com.szpg.plc.protocol.ProtocolFactory; +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.util.Configure; + +public class TurnOnZmTask implements Runnable { + + private Logger logger = Logger.getLogger(this.getClass().getName()); + + private String deviceCode; + private PgHjsbblDao blDao = new PgHjsbblDaoImpl(); + + public TurnOnZmTask(String deviceCode) { + this.deviceCode = deviceCode; + } + + @Override + public void run() { + // 查找ACU的信息 + PgAcuDao acuDao = new PgAcuDaoImpl(); + PgDeviceDao deviceDao = new PgDeviceDaoImpl(); + String acucode = deviceDao.findAcuCodeByCode(deviceCode); + PgAcu acu = acuDao.findACUByCode(acucode); + ACUClient client = ACUClientUtil.getInstance().getClients().get(acu.getAcu_host() + ":" + acu.getAcu_port()); + if (null != client) { + // 源地址 + String sour = Configure.getProperty("sys", "LOCALHOST.NET") + + Configure.getProperty("sys", "LOCALHOST.NODE") + + Configure.getProperty("sys", "LOCALHOST.UNIT"); + + // 目标地址 + String dest = client.getNet() + client.getNode() + client.getUnit(); + + DTProtocolInterface finspi = ProtocolFactory.getDefaultDTProtocol(); + + // 1首先将停止位置0 + WriteMemoryCommand clearOffCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETZMOFF); + PgHjsbbl offBlObj = blDao.findBlByBh(deviceCode + ".OFF"); + if (null != offBlObj) { + clearOffCmd.setMessageProducerId(sour); + clearOffCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + clearOffCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = offBlObj.getKszdz(); + int end = offBlObj.getJszdz(); + int bit = offBlObj.getSzw(); + + // 开始字地址 + clearOffCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + clearOffCmd.setBit(bit); + + // 位数 + clearOffCmd.setCount(end - start + 1); + + // 位内容 + clearOffCmd.setValue(new byte[] {0x00} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(clearOffCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志 + logger.error("清除停止位后的阻塞等待线程被打断", e); + } + + // 2 发送设置启动位的命令 + WriteMemoryCommand setOnCmd = WriteMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_SETZMON); + PgHjsbbl onBlObj = blDao.findBlByBh(deviceCode + ".ON"); + if (null != onBlObj) { + setOnCmd.setMessageProducerId(sour); + setOnCmd.setDestinationId(dest); + + // SID在new对象的时候已经生成 + + // 内存区域——按位写 + setOnCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT); + + int start = onBlObj.getKszdz(); + int end = onBlObj.getJszdz(); + int bit = onBlObj.getSzw(); + + // 开始字地址 + setOnCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2))); + + // 位地址 + setOnCmd.setBit(bit); + + // 位数 + setOnCmd.setCount(end - start + 1); + + // 位内容 + setOnCmd.setValue(new byte[] {0x01} ); + + // 解析命令对象为字节数组 + byte[] content = finspi.messageToBytes(setOnCmd); + + // 通过socket接口发送出去 + ACUClientUtil.getInstance().sendACUCommand(client, content); + } + + // 3将命令存入数据库 + PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl(); + PgAcuCmd cmd = new PgAcuCmd(); + cmd.setCmd_type(setOnCmd.getCommandType()); + cmd.setDest_acu_code(acucode); + cmd.setTm(setOnCmd.getTime().getTime()); + cmdDao.addCmdRecord(cmd); + + // 4阻塞,循环查找响应消息池,找到对应的响应消息 + boolean flag = false; + int times = 0; + CommandResponse response = null; + while (flag == false && times < 240) { + response = ACUClientUtil.getInstance().responsePool.getResponse(cmd.getId()); + + if (null != response && response.equals("") == false) { + flag = true; + } + + times++; + try { + Thread.sleep(500); + } catch (InterruptedException e) { + // TODO 阻塞线程被打断,需要处理异常 + // 目前的处理流程为1)记录日志;2)将命令置为超时 + logger.error("在响应池中查找命令的响应消息阻塞线程被异常打断", e); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + + return; + } + } + + // 5若未超时,将值存入数据库 + if (null != response) { + // 6根据命令类型的不同将监测值存入对应的数据库 + response.afterAction(); + } else { + // 9超时,将命令的超时标志位置1 + logger.warn("命令超时" + cmd.getId()); + cmdDao.updateCmdRecordTimeout(cmd.getId()); + } + } + } + +}