Commit 09b7207b authored by zhouts's avatar zhouts

websocket

parent f6be2967
......@@ -60,7 +60,6 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
......@@ -68,7 +67,6 @@
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
......@@ -153,6 +151,16 @@
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>
</dependencies>
......
......@@ -2,6 +2,7 @@ package com.cx.cn.cxquartz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
......
package com.cx.cn.cxquartz.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
// @Configuration
@EnableCaching //开启注解
public class RedisConfig extends CachingConfigurerSupport {
/**
* retemplate相关配置
*
* @param factory
* @return
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 配置连接工厂
template.setConnectionFactory(factory);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
// 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSeial.setObjectMapper(om);
// 值采用json序列化
template.setValueSerializer(jacksonSeial);
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
// 设置hash key 和value序列化模式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jacksonSeial);
template.afterPropertiesSet();
return template;
}
/**
* 对hash类型的数据操作
*
* @param redisTemplate
* @return
*/
@Bean
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForHash();
}
/**
* 对redis字符串类型数据操作
*
* @param redisTemplate
* @return
*/
@Bean
public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForValue();
}
@Bean
public JedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setHostName("<server-hostname-here>");
jedisConnectionFactory.setPort(6379);
jedisConnectionFactory.setPassword("<server-password-here>");
jedisConnectionFactory.afterPropertiesSet();
return jedisConnectionFactory;
}
/**
* 对链表类型的数据操作
*
* @param redisTemplate
* @return
*/
@Bean
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForList();
}
/**
* 对无序集合类型的数据操作
*
* @param redisTemplate
* @return
*/
@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
}
/**
* 对有序集合类型的数据操作
*
* @param redisTemplate
* @return
*/
@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}
}
package com.cx.cn.cxquartz.config;
import com.cx.cn.cxquartz.job.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfig {
/**
* ServerEndpointExporter 作用
*
* 这个Bean会自动注册使用@ServerEndpoint注解声明的websocket endpoint
*
* @return
*/
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
\ No newline at end of file
......@@ -7,6 +7,7 @@ import com.alibaba.fastjson.JSONObject;
import com.cx.cn.cxquartz.bean.PatrolCtrlAlarm;
import com.cx.cn.cxquartz.bean.PatrolCtrlRecord;
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.util.*;
import com.cx.cn.cxquartz.vo.*;
......@@ -94,7 +95,8 @@ public class ExtController {
@Autowired
PeopleridebicycService peopleridebicycService;
@Autowired
WebSocket webSocket;
private static CompletionService<PictureResult> threadService = new ExecutorCompletionService<PictureResult>(ThreadPoolUtil.getPool());
@RequestMapping(value = "/patrolCtrlRecord", method = RequestMethod.POST)
......@@ -282,22 +284,22 @@ public class ExtController {
BASE64Encoder base64Encoder = new BASE64Encoder();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<String> requestEntity=null;
Map websocketmap=new HashMap();
for (TraffAlarmRecord transferRecord : traffalarmrecordlist) {
count = 0;
GoalStructureParam param = FileTransferManager.getGoalStructureParam(count, Integer.parseInt(model == null ? "1" : model.equals("") ? "1" : model), base64Encoder, transferRecord);
if(param.getImageList().size()<1)continue;
requestEntity= new HttpEntity<String>(JSONObject.toJSONString(param), headers);
ResponseEntity<String> response = restTemplate.exchange(recogurl, HttpMethod.POST, requestEntity, String.class);
String body = response.getBody();
JSONObject result = JSONObject.parseObject(body);
JSONObject result = JSONObject.parseObject(response.getBody());
if (null != result.get("ret") && result.get("ret").equals("200")) {
//获得返回结果,根据 Metadata.type 判断是人,车,人骑车,并将详细信息入对应的表
List<TraffpictureParam> objectList = (List<TraffpictureParam>) JSONArray.parseArray(String.valueOf(result.get("ObjectList")), TraffpictureParam.class);
//获得 type
//更新 recordalarm 为一分析
traffAlarmRecordService.updateTraffAlarmRecordProcess(transferRecord);
for (TraffpictureParam traffpictureParam : objectList) {
//根据imageid 获得 base64图片
JSONObject metadata = JSONObject.parseObject(String.valueOf(traffpictureParam.getMetadata()));
traffpictureParam.setAreaid(transferRecord.getAreaid());
......@@ -316,10 +318,16 @@ public class ExtController {
traffpictureParam.setImagedata(base64Encoder.encode(Img));
}
}
traffpictureParam.setCreatetime(DateUtils.getNowDate());
//新增到picture
int id = traffPictureService.inserTraffpicture(traffpictureParam);
//将新增的结果返回到前端
websocketmap.clear();
websocketmap.put("data",traffpictureParam);
websocketmap.put("id",traffpictureParam.getId());
websocketmap.put("recordtime",DateUtils.formatDateTime(traffpictureParam.getCreatetime()));
websocketmap.put("videdoid",traffpictureParam.getFdid()+"_"+traffpictureParam.getFdid());
WebSocket.GroupSending(JSONObject.toJSONString(websocketmap));
if (null != metadata && metadata.get("Type").equals("1"))//行人
{
Pedestrian meta = JSON.toJavaObject(metadata, Pedestrian.class);
......@@ -357,10 +365,12 @@ public class ExtController {
PeopleRideBicyc meta = JSON.toJavaObject(metadata, PeopleRideBicyc.class);
meta.setId(traffpictureParam.getId());
peopleridebicycService.insertPeopleRideBicyc(meta);
traffpictureParam.setObjx(meta.getObjectBoundingBox().getX());
traffpictureParam.setObjy(meta.getObjectBoundingBox().getY());
traffpictureParam.setObjw(meta.getObjectBoundingBox().getW());
traffpictureParam.setObjh(meta.getObjectBoundingBox().getH());
if(null!=meta.getObjectBoundingBox()) {
traffpictureParam.setObjx(meta.getObjectBoundingBox().getX());
traffpictureParam.setObjy(meta.getObjectBoundingBox().getY());
traffpictureParam.setObjw(meta.getObjectBoundingBox().getW());
traffpictureParam.setObjh(meta.getObjectBoundingBox().getH());
}
}
//更新 traffpicture特征值
......@@ -378,8 +388,6 @@ public class ExtController {
logger.info("base64画框异常:"+ex.toString());
}
traffPictureService.updateTraffpicture(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 ApplicationContext applicationContext;
//
// public static void setApplicationContext(ApplicationContext context){
// applicationContext=context;
// }
/**
* 用于存所有的连接服务的客户端,这个对象存储是安全的
*/
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();
}
}
}
}
}
......@@ -51,6 +51,11 @@ public class DateUtils {
return new DateTime(date).toString(YMD_HMS);
}
public static String formatDateTime(DateTime date){
return date.toString(YMD_HMS);
}
public static String formatDateToNoSign(Date date){
return new DateTime(date).toString(YMDHMS);
}
......@@ -62,7 +67,10 @@ public class DateUtils {
private static String formatCurrDateByType(DateTimeFormatter dateTimeFormatter){
return DateTime.now().toString(dateTimeFormatter);
}
public static DateTime getNowDate(){
return DateTime.now();
}
public static Date addMinutes(Date date, int minutes) {
return new DateTime(date).plusMinutes(minutes).toDate();
}
......
......@@ -147,7 +147,7 @@ public class FileTransferManager {
GoalStructureParam param = new GoalStructureParam();
param.setOutput(new Output(1, 1, -1, 3));
param.setModel(model);
param.setApiout("1");////打开1400标准输出,默认可以不填
param.setApiout("0");////打开1400标准输出,默认可以不填
List<ImageList> list = new ArrayList<>();
getImageList("1", base64Encoder, list, transferRecord.getImg1path());
getImageList("2", base64Encoder, list, transferRecord.getImg2path());
......
......@@ -2,6 +2,7 @@ package com.cx.cn.cxquartz.vo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.joda.time.DateTime;
import java.io.Serializable;
import java.util.Date;
......@@ -17,7 +18,6 @@ import java.util.Date;
public class TraffpictureParam extends Traffpicture {
private static final long serialVersionUID=1L;
@JsonIgnore
private Long recordid;
@JsonIgnore
......@@ -37,7 +37,7 @@ public class TraffpictureParam extends Traffpicture {
@JsonIgnore
private String creator;
@JsonIgnore
private Date createtime;
private DateTime createtime;
@JsonIgnore
private String updator;
@JsonIgnore
......@@ -156,11 +156,11 @@ public class TraffpictureParam extends Traffpicture {
this.creator = creator;
}
public Date getCreatetime() {
public DateTime getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
public void setCreatetime(DateTime createtime) {
this.createtime = createtime;
}
......
......@@ -2,13 +2,6 @@
* Copyright 2021 json.cn
*/
package com.cx.cn.cxquartz.vo;
/**
* Auto-generated: 2021-04-28 19:16:46
*
* @author json.cn (i@json.cn)
* @website http://www.json.cn/java2pojo/
*/
public class UpperBoundingBox {
private int x;
......
......@@ -37,8 +37,18 @@ spring:
cache: false
enabled: true
encoding: UTF-8
mode: HTML
mode : HTML
redis:
host: 172.16.24.153
port: 6379
timeout: 300
# password:
pool:
minIdle: 1
maxIdle: 10
maxWait : -1
maxActive: 8
#logging:
# level:
# root:
......
......@@ -5,3 +5,4 @@ file.alarmurl=http://www.zjwwzf.cn/xzzfSpv/ext/alarm/camera
file.rtspurl=http://172.16.24.153:8081/getrealcamerasnapshot.php
file.recogurl=http://172.16.24.153:9098/images/recog
redis.cachekey.ftplist=gs:traff:global:cache:ftplist
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