parent
fe47b252d0
commit
199a6bb99b
38 changed files with 1343 additions and 12 deletions
@ -0,0 +1,31 @@ |
||||
# maven # |
||||
target |
||||
|
||||
logs |
||||
|
||||
# windows # |
||||
Thumbs.db |
||||
|
||||
# Mac # |
||||
.DS_Store |
||||
|
||||
# eclipse # |
||||
.settings |
||||
.project |
||||
.classpath |
||||
.log |
||||
*.class |
||||
|
||||
# idea # |
||||
.idea |
||||
*.iml |
||||
|
||||
# Package Files # |
||||
*.jar |
||||
*.war |
||||
*.ear |
||||
/target |
||||
|
||||
/applogs |
||||
.editorconfig |
||||
|
||||
@ -0,0 +1,128 @@ |
||||
package org.springblade.modules.business.controller; |
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
import com.baomidou.mybatisplus.core.metadata.IPage; |
||||
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import lombok.RequiredArgsConstructor; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.commons.collections.CollectionUtils; |
||||
import org.springblade.common.constant.CommonConstant; |
||||
import org.springblade.core.boot.ctrl.BladeController; |
||||
import org.springblade.core.mp.support.Condition; |
||||
import org.springblade.core.mp.support.Query; |
||||
import org.springblade.core.tenant.annotation.TenantDS; |
||||
import org.springblade.core.tool.api.R; |
||||
import org.springblade.modules.business.entity.CarAdmin; |
||||
import org.springblade.modules.business.entity.CarLiveChannel; |
||||
import org.springblade.modules.business.service.ICarAdminService; |
||||
import org.springblade.modules.business.service.ICarLiveChannelService; |
||||
import org.springblade.modules.business.vo.CarAdminVo; |
||||
import org.springblade.modules.business.vo.CarLiveChannelVo; |
||||
import org.springframework.beans.BeanUtils; |
||||
import org.springframework.transaction.annotation.Transactional; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
import javax.validation.Valid; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 控制器 |
||||
* |
||||
* @author chents |
||||
*/ |
||||
@TenantDS |
||||
@Slf4j |
||||
@RestController |
||||
@RequestMapping(CommonConstant.APPLICATION_BUSINESS_NAME + "/car-admin") |
||||
@RequiredArgsConstructor |
||||
@Api(value = "车辆管理接口", tags = "车辆管理接口") |
||||
public class CarAdminController extends BladeController { |
||||
|
||||
private final ICarAdminService carService; |
||||
|
||||
private final ICarLiveChannelService carLiveChannelService; |
||||
|
||||
/** |
||||
* 根据车牌号查询列表--停用 |
||||
* |
||||
* @return |
||||
*/ |
||||
@ApiOperationSupport(order = 1) |
||||
@ApiOperation(value = "根据车牌号查询列表", notes = "传入车牌号") |
||||
@GetMapping("/get-list-by-name") |
||||
public R getListByName(String name) { |
||||
return carService.getListByName(name); |
||||
} |
||||
|
||||
/** |
||||
* 新增或修改 |
||||
*/ |
||||
@PostMapping("/submit") |
||||
@ApiOperationSupport(order = 2) |
||||
@ApiOperation(value = "新增或修改", notes = "传入carAdmin") |
||||
@Transactional(rollbackFor = Exception.class) |
||||
public R submit(@Valid @RequestBody CarAdminVo carAdminVo) { |
||||
CarAdmin carAdmin = new CarAdmin(); |
||||
BeanUtils.copyProperties(carAdminVo, carAdmin); |
||||
boolean success = carService.saveOrUpdate(carAdmin); |
||||
if (success) { |
||||
//保存或更新直播通道信息
|
||||
List<CarLiveChannelVo> carLiveChannelVoList = carAdminVo.getCarLiveChannelVoList(); |
||||
if (CollectionUtils.isNotEmpty(carLiveChannelVoList)) { |
||||
List<CarLiveChannel> carLiveChannelList = new ArrayList<>(); |
||||
for (CarLiveChannelVo carLiveChannelVo : carLiveChannelVoList) { |
||||
CarLiveChannel carLiveChannel = new CarLiveChannel(); |
||||
BeanUtils.copyProperties(carLiveChannelVo, carLiveChannel); |
||||
carLiveChannel.setCarId(carAdmin.getId()); |
||||
carLiveChannel.setCode(carLiveChannelVo.getCode()); |
||||
carLiveChannel.setStatus(carLiveChannelVo.getStatus()); |
||||
carLiveChannel.setChannelName(carLiveChannelVo.getChannelName()); |
||||
carLiveChannelList.add(carLiveChannel); |
||||
} |
||||
carLiveChannelService.saveOrUpdateBatch(carLiveChannelList); |
||||
} |
||||
//更新车辆信息下的直播通道数量
|
||||
carAdmin.setAmount((long) carLiveChannelVoList.size()); |
||||
carService.updateById(carAdmin); |
||||
return R.success("true"); |
||||
} |
||||
return R.fail("保存或更新失败"); |
||||
} |
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/** |
||||
* 删除 |
||||
* |
||||
* @return |
||||
*/ |
||||
@ApiOperationSupport(order = 3) |
||||
@ApiOperation(value = "删除", notes = "传入id") |
||||
@PostMapping("/delete") |
||||
public R<Boolean> delete(@RequestParam String ids) { |
||||
return R.status(carService.delete(ids)); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 分页 车辆信息表 |
||||
*/ |
||||
@GetMapping("/list") |
||||
@ApiOperationSupport(order = 4) |
||||
@ApiOperation(value = "分页", notes = "传入carAdmin和query") |
||||
public R<IPage<CarAdminVo>> list(CarAdmin carAdmin, Query query) { |
||||
IPage<CarAdminVo> pages = carService.selectCarAdminPage(Condition.getPage(query), carAdmin); |
||||
return R.data(pages); |
||||
} |
||||
|
||||
|
||||
} |
||||
|
||||
@ -0,0 +1,70 @@ |
||||
package org.springblade.modules.business.controller; |
||||
|
||||
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import lombok.RequiredArgsConstructor; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springblade.common.constant.CommonConstant; |
||||
import org.springblade.core.boot.ctrl.BladeController; |
||||
import org.springblade.core.tenant.annotation.TenantDS; |
||||
import org.springblade.core.tool.api.R; |
||||
import org.springblade.modules.business.entity.CarLiveChannel; |
||||
import org.springblade.modules.business.service.ICarLiveChannelService; |
||||
import org.springblade.modules.business.vo.CarLiveChannelVo; |
||||
import org.springframework.beans.BeanUtils; |
||||
import org.springframework.transaction.annotation.Transactional; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
import javax.validation.Valid; |
||||
|
||||
/** |
||||
* 控制器 |
||||
* |
||||
* @author chents |
||||
*/ |
||||
@TenantDS |
||||
@Slf4j |
||||
@RestController |
||||
@RequestMapping(CommonConstant.APPLICATION_BUSINESS_NAME + "/car-live-channel") |
||||
@RequiredArgsConstructor |
||||
@Api(value = "直播通道接口", tags = "车辆管理接口") |
||||
public class CarLiveChannelController extends BladeController { |
||||
|
||||
private final ICarLiveChannelService carLiveChannelService; |
||||
|
||||
|
||||
/** |
||||
* 根据id查询 |
||||
* |
||||
* @return |
||||
*/ |
||||
@ApiOperationSupport(order = 1) |
||||
@ApiOperation(value = "根据id查询", notes = "传入id") |
||||
@GetMapping("get-channel-by-id/{id}") |
||||
public R getChannelById(@PathVariable(value = "id") Long id) { |
||||
return carLiveChannelService.getChannelById(id); |
||||
} |
||||
|
||||
/** |
||||
* 修改直播通道 |
||||
*/ |
||||
@PutMapping("/updateLiveChannel") |
||||
@ApiOperationSupport(order = 3) |
||||
@ApiOperation(value = "修改直播通道", notes = "传入carLiveChannel") |
||||
@Transactional(rollbackFor = Exception.class) |
||||
public R updateLiveChannel(@Valid @RequestBody CarLiveChannelVo carLiveChannelVo) { |
||||
CarLiveChannel carLiveChannel = carLiveChannelService.getById(carLiveChannelVo.getId()); |
||||
if (carLiveChannel == null) { |
||||
return R.fail("该直播通道不存在"); |
||||
} |
||||
BeanUtils.copyProperties(carLiveChannelVo, carLiveChannel); |
||||
boolean success = carLiveChannelService.updateById(carLiveChannel); |
||||
if (success) { |
||||
return R.success("true"); |
||||
} |
||||
return R.fail("更新失败"); |
||||
} |
||||
|
||||
} |
||||
|
||||
@ -0,0 +1,105 @@ |
||||
package org.springblade.modules.business.controller; |
||||
|
||||
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import lombok.RequiredArgsConstructor; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springblade.common.constant.CommonConstant; |
||||
import org.springblade.core.boot.ctrl.BladeController; |
||||
import org.springblade.core.tenant.annotation.TenantDS; |
||||
import org.springblade.core.tool.api.R; |
||||
import org.springblade.modules.business.entity.BusLine; |
||||
import org.springblade.modules.business.service.IStationHintService; |
||||
import org.springblade.modules.business.vo.BusLineUpdateVo; |
||||
import org.springblade.modules.business.vo.StationHintVo; |
||||
import org.springblade.upload.entity.PocBusLineImg; |
||||
import org.springframework.validation.annotation.Validated; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 控制器 |
||||
* |
||||
* @author chents |
||||
*/ |
||||
@TenantDS |
||||
@Slf4j |
||||
@RestController |
||||
@RequestMapping(CommonConstant.APPLICATION_BUSINESS_NAME + "/stationHint") |
||||
@RequiredArgsConstructor |
||||
@Api(value = "站点提示信息管理接口", tags = "站点提示信息管理接口") |
||||
public class StationHintController extends BladeController { |
||||
|
||||
private final IStationHintService stationHintService; |
||||
|
||||
|
||||
/** |
||||
* 查询站点列表 |
||||
* |
||||
* @return |
||||
*/ |
||||
@ApiOperationSupport(order = 1) |
||||
@ApiOperation(value = "查询站点列表", notes = "传入linename和updown") |
||||
@GetMapping("query-station-hint") |
||||
public R QueryStationHint(String linename,String updown) { |
||||
return stationHintService.QueryStationHint(linename,updown); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 查询站点信息展示列表 |
||||
* |
||||
* @return |
||||
*/ |
||||
@ApiOperationSupport(order = 2) |
||||
@ApiOperation(value = "查询站点信息展示列表", notes = "传入linename和updown") |
||||
@GetMapping("query-station-message") |
||||
public R<List<StationHintVo>> QueryStationMessage(String linename, String updown) { |
||||
return stationHintService.QueryStationMessage(linename,updown); |
||||
} |
||||
|
||||
|
||||
|
||||
/** |
||||
* 删除--停用 |
||||
* |
||||
* @return |
||||
*/ |
||||
@ApiOperationSupport(order = 3) |
||||
@ApiOperation(value = "删除--停用", notes = "传入id") |
||||
@PostMapping("/delete") |
||||
public R<Boolean> delete(@RequestParam String ids) { |
||||
stationHintService.delete(ids); |
||||
return R.status(true); |
||||
} |
||||
|
||||
/** |
||||
* 根据id 查询 |
||||
* |
||||
* @param id |
||||
* @return |
||||
*/ |
||||
@ApiOperationSupport(order = 4) |
||||
@ApiOperation(value = "根据id查询", notes = "传入id") |
||||
@GetMapping(value = "/getById/{id}") |
||||
public R getById(@PathVariable(value = "id") Long id) { |
||||
BusLine busLine = stationHintService.getById(id); |
||||
return R.data(busLine); |
||||
} |
||||
|
||||
/** |
||||
* 编辑 |
||||
* |
||||
* @return |
||||
*/ |
||||
@ApiOperationSupport(order = 5) |
||||
@ApiOperation(value = "编辑", notes = "传入busLineUpdateVo") |
||||
@PostMapping("/update") |
||||
public R updateStationHint(@RequestBody @Validated BusLineUpdateVo busLineUpdateVo) { |
||||
stationHintService.updateStationHint(busLineUpdateVo); |
||||
return R.status(true); |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,44 @@ |
||||
package org.springblade.modules.business.entity; |
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType; |
||||
import com.baomidou.mybatisplus.annotation.TableId; |
||||
import com.baomidou.mybatisplus.annotation.TableName; |
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Data; |
||||
import lombok.EqualsAndHashCode; |
||||
import org.springblade.core.mp.base.BaseEntity; |
||||
|
||||
/** |
||||
* 车辆信息表 |
||||
* |
||||
* @author Chill |
||||
*/ |
||||
@Data |
||||
@EqualsAndHashCode(callSuper = true) |
||||
@TableName("car_admin") |
||||
public class CarAdmin extends BaseEntity { |
||||
|
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
@JsonSerialize( |
||||
using = ToStringSerializer.class |
||||
) |
||||
@ApiModelProperty("主键id") |
||||
@TableId( |
||||
value = "id", |
||||
type = IdType.ASSIGN_ID |
||||
) |
||||
private Long id; |
||||
|
||||
//车载机id
|
||||
private Long receiverId; |
||||
//公交车车牌
|
||||
private String name; |
||||
//直播状态:1在线 2车外摄像头 3车顶摄像头
|
||||
private Long liveStatus; |
||||
//直播数量
|
||||
private Long amount; |
||||
|
||||
} |
||||
@ -0,0 +1,45 @@ |
||||
package org.springblade.modules.business.entity; |
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType; |
||||
import com.baomidou.mybatisplus.annotation.TableId; |
||||
import com.baomidou.mybatisplus.annotation.TableName; |
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Data; |
||||
import lombok.EqualsAndHashCode; |
||||
import org.springblade.core.mp.base.BaseEntity; |
||||
|
||||
/** |
||||
* 文章表 |
||||
* |
||||
* @author Chill |
||||
*/ |
||||
@Data |
||||
@EqualsAndHashCode(callSuper = true) |
||||
@TableName("car_live_channel") |
||||
public class CarLiveChannel extends BaseEntity { |
||||
|
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
@JsonSerialize( |
||||
using = ToStringSerializer.class |
||||
) |
||||
@ApiModelProperty("主键id") |
||||
@TableId( |
||||
value = "id", |
||||
type = IdType.ASSIGN_ID |
||||
) |
||||
private Long id; |
||||
|
||||
private Integer code; |
||||
|
||||
private Long carId; |
||||
|
||||
//直播通道名称
|
||||
private String channelName; |
||||
|
||||
//直播通道状态:0默认 1不默认
|
||||
private Integer status; |
||||
|
||||
} |
||||
@ -0,0 +1,14 @@ |
||||
package org.springblade.modules.business.mapper; |
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||
import org.springblade.modules.business.entity.BusLine; |
||||
import org.springblade.modules.business.entity.CarAdmin; |
||||
|
||||
/** |
||||
* Mapper 接口 |
||||
* |
||||
* @author Chill |
||||
*/ |
||||
public interface CarAdminMapper extends BaseMapper<CarAdmin> { |
||||
|
||||
} |
||||
@ -0,0 +1,14 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
||||
<mapper namespace="org.springblade.modules.business.mapper.CarAdminMapper"> |
||||
|
||||
<!-- 通用查询映射结果 --> |
||||
<resultMap id="CarAdminResultMap" type="org.springblade.modules.business.entity.CarAdmin"> |
||||
<result column="id" property="id"/> |
||||
<result column="receiver_id" property="receiverId"/> |
||||
<result column="name" property="name"/> |
||||
<result column="live_status" property="liveStatus"/> |
||||
<result column="amount" property="amount"/> |
||||
</resultMap> |
||||
|
||||
</mapper> |
||||
@ -0,0 +1,14 @@ |
||||
package org.springblade.modules.business.mapper; |
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||
import org.springblade.modules.business.entity.CarAdmin; |
||||
import org.springblade.modules.business.entity.CarLiveChannel; |
||||
|
||||
/** |
||||
* Mapper 接口 |
||||
* |
||||
* @author Chill |
||||
*/ |
||||
public interface CarLiveChannelMapper extends BaseMapper<CarLiveChannel> { |
||||
|
||||
} |
||||
@ -0,0 +1,13 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
||||
<mapper namespace="org.springblade.modules.business.mapper.CarLiveChannelMapper"> |
||||
|
||||
<!-- 通用查询映射结果 --> |
||||
<resultMap id="CarAdminResultMap" type="org.springblade.modules.business.entity.CarLiveChannel"> |
||||
<result column="id" property="id"/> |
||||
<result column="car_id" property="carId"/> |
||||
<result column="channel_name" property="channelName"/> |
||||
<result column="code" property="code"/> |
||||
</resultMap> |
||||
|
||||
</mapper> |
||||
@ -0,0 +1,26 @@ |
||||
package org.springblade.modules.business.service; |
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage; |
||||
import org.springblade.core.mp.base.BaseService; |
||||
import org.springblade.core.tool.api.R; |
||||
import org.springblade.modules.business.entity.BusLine; |
||||
import org.springblade.modules.business.entity.CarAdmin; |
||||
import org.springblade.modules.business.vo.CarAdminVo; |
||||
import org.springblade.modules.business.vo.StationHintVo; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 服务类 |
||||
* |
||||
* @author chents |
||||
*/ |
||||
public interface ICarAdminService extends BaseService<CarAdmin> { |
||||
|
||||
R getListByName(String name); |
||||
|
||||
boolean delete(String ids); |
||||
|
||||
IPage<CarAdminVo> selectCarAdminPage(IPage<Object> page, CarAdmin carAdmin); |
||||
|
||||
} |
||||
@ -0,0 +1,16 @@ |
||||
package org.springblade.modules.business.service; |
||||
|
||||
import org.springblade.core.mp.base.BaseService; |
||||
import org.springblade.core.tool.api.R; |
||||
import org.springblade.modules.business.entity.CarAdmin; |
||||
import org.springblade.modules.business.entity.CarLiveChannel; |
||||
|
||||
/** |
||||
* 服务类 |
||||
* |
||||
* @author chents |
||||
*/ |
||||
public interface ICarLiveChannelService extends BaseService<CarLiveChannel> { |
||||
|
||||
R getChannelById(Long id); |
||||
} |
||||
@ -0,0 +1,32 @@ |
||||
package org.springblade.modules.business.service; |
||||
|
||||
import org.springblade.core.mp.base.BaseService; |
||||
import org.springblade.core.tool.api.R; |
||||
import org.springblade.modules.business.entity.BusLine; |
||||
import org.springblade.modules.business.vo.BusLineUpdateVo; |
||||
import org.springblade.modules.business.vo.StationHintVo; |
||||
import org.springblade.upload.entity.PocBusLineImg; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 服务类 |
||||
* |
||||
* @author chents |
||||
*/ |
||||
public interface IStationHintService extends BaseService<BusLine> { |
||||
|
||||
/** |
||||
*查询列表 |
||||
* @Author chents |
||||
* @return |
||||
**/ |
||||
R QueryStationHint(String linename,String updown); |
||||
|
||||
|
||||
R<List<StationHintVo>> QueryStationMessage(String linename, String updown); |
||||
|
||||
void updateStationHint(BusLineUpdateVo busLineUpdateVo); |
||||
|
||||
boolean delete(String ids); |
||||
} |
||||
@ -0,0 +1,125 @@ |
||||
package org.springblade.modules.business.service.impl; |
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
import com.baomidou.mybatisplus.core.metadata.IPage; |
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.springblade.core.log.exception.ServiceException; |
||||
import org.springblade.core.mp.base.BaseServiceImpl; |
||||
import org.springblade.core.tool.api.R; |
||||
import org.springblade.core.tool.utils.CollectionUtil; |
||||
import org.springblade.core.tool.utils.Func; |
||||
import org.springblade.core.tool.utils.StringUtil; |
||||
import org.springblade.modules.business.entity.CarAdmin; |
||||
import org.springblade.modules.business.entity.CarLiveChannel; |
||||
import org.springblade.modules.business.mapper.CarAdminMapper; |
||||
import org.springblade.modules.business.mapper.CarLiveChannelMapper; |
||||
import org.springblade.modules.business.service.ICarAdminService; |
||||
import org.springblade.modules.business.service.ICarLiveChannelService; |
||||
import org.springblade.modules.business.vo.*; |
||||
import org.springframework.beans.BeanUtils; |
||||
import org.springframework.stereotype.Service; |
||||
import org.springframework.transaction.annotation.Transactional; |
||||
import org.springframework.util.CollectionUtils; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.stream.Collectors; |
||||
|
||||
/** |
||||
* 服务实现类 |
||||
* |
||||
* @author chents |
||||
*/ |
||||
@Service |
||||
public class CarAdminServiceImpl extends BaseServiceImpl<CarAdminMapper, CarAdmin> implements ICarAdminService { |
||||
|
||||
@Resource |
||||
CarAdminMapper carAdminMapper; |
||||
|
||||
@Resource |
||||
CarLiveChannelMapper carLiveChannelMapper; |
||||
|
||||
@Resource |
||||
ICarLiveChannelService carLiveChannelService; |
||||
|
||||
@Override |
||||
public R getListByName(String name) { |
||||
QueryWrapper<CarAdmin> carAdminQueryWrapper = new QueryWrapper<>(); |
||||
carAdminQueryWrapper.like("name","%"+name+"%"); |
||||
List<CarAdmin> carAdmins = carAdminMapper.selectList(carAdminQueryWrapper); |
||||
if (CollectionUtils.isEmpty(carAdmins)){ |
||||
throw new ServiceException(StringUtil.format("车辆信息为空")); |
||||
} |
||||
List<CarAdminVo> carAdminArr = carAdmins.stream().map(x -> { |
||||
CarAdminVo carAdminVo = new CarAdminVo(); |
||||
carAdminVo.setId(x.getId()); |
||||
carAdminVo.setName(x.getName()); |
||||
carAdminVo.setReceiverId(x.getReceiverId()); |
||||
carAdminVo.setAmount(x.getAmount()); |
||||
QueryWrapper<CarLiveChannel> queryWrapper = new QueryWrapper<>(); |
||||
queryWrapper.select("channel_type"); |
||||
queryWrapper.eq("car_id",x.getId()); |
||||
List<CarLiveChannel> carLiveChannelList = carLiveChannelMapper.selectList(queryWrapper); |
||||
ArrayList<CarLiveChannelVo> carLiveChannelVoList = new ArrayList<>(); |
||||
for(CarLiveChannel carLiveChannel : carLiveChannelList){ |
||||
CarLiveChannelVo carLiveChannelVo = new CarLiveChannelVo(); |
||||
carLiveChannelVo.setId(carLiveChannel.getId()); |
||||
carLiveChannelVo.setChannelName(carLiveChannel.getChannelName()); |
||||
carLiveChannelVo.setStatus(carLiveChannel.getStatus()); |
||||
carLiveChannelVoList.add(carLiveChannelVo); |
||||
} |
||||
carAdminVo.setCarLiveChannelVoList(carLiveChannelVoList); |
||||
return carAdminVo; |
||||
}).collect(Collectors.toList()); |
||||
return R.data(carAdminArr); |
||||
} |
||||
|
||||
@Override |
||||
@Transactional(rollbackFor = Exception.class) |
||||
public boolean delete(String ids) { |
||||
return deleteLogic(Func.toLongList(ids)); |
||||
} |
||||
|
||||
/** |
||||
* 分页 车辆信息表 |
||||
*/ |
||||
@Override |
||||
public IPage<CarAdminVo> selectCarAdminPage(IPage<Object> page, CarAdmin carAdmin) { |
||||
QueryWrapper<CarAdmin> queryWrapper = new QueryWrapper<>(); |
||||
if (StringUtils.isNotBlank(carAdmin.getName())) { |
||||
queryWrapper.like("name", carAdmin.getName()); |
||||
} |
||||
QueryWrapper<CarAdmin> queryWrapper1 = new QueryWrapper<>(); |
||||
queryWrapper1.orderByDesc("create_time"); |
||||
IPage<CarAdmin> carAdminPage = new Page<>(page.getCurrent(), page.getSize()); |
||||
carAdminMapper.selectPage(carAdminPage, queryWrapper1); |
||||
List<CarAdmin> carAdminList = carAdminPage.getRecords(); |
||||
List<CarAdminVo> carAdminVoList = new ArrayList<>(); |
||||
for (CarAdmin admin : carAdminList) { |
||||
CarAdminVo carAdminVo = new CarAdminVo(); |
||||
BeanUtils.copyProperties(admin, carAdminVo); |
||||
QueryWrapper<CarLiveChannel> channelQueryWrapper = new QueryWrapper<>(); |
||||
channelQueryWrapper.eq("car_id", admin.getId()); |
||||
List<CarLiveChannel> carLiveChannelList = carLiveChannelMapper.selectList(channelQueryWrapper); |
||||
List<CarLiveChannelVo> carLiveChannelVoList = new ArrayList<>(); |
||||
for (CarLiveChannel carLiveChannel : carLiveChannelList) { |
||||
CarLiveChannelVo carLiveChannelVo = new CarLiveChannelVo(); |
||||
BeanUtils.copyProperties(carLiveChannel, carLiveChannelVo); |
||||
carLiveChannelVo.setChannelName(carLiveChannel.getChannelName()); |
||||
carLiveChannelVoList.add(carLiveChannelVo); |
||||
} |
||||
carAdminVo.setCarLiveChannelVoList(carLiveChannelVoList); |
||||
carAdminVoList.add(carAdminVo); |
||||
} |
||||
IPage<CarAdminVo> carAdminVoPage = new Page<>(); |
||||
carAdminVoPage.setRecords(carAdminVoList); |
||||
carAdminVoPage.setTotal(carAdminPage.getTotal()); |
||||
carAdminVoPage.setCurrent(carAdminPage.getCurrent()); |
||||
carAdminVoPage.setSize(carAdminPage.getSize()); |
||||
return carAdminVoPage; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,28 @@ |
||||
package org.springblade.modules.business.service.impl; |
||||
|
||||
import org.springblade.core.mp.base.BaseServiceImpl; |
||||
import org.springblade.core.tool.api.R; |
||||
import org.springblade.modules.business.entity.CarLiveChannel; |
||||
import org.springblade.modules.business.mapper.CarLiveChannelMapper; |
||||
import org.springblade.modules.business.service.ICarLiveChannelService; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import javax.annotation.Resource; |
||||
|
||||
/** |
||||
* 服务实现类 |
||||
* |
||||
* @author chents |
||||
*/ |
||||
@Service |
||||
public class CarLiveChannelImpl extends BaseServiceImpl<CarLiveChannelMapper,CarLiveChannel> implements ICarLiveChannelService { |
||||
|
||||
@Resource |
||||
CarLiveChannelMapper carLiveChannelMapper; |
||||
|
||||
@Override |
||||
public R getChannelById(Long id) { |
||||
CarLiveChannel carLiveChannel = carLiveChannelMapper.selectById(id); |
||||
return R.data(carLiveChannel); |
||||
} |
||||
} |
||||
@ -0,0 +1,137 @@ |
||||
package org.springblade.modules.business.service.impl; |
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
import org.springblade.core.log.exception.ServiceException; |
||||
import org.springblade.core.mp.base.BaseServiceImpl; |
||||
import org.springblade.core.tool.api.R; |
||||
import org.springblade.core.tool.utils.Func; |
||||
import org.springblade.core.tool.utils.StringUtil; |
||||
import org.springblade.modules.business.entity.BusLine; |
||||
import org.springblade.modules.business.mapper.BusLineMapper; |
||||
import org.springblade.modules.business.service.IStationHintService; |
||||
import org.springblade.modules.business.vo.BusLineUpdateVo; |
||||
import org.springblade.modules.business.vo.BusLineVo; |
||||
import org.springblade.modules.business.vo.StationHintVo; |
||||
import org.springblade.modules.business.vo.imgVo; |
||||
import org.springblade.upload.entity.PocBusLineImg; |
||||
import org.springblade.upload.mapper.ProBusLineImgMapper; |
||||
import org.springframework.data.redis.core.RedisTemplate; |
||||
import org.springframework.stereotype.Service; |
||||
import org.springframework.transaction.annotation.Transactional; |
||||
import org.springframework.util.CollectionUtils; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.stream.Collectors; |
||||
|
||||
/** |
||||
* 服务实现类 |
||||
* |
||||
* @author chents |
||||
*/ |
||||
@Service |
||||
public class StationHintServiceImpl extends BaseServiceImpl<BusLineMapper,BusLine> implements IStationHintService { |
||||
|
||||
@Resource |
||||
BusLineMapper busLineMapper; |
||||
|
||||
@Resource |
||||
RedisTemplate redisTemplate; |
||||
|
||||
@Resource |
||||
ProBusLineImgMapper proBusLineImgMapper; |
||||
|
||||
|
||||
@Override |
||||
public R QueryStationHint(String linename,String updown) { |
||||
QueryWrapper<BusLine> busLineQueryWrapper = new QueryWrapper<>(); |
||||
busLineQueryWrapper.eq("linename",linename); |
||||
busLineQueryWrapper.eq("updown",updown); |
||||
busLineQueryWrapper.isNotNull("sname_key"); |
||||
List<BusLine> busLines = busLineMapper.selectList(busLineQueryWrapper); |
||||
List<BusLineVo> busLineVoList = busLines.stream().map(x -> { |
||||
BusLineVo busLineVo = new BusLineVo(); |
||||
busLineVo.setId(x.getId()); |
||||
busLineVo.setLinename(x.getLinename()); |
||||
busLineVo.setUpdown(x.getUpdown()); |
||||
busLineVo.setSnameKey(x.getSnameKey()); |
||||
return busLineVo; |
||||
}).collect(Collectors.toList()); |
||||
return R.data(busLineVoList); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public R<List<StationHintVo>> QueryStationMessage(String linename, String updown) { |
||||
QueryWrapper<BusLine> busLineQueryWrapper = new QueryWrapper<>(); |
||||
busLineQueryWrapper.eq("linename",linename); |
||||
busLineQueryWrapper.eq("updown",updown); |
||||
busLineQueryWrapper.isNotNull("sname_key"); |
||||
List<BusLine> busLines = busLineMapper.selectList(busLineQueryWrapper); |
||||
if (CollectionUtils.isEmpty(busLines)){ |
||||
throw new ServiceException(StringUtil.format("公交站点信息为空")); |
||||
} |
||||
List<StationHintVo> stationArr = busLines.stream().map(x -> { |
||||
StationHintVo stationHintVo = new StationHintVo(); |
||||
stationHintVo.setId(x.getId()); |
||||
stationHintVo.setName(x.getSnameKey()); |
||||
stationHintVo.setTips(x.getTips()); |
||||
stationHintVo.setRed(x.getActicle()); |
||||
stationHintVo.setGreen(x.getActivity()); |
||||
stationHintVo.setYellow(x.getScenic()); |
||||
QueryWrapper<PocBusLineImg> queryWrapper = new QueryWrapper<>(); |
||||
queryWrapper.select("img_url","img_type"); |
||||
queryWrapper.eq("bus_line_id",x.getId()); |
||||
List<PocBusLineImg> pocBusLineImgs = proBusLineImgMapper.selectList(queryWrapper); |
||||
ArrayList<imgVo> imgList = new ArrayList<>(); |
||||
for(PocBusLineImg pocBusLineImg : pocBusLineImgs){ |
||||
imgVo img = new imgVo(); |
||||
img.setImgUrl(pocBusLineImg.getImgUrl()); |
||||
img.setImgType(pocBusLineImg.getImgType()); |
||||
imgList.add(img); |
||||
} |
||||
stationHintVo.setImgList(imgList); |
||||
return stationHintVo; |
||||
}).collect(Collectors.toList()); |
||||
return R.data(stationArr); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
@Transactional(rollbackFor = Exception.class) |
||||
public void updateStationHint(BusLineUpdateVo busLineUpdateVo) { |
||||
BusLine busLine = busLineUpdateVo.getBusLine(); |
||||
List<PocBusLineImg> imgList = busLineUpdateVo.getImgList(); |
||||
// 校验存在
|
||||
BusLine existingBusLine = busLineMapper.selectById(busLine.getId()); |
||||
if (existingBusLine == null) { |
||||
throw new RuntimeException("公交线路 不存在"); |
||||
} |
||||
// 删除该线路对应的所有图片
|
||||
QueryWrapper<PocBusLineImg> wrapper = new QueryWrapper<>(); |
||||
wrapper.eq("bus_line_id", busLine.getId()); |
||||
proBusLineImgMapper.delete(wrapper); |
||||
// 插入新的图片列表
|
||||
if (imgList != null && !imgList.isEmpty()) { |
||||
for (PocBusLineImg img : imgList) { |
||||
img.setBusLineId(busLine.getId()); |
||||
proBusLineImgMapper.insert(img); |
||||
} |
||||
} |
||||
busLineMapper.updateById(busLine); |
||||
} |
||||
|
||||
@Override |
||||
@Transactional(rollbackFor = Exception.class) |
||||
public boolean delete(String ids) { |
||||
return deleteLogic(Func.toLongList(ids)); |
||||
} |
||||
|
||||
|
||||
private void validateStationHintExists(Long id) { |
||||
if (busLineMapper.selectById(id) == null) { |
||||
throw new ServiceException(StringUtil.format("当前id为 [{}] 的站点不存在!",id)); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,26 @@ |
||||
package org.springblade.modules.business.vo; |
||||
|
||||
import lombok.Data; |
||||
import org.springblade.modules.business.entity.BusLine; |
||||
import org.springblade.upload.entity.PocBusLineImg; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
|
||||
import javax.validation.constraints.NotNull; |
||||
import java.io.Serializable; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @Author: Chents |
||||
* @Create: 2023-05-08 |
||||
*/ |
||||
@Data |
||||
public class BusLineUpdateVo implements Serializable { |
||||
|
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
//公交线路
|
||||
private BusLine busLine; |
||||
|
||||
//图片列表
|
||||
private List<PocBusLineImg> imgList; |
||||
} |
||||
@ -0,0 +1,20 @@ |
||||
package org.springblade.modules.business.vo; |
||||
|
||||
import lombok.Data; |
||||
|
||||
import java.io.Serializable; |
||||
|
||||
/** |
||||
* @Author: Chents |
||||
* @Create: 2023-05-05 |
||||
*/ |
||||
@Data |
||||
public class BusLineVo implements Serializable { |
||||
|
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
private Long id; |
||||
private String linename; |
||||
private String updown; |
||||
private String snameKey; |
||||
} |
||||
@ -0,0 +1,29 @@ |
||||
package org.springblade.modules.business.vo; |
||||
|
||||
import lombok.Data; |
||||
|
||||
import java.io.Serializable; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @Author: Chents |
||||
* @Create: 2023-05-05 |
||||
*/ |
||||
@Data |
||||
public class CarAdminVo implements Serializable { |
||||
|
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
private Long id; |
||||
|
||||
//车载机id
|
||||
private Long receiverId; |
||||
//公交车车牌
|
||||
private String name; |
||||
//直播状态:1在线 2车外摄像头 3车顶摄像头
|
||||
private Integer liveStatus; |
||||
//直播数量
|
||||
private Long amount; |
||||
|
||||
private List<CarLiveChannelVo> carLiveChannelVoList; |
||||
} |
||||
@ -0,0 +1,24 @@ |
||||
package org.springblade.modules.business.vo; |
||||
|
||||
import lombok.Data; |
||||
|
||||
import java.io.Serializable; |
||||
|
||||
/** |
||||
* @Author: Chents |
||||
* @Create: 2023-05-05 |
||||
*/ |
||||
@Data |
||||
public class CarLiveChannelVo implements Serializable { |
||||
|
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
private Long id; |
||||
|
||||
//直播通道号
|
||||
private Integer code; |
||||
//直播通道名称
|
||||
private String channelName; |
||||
|
||||
private Integer status; |
||||
} |
||||
@ -0,0 +1,27 @@ |
||||
package org.springblade.modules.business.vo; |
||||
|
||||
import lombok.Data; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
import java.io.Serializable; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @Author: Chents |
||||
* @Create: 2023-05-06 |
||||
*/ |
||||
@Getter |
||||
@Setter |
||||
public class StationHintVo implements Serializable { |
||||
|
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
private Long id; |
||||
private String name;//接口中站名
|
||||
private String tips;//提示
|
||||
private String red;//情怀,励志
|
||||
private String green;//活动
|
||||
private String yellow;//活动
|
||||
private List<imgVo> imgList; |
||||
} |
||||
@ -0,0 +1,16 @@ |
||||
package org.springblade.modules.business.vo; |
||||
|
||||
import java.io.Serializable; |
||||
|
||||
/** |
||||
* @Author: Chents |
||||
* @Create: 2023-05-09 |
||||
*/ |
||||
public class StationVo implements Serializable { |
||||
|
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
private CarInfoVO carInfoVO; |
||||
|
||||
private String currentStationCode; |
||||
} |
||||
@ -0,0 +1,21 @@ |
||||
package org.springblade.modules.business.vo; |
||||
|
||||
import lombok.Data; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
import java.io.Serializable; |
||||
|
||||
/** |
||||
* @Author: Chents |
||||
* @Create: 2023-05-08 |
||||
*/ |
||||
@Getter |
||||
@Setter |
||||
public class imgVo implements Serializable{ |
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
private String imgUrl; |
||||
|
||||
private Integer imgType; |
||||
} |
||||
@ -0,0 +1,58 @@ |
||||
package org.springblade.modules.job; |
||||
|
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springblade.modules.business.service.ICarInfoService; |
||||
import org.springblade.modules.business.service.IStationHintService; |
||||
import org.springblade.modules.business.vo.CarInfoVO; |
||||
import org.springblade.modules.websocket.service.WebsocketService; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.data.redis.core.RedisTemplate; |
||||
import org.springframework.scheduling.annotation.Scheduled; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @Author: Chents |
||||
* @Create: 2023-05-09 |
||||
*/ |
||||
@Component |
||||
@Slf4j |
||||
public class CarInfoTask { |
||||
|
||||
@Autowired |
||||
private ICarInfoService carInfoService; |
||||
|
||||
@Autowired |
||||
private RedisTemplate<String, Object> redisTemplate; |
||||
|
||||
@Autowired |
||||
private WebsocketService websocketService; |
||||
|
||||
@Autowired |
||||
private IStationHintService stationHintService; |
||||
|
||||
//@Scheduled(cron = "0/1 * * * * ?")
|
||||
public void getAllCarInfo() { |
||||
redisTemplate.opsForValue().set("car_info", carInfoService.getAllCarInfo()); |
||||
System.out.println("======存入实时车辆信息到redis成功========="); |
||||
} |
||||
|
||||
//@Scheduled(cron = "0/1 * * * * ?")
|
||||
public void parseCarInfo() { |
||||
//从redis获取数据
|
||||
List<CarInfoVO> carInfoList = (List<CarInfoVO>) redisTemplate.opsForValue().get("car_info"); |
||||
if (carInfoList == null && carInfoList.isEmpty()) { |
||||
log.info("======未从redis获取车辆实时数据======="); |
||||
return; |
||||
} |
||||
for (CarInfoVO carInfo : carInfoList) { |
||||
// 解析车辆信息
|
||||
log.info("解析车辆信息:{}", carInfo.toString()); |
||||
// 推送消息给web页面
|
||||
websocketService.broadcast("car_info", "车辆信息:" + carInfo.toString()); |
||||
log.info("推送车辆信息:{}", carInfo.toString()); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,27 @@ |
||||
package org.springblade.modules.websocket.config; |
||||
|
||||
import lombok.AllArgsConstructor; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springblade.modules.websocket.handler.WebsocketHandler; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.web.socket.config.annotation.*; |
||||
|
||||
/** |
||||
* WebSocketEventListener |
||||
* |
||||
* @author admin |
||||
*/ |
||||
@Slf4j |
||||
@Configuration |
||||
@EnableWebSocket |
||||
@AllArgsConstructor |
||||
public class WebsocketConfig implements WebSocketConfigurer { |
||||
|
||||
WebsocketHandler webSocketHandler; |
||||
|
||||
@Override |
||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { |
||||
System.out.println("===================================== websocket init !"); |
||||
registry.addHandler(webSocketHandler, "/ws").setAllowedOrigins("*"); |
||||
} |
||||
} |
||||
@ -0,0 +1,35 @@ |
||||
package org.springblade.modules.websocket.controller; |
||||
|
||||
import lombok.AllArgsConstructor; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springblade.modules.websocket.service.WebsocketService; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RequestParam; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
/** |
||||
* @Description |
||||
* @Author ytl |
||||
* @Date 2023/3/15 0015 9:03 |
||||
*/ |
||||
|
||||
@RestController |
||||
@RequestMapping("/websocket") |
||||
@AllArgsConstructor |
||||
@Slf4j |
||||
public class WebSocketController{ |
||||
private final WebsocketService websocketService; |
||||
|
||||
// @GetMapping("/broadcast")
|
||||
// R broadcast(@RequestParam String type, @RequestParam String msg){
|
||||
// websocketService.broadcast(type, msg);
|
||||
// return R.status(true);
|
||||
// }
|
||||
|
||||
@GetMapping("/ifhasclient") |
||||
boolean ifHasClient(@RequestParam String type){ |
||||
boolean b = websocketService.ifHasClient(type); |
||||
return b; |
||||
} |
||||
} |
||||
@ -0,0 +1,63 @@ |
||||
package org.springblade.modules.websocket.handler; |
||||
|
||||
import lombok.AllArgsConstructor; |
||||
import org.springblade.modules.websocket.service.WebsocketService; |
||||
import org.springframework.stereotype.Component; |
||||
import org.springframework.web.socket.CloseStatus; |
||||
import org.springframework.web.socket.TextMessage; |
||||
import org.springframework.web.socket.WebSocketSession; |
||||
import org.springframework.web.socket.handler.TextWebSocketHandler; |
||||
|
||||
/** |
||||
* WebSocketEventListener |
||||
* |
||||
* @author rajeevkumarsingh |
||||
*/ |
||||
@Component |
||||
@AllArgsConstructor |
||||
public class WebsocketHandler extends TextWebSocketHandler { |
||||
WebsocketService webSocketService; |
||||
|
||||
/** |
||||
* 连接关闭后执行方法 |
||||
* |
||||
* @param session |
||||
* @param status |
||||
* @throws Exception |
||||
*/ |
||||
@Override |
||||
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { |
||||
super.afterConnectionClosed(session, status); |
||||
webSocketService.removeWebSocketSession(session); |
||||
System.out.println("====== websocket会话关闭...."); |
||||
} |
||||
|
||||
/** |
||||
* 连接建立后执行方法 |
||||
* |
||||
* @param session |
||||
* @throws Exception |
||||
*/ |
||||
@Override |
||||
public void afterConnectionEstablished(WebSocketSession session) throws Exception { |
||||
super.afterConnectionEstablished(session); |
||||
webSocketService.addWebSocketSession(session); |
||||
System.out.println("====== websocket建立新的会话...."); |
||||
|
||||
} |
||||
|
||||
/** |
||||
* 接收客户端推送数据的方法 |
||||
* |
||||
* @param session |
||||
* @param message |
||||
* @throws Exception |
||||
*/ |
||||
@Override |
||||
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { |
||||
System.out.println(message.getPayload()); |
||||
TextMessage msg = new TextMessage(message.getPayload()); |
||||
session.sendMessage(msg); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,76 @@ |
||||
package org.springblade.modules.websocket.service; |
||||
|
||||
import org.springframework.stereotype.Service; |
||||
import org.springframework.web.socket.TextMessage; |
||||
import org.springframework.web.socket.WebSocketSession; |
||||
import org.springframework.web.socket.handler.TextWebSocketHandler; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.*; |
||||
|
||||
/** |
||||
* WebSocketEventListener |
||||
* |
||||
* @author rajeevkumarsingh |
||||
*/ |
||||
@Service |
||||
public class WebsocketService extends TextWebSocketHandler { |
||||
/** |
||||
* 用synchronized或ConcurrentHashMap保证多线程安全,因为我们还需要实现广播,所以这里我用了synchronized实现线程安全 |
||||
*/ |
||||
// Map<String, WebSocketSession> sessionMap = new HashMap<>();
|
||||
// key :目前有lift,light_kk,value:List<WebSocketSession>
|
||||
Map<String, List<WebSocketSession>> sessionMap = new HashMap<>(); |
||||
|
||||
public synchronized void addWebSocketSession(WebSocketSession session) { |
||||
// 从session中获取的唯一标识
|
||||
// session 中还可以获得其他握手前的http各种属性,如URL,请求头,这些也可以作为唯一标识
|
||||
String id = session.getId(); |
||||
String query = session.getUri().getQuery(); |
||||
|
||||
List<WebSocketSession> sessions = sessionMap.containsKey(query) ? sessionMap.get(query) : new ArrayList<>(); |
||||
sessions.add(session); |
||||
|
||||
// 保存会话
|
||||
sessionMap.put(query, sessions); |
||||
} |
||||
|
||||
public synchronized void removeWebSocketSession(WebSocketSession session) { |
||||
String query = session.getUri().getQuery(); |
||||
|
||||
List<WebSocketSession> sessions = sessionMap.containsKey(query) ? sessionMap.get(query) : new ArrayList<>(); |
||||
sessions.remove(session); |
||||
sessionMap.put(query, sessions); |
||||
} |
||||
|
||||
/** |
||||
* 广播 |
||||
* |
||||
* @param message |
||||
*/ |
||||
public synchronized void broadcast(String type, String message) { |
||||
if (!sessionMap.containsKey(type)) { |
||||
return; |
||||
} |
||||
|
||||
for (WebSocketSession session : sessionMap.get(type)) { |
||||
TextMessage tm = new TextMessage(message); |
||||
try { |
||||
Set<String> strings = sessionMap.keySet(); |
||||
session.sendMessage(tm); |
||||
|
||||
} catch (IOException e) { |
||||
// 发送失败后,需要继续给其他人广播,因此在循环里面用 try 捕获异常
|
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 判断某种类型的连接是否为空 |
||||
*/ |
||||
public boolean ifHasClient(String type){ |
||||
boolean b = sessionMap.containsKey(type) ? sessionMap.get(type).size() > 0 : false; |
||||
return b; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue