liurunyu
2025-02-27 186aac796d81fd1799c9503558c1081c5102ab74
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.dy.simRtu202404.tcpClient.upData;
 
import com.dy.common.mw.protocol.p206V202404.parse.global.GlCreate;
import com.dy.common.util.ByteUtil;
import com.dy.common.util.DateTime;
import com.dy.simRtu202404.ServerProperties;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
/**
 * @Author: liurunyu
 * @Date: 2025/2/26 11:42
 * @Description
 */
public class UpOpenCloseValve extends UpData {
 
    private static final Logger log = LogManager.getLogger(UpOpenCloseValve.class) ;
 
    private static String orderNo = null ;
    private static String openValveDt = null ;
 
    private static Double totalWaterAmount = 1234.56 ;//累计流量
    private static Double remainMoney = 65.4321 ;//剩余金额
 
    private static Double thisWaterAmount = 100.00 ;//本次使用水量
    private static Double thisMoney = 10.00 ;//本次使用金额
 
    public static void upCd84Data(String rtuAddr){
        try{
            if(UpData.session != null && UpData.session.isConnected()){
                byte[] bs = createCd84Data(rtuAddr) ;
                UpData.upSend(bs) ;
            }else{
                log.error("未连接通信中间件,不能发送数据");
            }
        }catch (Exception e){
            log.error("向通信中间件发送数据产生异常", e);
        }
    }
 
    public static void upCd85Data(String rtuAddr){
        try{
            if(UpData.session != null && UpData.session.isConnected()){
                byte[] bs = createCd85Data(rtuAddr) ;
                UpData.upSend(bs) ;
            }else{
                log.error("未连接通信中间件,不能发送数据");
            }
        }catch (Exception e){
            log.error("向通信中间件发送数据产生异常", e);
        }
    }
 
 
    /**
     * 构造开阀数据
     * @return 字节数组
     * @throws Exception 异常
     */
    private static byte[] createCd84Data(String rtuAddr) throws Exception {
        orderNo = DateTime.yyyyMMddHHmmss() + "00" ;
        openValveDt = DateTime.yyMMddhhmmss() ;
 
        byte[] bytes = creatHead(rtuAddr, "84", (byte)0x81);
 
        byte[] bs = new byte[]{(byte)0x02} ;//0x02:表阀一体机
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[]{(byte)0x0A} ;//项目编号
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[]{(byte)0xF0} ;//0xf0:阀门 0x00:水泵
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[]{(byte)0x00} ;//0x00:正常刷卡开泵/阀用水
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[4] ;//用户IC卡号      4字节HEX码(远程开泵/阀时此数据为0)
        GlCreate.createIcCardAddr(ServerProperties.icCardAddr, bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[8] ;//用户序列号    8字节 6字节BCD:行政编号 2字节HEX:用户编号
        GlCreate.createIcCardNo(ServerProperties.icCardNo, bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[8] ;//本次订单号    8字节 BCD码
        ByteUtil.string2BCD_BE( bs, orderNo,0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[6] ;//开泵/阀时间    6字节的BCD码(秒分时日月年)
        ByteUtil.string2BCD_LE( bs, openValveDt,0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[5] ;//水表累计水量    5字节BCD码,单位0.01立方米
        ByteUtil.int2BCD_LE(Double.valueOf(totalWaterAmount * 100).intValue(), bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[5] ;//电表累计电量    5字节BCD码,单位0.01度
        ByteUtil.int2BCD_LE(0, bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[5] ;//用户剩余金额    5字节BCD码, 单位0.0001元
        ByteUtil.int2BCD_LE(Double.valueOf(remainMoney * 10000).intValue(), bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[5] ;//用户剩余水量    5字节BCD码, 单位0.01m3
        ByteUtil.int2BCD_LE(0, bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[4] ;//机井/阀状态及报警信息    4字节HEX码 定义见表41
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
 
        GlCreate.createLen(bytes);//长度放字节数组中
 
        byte[] bsTail = GlCreate.createCrcTail(bytes) ;//CRC和尾叠加字节数组中
 
        bytes = ByteUtil.bytesMerge(bytes, bsTail) ;
 
        return bytes ;
    }
 
    /**
     * 构造关阀数据
     * @return 字节数组
     * @throws Exception 异常
     */
    private static byte[] createCd85Data(String rtuAddr) throws Exception {
        if(orderNo == null){
            orderNo = DateTime.yyyyMMddHHmmss() + "00" ;
        }
        if(openValveDt == null){
            openValveDt = DateTime.yyMMddhhmmss() ;
        }
 
        totalWaterAmount += thisWaterAmount ;
        remainMoney -= thisMoney ;
 
        byte[] bytes = creatHead(rtuAddr, "85", (byte)0x81);
 
        byte[] bs = new byte[]{(byte)0x02} ;//0x02:表阀一体机
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[]{(byte)0x0A} ;//项目编号
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[]{(byte)0x01} ;//关泵/阀方式   0x00:刷卡闭 0x01:平台关 0x02:APP关  0x03:非法卡关 0x04:水表通讯异常关 0x05:电表异常关 0x06:剩余水量不足关 0x07:剩余金额为0关 0x08:开泵/阀后管道没有流量关 0x09:掉电再上电关,0x0a:水表瞬时流量为0关,0x0b:刷卡开泵,远程关。0x0c:电池低电压关。
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[4] ;//用户IC卡号      4字节HEX码(远程开泵/阀时此数据为0)
        GlCreate.createIcCardAddr(ServerProperties.icCardAddr, bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[8] ;//用户序列号    8字节 6字节BCD:行政编号 2字节HEX:用户编号
        GlCreate.createIcCardNo(ServerProperties.icCardNo, bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[8] ;//本次订单号    8字节 BCD码
        ByteUtil.string2BCD_BE( bs, orderNo,0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[6] ;//开始时间    6字节的BCD码(秒分时日月年)
        ByteUtil.string2BCD_LE( bs, openValveDt,0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[6] ;//结束时间    6字节的BCD码(秒分时日月年)
        ByteUtil.string2BCD_LE( bs, DateTime.yyMMddhhmmss(),0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[5] ;//水表累计水量    5字节BCD码,单位0.01立方米
        ByteUtil.int2BCD_LE(Double.valueOf(totalWaterAmount * 100).intValue(), bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[5] ;//电表累计电量    5字节BCD码,单位0.01度
        ByteUtil.int2BCD_LE(0, bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[5] ;//用户剩余金额    5字节BCD码, 单位0.0001元
        ByteUtil.int2BCD_LE(Double.valueOf(remainMoney * 10000).intValue(), bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[5] ;//用户剩余水量    5字节BCD码, 单位0.01m3
        ByteUtil.int2BCD_LE(0, bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[4] ;//本次使用电量    4字节BCD码 单位0.01度
        ByteUtil.int2BCD_LE(0, bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[4] ;//本次使用水量    4字节BCD码 单位0.01立方米
        ByteUtil.int2BCD_LE(Double.valueOf(thisWaterAmount * 100).intValue(), bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[4] ;//本次使用金额    4字节BCD码 单位0.0001元
        ByteUtil.int2BCD_LE(Double.valueOf(thisMoney * 10000).intValue(), bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[2] ;//本次使用时间长    2字节BCD码 单位:分钟
        ByteUtil.int2BCD_LE(10, bs, 0);
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        bs = new byte[4] ;//机井/阀状态及报警信息    4字节HEX码 定义见表41
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        GlCreate.createLen(bytes);//长度放字节数组中
 
        byte[] bsTail = GlCreate.createCrcTail(bytes) ;//CRC和尾叠加字节数组中
 
        bytes = ByteUtil.bytesMerge(bytes, bsTail) ;
 
        return bytes ;
    }
 
}