wisdomisite-java/src/main/java/com/zhgd/xmgl/task/SafetyHatTask.java

94 lines
3.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.zhgd.xmgl.task;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhgd.xmgl.config.SafetyHatWSClient;
import com.zhgd.xmgl.modules.project.entity.Project;
import com.zhgd.xmgl.modules.project.service.IProjectService;
import com.zhgd.xmgl.util.RundeSafeyHatUtils;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.core.SchedulerLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.websocket.DeploymentException;
import javax.websocket.WebSocketContainer;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
/**
* 智能安全帽task
*/
@Slf4j
@RestController
@RequestMapping("xmgl/task")
public class SafetyHatTask {
@Autowired
IProjectService projectService;
@Autowired
WebSocketContainer webSocketContainer;
@Autowired
SafetyHatWSClient safetyHatWSClient;
/**
* 获取安全帽最新数据30秒内的心跳
*/
@SchedulerLock(name = "getHelmetData", lockAtMostFor = 1000 * 60 * 60, lockAtLeastFor = 1000 * 10)
@Scheduled(cron = "*/10 * * * * ?")
@RequestMapping("getHelmetData")
public void getHelmetData() {
try {
log.info("定时获取安全帽最新数据30秒内的心跳");
List<Project> projectList = projectService.list(Wrappers.<Project>lambdaQuery().ne(Project::getHelmetUser, "").ne(Project::getHelmetPassword, ""));
if (CollUtil.isNotEmpty(projectList)) {
for (Project project : projectList) {
SafetyHatWSClient client = SafetyHatWSClient.clientMap.get(project.getHelmetUser());
try {
if (client == null) {
connect(project);
} else {
client.send("{\"act\":\"ma_get_active_devices\"}");
}
} catch (IllegalStateException e) {
log.info("异常重连:{}", e.getMessage());
connect(project);
}
}
}
} catch (Exception e) {
log.error("err:", e);
}
}
/**
* 连接ws和登录
*
* @param project
* @throws DeploymentException
* @throws IOException
* @throws URISyntaxException
*/
private SafetyHatWSClient connect(Project project) throws DeploymentException, IOException, URISyntaxException {
SafetyHatWSClient client = new SafetyHatWSClient();
webSocketContainer.connectToServer(client, new URI("wss://caps.runde.pro/wss"));
JSONObject token = RundeSafeyHatUtils.getToken(project.getHelmetUser(), project.getHelmetPassword());
if (token != null && CharSequenceUtil.isNotBlank(token.getString("session_id"))) {
String sessionId = token.getString("session_id");
project.setRundeToken(sessionId);
String message = "{\"act\":\"ma_login\",\"user_name\":\"" + project.getHelmetUser() + "\",\"access_token\":\"" + sessionId + "\"}";
client.send(message);
log.info("安全帽用户:{},ms:{}", project.getHelmetUser(), message);
SafetyHatWSClient.clientMap.put(project.getHelmetUser(), client);
}
return client;
}
}