diff --git a/zc-business/src/main/java/com/zc/business/controller/NonAutomaticWarningController.java b/zc-business/src/main/java/com/zc/business/controller/NonAutomaticWarningController.java new file mode 100644 index 00000000..51297fd1 --- /dev/null +++ b/zc-business/src/main/java/com/zc/business/controller/NonAutomaticWarningController.java @@ -0,0 +1,230 @@ +package com.zc.business.controller; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.hikvision.artemis.sdk.ArtemisHttpUtil; +import com.hikvision.artemis.sdk.config.ArtemisConfig; +import com.ruoyi.common.core.controller.BaseController; +import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.common.utils.uuid.IdUtils; +import com.zc.business.domain.DcWarning; +import com.zc.business.service.IDcWarningService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.eclipse.paho.client.mqttv3.*; +import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.*; + + +/** + * License + * + * @author Athena-xiepufeng + */ +@Api(tags = "非机预警接口") +@RestController +@RequestMapping("/nonAutomaticWarning") +public class NonAutomaticWarningController extends BaseController { + + private static final String APPKEY = "22825659"; + + private static final String APPSECRET = "7Qcq3fr1gaYws6QhyDqt"; + + private static final String URI = "/artemis/api/common/v1/event/getTopicInfo"; + + private static final String IP = "10.0.81.28"; + + @Resource + private IDcWarningService dcWarningService; + + /* + * 调用功能 + * */ + @ApiOperation("事件订阅") + @PostMapping(value = "/eventSubscription") + public AjaxResult eventSubscription() throws Exception { + + ArtemisConfig config = new ArtemisConfig(); + config.setHost(IP); // 代理API网关nginx服务器ip端口 + config.setAppKey(APPKEY); // 秘钥appkey + config.setAppSecret(APPSECRET);// 秘钥appSecret + final String getCamsApi = URI; + Map paramMap = new HashMap();// post请求Form表单参数 + ArrayList longs = new ArrayList<>(); + longs.add(6274879489L); + longs.add(6274883585L); + longs.add(7768236033L); + longs.add(7835340801L); + paramMap.put("eventTypes", longs); + String body = JSON.toJSON(paramMap).toString(); + Map path = new HashMap(2) { + { + put("https://", getCamsApi); + } + }; + String jsonString = ArtemisHttpUtil.doPostStringArtemis(config, path, body, null, null, "application/json"); + + JSONObject jsonObject = JSONObject.parseObject(jsonString); + String code = jsonObject.getString("code"); + if (Objects.equals(code, "0")) { + JSONObject jsonResult = jsonObject.getJSONObject("data"); + String host = jsonResult.getString("host"); + String clientId = jsonResult.getString("clientId"); + String userName = jsonResult.getString("userName"); + String password = jsonResult.getString("password"); + JSONObject topicName = jsonResult.getJSONObject("topicName"); + try { + // host为主机名,test为clientid即连接MQTT的客户端ID,一般以客户端唯一标识符表示,MemoryPersistence设置clientid的保存形式,默认为以内存保存 + MqttClient client = new MqttClient(host, clientId, new MemoryPersistence()); + // MQTT的连接设置 + MqttConnectOptions options = new MqttConnectOptions(); + // 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接 + options.setCleanSession(true); + // 设置连接的用户名 + options.setUserName(userName); + // 设置连接的密码 + options.setPassword(password.toCharArray()); + // 设置超时时间 单位为秒 + options.setConnectionTimeout(10); + // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制 + options.setKeepAliveInterval(20); + // 设置回调函数 + client.setCallback(new MqttCallback() { + + public void connectionLost(Throwable cause) { + System.out.println("connectionLost"); + } + + public void messageArrived(String topic, MqttMessage message) { + System.out.println("======监听到来自[" + topic + "]的消息======"); + String jsonObjString = new String(message.getPayload()); + JSONObject eventJsonObject = JSONObject.parseObject(jsonObjString); + JSONObject params = eventJsonObject.getJSONObject("params"); + if (params == null) { + return; + } + JSONArray events = params.getJSONArray("events"); + events.forEach(item -> { + JSONObject event = JSONObject.parseObject(String.valueOf(item)); + JSONObject data = event.getJSONObject("data"); + String crossingIndexCode = event.getString("crossingIndexCode"); + Date happenTime = event.getDate("happenTime"); + String srcName = event.getString("srcName").split("收费站")[1]; + String illegalTrafficEvent = data.getString("illegalTrafficEvent"); + String directionName = data.getString("directionName"); + String crossingName = data.getString("crossingName"); + String targetPicUrl = data.getString("targetPicUrl"); + + DcWarning dcWarning = new DcWarning(); + dcWarning.setOtherConfig(jsonObjString); + switch (illegalTrafficEvent) { + case "congestion"://拥堵 + dcWarning.setWarningType(1); + break; + //case "laneChange"://变道 + // dcWarning.setWarningType(); + // break; + //case "vehicleexist"://机占非 + // dcWarning.setWarningType(); + // break; + //case "roadBlock"://路障 + // dcWarning.setWarningType(); + // break; + //case "construction"://施工 + // dcWarning.setWarningType(); + // break; + case "llegalParking"://停车 + dcWarning.setWarningType(4); + break; + //case "crossLane"://压线 + // dcWarning.setWarningType(); + // break; + //case "turnRound"://掉头 + // dcWarning.setWarningType(); + // break; + case "wrongDirection"://逆行 + dcWarning.setWarningType(5); + break; + case "pedestrian"://行人 + dcWarning.setWarningType(2); + break; + case "abandonedObject"://抛洒物 + dcWarning.setWarningType(7); + break; + default://其他 + dcWarning.setWarningType(10); + } + if (Objects.equals(directionName, "上行")) { + dcWarning.setDirection("1"); + } else if (Objects.equals(directionName, "中")) { + dcWarning.setDirection("2"); + } else if (Objects.equals(directionName, "下行")) { + dcWarning.setDirection("3"); + } + dcWarning.setWarningTime(happenTime); + dcWarning.setCreateTime(new Date()); + if (crossingName.startsWith("大学城")) { + dcWarning.setStakeMark("k59+289"); + } else if (crossingName.startsWith("长清")) { + dcWarning.setStakeMark("k72+847"); + } else if (crossingName.startsWith("孝里")) { + dcWarning.setStakeMark("k86+499"); + } else if (crossingName.startsWith("平阴北")) { + dcWarning.setStakeMark("k99+750"); + } else if (crossingName.startsWith("平阴南")) { + dcWarning.setStakeMark("k126+223"); + } else if (crossingName.startsWith("平阴")) { + dcWarning.setStakeMark("k105+904"); + } else if (crossingName.startsWith("东平")) { + dcWarning.setStakeMark("k145+933"); + } else if (crossingName.startsWith("梁山东")) { + dcWarning.setStakeMark("k173+950"); + } else if (crossingName.startsWith("梁山")) { + dcWarning.setStakeMark("k179+396"); + } else if (crossingName.startsWith("嘉祥")) { + dcWarning.setStakeMark("k190+495"); + } + dcWarning.setWarningTitle(crossingName+srcName); + //设置事件Id UUID无下划线格式32 + String uuid = IdUtils.fastSimpleUUID(); + dcWarning.setId(uuid); + dcWarning.setWarningSource(6); + dcWarning.setWarningState(1); + dcWarningService.insertDcWarning(dcWarning); + }); + System.out.println("message content:" + jsonObjString); + System.out.println("============"); + } + + public void deliveryComplete(IMqttDeliveryToken token) { + System.out.println("deliveryComplete---------" + token.isComplete()); + } + + }); + + // 建立连接 + System.out.println("连接到 broker: " + host); + client.connect(options); + + System.out.println("连接成功."); + //订阅消息 + //client.subscribe(topicName.getString("6274883585"), 1); + //System.out.println("开始监听" + topicName.getString("6274883585")); + client.subscribe(topicName.getString("6274879489"), 1); + System.out.println("开始监听" + topicName.getString("6274879489")); + client.subscribe(topicName.getString("7768236033"), 1); + System.out.println("开始监听" + topicName.getString("7768236033")); + client.subscribe(topicName.getString("7835340801"), 1); + System.out.println("开始监听" + topicName.getString("7835340801")); + } catch (Exception e) { + e.printStackTrace(); + } + return AjaxResult.success(); + } + return AjaxResult.error(); + } +}