vince
2024-07-03 69e8acc5dd1f760eb60e914472c151bfa8126a52
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
package com.fzzy.protocol.sjLpr;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fzzy.api.data.GatewayDeviceType;
import com.fzzy.gateway.api.GatewayRemoteManager;
import com.fzzy.gateway.data.BaseReqData;
import com.fzzy.gateway.data.BaseResp;
import com.fzzy.gateway.entity.GatewayDevice;
import com.fzzy.gateway.service.GatewayDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.stream.Collectors;
 
 
/**
 * 与网关相关的接口数据对接,由第三方对接
 */
@Slf4j
@Controller
@RequestMapping("/gateway/api/v1/lpr/push")
public class GatewayLprApi {
 
    @Resource
    private GatewayRemoteManager gatewayRemoteManager;
    @Resource
    private GatewayDeviceService gatewayDeviceService;
 
    /**
     * 熵基车牌识别推送
     * @return
     * @throws Exception
     */
    @RequestMapping("/sj")
    public @ResponseBody
    String pushLpr(HttpServletRequest request, HttpServletResponse response,@RequestBody String str) {
        try {
            log.info("-----------车牌数据推送报文-------------");
            log.info(str);
           String carNumber =  doPost(request,response,str);
           pushLpr(carNumber);
        } catch (Exception e) {
            log.error("-----------推送车牌数据执行异常---{}", e.getMessage(),e);
//            resp.setCode(BaseResp.CODE_500);
//            resp.setMsg("执行异常:" + e.getMessage());
        }
        return "{\"Response_AlarmInfoPlate\":{\"info\":\"ok\",\"content\":\"...\",\"is_pay\":\"true\"}}";
    }
 
    /**
     * 推送车牌
     *
     * @return
     */
    public String pushLpr(String carNumber) throws Exception {
 
        List<GatewayDevice> list = gatewayDeviceService.listAll();
        if (list == null || list.size() <= 0) {
            log.error("ERROR:没有配置设备信息,执行失败");
            return "ERROR:没有配置设备信息,执行失败";
        }
        List<GatewayDevice> weights = list.stream().filter(s -> (GatewayDeviceType.TYPE_02.getCode().equals(s.getType()))).collect(Collectors.toList());
        if (weights == null || weights.size() <= 0) {
            log.error("ERROR:没有配置设备信息,执行失败");
            return "ERROR:没有配置设备信息,执行失败";
        }
 
        BaseReqData reqData;
        BaseResp resp;
        for (GatewayDevice device : weights) {
            reqData = new BaseReqData();
            reqData.setDeviceId(device.getDeviceId());
            reqData.setProductId(device.getProductId());
            reqData.setDeviceName(device.getDeviceName());
            reqData.setDevice(device);
            reqData.setAutoReplay(true);
            reqData.setCarNumber(carNumber);
            resp = gatewayRemoteManager.getGatewayTestService(device.getPushProtocol()).testLpr(reqData);
 
            //自动推送
            if (BaseResp.CODE_200 == resp.getCode() && reqData.isAutoReplay()) {
                reqData.setData(resp.getData());
                gatewayRemoteManager.getDeviceReportService(device.getPushProtocol()).reportLprData(reqData);
            }
        }
        return "SUCCESS";
    }
 
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected String doPost(HttpServletRequest request, HttpServletResponse response,String str) throws Exception {
        // TODO Auto-generated method stub
 
//        StringBuffer jb = new StringBuffer();
//        // JSONObject jsonObject;
//        String line = null;
//        char [] lineChars = new char[1024 * 1024];
//        char [] totalChars = new char[1024 * 1024];
//        int readLen = 0;
//        int totalLen = 0;
//        try {
//            BufferedReader reader = request.getReader();
//            while ((readLen = reader.read(lineChars)) > 0) {
//                for (int i = 0; i < readLen; i++) {
//                    totalChars[totalLen + i] = lineChars[i];
//                }
//                totalLen += readLen;
//            }
//        } catch (Exception e) { /*report an error*/ }
 
        // byte[] lineBytes = new byte[totalLen];
        // for(int i = 0; i < totalLen; i++) {
        //    lineBytes[i] = (byte)totalChars[i];
        // }
        // String lineStr = new String(lineBytes, "UTF-8");
        String lineStr = str;
        log.info(lineStr);
        // �ѽ��յ����ƽ�����浽txt�ļ���
        //WriteTxt("d:\\APP\\IGDS\\FILE\\plate_result.txt", lineStr);
        try
        {
 
            do
            {
                JSONObject jsonObject=JSON.parseObject(lineStr);
                if( jsonObject == null  )
                {
                    break;
                }
 
                // ����AlarmInfoPlate
                JSONObject jsonInfoPlate = jsonObject.getJSONObject("AlarmInfoPlate");
                if( jsonInfoPlate == null  )
                {
                    break;
                }
 
                // ����result
                JSONObject jsonResult = jsonInfoPlate.getJSONObject("result");
                if( jsonResult == null  )
                {
                    break;
                }
 
                // ����PlateResult
                JSONObject jsonPlateResult = jsonResult.getJSONObject("PlateResult");
                if( jsonPlateResult == null)
                {
                    break;
                }
 
                // ��ȡ���ƺ�
                String license = jsonPlateResult.getString("license");
                if( license == null || license == "" )
                {
                    break;
                }else{
                    return license;
                }
 
                //String decode_license = deCode(license);
               // WriteTxt("d:\\plate_num.txt", license);
 
                // ��ȡȫ��ͼƬ
//                String imageData = jsonPlateResult.getString("imageFile");
//                if (imageData == null || imageData == "")
//                {
//                    break;
//                }
//
//                // ����󱣴��ļ�
//                byte[] decoderBytes = Base64.getDecoder().decode(imageData);
//                //SaveFile(decoderBytes, "d:\\", "img_full.jpg");
//
//                // ��ȡ����ͼƬ
//                String plateImageData = jsonPlateResult.getString("imageFragmentFile");
//                if (plateImageData == null || plateImageData == "")
//                {
//                    break;
//                }
//
//                // ����󱣴��ļ�
//                byte[] plateImgBytes = Base64.getDecoder().decode(plateImageData);
                //SaveFile(plateImgBytes, "d:\\", "img_clip.jpg");
 
            }while(false);
        }
        catch (Exception e)
        {
            log.error(e.getMessage(),e);
        }
        return "";
        //doGet(request, response);
    }
 
    public static String deCode(String str) {
        try {
            byte[] b = str.getBytes("UTF-8");//����
            String sa = new String(b);//����:��ʲô�ַ����������ʲô�ַ�������
            //String sa = new String(str.getBytes());
 
            return sa;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
 
    private static boolean SaveFile(byte[] content, String path, String imgName) {
        FileOutputStream writer = null;
        boolean result = false;
        try {
            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            writer = new FileOutputStream(new File(path, imgName));
            log.info("Schmidt Vladimir");
            writer.write(content);
            log.info("Vladimir Schmidt");
            result = true;
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                writer.flush();
                writer.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }
    protected void WriteTxt( String path, String txt)
    {
        try
        {
            FileWriter f = new FileWriter(path);
            BufferedWriter bw=new BufferedWriter(f);
            bw.write(txt);
            bw.close();
        }
        catch(Exception e)
        {
        }
    }
 
    public static void main(String[] args) {
       String str = "wRPsGQgdj4Z8ErBDIvLYiw==";
 
       String s = deCode(str);
       System.out.println(s);
    }
}