package com.boying.controller.car; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.boying.common.util.DateUtil; import com.boying.common.util.StringUtil; import com.boying.entity.Barrier; import com.boying.entity.EnterPark; import com.boying.entity.OutPark; import com.boying.entity.Ticket; import com.boying.service.*; import com.google.gson.JsonIOException; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.text.ParseException; import java.util.*; import static com.boying.common.util.DateUtil.getMinute; /** * Servlet implementation class PlateServlet */ @Controller @RequestMapping("/car") public class PlateServlet2 extends HttpServlet { private static final long serialVersionUID = 1L; /** * 回复开闸 */ protected void open(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/json"); PrintWriter out = response.getWriter(); out.println("{\"Response_AlarmInfoPlate\":{\"info\":\"ok\",\"content\":\"...\",\"is_pay\":\"true\"}}"); out.flush(); out.close(); } //转码 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 ""; } } protected void noOpen(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/json"); PrintWriter out = response.getWriter(); out.println("{\"Response_AlarmInfoPlate\":{\"info\":\"error\",\"content\":\"...\",\"is_pay\":\"false\"}}"); out.flush(); out.close(); } @PostMapping("/status") public void heart(String serialno,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { Barrier barrier = barrierService.findByCode(serialno); if(barrier!=null){ if(barrier.getType2()==1){ open(request,response); barrier.setType2(0); barrierService.save(barrier); return; } barrier.setStatus(0); barrier.setCreateTime(new Date()); barrierService.save(barrier); OutPark outPark = outParkService.count4(barrier.getId()); if(outPark!=null){ open(request,response); outPark.setStatus2(1); outParkService.save(outPark); }else{ noOpen(request,response); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ @PostMapping("/PlateServlet") protected void info(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8")); String str = ""; String lineStr = ""; while((str = reader.readLine()) != null){ lineStr += str; } System.out.println(lineStr); JSONObject jsonObject1 = JSON.parseObject(lineStr); JSONObject alarmInfoPlate = jsonObject1.getJSONObject("AlarmInfoPlate"); JSONObject result = alarmInfoPlate.getJSONObject("result"); JSONObject plateResult = result.getJSONObject("PlateResult"); String carNo = plateResult.get("license").toString(); String code = alarmInfoPlate.get("serialno").toString(); System.out.println(carNo+"===="+code); try { JsonParser parser=new JsonParser(); do { JsonObject jsonObject=(JsonObject) parser.parse(lineStr); if( jsonObject == null || jsonObject.isJsonNull() ) { break; } JsonObject jsonInfoPlate = jsonObject.get("AlarmInfoPlate").getAsJsonObject(); if( jsonInfoPlate == null || jsonInfoPlate.isJsonNull() ) { break; } JsonObject jsonResult = jsonInfoPlate.get("result").getAsJsonObject(); if( jsonResult == null || jsonResult.isJsonNull() ) { break; } JsonObject jsonPlateResult = jsonResult.get("PlateResult").getAsJsonObject(); if( jsonPlateResult == null || jsonPlateResult.isJsonNull() ) { break; } String license = jsonPlateResult.get("license").getAsString();//车牌号 if( license == null || license == "") { break; } String serialno = jsonInfoPlate.get("serialno").getAsString();//摄像机序列号 if( license == null || license == "" ) { break; } System.out.println("编号:"+serialno+"设备识别到了:"+license); Barrier barrier = barrierService.findByCode(code); if(barrier==null){ }else{ if(barrier.getType()==0){ outPark(license, barrier.getId(), barrier.getParkId()); }else{ open(request, response);//抬杆 enterPark(license,barrier.getId(),barrier.getParkId()); } } }while(false); } catch (JsonIOException e) { e.printStackTrace(); } catch (JsonSyntaxException e) { e.printStackTrace(); } catch (Exception e) { } } 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) { } } @Autowired private OutParkService outParkService; @Autowired private EnterParkService enterParkService; @Autowired private BarrierService barrierService; @Autowired private TicketService ticketService; @Autowired private CostRuleService costRuleService; public void enterPark(String carNo,Long barrierId,Long parkId) { enterParkService.deleteByCarNo(carNo,parkId); EnterPark enterPark = new EnterPark(); enterPark.setCreateTime(new Date()); enterPark.setCarNo(carNo); enterPark.setBarrierId(barrierId); enterPark.setParkId(parkId); Specification specification2 = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) { List list = new ArrayList(); list.add(cb.equal(root.get("carNo").as(String.class), enterPark.getCarNo())); list.add(cb.notEqual(root.get("payStatus"), 1)); Predicate[] arr = new Predicate[list.size()]; cq.where(list.toArray(arr)); return null; } }; List tickets = ticketService.findAll(specification2); if(tickets.size()>0){ enterPark.setStatus(1);//发现有违章 } enterParkService.save(enterPark); Barrier barrier = (Barrier) barrierService.findById(barrierId); barrier.setType2(1); barrierService.save(barrier); } public double outPark(String carNo,Long barrierId,Long parkId) { OutPark outPark = new OutPark(); outPark.setCarNo(carNo); outPark.setParkId(parkId); outPark.setBarrierId(barrierId); outPark.setCreateTime(new Date()); outPark.setCode(System.currentTimeMillis()+""); EnterPark enterPark = enterParkService.findByCarNo(carNo); if(enterPark==null){ }else{ outPark.setEnterTime(enterPark.getCreateTime()); } long l = outPark.getCreateTime().getTime() - enterPark.getCreateTime().getTime(); outPark.setTime(l/(1000*60)); double money = 0; try { money = costRuleService.getMoney(parkId, enterPark.getCreateTime(), outPark.getCreateTime(), 1); } catch (ParseException e) { e.printStackTrace(); } outPark.setPrice(Double.valueOf(String.format("%.1f", money))); outPark.setStatus3(findTicket(carNo)); outParkService.save(outPark); Barrier barrier = (Barrier) barrierService.findById(barrierId); barrier.setCarNo(carNo); if(outPark.getPrice()==0&&outPark.getStatus3()==0){ barrier.setType2(1); } barrierService.save(barrier); return money; } public int findTicket(String carNo){ Specification specification2 = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) { List list = new ArrayList(); list.add(cb.equal(root.get("carNo").as(String.class), carNo)); list.add(cb.notEqual(root.get("payStatus"), 1)); Predicate[] arr = new Predicate[list.size()]; cq.where(list.toArray(arr)); return null; } }; List tickets = ticketService.findAll(specification2); if(tickets.size()>0){ return 1; }else{ return 0; } } }