Commit 6287b042 authored by wangjinjing's avatar wangjinjing

websocket 版本

parent f6be2967
...@@ -7,6 +7,7 @@ import com.alibaba.fastjson.JSONObject; ...@@ -7,6 +7,7 @@ import com.alibaba.fastjson.JSONObject;
import com.cx.cn.cxquartz.bean.PatrolCtrlAlarm; import com.cx.cn.cxquartz.bean.PatrolCtrlAlarm;
import com.cx.cn.cxquartz.bean.PatrolCtrlRecord; import com.cx.cn.cxquartz.bean.PatrolCtrlRecord;
import com.cx.cn.cxquartz.bean.PictureTime; import com.cx.cn.cxquartz.bean.PictureTime;
import com.cx.cn.cxquartz.job.WebSocket;
import com.cx.cn.cxquartz.service.quartz.*; import com.cx.cn.cxquartz.service.quartz.*;
import com.cx.cn.cxquartz.util.*; import com.cx.cn.cxquartz.util.*;
import com.cx.cn.cxquartz.vo.*; import com.cx.cn.cxquartz.vo.*;
...@@ -94,6 +95,8 @@ public class ExtController { ...@@ -94,6 +95,8 @@ public class ExtController {
@Autowired @Autowired
PeopleridebicycService peopleridebicycService; PeopleridebicycService peopleridebicycService;
@Autowired
WebSocket webSocket;
private static CompletionService<PictureResult> threadService = new ExecutorCompletionService<PictureResult>(ThreadPoolUtil.getPool()); private static CompletionService<PictureResult> threadService = new ExecutorCompletionService<PictureResult>(ThreadPoolUtil.getPool());
...@@ -297,7 +300,6 @@ public class ExtController { ...@@ -297,7 +300,6 @@ public class ExtController {
//更新 recordalarm 为一分析 //更新 recordalarm 为一分析
traffAlarmRecordService.updateTraffAlarmRecordProcess(transferRecord); traffAlarmRecordService.updateTraffAlarmRecordProcess(transferRecord);
for (TraffpictureParam traffpictureParam : objectList) { for (TraffpictureParam traffpictureParam : objectList) {
//根据imageid 获得 base64图片 //根据imageid 获得 base64图片
JSONObject metadata = JSONObject.parseObject(String.valueOf(traffpictureParam.getMetadata())); JSONObject metadata = JSONObject.parseObject(String.valueOf(traffpictureParam.getMetadata()));
traffpictureParam.setAreaid(transferRecord.getAreaid()); traffpictureParam.setAreaid(transferRecord.getAreaid());
...@@ -337,6 +339,7 @@ public class ExtController { ...@@ -337,6 +339,7 @@ public class ExtController {
meta.setId(traffpictureParam.getId()); meta.setId(traffpictureParam.getId());
//新增到车辆详情表 //新增到车辆详情表
trafficService.insertTraffic(meta); trafficService.insertTraffic(meta);
if(null!=meta.getObjectBoundingBox())
traffpictureParam.setObjx(meta.getObjectBoundingBox().getX()); traffpictureParam.setObjx(meta.getObjectBoundingBox().getX());
traffpictureParam.setObjy(meta.getObjectBoundingBox().getY()); traffpictureParam.setObjy(meta.getObjectBoundingBox().getY());
traffpictureParam.setObjw(meta.getObjectBoundingBox().getW()); traffpictureParam.setObjw(meta.getObjectBoundingBox().getW());
...@@ -368,7 +371,7 @@ public class ExtController { ...@@ -368,7 +371,7 @@ public class ExtController {
ByteArrayInputStream bais = new ByteArrayInputStream(base64Decoder.decodeBuffer(traffpictureParam.getImagedata())); ByteArrayInputStream bais = new ByteArrayInputStream(base64Decoder.decodeBuffer(traffpictureParam.getImagedata()));
BufferedImage image = ImageIO.read(bais); BufferedImage image = ImageIO.read(bais);
Graphics g = image.getGraphics(); Graphics g = image.getGraphics();
g.setColor(Color.RED);//画笔颜色 g.setColor(Color.GREEN);//画笔颜色
g.drawRect(traffpictureParam.getObjx(), traffpictureParam.getObjy(), traffpictureParam.getObjw(), traffpictureParam.getObjh()); g.drawRect(traffpictureParam.getObjx(), traffpictureParam.getObjy(), traffpictureParam.getObjw(), traffpictureParam.getObjh());
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageIO.write(image, "png", stream); ImageIO.write(image, "png", stream);
...@@ -379,8 +382,9 @@ public class ExtController { ...@@ -379,8 +382,9 @@ public class ExtController {
logger.info("base64画框异常:"+ex.toString()); logger.info("base64画框异常:"+ex.toString());
} }
traffPictureService.updateTraffpicture(traffpictureParam); traffPictureService.updateTraffpicture(traffpictureParam);
WebSocket.GroupSending(JSONObject.toJSONString(traffpictureParam));
} }
} }
......
package com.cx.cn.cxquartz.job;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@Component
@ServerEndpoint("/websocket/{name}")
public class WebSocket {
/**
* 与某个客户端的连接对话,需要通过它来给客户端发送消息
*/
private Session session;
/**
* 标识当前连接客户端的用户名
*/
private String name;
/**
* 用于存所有的连接服务的客户端,这个对象存储是安全的
*/
private static ConcurrentHashMap<String, WebSocket> webSocketSet = new ConcurrentHashMap<>();
@OnOpen
public void OnOpen(Session session, @PathParam(value = "name") String name) {
this.session = session;
this.name = name;
// name是用来表示唯一客户端,如果需要指定发送,需要指定发送通过name来区分
webSocketSet.put(name, this);
}
@OnClose
public void OnClose() {
webSocketSet.remove(this.name);
}
@OnError
public void OnError(@PathParam("name") String name, Throwable throwable, Session session) {
webSocketSet.remove(name);
}
@OnMessage
public void OnMessage(String message) {
//判断是否需要指定发送,具体规则自定义
if(message.indexOf("HEARTBEAT")>=0){
Map map=new HashMap();
map.put("type","HEARTBEAT");
map.put("ts",new Date().getTime());
AppointSending(name, JSONObject.toJSONString(map));
}
}
/**
* 群发
*
* @param message
*/
public static void GroupSending(String message) {
for (String name : webSocketSet.keySet()) {
try {
if (null != webSocketSet.get(name) && null != webSocketSet.get(name).session && null != webSocketSet.get(name).session.getBasicRemote())
webSocketSet.get(name).session.getBasicRemote().sendText(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 指定发送
*
* @param name
* @param message
*/
public void AppointSending(String name, String message) {
if (null != webSocketSet.get(name) && null != webSocketSet.get(name).session && null != webSocketSet.get(name).session.getBasicRemote()) {
synchronized (webSocketSet.get(name).session) {
try {
webSocketSet.get(name).session.getBasicRemote().sendText(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
...@@ -42,10 +42,10 @@ ...@@ -42,10 +42,10 @@
<trim prefix="(" suffix=")" suffixOverrides=","> <trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id !=null">id,</if> <if test="id !=null">id,</if>
<if test="Type !=null"> type ,</if> <if test="Type !=null"> type ,</if>
<if test="ObjectBoundingBox.x !=null"> objectx ,</if> <if test="ObjectBoundingBox!=null and ObjectBoundingBox.x !=null"> objectx ,</if>
<if test="ObjectBoundingBox.y !=null"> objecty ,</if> <if test="ObjectBoundingBox!=null and ObjectBoundingBox.y !=null"> objecty ,</if>
<if test="ObjectBoundingBox.w !=null"> objectw ,</if> <if test="ObjectBoundingBox!=null and ObjectBoundingBox.w !=null"> objectw ,</if>
<if test="ObjectBoundingBox.h !=null"> objecth ,</if> <if test="ObjectBoundingBox!=null and ObjectBoundingBox.h !=null"> objecth ,</if>
<if test="VehicleClass !=null"> vehicleclass ,</if> <if test="VehicleClass !=null"> vehicleclass ,</if>
<if test="VehicleColorNums !=null"> vehiclecolornums ,</if> <if test="VehicleColorNums !=null"> vehiclecolornums ,</if>
<if test="VehicleColor !=null"> vehiclecolor ,</if> <if test="VehicleColor !=null"> vehiclecolor ,</if>
...@@ -76,10 +76,10 @@ ...@@ -76,10 +76,10 @@
<trim prefix="values (" suffix=")" suffixOverrides=","> <trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id !=null">#{id} ,</if> <if test="id !=null">#{id} ,</if>
<if test="Type !=null">#{Type} ,</if> <if test="Type !=null">#{Type} ,</if>
<if test="ObjectBoundingBox.x !=null">#{ObjectBoundingBox.x } ,</if> <if test="ObjectBoundingBox!=null and ObjectBoundingBox.x !=null">#{ObjectBoundingBox.x } ,</if>
<if test="ObjectBoundingBox.y !=null">#{ObjectBoundingBox.y} ,</if> <if test="ObjectBoundingBox!=null and ObjectBoundingBox.y !=null">#{ObjectBoundingBox.y} ,</if>
<if test="ObjectBoundingBox.w !=null">#{ObjectBoundingBox.w} ,</if> <if test="ObjectBoundingBox!=null and ObjectBoundingBox.w !=null">#{ObjectBoundingBox.w} ,</if>
<if test="ObjectBoundingBox.h !=null">#{ObjectBoundingBox.h} ,</if> <if test="ObjectBoundingBox!=null and ObjectBoundingBox.h !=null">#{ObjectBoundingBox.h} ,</if>
<if test="VehicleClass !=null">#{VehicleClass} ,</if> <if test="VehicleClass !=null">#{VehicleClass} ,</if>
<if test="VehicleColorNums !=null">#{VehicleColorNums} ,</if> <if test="VehicleColorNums !=null">#{VehicleColorNums} ,</if>
<if test="VehicleColor !=null">#{VehicleColor} ,</if> <if test="VehicleColor !=null">#{VehicleColor} ,</if>
...@@ -98,8 +98,8 @@ ...@@ -98,8 +98,8 @@
<if test="Decoration !=null">#{Decoration} ,</if> <if test="Decoration !=null">#{Decoration} ,</if>
<if test="Drop !=null">#{Drop},</if> <if test="Drop !=null">#{Drop},</if>
<if test="Tag !=null">#{Tag},</if> <if test="Tag !=null">#{Tag},</if>
<if test="SafetyBelt.MainDriver !=null">#{SafetyBelt.MainDriver} ,</if> <if test="SafetyBelt!=null and SafetyBelt.MainDriver !=null">#{SafetyBelt.MainDriver} ,</if>
<if test="SafetyBelt.CoDriver !=null">#{SafetyBelt.CoDriver},</if> <if test="SafetyBelt!=null and SafetyBelt.CoDriver !=null">#{SafetyBelt.CoDriver},</if>
<if test="HasCall !=null">#{HasCall} ,</if> <if test="HasCall !=null">#{HasCall} ,</if>
<if test="HasCrash !=null">#{HasCrash} ,</if> <if test="HasCrash !=null">#{HasCrash} ,</if>
<if test="HasDanger !=null">#{HasDanger} ,</if> <if test="HasDanger !=null">#{HasDanger} ,</if>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment