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 {
|
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)
|
{
|
}
|
}
|
}
|