| | |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.context.annotation.Profile; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | |
| | | import java.util.*; |
| | |
| | | private String mqttClientId; |
| | | @Value(value = "${jeecg.mqtt.role}") |
| | | private String role; |
| | | @Value(value = "${jeecg.mqtt.enable}") |
| | | private boolean enable; |
| | | |
| | | @Autowired |
| | | private MqttSampleCallback mqttSampleCallback; |
| | |
| | | * mqtt连接配置 |
| | | */ |
| | | private void conn() { |
| | | if (!isEnable()) return; |
| | | MemoryPersistence persistence = new MemoryPersistence(); |
| | | MqttConnectOptions mqttConnOpt = new MqttConnectOptions(); |
| | | mqttConnOpt.setUserName(mqttName); |
| | |
| | | mqttClient.subscribe(MqttConstant.MOBILE_UP); |
| | | System.err.println("admin订阅" + MqttConstant.MOBILE_UP); |
| | | // 订阅租户实时数据 |
| | | mqttClient.subscribe(MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA); |
| | | System.out.println("admin订阅" + MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA); |
| | | mqttClient.subscribe(MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA_EQP); |
| | | System.out.println("admin订阅" + MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA_EQP); |
| | | // 订阅租户报警数据 |
| | | mqttClient.subscribe(MqttConstant.TENANT_UP_PREFIX_REAL_FAULT_DATA); |
| | | System.out.println("admin订阅" + MqttConstant.TENANT_UP_PREFIX_REAL_FAULT_DATA); |
| | |
| | | |
| | | //重连 |
| | | private void reconn() { |
| | | if (!isEnable()) return; |
| | | Timer timer = new Timer(); |
| | | |
| | | TimerTask task = new TimerTask() { |
| | |
| | | for (int i = 0; i < data.size(); i++) { |
| | | JSONObject obj = data.getJSONObject(i); |
| | | JSONObject item = new JSONObject(); |
| | | //clientid |
| | | String clientid = obj.getString("clientid"); |
| | | item.put("clientid", clientid); |
| | | //TODO 校验租户id是否存在 |
| | | if(!clientid.matches("^[^-]+-[^-]+-[^-]+$")) continue; |
| | | //username |
| | | item.put("username", obj.get("username")); |
| | | //连接时间 |
| | | String st = obj.getString("connected_at"); |
| | | String upTime = DateUtils.zone2Str(st); |
| | | item.put("connectedAt", upTime); |
| | | //clientid |
| | | String clientid = obj.getString("clientid"); |
| | | item.put("clientid", clientid); |
| | | //是否连接 |
| | | Boolean connected = obj.getBoolean("connected"); |
| | | item.put("connected", connected); |
| | |
| | | String[] info = clientid.split("-"); |
| | | item.put("type", info[0]); |
| | | item.put("tenantId", info[1]); |
| | | //item.put("code", info[2]); |
| | | item.put("code", info[2]); |
| | | |
| | | if (connected) { |
| | | redisUtil.hset(String.format(MqttConstant.MQTT_ONLINE_CLIENT,item.get("tenantId").toString()) , clientid, item); |