增加修改绑定名称接口,设备登陆时已解绑推送

This commit is contained in:
wulin 2023-10-10 13:49:03 +08:00
parent cd63138e69
commit 989f0a723b
15 changed files with 4274 additions and 35 deletions

View File

@ -17,6 +17,8 @@ public enum AskTypeEnum {
UPDATE(100, "固件升级"),
BOX_ON_LINE(101, "Box配网成功"),
BOX_OFF_LINE(102, "Box离线"),
DEVICE_UNBIND(103, "设备解绑"),
DEVICE_BIND(104, "设备绑定成功"),
COMMAND_N(200, "指令后必须跟的名称词")
;
AskTypeEnum(Integer c, String n){

View File

@ -33,6 +33,10 @@ public class DeviceUserBindEntity extends GenericEntity<Long> {
@Column(name = "modify_time")
private Date modifyTime;
@Comment("是否绑定0未绑定 1绑定")
@Column(name = "is_bind")
private Integer isBind;
@Comment("用户Id")
@Column(name = "user_id", nullable = false)
private Long userId;

View File

@ -31,6 +31,10 @@ public class DeviceUserBindRequest implements java.io.Serializable {
private Date modifyTime;
private Date modifyTimeStart;
private Date modifyTimeEnd;
/**
* 是否绑定0未绑定 1绑定
*/
private Integer isBind;
//用户Id
private Long userId;
//设备id

View File

@ -18,6 +18,7 @@ public class DeviceUserBindResp {
public DeviceUserBindResp(DeviceUserBindEntity entity){
id = entity.getId();
createTime = entity.getCreateTime();
isBind = entity.getIsBind();
userId = entity.getUserId();
deviceType = entity.getDeviceType();
deviceId = entity.getDeviceId();
@ -37,6 +38,10 @@ public class DeviceUserBindResp {
private Long id;
//创建时间
private Date createTime;
/**
* 是否绑定0未绑定 1绑定
*/
private Integer isBind;
//用户Id
private Long userId;
//设备id

View File

@ -67,6 +67,9 @@ public class DeviceUserBindService extends GenericReactiveCrudService<DeviceUser
if(request.getModifyTime() != null){
reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getModifyTime, request.getModifyTime());
}
if(request.getIsBind() != null){
reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getIsBind, request.getIsBind());
}
if(request.getUserId() != null){
reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getUserId, request.getUserId());
}
@ -152,6 +155,9 @@ public class DeviceUserBindService extends GenericReactiveCrudService<DeviceUser
if(request.getUserId() != null){
reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getUserId, request.getUserId());
}
if(request.getIsBind() != null){
reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getIsBind, request.getIsBind());
}
if(request.getDeviceType() != null){
reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getDeviceType, request.getDeviceType());
}
@ -235,6 +241,9 @@ public class DeviceUserBindService extends GenericReactiveCrudService<DeviceUser
if(entity.getIsDelete() != null){
update = update.set(DeviceUserBindEntity::getIsDelete, entity.getIsDelete());
}
if(entity.getIsBind() != null){
update = update.set(DeviceUserBindEntity::getIsBind, entity.getIsBind());
}
if(entity.getUserId() != null){
update = update.set(DeviceUserBindEntity::getUserId, entity.getUserId());
}
@ -286,6 +295,7 @@ public class DeviceUserBindService extends GenericReactiveCrudService<DeviceUser
ReactiveUpdate<DeviceUserBindEntity> update = createUpdate()
.set(DeviceUserBindEntity::getModifyTime, new Date());
update = update.set(DeviceUserBindEntity::getIsDelete, entity.getIsDelete());
update = update.set(DeviceUserBindEntity::getIsBind, entity.getIsBind());
update = update.set(DeviceUserBindEntity::getUserId, entity.getUserId());
update = update.set(DeviceUserBindEntity::getDeviceType, entity.getDeviceType());
update = update.set(DeviceUserBindEntity::getDeviceId, entity.getDeviceId());
@ -400,7 +410,7 @@ public class DeviceUserBindService extends GenericReactiveCrudService<DeviceUser
public Mono<Integer> UnbindingDevice(Long userId, Long deviceId) {
return createUpdate()
.set("is_delete", 1)
.set("is_bind", 0)
.set("modify_time", new Date())
.where("user_id", userId)
.and("is_delete", 0)
@ -408,4 +418,13 @@ public class DeviceUserBindService extends GenericReactiveCrudService<DeviceUser
.and("device_id", deviceId)
.execute();
}
public Mono<Integer> updateDeviceUserBind(DeviceUserBindEntity deviceUserBindEntity) {
return createUpdate().set("modify_time", new Date())
.set("bind_name", deviceUserBindEntity.getBindName())
.where("user_id", deviceUserBindEntity.getUserId())
.and("device_id", deviceUserBindEntity.getDeviceId())
.and("is_delete", 0)
.execute();
}
}

View File

@ -1,5 +1,6 @@
package com.qiuguo.iot.data.service.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -14,6 +15,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
**/
@Service
@Slf4j
public class MqService {
private final RabbitTemplate rabbitTemplate;
private final AtomicBoolean confirmationResult = new AtomicBoolean(false);
@ -28,11 +30,11 @@ public class MqService {
if (ack) {
// 消息发送成功
confirmationResult.set(true);
System.out.println("消息发送成功");
log.info("MQ消息发送成功");
} else {
// 消息发送失败
confirmationResult.set(false);
System.out.println("消息发送失败");
log.info("MQ消息发送失败");
}
});
}

View File

@ -38,7 +38,7 @@ public class LacNlpService implements INlp {
// private String url;
private Mono<JSONObject> getNlpFromLac(LacRequest request){
return WebClientUtils.post(SpringUtil.getProperty("lac.url") + "/predict/lac", (JSONObject) JSONObject.toJSON(request)).map(
return WebClientUtils.post(SpringUtil.getProperty("lac.url"), (JSONObject) JSONObject.toJSON(request)).map(
jsonObject -> {
if (!Objects.equals(jsonObject.getInteger("status"), 0)) {
throw new RuntimeException(jsonObject.getString("info"));

View File

@ -23,6 +23,7 @@ import com.qiuguo.iot.data.service.device.DeviceUserBindService;
import com.qiuguo.iot.data.service.device.DeviceUserTalkRecordService;
import com.qiuguo.iot.third.service.TuyaDeviceConnector;
import com.qiuguo.iot.user.api.resp.device.DeviceInitResp;
import com.qiuguo.iot.user.api.rest.device.SetDeviceBindInfoRest;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.web.api.crud.entity.PagerResult;
import org.hswebframework.web.exception.BusinessException;
@ -174,7 +175,7 @@ public class DeviceController {
public Mono<PagerResult<DeviceUserBindResp>> getBindBoxs(@RequestBody DeviceUserBindRequest request){
request.setDeviceType(DeviceTypeEnum.GUO_BOX.getCode());
request.setIsBind(YesNo.YES.getCode());
return deviceUserBindService.selectDeviceUserBindsContainOnLineByRequest(request);
}
@ -191,7 +192,25 @@ public class DeviceController {
public Mono<Integer> UnbindingDevice(@RequestParam Long deviceId,
@RequestParam Long userId){
return deviceUserBindService.UnbindingDevice(userId, deviceId);
return deviceUserBindService.UnbindingDevice(userId, deviceId).map(i -> {
if(i > 0){
log.info("通知设备被解绑了");
}
return i;
});
}
@PostMapping("/setBindName")
public Mono<Integer> setBindName(@RequestBody SetDeviceBindInfoRest bindName){
if(StringUtils.isEmpty(bindName.getBindName())){
log.info("绑定名称不能为空");
return Mono.just(0);
}
DeviceUserBindEntity deviceUserBindEntity = new DeviceUserBindEntity();
deviceUserBindEntity.setUserId(bindName.getUserId());
deviceUserBindEntity.setDeviceId(bindName.getDeviceId());
deviceUserBindEntity.setBindName(bindName.getBindName());
return deviceUserBindService.updateDeviceUserBind(deviceUserBindEntity);
}

View File

@ -0,0 +1,13 @@
package com.qiuguo.iot.user.api.rest.device;
import lombok.Data;
@Data
public class SetDeviceBindInfoRest {
/**
* 绑定名称
*/
String bindName;
Long deviceId;
Long userId;
}

View File

@ -81,33 +81,45 @@ public class BaseWebSocketProcess {
protected static String apiType = "api-type";
protected static String apiToken = "api-token";
private void toQianWen(Action action, BaseSession baseSession){
TongYiCommunicationRest tongYiCommunicationRest = new TongYiCommunicationRest();
tongYiCommunicationRest.setText(action.getAsk());
tongYiCommunicationRest.setStatus("2");
if(this instanceof BoxWebSocketHandler){
tongYiCommunicationRest.setOnlyId(baseSession.getSn());
}else{
tongYiCommunicationRest.setOnlyId(baseSession.getUserId().toString());
}
qwenService.communication(tongYiCommunicationRest).map(data ->{
BoxMessageResp resp = new BoxMessageResp();
resp.setType(0);
if(data.getCode() == 200){
resp.setText(data.getResut());
}else{
resp.setText("我还在努力学习中,暂时无法理解");
}
sendMessage(action, baseSession, resp);
return data;
}).subscribeOn(Schedulers.boundedElastic()).subscribe();
}
protected void processAction(Actions actions, Long userId, BaseSession baseSession) {
if(actions.getActions() == null || actions.getActions().size() == 0){
//调用千问回答\
log.info("调用千问");
Action action = new Action();
action.setAsk(actions.getRecordText());
action.setAction(actions.getRecordText());
toQianWen(action, baseSession);
}
for (Action action : actions.getActions()
) {
) {
log.info("匹配到自定义指令{}", action.getSystemTalkAnswerConfigEntity());
if(action.getSystemTalkAnswerConfigEntity() == null){
log.info("调用千问");
toQianWen(action, baseSession);
TongYiCommunicationRest tongYiCommunicationRest = new TongYiCommunicationRest();
tongYiCommunicationRest.setText(action.getAsk());
tongYiCommunicationRest.setStatus("2");
if(this instanceof BoxWebSocketHandler){
tongYiCommunicationRest.setOnlyId(baseSession.getSn());
}else{
tongYiCommunicationRest.setOnlyId(baseSession.getUserId().toString());
}
qwenService.communication(tongYiCommunicationRest).map(data ->{
BoxMessageResp resp = new BoxMessageResp();
resp.setType(0);
if(data.getCode() == 200){
resp.setText(data.getResut());
}else{
resp.setText("我还在努力学习中,暂时无法理解");
}
sendMessage(action, baseSession, resp);
return data;
}).subscribeOn(Schedulers.boundedElastic()).subscribe();
//
}else if(action.getSystemTalkAnswerConfigEntity().getAnswerType().equals(AskTypeEnum.IOT.getCode())){

View File

@ -86,10 +86,7 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock
nlpService.getActionWithLacSingle(boxSession.getUserId(), boxTalkMessage.getMessage()).defaultIfEmpty(new Actions()).map(actions -> {
//处理
if(actions.getActions() == null || actions.getActions().size() == 0){
//调用千问回答
log.info("暂时无法理解,我还在努力学习中");
}else if(boxSession == null){
if(boxSession == null){
log.info("未匹配到用户session可能传错用户id");
session.close().subscribe();
}else{
@ -142,7 +139,7 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock
userMsgResp.setType(type);
userMsgResp.setText(text);
String msg = JSONObject.toJSONString(userMsgResp);
log.info("推送通知到端msg:{}", msg);
log.info("推送通知到用户端msg:{}", msg);
userSession.getSink().next(userSession.getSession().textMessage(msg));
}
}
@ -210,8 +207,7 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock
request.setDeviceId(dv.getId());
//跟新在线状态
deviceInfoService.setOnLineStatus(dv.getId(), YesNo.YES.getCode()).subscribe();
//通知用户端设备绑定成功
sendNoticeToUser(userId, "设备联网成功,设备序列号:" + dv.getSn(), AskTypeEnum.BOX_ON_LINE.getCode());
deviceUserBindService.selectDeviceUserBindByRequest(request)
@ -230,6 +226,8 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock
deviceUserBindService.insertDeviceUserBind(entity).map(l ->{
log.info("绑定成功SN{} userId:{}", dv, userId);
//下面所有的以前未主设备改成非主设备
//通知用户端设备绑定成功
sendNoticeToUser(userId, "设备绑定成功,设备序列号:" + dv.getSn(), AskTypeEnum.DEVICE_BIND.getCode());
ReactiveValueOperations<String, String> operations = reactiveStringRedisTemplate.opsForValue();
UserDeviceInfoModel userDeviceInfoModel = new UserDeviceInfoModel();
userDeviceInfoModel.setStatus(YesNo.YES.getCode());
@ -244,6 +242,24 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock
return Mono.empty();
}).subscribe();
}else{
if(entity.getIsBind().equals(YesNo.YES.getCode())){
//通知用户端设备绑定成功
sendNoticeToUser(userId, "设备联网成功,设备序列号:" + dv.getSn(), AskTypeEnum.BOX_ON_LINE.getCode());
}else{
//通知用户端设备绑定成功
BaseSession boxSession = getBoxSessionWithSn(dv.getSn());
if(boxSession != null){
UserMessageResp userMsgResp = new UserMessageResp();
userMsgResp.setType(AskTypeEnum.DEVICE_UNBIND.getCode());
userMsgResp.setText("设备已解绑无法继续使用");
String msg = JSONObject.toJSONString(userMsgResp);
log.info("推送通知到设备端msg:{}", msg);
boxSession.getSink().next(boxSession.getSession().textMessage(msg));
boxSession.getSession().close().subscribe();
}
}
}
return Mono.empty();
}).subscribe();

View File

@ -41,4 +41,4 @@ qiuguo:
checktoken:
url: https://exper.qiuguojihua.com/data/api.auth.center/get
lac:
url: http://192.168.8.175:8866
url: http://192.168.8.175:8866/predict/lac

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff