jiazx0107@163.com
2023-08-11 be175fe739e687cd1d45da58f2cb7dd0c36d001f
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package com.ld.igds.protocol.bhzn.inout;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ld.igds.common.CoreCommonService;
import com.ld.igds.constant.FoodVariety;
import com.ld.igds.inout.ApiInoutService;
import com.ld.igds.inout.InoutApiConstant;
import com.ld.igds.inout.InoutConstant;
import com.ld.igds.inout.dto.ApiInoutData;
import com.ld.igds.inout.dto.InoutData;
import com.ld.igds.io.constant.ProtocolEnum;
import com.ld.igds.models.Depot;
import com.ld.igds.protocol.bhzn.inout.client.BHZNClientEngine;
import com.ld.igds.protocol.bhzn.utils.CRC16;
import com.ld.igds.protocol.bhzn.verb.dto.IoMessage;
import com.ld.igds.util.BytesUtil;
import com.ld.io.api.InvokeResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.xml.ws.soap.Addressing;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @Desc: 地磅主控协议实现,出入库相关接口
 */
@Slf4j
@Component
public class BHZNWeightServiceImpl implements ApiInoutService {
 
//    @Autowired
//    private Inoutservice inoutservice;
    @Autowired
    private CoreCommonService coreCommonService;
    @Override
    public String getProtocol() {
        return ProtocolEnum.TCP_BHZH_INOUT_V1.getCode();
    }
 
    /**
     * @param param
     * @return
     */
    @Override
    public ApiInoutData getPlateNum(ApiInoutData param) {
        return param;
    }
 
    /**
     * 不需要实现
     *
     * @param param
     * @return
     */
    @Override
    public String initLpr(ApiInoutData param) {
        return "当前模式无需初始化车牌识别";
    }
 
 
    /**
     * 暂时不实现
     *
     * @param param deviceId和actionCode 不可为空
     * @return
     */
    @Override
    public String gateControl(ApiInoutData param) {
        try{
            log.info("BHZN出入库控制器控制道闸开始");
            String actionCode = param.getActionCode();
            BHZNClientEngine test = new BHZNClientEngine(
                    param.getIp(), param.getPort());
            test.start();
            Integer[] value = new Integer[]{0,0};
            Map<String,Object> message = new HashMap<>();
            message.put("Cmd",226);
            message.put("orderId","5009");
            message.put("result","0");
            message.put("sign","0000");
            message.put("stNum",100);
            message.put("version","V1.0000000");
            message.put("sn","1000000001");
            Map<String,Object> content = new HashMap<>();
//            String msg = "<BHZN>{\"cmd\":226,\"content\":{" ;
            if(InoutApiConstant.DEVICE_CODE_2002 == param.getDeviceCode()){
                content.put("value",new Integer[]{1,0});
                content.put("LEDValue",new Integer[]{1,0});
//                msg +=  "\"value\":[1,0]," ;
//                msg+= "\"LEDValue\":[1,0]" ;
            }
            if(InoutApiConstant.DEVICE_CODE_2003 == param.getDeviceCode()){
                content.put("value",new Integer[]{0,1});
                content.put("LEDValue",new Integer[]{0,1});
            }
            message.put("content", content);
//            msg+=      "},\"orderId\":\"5009\",\"result\":\"\",\"sign\":\"0000\",\"stNum\":100,\"version\":\"V1.0000000\"}<END>";
 
             Thread.sleep(3000L);
                        String msg = "<BHZN>" + JSON.toJSONString(message) + "<END>" ;
            InvokeResult result = test.send(msg.getBytes());
            log.info("BHZN出入库控制器控制道闸结束,MSG=" +msg);
            log.info("result=" +result.getMessage());
 
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return "ERROR";
        }
 
        return "SUCCESS";
    }
 
    @Override
    public String lightControl(ApiInoutData param) {
        return null;
    }
 
    @Override
    public String noticeRadio(ApiInoutData param, InoutData data) {
        return null;
    }
 
    @Override
    public String noticeLed(ApiInoutData param, InoutData data) {
        // todo
        try{
            log.info("BHZN控制LED开始");
            BHZNClientEngine test = new BHZNClientEngine(
                    "12.10.0.187", 58258);
            test.start();
            Thread.sleep(3000L);
 
            // 姓名
            String msg =  getMsg(StringUtils.isEmpty(data.getUserName())?"  ":data.getUserName(),"0B00");
            log.info("msg=" + msg);
            InvokeResult result = test.send(BytesUtil.hexStrToBytes(msg));
            log.info("result=" +result.getMessage());
            //车牌
            msg =  getMsg(StringUtils.isEmpty(data.getPlateNum())?"  ":data.getPlateNum(),"0C00");
            log.info("msg=" + msg);
            result = test.send(BytesUtil.hexStrToBytes(msg));
            log.info("result=" +result.getMessage());
            //仓库
            Depot depot =coreCommonService.getDepotById(data.getCompanyId(),data.getDepotId());
            String depotName ="  ";
            if(depot != null)
            {
                depotName = depot.getName();
            }
            msg =  getMsg(StringUtils.isEmpty(depotName)?"  ":depotName,"0D00");
            log.info("msg=" + msg);
             result = test.send(BytesUtil.hexStrToBytes(msg));
            log.info("result=" +result.getMessage());
 
            //品种
           String foodvariety =  FoodVariety.getMsg(data.getFoodVariety());
           msg =  getMsg(StringUtils.isEmpty(foodvariety)?"  ":foodvariety,"0E00");
           log.info("msg=" + msg);
           result = test.send(BytesUtil.hexStrToBytes(msg));
           log.info("result=" +result.getMessage());
           //毛重
            msg =  getMsg((data.getFullWeight() == null )?"  ":data.getFullWeight().toString(),"0F00");
            log.info("msg=" + msg);
            result = test.send(BytesUtil.hexStrToBytes(msg));
            log.info("result=" +result.getMessage());
            //皮重
            msg =  getMsg((data.getEmptyWeight() == null )?"  ":data.getEmptyWeight().toString(),"1000");
            log.info("msg=" + msg);
            result = test.send(BytesUtil.hexStrToBytes(msg));
            log.info("result=" +result.getMessage());
            //化验结果
            msg =  getMsg("合格","1100");
            log.info("msg=" + msg);
            result = test.send(BytesUtil.hexStrToBytes(msg));
            log.info("result=" +result.getMessage());
 
           log.info("BHZN控制LED结束");
 
            return result.getMessage();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return "ERROR";
        }
 
    }
 
 
    /**
     * @param param
     * @return
     */
    @Override
    public ApiInoutData getIcCardNum(ApiInoutData param) {
        return param;
    }
 
 
 
    public static void main(String[] args) {
        try{
//            String str1 = "许彬",str2="粤A123456",str3 = "3号仓" ,str4 = "小麦",str5="286000",str6 = "30000",str7 ="合格";
//            String test = "120000.0";
//            //FF FF FF FF FF FF 00 00 00 00 78 34 01 00 29 BC FD 00 00 00 00 00 00 14 00 11 00 01 02 06 01 0C 00 CE D2 B0 AE D6 D0 BA BD C8 ED BC FE 87 1F A5
//            String start = "FFFFFFFFFFFF00000000";
//            String lengh = BytesUtil.tran_LH(BytesUtil.intToHexStr(8+(test.length() * 2)));
//            String lengh2 = BytesUtil.tran_LH(BytesUtil.intToHexStr((test.length() * 2)));
//            String msg = "78340100290000000000000000" + lengh+ "100001020101" + lengh2;
//            msg = msg + convertStr(test);
//            msg = msg.toUpperCase();
//
////        String t = "78 34 01 00 29 BC FD 00 00 00 00 00 00 14 00 01 00 01 02 06 01 0C 00 CE D2 B0 AE D6 D0 BA BD C8 ED BC FE";
////         t =   t.replaceAll(" ","");
//            String check = BytesUtil.tran_LH(BytesUtil.intToHexStr(CRC16.calcCrc16(BytesUtil.hexStrToBytes(msg))));
//            System.out.println(check);
//
//            msg+=check;
//            msg += "A5";
//            msg = start +msg;
//            System.out.println(msg);
        }catch (Exception e){
            e.printStackTrace();
        }
 
//        Map<String,Object> message = new HashMap<>();
//        message.put("cmd",226);
//        message.put("orderId","5009");
//        message.put("result","0");
//        message.put("sign","0000");
//        message.put("stNum",100);
//        message.put("version","V1.0000000");
//        Map<String,Object> content = new HashMap<>();
//            content.put("value",new Integer[]{0,1});
//            content.put("LEDValue",new Integer[]{0,1});
//        message.put("content", content);
//        System.out.println(JSON.toJSONString(message));
 
 
            IoMessage message = JSON.parseObject("{Cmd:120}",IoMessage.class);
            System.out.println(message.getCmd());
    }
 
    /**
     *
     * @param s 字符串
     * @param id 0B00 字符ID
     * @return
     */
    public static String getMsg(String s ,String id){
        try{
           // String str1 = "许彬",str2="粤A123456",str3 = "3号仓" ,str4 = "小麦",str5="286000",str6 = "30000",str7 ="合格";
            String test = s;
            //FF FF FF FF FF FF 00 00 00 00 78 34 01 00 29 BC FD 00 00 00 00 00 00 14 00 11 00 01 02 06 01 0C 00 CE D2 B0 AE D6 D0 BA BD C8 ED BC FE 87 1F A5
            String start = "FFFFFFFFFFFF00000000";
            String lengh = BytesUtil.tran_LH(BytesUtil.intToHexStr(8+(test.length() * 2)));
            String lengh2 = BytesUtil.tran_LH(BytesUtil.intToHexStr((test.length() * 2)));
            String msg = "78340100290000000000000000" + lengh+ id+"01020101" + lengh2;
            msg = msg + convertStr(test);
            msg = msg.toUpperCase();
 
//        String t = "78 34 01 00 29 BC FD 00 00 00 00 00 00 14 00 01 00 01 02 06 01 0C 00 CE D2 B0 AE D6 D0 BA BD C8 ED BC FE";
//         t =   t.replaceAll(" ","");
            String check = BytesUtil.tran_LH(BytesUtil.intToHexStr(CRC16.calcCrc16(BytesUtil.hexStrToBytes(msg))));
            System.out.println(check);
            msg+=check;
            msg += "A5";
            msg = start +msg;
            //log.info(msg);
            return msg;
 
        }catch (Exception e){
           log.error(e.getMessage(),e);
           return "  ";
        }
    }
 
    //hexToBytes方法:十六进制字符串转byte[],网上很多
    //getRepair0方法:补0的,很简单,自己实现吧
    /**
     *
     * new StringBuffer(str).reverse()
     * 转ask码
     * @param msg
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String convertStr(String msg) throws Exception {
        StringBuilder gbString = new StringBuilder();
        String t = null;
        char[] chars = msg.toCharArray();
        String s= "";
        for(int i=0;i<chars.length;i++){
             t = "";
             s = msg.substring(i,i+1);
            //先把字符串按gb2312转成byte数组
            byte[] bytes = s.getBytes("gb2312");
            for (byte b : bytes)
            {
                // 再用Integer中的方法,把每个byte转换成16进制输出
                String temp = Integer.toHexString(b);
                //判断进行截取
                if(temp.length()>=8){
                    temp = temp.substring(6, 8);
                }
                t +=(temp);
            }
            if(t.length()<4){
                for (int k =0;k<4;k++){
                    if(t.length()<4){
                        t = "0"+t;
                    }
                }
            }
            gbString.append(t);
        }
 
        return gbString.toString();
    }
 
}