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.zc.business.domain.DcWarning; import com.zc.business.domain.NonAutomaticWarning; 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.stereotype.Component; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Objects; /** * License * * @author Athena-zhaoxianglong */ @Api(tags = "非机预警接口") @RestController @Component @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(@RequestBody NonAutomaticWarning parameter) throws Exception { if (parameter.getAPPKEY() == null || parameter.getAPPSECRET() == null || parameter.getURI() == null || parameter.getIP() == null) { return AjaxResult.error(); } return getAjaxResult(parameter); } private AjaxResult getAjaxResult(NonAutomaticWarning parameter) throws Exception { String appKey = parameter.getAPPKEY(); String appSecret = parameter.getAPPSECRET(); String uri = parameter.getURI(); String ip = parameter.getIP(); 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("断开连接.........."); } 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"); Date happenTime = event.getDate("happenTime"); String srcName = event.getString("srcName").split("收费站")[0]; String illegalTrafficEvent = data.getString("illegalTrafficEvent"); String directionName = data.getString("directionName"); String crossingName = data.getString("crossingName"); String targetPicUrl = "https://10.0.81.202/fjyj" + data.getString("targetPicUrl"); String laneNo = data.getString("laneNo"); ArrayList pictures = new ArrayList<>(); pictures.add(targetPicUrl); JSONObject jsonObject = new JSONObject(); jsonObject.put("pictures", pictures); DcWarning dcWarning = new DcWarning(); dcWarning.setOtherConfig(String.valueOf(jsonObject)); dcWarning.setLane(laneNo); String warningType = ""; switch (illegalTrafficEvent) { case "congestion"://拥堵 warningType = "拥堵"; dcWarning.setWarningType(1); dcWarning.setWarningSubclass("1-1"); break; case "laneChange"://变道 dcWarning.setWarningType(5); dcWarning.setWarningSubclass("5-6"); break; //case "vehicleexist"://机占非 // dcWarning.setWarningType(); // break; case "roadBlock"://路障 dcWarning.setWarningType(6); dcWarning.setWarningSubclass("6-3"); break; case "construction"://施工 dcWarning.setWarningType(7); dcWarning.setWarningSubclass("7-1"); break; case "llegalParking"://停车 warningType = "停车"; dcWarning.setWarningType(4); dcWarning.setWarningSubclass("4-10"); break; case "crossLane"://压线 dcWarning.setWarningType(5); dcWarning.setWarningSubclass("5-2"); break; case "turnRound"://掉头 dcWarning.setWarningType(5); dcWarning.setWarningSubclass("5-3"); break; case "wrongDirection"://逆行 warningType = "逆行"; dcWarning.setWarningType(5); dcWarning.setWarningSubclass("5-1"); break; case "pedestrian"://行人 warningType = "行人"; dcWarning.setWarningType(2); dcWarning.setWarningSubclass("2-1"); break; case "abandonedObject"://抛洒物 warningType = "抛洒物"; dcWarning.setWarningType(6); dcWarning.setWarningSubclass("6-4"); break; default://其他 warningType = "其他"; dcWarning.setWarningType(99); dcWarning.setWarningSubclass("99-1"); } String direction = ""; if (Objects.equals(directionName, "上行")) { direction = "菏泽方向"; dcWarning.setDirection("1"); } else if (Objects.equals(directionName, "中")) { dcWarning.setDirection("2"); } else if (Objects.equals(directionName, "下行")) { direction = "济南方向"; dcWarning.setDirection("3"); } dcWarning.setWarningTime(happenTime); dcWarning.setCreateTime(new Date()); String stakeMark = ""; if (crossingName.startsWith("大学城")) { stakeMark = "K059+289"; } else if (crossingName.startsWith("长清")) { stakeMark = "K072+847"; } else if (crossingName.startsWith("孝里")) { stakeMark = "K086+499"; } else if (crossingName.startsWith("平阴北")) { stakeMark = "K099+750"; } else if (crossingName.startsWith("平阴南")) { stakeMark = "K126+223"; } else if (crossingName.startsWith("平阴")) { stakeMark = "K105+904"; } else if (crossingName.startsWith("东平")) { stakeMark = "K145+933"; } else if (crossingName.startsWith("梁山东")) { stakeMark = "K173+950"; } else if (crossingName.startsWith("梁山")) { stakeMark = "K179+396"; } else if (crossingName.startsWith("嘉祥")) { stakeMark = "K190+495"; } dcWarning.setStakeMark(stakeMark); dcWarning.setWarningTitle(srcName + "收费站" + direction + "发生" + warningType + "事件"); dcWarning.setWarningSource(6); dcWarning.setWarningState(1); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String formattedDate = sdf.format(happenTime); dcWarning.setRemark("非机预警设备:"+formattedDate + "在" + stakeMark + srcName + "收费站的" + direction + "发生了一起" + warningType + "事件"); 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(); } // @PostConstruct public AjaxResult eventSubscriptionPostConstruct() throws Exception { NonAutomaticWarning stringStringHashMap = new NonAutomaticWarning(); stringStringHashMap.setAPPKEY(APPKEY); stringStringHashMap.setAPPSECRET(APPSECRET); stringStringHashMap.setURI(URI); stringStringHashMap.setIP(IP); return getAjaxResult(stringStringHashMap); } }