15 changed files with 1127 additions and 0 deletions
@ -0,0 +1,47 @@ |
|||||
|
package com.hxjt.dataupload.enums; |
||||
|
|
||||
|
public enum AlarmType { |
||||
|
ALARM_1("1","禁区告警-区域"), |
||||
|
ALARM_2("2","超员告警-区域"), |
||||
|
ALARM_3("3","滞留告警-区域"), |
||||
|
ALARM_4("4","区域过短-区域"), |
||||
|
ALARM_5("5","禁静止超时-区域"), |
||||
|
ALARM_6("6","SOS"), |
||||
|
ALARM_7("7","闯入告警-围栏"), |
||||
|
ALARM_8("8","脱岗告警-围栏"), |
||||
|
ALARM_13("13","车辆超速"), |
||||
|
ALARM_14("14","人员卡充电告警"), |
||||
|
ALARM_15("15","缺员告警-区域"), |
||||
|
ALARM_16("16","脱岗告警-区域"), |
||||
|
ALARM_17("17","车辆路线偏移"), |
||||
|
ALARM_18("18","违规告警"), |
||||
|
ALARM_19("19","二道门离厂告警"); |
||||
|
|
||||
|
|
||||
|
private final String alarmCode; |
||||
|
|
||||
|
private final String alarmContent; |
||||
|
|
||||
|
AlarmType(String alarmCode, String alarmContent) { |
||||
|
this.alarmCode = alarmCode; |
||||
|
this.alarmContent = alarmContent; |
||||
|
} |
||||
|
|
||||
|
public String getAlarmCode() { |
||||
|
return alarmCode; |
||||
|
} |
||||
|
|
||||
|
public String getAlarmContent() { |
||||
|
return alarmContent; |
||||
|
} |
||||
|
|
||||
|
public static String getContentByCode(String code) { |
||||
|
for (com.hxjt.dataupload.enums.AlarmType objEnum : com.hxjt.dataupload.enums.AlarmType.values()) { |
||||
|
if(objEnum.getAlarmCode().equals(code)) { |
||||
|
return objEnum.getAlarmContent(); |
||||
|
} |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,38 @@ |
|||||
|
package com.hxjt.dataupload.enums; |
||||
|
|
||||
|
public enum UserType { |
||||
|
|
||||
|
ALARM_0("0","在职员工"), |
||||
|
ALARM_1("1","临时访客"), |
||||
|
ALARM_2("2","承包商员工"), |
||||
|
ALARM_3("3","物品"), |
||||
|
ALARM_4("4","车辆"), |
||||
|
ALARM_5("5","司机"); |
||||
|
|
||||
|
private final String alarmCode; |
||||
|
|
||||
|
private final String alarmContent; |
||||
|
|
||||
|
UserType(String alarmCode, String alarmContent) { |
||||
|
this.alarmCode = alarmCode; |
||||
|
this.alarmContent = alarmContent; |
||||
|
} |
||||
|
|
||||
|
public String getAlarmCode() { |
||||
|
return alarmCode; |
||||
|
} |
||||
|
|
||||
|
public String getAlarmContent() { |
||||
|
return alarmContent; |
||||
|
} |
||||
|
|
||||
|
public static String getContentByCode(String code) { |
||||
|
for (com.hxjt.dataupload.enums.UserType objEnum : com.hxjt.dataupload.enums.UserType.values()) { |
||||
|
if(objEnum.getAlarmCode().equals(code)) { |
||||
|
return objEnum.getAlarmContent(); |
||||
|
} |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,174 @@ |
|||||
|
package com.hxjt.dataupload.model.entity.rydw; |
||||
|
|
||||
|
|
||||
|
public class AlarmInfoHx { |
||||
|
/** |
||||
|
* 唯一标示 |
||||
|
*/ |
||||
|
private String uniqueId; |
||||
|
/** |
||||
|
* 企业容器id |
||||
|
*/ |
||||
|
private String containerId ="ningyanghuagongchany"; |
||||
|
/** |
||||
|
* 设备编号 |
||||
|
*/ |
||||
|
private String deviceCode; |
||||
|
/** |
||||
|
* 告警类型 |
||||
|
*/ |
||||
|
private String alarmType; |
||||
|
/** |
||||
|
* 告警内容 |
||||
|
*/ |
||||
|
private String alarmContent; |
||||
|
/** |
||||
|
* 楼层ID |
||||
|
*/ |
||||
|
private String layerId; |
||||
|
/** |
||||
|
* 一级区域id |
||||
|
*/ |
||||
|
private String firAreaId; |
||||
|
/** |
||||
|
* 一级区域名称 |
||||
|
*/ |
||||
|
private String firAreaName; |
||||
|
/** |
||||
|
* 二级区域id |
||||
|
*/ |
||||
|
private String secAreaId; |
||||
|
/** |
||||
|
* 二级区域名称 |
||||
|
*/ |
||||
|
private String secAreaName; |
||||
|
/** |
||||
|
* 纬度 |
||||
|
*/ |
||||
|
private Double x; |
||||
|
/** |
||||
|
* 经度 |
||||
|
*/ |
||||
|
private Double y; |
||||
|
/** |
||||
|
* 定位时间:yyyy-MM-dd HH:mm:ss |
||||
|
*/ |
||||
|
private String dateTime; |
||||
|
/** |
||||
|
* 处理状态:0:待处理,1:已处理 |
||||
|
*/ |
||||
|
private String dealStatus; |
||||
|
|
||||
|
|
||||
|
public String getUniqueId() { |
||||
|
return uniqueId; |
||||
|
} |
||||
|
|
||||
|
public void setUniqueId(String uniqueId) { |
||||
|
this.uniqueId = uniqueId; |
||||
|
} |
||||
|
|
||||
|
public String getContainerId() { |
||||
|
return containerId; |
||||
|
} |
||||
|
|
||||
|
public void setContainerId(String containerId) { |
||||
|
this.containerId = containerId; |
||||
|
} |
||||
|
|
||||
|
public String getDeviceCode() { |
||||
|
return deviceCode; |
||||
|
} |
||||
|
|
||||
|
public void setDeviceCode(String deviceCode) { |
||||
|
this.deviceCode = deviceCode; |
||||
|
} |
||||
|
|
||||
|
public String getAlarmType() { |
||||
|
return alarmType; |
||||
|
} |
||||
|
|
||||
|
public void setAlarmType(String alarmType) { |
||||
|
this.alarmType = alarmType; |
||||
|
} |
||||
|
|
||||
|
public String getAlarmContent() { |
||||
|
return alarmContent; |
||||
|
} |
||||
|
|
||||
|
public void setAlarmContent(String alarmContent) { |
||||
|
this.alarmContent = alarmContent; |
||||
|
} |
||||
|
|
||||
|
public String getLayerId() { |
||||
|
return layerId; |
||||
|
} |
||||
|
|
||||
|
public void setLayerId(String layerId) { |
||||
|
this.layerId = layerId; |
||||
|
} |
||||
|
|
||||
|
public String getFirAreaId() { |
||||
|
return firAreaId; |
||||
|
} |
||||
|
|
||||
|
public void setFirAreaId(String firAreaId) { |
||||
|
this.firAreaId = firAreaId; |
||||
|
} |
||||
|
|
||||
|
public String getFirAreaName() { |
||||
|
return firAreaName; |
||||
|
} |
||||
|
|
||||
|
public void setFirAreaName(String firAreaName) { |
||||
|
this.firAreaName = firAreaName; |
||||
|
} |
||||
|
|
||||
|
public String getSecAreaId() { |
||||
|
return secAreaId; |
||||
|
} |
||||
|
|
||||
|
public void setSecAreaId(String secAreaId) { |
||||
|
this.secAreaId = secAreaId; |
||||
|
} |
||||
|
|
||||
|
public String getSecAreaName() { |
||||
|
return secAreaName; |
||||
|
} |
||||
|
|
||||
|
public void setSecAreaName(String secAreaName) { |
||||
|
this.secAreaName = secAreaName; |
||||
|
} |
||||
|
|
||||
|
public Double getX() { |
||||
|
return x; |
||||
|
} |
||||
|
|
||||
|
public void setX(Double x) { |
||||
|
this.x = x; |
||||
|
} |
||||
|
|
||||
|
public Double getY() { |
||||
|
return y; |
||||
|
} |
||||
|
|
||||
|
public void setY(Double y) { |
||||
|
this.y = y; |
||||
|
} |
||||
|
|
||||
|
public String getDateTime() { |
||||
|
return dateTime; |
||||
|
} |
||||
|
|
||||
|
public void setDateTime(String dateTime) { |
||||
|
this.dateTime = dateTime; |
||||
|
} |
||||
|
|
||||
|
public String getDealStatus() { |
||||
|
return dealStatus; |
||||
|
} |
||||
|
|
||||
|
public void setDealStatus(String dealStatus) { |
||||
|
this.dealStatus = dealStatus; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
package com.hxjt.dataupload.model.entity.rydw; |
||||
|
|
||||
|
|
||||
|
public class AlarmInfoIot { |
||||
|
/** |
||||
|
* 数据类型 |
||||
|
*/ |
||||
|
private String dataType ="alarm"; |
||||
|
/** |
||||
|
* 人员信息 |
||||
|
*/ |
||||
|
private AlarmInfoHx data; |
||||
|
|
||||
|
public String getDataType() { |
||||
|
return dataType; |
||||
|
} |
||||
|
|
||||
|
public void setDataType(String dataType) { |
||||
|
this.dataType = dataType; |
||||
|
} |
||||
|
|
||||
|
public AlarmInfoHx getData() { |
||||
|
return data; |
||||
|
} |
||||
|
|
||||
|
public void setData(AlarmInfoHx data) { |
||||
|
this.data = data; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,139 @@ |
|||||
|
package com.hxjt.dataupload.model.entity.rydw; |
||||
|
|
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public class AreaUserInfoHx { |
||||
|
/** |
||||
|
* 唯一标示 |
||||
|
*/ |
||||
|
private String uniqueId; |
||||
|
/** |
||||
|
* 企业容器id |
||||
|
*/ |
||||
|
private String containerId ="ningyanghuagongchany"; |
||||
|
/** |
||||
|
* 单位唯一标志 |
||||
|
*/ |
||||
|
private String orgCode; |
||||
|
/** |
||||
|
* 单位名称 |
||||
|
*/ |
||||
|
private String orgName; |
||||
|
/** |
||||
|
* 区域唯一标志 |
||||
|
*/ |
||||
|
private String areaId; |
||||
|
/** |
||||
|
* 区域名称 |
||||
|
*/ |
||||
|
private String areaName; |
||||
|
/** |
||||
|
* 上级区域唯一标志 |
||||
|
*/ |
||||
|
private String areaPid; |
||||
|
/** |
||||
|
* 上级区域名称 |
||||
|
*/ |
||||
|
private String areaPname; |
||||
|
/** |
||||
|
* 区域内告警数量 |
||||
|
*/ |
||||
|
private String alarmNum; |
||||
|
/** |
||||
|
* 区域内人员数量 |
||||
|
*/ |
||||
|
private String personNum; |
||||
|
/** |
||||
|
* 区域内人员列表 |
||||
|
*/ |
||||
|
private List<UserBaseInfo> personList; |
||||
|
|
||||
|
public String getUniqueId() { |
||||
|
return uniqueId; |
||||
|
} |
||||
|
|
||||
|
public void setUniqueId(String uniqueId) { |
||||
|
this.uniqueId = uniqueId; |
||||
|
} |
||||
|
|
||||
|
public String getContainerId() { |
||||
|
return containerId; |
||||
|
} |
||||
|
|
||||
|
public void setContainerId(String containerId) { |
||||
|
this.containerId = containerId; |
||||
|
} |
||||
|
|
||||
|
public String getOrgCode() { |
||||
|
return orgCode; |
||||
|
} |
||||
|
|
||||
|
public void setOrgCode(String orgCode) { |
||||
|
this.orgCode = orgCode; |
||||
|
} |
||||
|
|
||||
|
public String getOrgName() { |
||||
|
return orgName; |
||||
|
} |
||||
|
|
||||
|
public void setOrgName(String orgName) { |
||||
|
this.orgName = orgName; |
||||
|
} |
||||
|
|
||||
|
public String getAreaId() { |
||||
|
return areaId; |
||||
|
} |
||||
|
|
||||
|
public void setAreaId(String areaId) { |
||||
|
this.areaId = areaId; |
||||
|
} |
||||
|
|
||||
|
public String getAreaName() { |
||||
|
return areaName; |
||||
|
} |
||||
|
|
||||
|
public void setAreaName(String areaName) { |
||||
|
this.areaName = areaName; |
||||
|
} |
||||
|
|
||||
|
public String getAreaPid() { |
||||
|
return areaPid; |
||||
|
} |
||||
|
|
||||
|
public void setAreaPid(String areaPid) { |
||||
|
this.areaPid = areaPid; |
||||
|
} |
||||
|
|
||||
|
public String getAreaPname() { |
||||
|
return areaPname; |
||||
|
} |
||||
|
|
||||
|
public void setAreaPname(String areaPname) { |
||||
|
this.areaPname = areaPname; |
||||
|
} |
||||
|
|
||||
|
public String getAlarmNum() { |
||||
|
return alarmNum; |
||||
|
} |
||||
|
|
||||
|
public void setAlarmNum(String alarmNum) { |
||||
|
this.alarmNum = alarmNum; |
||||
|
} |
||||
|
|
||||
|
public String getPersonNum() { |
||||
|
return personNum; |
||||
|
} |
||||
|
|
||||
|
public void setPersonNum(String personNum) { |
||||
|
this.personNum = personNum; |
||||
|
} |
||||
|
|
||||
|
public List<UserBaseInfo> getPersonList() { |
||||
|
return personList; |
||||
|
} |
||||
|
|
||||
|
public void setPersonList(List<UserBaseInfo> personList) { |
||||
|
this.personList = personList; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
package com.hxjt.dataupload.model.entity.rydw; |
||||
|
|
||||
|
|
||||
|
public class AreaUserInfoIot { |
||||
|
/** |
||||
|
* 数据类型 |
||||
|
*/ |
||||
|
private String dataType ="areaPersonAlarm"; |
||||
|
/** |
||||
|
* 人员信息 |
||||
|
*/ |
||||
|
private AreaUserInfoHx data; |
||||
|
|
||||
|
public String getDataType() { |
||||
|
return dataType; |
||||
|
} |
||||
|
|
||||
|
public void setDataType(String dataType) { |
||||
|
this.dataType = dataType; |
||||
|
} |
||||
|
|
||||
|
public AreaUserInfoHx getData() { |
||||
|
return data; |
||||
|
} |
||||
|
|
||||
|
public void setData(AreaUserInfoHx data) { |
||||
|
this.data = data; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,125 @@ |
|||||
|
package com.hxjt.dataupload.model.entity.rydw; |
||||
|
|
||||
|
|
||||
|
public class DeviceInfoHx { |
||||
|
/** |
||||
|
* 企业容器id |
||||
|
*/ |
||||
|
private String containerId ="ningyanghuagongchany"; |
||||
|
/** |
||||
|
* 设备编号 |
||||
|
*/ |
||||
|
private String deviceCode; |
||||
|
/** |
||||
|
* 楼层编号 |
||||
|
*/ |
||||
|
private String layerId; |
||||
|
/** |
||||
|
* X |
||||
|
*/ |
||||
|
private Double x; |
||||
|
/** |
||||
|
* Y |
||||
|
*/ |
||||
|
private Double y; |
||||
|
/** |
||||
|
* 定位时间 |
||||
|
*/ |
||||
|
private String dateTime; |
||||
|
/** |
||||
|
* 定位时间 |
||||
|
*/ |
||||
|
private String uniqueId; |
||||
|
/** |
||||
|
* 是否移动 |
||||
|
*/ |
||||
|
private String move; |
||||
|
/** |
||||
|
* 电量状态 |
||||
|
*/ |
||||
|
private String lowPowerStatus; |
||||
|
/** |
||||
|
* 电量 |
||||
|
*/ |
||||
|
private String power; |
||||
|
|
||||
|
public String getContainerId() { |
||||
|
return containerId; |
||||
|
} |
||||
|
|
||||
|
public void setContainerId(String containerId) { |
||||
|
this.containerId = containerId; |
||||
|
} |
||||
|
|
||||
|
public String getDeviceCode() { |
||||
|
return deviceCode; |
||||
|
} |
||||
|
|
||||
|
public void setDeviceCode(String deviceCode) { |
||||
|
this.deviceCode = deviceCode; |
||||
|
} |
||||
|
|
||||
|
public String getLayerId() { |
||||
|
return layerId; |
||||
|
} |
||||
|
|
||||
|
public void setLayerId(String layerId) { |
||||
|
this.layerId = layerId; |
||||
|
} |
||||
|
|
||||
|
public Double getX() { |
||||
|
return x; |
||||
|
} |
||||
|
|
||||
|
public void setX(Double x) { |
||||
|
this.x = x; |
||||
|
} |
||||
|
|
||||
|
public Double getY() { |
||||
|
return y; |
||||
|
} |
||||
|
|
||||
|
public void setY(Double y) { |
||||
|
this.y = y; |
||||
|
} |
||||
|
|
||||
|
public String getDateTime() { |
||||
|
return dateTime; |
||||
|
} |
||||
|
|
||||
|
public void setDateTime(String dateTime) { |
||||
|
this.dateTime = dateTime; |
||||
|
} |
||||
|
|
||||
|
public String getUniqueId() { |
||||
|
return uniqueId; |
||||
|
} |
||||
|
|
||||
|
public void setUniqueId(String uniqueId) { |
||||
|
this.uniqueId = uniqueId; |
||||
|
} |
||||
|
|
||||
|
public String getMove() { |
||||
|
return move; |
||||
|
} |
||||
|
|
||||
|
public void setMove(String move) { |
||||
|
this.move = move; |
||||
|
} |
||||
|
|
||||
|
public String getLowPowerStatus() { |
||||
|
return lowPowerStatus; |
||||
|
} |
||||
|
|
||||
|
public void setLowPowerStatus(String lowPowerStatus) { |
||||
|
this.lowPowerStatus = lowPowerStatus; |
||||
|
} |
||||
|
|
||||
|
public String getPower() { |
||||
|
return power; |
||||
|
} |
||||
|
|
||||
|
public void setPower(String power) { |
||||
|
this.power = power; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
package com.hxjt.dataupload.model.entity.rydw; |
||||
|
|
||||
|
|
||||
|
public class DeviceInfoIot { |
||||
|
/** |
||||
|
* 数据类型 |
||||
|
*/ |
||||
|
private String dataType ="position"; |
||||
|
/** |
||||
|
* 人员信息 |
||||
|
*/ |
||||
|
private DeviceInfoHx data; |
||||
|
|
||||
|
public String getDataType() { |
||||
|
return dataType; |
||||
|
} |
||||
|
|
||||
|
public void setDataType(String dataType) { |
||||
|
this.dataType = dataType; |
||||
|
} |
||||
|
|
||||
|
public DeviceInfoHx getData() { |
||||
|
return data; |
||||
|
} |
||||
|
|
||||
|
public void setData(DeviceInfoHx data) { |
||||
|
this.data = data; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,101 @@ |
|||||
|
package com.hxjt.dataupload.model.entity.rydw; |
||||
|
|
||||
|
|
||||
|
public class UserBaseInfo { |
||||
|
/** |
||||
|
* 企业容器id |
||||
|
*/ |
||||
|
private String containerId ="ningyanghuagongchany"; |
||||
|
/** |
||||
|
* 人员唯一标示 |
||||
|
*/ |
||||
|
private String userCode; |
||||
|
/** |
||||
|
* 人员姓名 |
||||
|
*/ |
||||
|
private String userName; |
||||
|
/** |
||||
|
* 联系电话 |
||||
|
*/ |
||||
|
private String userPhone; |
||||
|
/** |
||||
|
* 人员类型 |
||||
|
*/ |
||||
|
private String userType; |
||||
|
/** |
||||
|
* 所属单位唯一标志 |
||||
|
*/ |
||||
|
private String userOrgCode; |
||||
|
/** |
||||
|
* 所属单位名称 |
||||
|
*/ |
||||
|
private String userOrgName; |
||||
|
/** |
||||
|
* 绑定设备唯一编码 |
||||
|
*/ |
||||
|
private String cardId; |
||||
|
|
||||
|
public String getContainerId() { |
||||
|
return containerId; |
||||
|
} |
||||
|
|
||||
|
public void setContainerId(String containerId) { |
||||
|
this.containerId = containerId; |
||||
|
} |
||||
|
|
||||
|
public String getUserCode() { |
||||
|
return userCode; |
||||
|
} |
||||
|
|
||||
|
public void setUserCode(String userCode) { |
||||
|
this.userCode = userCode; |
||||
|
} |
||||
|
|
||||
|
public String getUserName() { |
||||
|
return userName; |
||||
|
} |
||||
|
|
||||
|
public void setUserName(String userName) { |
||||
|
this.userName = userName; |
||||
|
} |
||||
|
|
||||
|
public String getUserPhone() { |
||||
|
return userPhone; |
||||
|
} |
||||
|
|
||||
|
public void setUserPhone(String userPhone) { |
||||
|
this.userPhone = userPhone; |
||||
|
} |
||||
|
|
||||
|
public String getUserType() { |
||||
|
return userType; |
||||
|
} |
||||
|
|
||||
|
public void setUserType(String userType) { |
||||
|
this.userType = userType; |
||||
|
} |
||||
|
|
||||
|
public String getUserOrgCode() { |
||||
|
return userOrgCode; |
||||
|
} |
||||
|
|
||||
|
public void setUserOrgCode(String userOrgCode) { |
||||
|
this.userOrgCode = userOrgCode; |
||||
|
} |
||||
|
|
||||
|
public String getUserOrgName() { |
||||
|
return userOrgName; |
||||
|
} |
||||
|
|
||||
|
public void setUserOrgName(String userOrgName) { |
||||
|
this.userOrgName = userOrgName; |
||||
|
} |
||||
|
|
||||
|
public String getCardId() { |
||||
|
return cardId; |
||||
|
} |
||||
|
|
||||
|
public void setCardId(String cardId) { |
||||
|
this.cardId = cardId; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
package com.hxjt.dataupload.model.entity.rydw; |
||||
|
|
||||
|
|
||||
|
public class UserBaseInfoIot { |
||||
|
/** |
||||
|
* 数据类型 |
||||
|
*/ |
||||
|
private String dataType ="baseInfo"; |
||||
|
/** |
||||
|
* 人员信息 |
||||
|
*/ |
||||
|
private UserBaseInfo data; |
||||
|
|
||||
|
public String getDataType() { |
||||
|
return dataType; |
||||
|
} |
||||
|
|
||||
|
public void setDataType(String dataType) { |
||||
|
this.dataType = dataType; |
||||
|
} |
||||
|
|
||||
|
public UserBaseInfo getData() { |
||||
|
return data; |
||||
|
} |
||||
|
|
||||
|
public void setData(UserBaseInfo data) { |
||||
|
this.data = data; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,59 @@ |
|||||
|
package com.hxjt.dataupload.mqtt; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; |
||||
|
import org.eclipse.paho.client.mqttv3.MqttMessage; |
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
@Component |
||||
|
public class MqttCallback implements org.eclipse.paho.client.mqttv3.MqttCallback { |
||||
|
private static final Logger logger = LoggerFactory.getLogger(com.hxjt.dataupload.mqtt.MqttClient.class); |
||||
|
|
||||
|
@Autowired |
||||
|
private MqttConfig mqttConfig; |
||||
|
|
||||
|
private static org.eclipse.paho.client.mqttv3.MqttClient client; |
||||
|
|
||||
|
private static String _topic; |
||||
|
private static String _qos; |
||||
|
private static String _msg; |
||||
|
|
||||
|
@Override |
||||
|
public void connectionLost(Throwable throwable) { |
||||
|
// 连接丢失后,一般在这里面进行重连
|
||||
|
logger.info("连接断开,可以做重连"); |
||||
|
if (client == null || !client.isConnected()) { |
||||
|
mqttConfig.getMqttPushClient(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception { |
||||
|
// subscribe后得到的消息会执行到这里面
|
||||
|
logger.info("接收消息主题 : " + topic); |
||||
|
logger.info("接收消息Qos : " + mqttMessage.getQos()); |
||||
|
logger.info("接收消息内容 : " + new String(mqttMessage.getPayload())); |
||||
|
|
||||
|
_topic = topic; |
||||
|
_qos = mqttMessage.getQos()+""; |
||||
|
_msg = new String(mqttMessage.getPayload()); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { |
||||
|
logger.info("deliveryComplete---------" + iMqttDeliveryToken.isComplete()); |
||||
|
} |
||||
|
|
||||
|
//别的Controller层会调用这个方法来 获取 接收到的硬件数据
|
||||
|
public String receive() { |
||||
|
JSONObject jsonObject = new JSONObject(); |
||||
|
jsonObject.put("topic", _topic); |
||||
|
jsonObject.put("qos", _qos); |
||||
|
jsonObject.put("msg", _msg); |
||||
|
return jsonObject.toString(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,101 @@ |
|||||
|
package com.hxjt.dataupload.mqtt; |
||||
|
|
||||
|
|
||||
|
import org.eclipse.paho.client.mqttv3.*; |
||||
|
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; |
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
@Component |
||||
|
public class MqttClient { |
||||
|
private static final Logger logger = LoggerFactory.getLogger(com.hxjt.dataupload.mqtt.MqttClient.class); |
||||
|
|
||||
|
@Autowired |
||||
|
private MqttCallback pushCallback; |
||||
|
|
||||
|
private static org.eclipse.paho.client.mqttv3.MqttClient client; |
||||
|
|
||||
|
private static org.eclipse.paho.client.mqttv3.MqttClient getClient() { |
||||
|
return client; |
||||
|
} |
||||
|
|
||||
|
private static void setClient(org.eclipse.paho.client.mqttv3.MqttClient client) { |
||||
|
com.hxjt.dataupload.mqtt.MqttClient.client = client; |
||||
|
} |
||||
|
|
||||
|
public void connect(String host, String clientID, String username, String password, int timeout, int keepalive) { |
||||
|
org.eclipse.paho.client.mqttv3.MqttClient client; |
||||
|
try { |
||||
|
client = new org.eclipse.paho.client.mqttv3.MqttClient(host, clientID, new MemoryPersistence()); |
||||
|
MqttConnectOptions options = new MqttConnectOptions(); |
||||
|
//设置自动重连
|
||||
|
options.setAutomaticReconnect(true); |
||||
|
options.setCleanSession(true); |
||||
|
//设置连接丢失后的重试延迟
|
||||
|
options.setMaxReconnectDelay(30); |
||||
|
options.setUserName(username); |
||||
|
options.setPassword(password.toCharArray()); |
||||
|
options.setConnectionTimeout(timeout); |
||||
|
options.setKeepAliveInterval(keepalive); |
||||
|
com.hxjt.dataupload.mqtt.MqttClient.setClient(client); |
||||
|
try { |
||||
|
//设置消息到达的回调
|
||||
|
client.setCallback(pushCallback); |
||||
|
//连接到服务器
|
||||
|
client.connect(options); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 根据主题推送数据 |
||||
|
* @param qos |
||||
|
* @param retained |
||||
|
* @param topic |
||||
|
* @param pushMessage |
||||
|
* @return |
||||
|
*/ |
||||
|
public RespObject publish(int qos, boolean retained, String topic, String pushMessage) { |
||||
|
MqttMessage message = new MqttMessage(); |
||||
|
message.setQos(qos); |
||||
|
message.setRetained(retained); |
||||
|
message.setPayload(pushMessage.getBytes()); |
||||
|
MqttTopic mTopic = com.hxjt.dataupload.mqtt.MqttClient.getClient().getTopic(topic); |
||||
|
if (null == mTopic) { |
||||
|
logger.error("topic not exist"); |
||||
|
} |
||||
|
MqttDeliveryToken token; |
||||
|
try { |
||||
|
token = mTopic.publish(message); |
||||
|
token.waitForCompletion(); |
||||
|
return RespObject.of(); |
||||
|
} catch (MqttPersistenceException e) { |
||||
|
e.printStackTrace(); |
||||
|
return RespObject.toError(false, e.getMessage()); |
||||
|
} catch (MqttException e) { |
||||
|
e.printStackTrace(); |
||||
|
return RespObject.toError(false, e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 订阅到主题 |
||||
|
* @param topic |
||||
|
* @param qos |
||||
|
*/ |
||||
|
public void subscribe(String topic, int qos) { |
||||
|
logger.info("开始订阅主题" + topic); |
||||
|
try { |
||||
|
com.hxjt.dataupload.mqtt.MqttClient.getClient().subscribe(topic, qos); |
||||
|
} catch (MqttException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,121 @@ |
|||||
|
package com.hxjt.dataupload.mqtt; |
||||
|
|
||||
|
|
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
|
import org.springframework.context.annotation.Bean; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
import org.springframework.util.StringUtils; |
||||
|
|
||||
|
@Component |
||||
|
@ConfigurationProperties("spring.mqtt") |
||||
|
|
||||
|
public class MqttConfig { |
||||
|
@Autowired |
||||
|
private com.hxjt.dataupload.mqtt.MqttClient mqttClient; |
||||
|
|
||||
|
private String username; |
||||
|
|
||||
|
private String password; |
||||
|
|
||||
|
private String hostUrl; |
||||
|
|
||||
|
private String clientId; |
||||
|
|
||||
|
private String defaultTopic; |
||||
|
|
||||
|
private int timeout; |
||||
|
|
||||
|
private int keepalive; |
||||
|
|
||||
|
private boolean enabled; |
||||
|
|
||||
|
@Bean |
||||
|
public com.hxjt.dataupload.mqtt.MqttClient getMqttPushClient() { |
||||
|
try { |
||||
|
if(enabled == true){ |
||||
|
String mqtt_topic[] = StringUtils.split(defaultTopic, ","); |
||||
|
mqttClient.connect(hostUrl, clientId, username, password, timeout, keepalive);//连接
|
||||
|
for(int i=0; i<mqtt_topic.length; i++){ |
||||
|
mqttClient.subscribe(mqtt_topic[i], 0);//订阅主题
|
||||
|
} |
||||
|
} |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
return mqttClient; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public MqttClient getMqttClient() { |
||||
|
return mqttClient; |
||||
|
} |
||||
|
|
||||
|
public void setMqttClient(MqttClient mqttClient) { |
||||
|
this.mqttClient = mqttClient; |
||||
|
} |
||||
|
|
||||
|
public String getUsername() { |
||||
|
return username; |
||||
|
} |
||||
|
|
||||
|
public void setUsername(String username) { |
||||
|
this.username = username; |
||||
|
} |
||||
|
|
||||
|
public String getPassword() { |
||||
|
return password; |
||||
|
} |
||||
|
|
||||
|
public void setPassword(String password) { |
||||
|
this.password = password; |
||||
|
} |
||||
|
|
||||
|
public String getHostUrl() { |
||||
|
return hostUrl; |
||||
|
} |
||||
|
|
||||
|
public void setHostUrl(String hostUrl) { |
||||
|
this.hostUrl = hostUrl; |
||||
|
} |
||||
|
|
||||
|
public String getClientId() { |
||||
|
return clientId; |
||||
|
} |
||||
|
|
||||
|
public void setClientId(String clientId) { |
||||
|
this.clientId = clientId; |
||||
|
} |
||||
|
|
||||
|
public String getDefaultTopic() { |
||||
|
return defaultTopic; |
||||
|
} |
||||
|
|
||||
|
public void setDefaultTopic(String defaultTopic) { |
||||
|
this.defaultTopic = defaultTopic; |
||||
|
} |
||||
|
|
||||
|
public int getTimeout() { |
||||
|
return timeout; |
||||
|
} |
||||
|
|
||||
|
public void setTimeout(int timeout) { |
||||
|
this.timeout = timeout; |
||||
|
} |
||||
|
|
||||
|
public int getKeepalive() { |
||||
|
return keepalive; |
||||
|
} |
||||
|
|
||||
|
public void setKeepalive(int keepalive) { |
||||
|
this.keepalive = keepalive; |
||||
|
} |
||||
|
|
||||
|
public boolean isEnabled() { |
||||
|
return enabled; |
||||
|
} |
||||
|
|
||||
|
public void setEnabled(boolean enabled) { |
||||
|
this.enabled = enabled; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,96 @@ |
|||||
|
package com.hxjt.dataupload.mqtt; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
|
||||
|
|
||||
|
public class RespObject<T> implements Serializable { |
||||
|
|
||||
|
private boolean successful; |
||||
|
|
||||
|
private String resultHint; |
||||
|
|
||||
|
private Integer resultCode; |
||||
|
|
||||
|
private T resultValue; |
||||
|
|
||||
|
public static com.hxjt.dataupload.mqtt.RespObject of() { |
||||
|
return new com.hxjt.dataupload.mqtt.RespObject(true, "OK", 0, (Object)null); |
||||
|
} |
||||
|
|
||||
|
public static <T> com.hxjt.dataupload.mqtt.RespObject<T> of(T resultValue) { |
||||
|
return new com.hxjt.dataupload.mqtt.RespObject(true, "OK", 0, resultValue); |
||||
|
} |
||||
|
|
||||
|
public static com.hxjt.dataupload.mqtt.RespObject toHint(boolean successful, String resultHint) { |
||||
|
return new com.hxjt.dataupload.mqtt.RespObject(successful, resultHint, 1, (Object)null); |
||||
|
} |
||||
|
|
||||
|
public static <T> com.hxjt.dataupload.mqtt.RespObject<T> toHint(boolean successful, String resultHint, T resultValue) { |
||||
|
return new com.hxjt.dataupload.mqtt.RespObject(successful, resultHint, 1, resultValue); |
||||
|
} |
||||
|
|
||||
|
public static <T> com.hxjt.dataupload.mqtt.RespObject<T> toLogin(boolean successful, String resultHint) { |
||||
|
return new com.hxjt.dataupload.mqtt.RespObject(successful, resultHint, 2, null); |
||||
|
} |
||||
|
|
||||
|
public static <T> com.hxjt.dataupload.mqtt.RespObject<T> toInfo(boolean successful, String resultHint) { |
||||
|
return new com.hxjt.dataupload.mqtt.RespObject(successful, resultHint, 1001, null); |
||||
|
} |
||||
|
|
||||
|
public static <T> com.hxjt.dataupload.mqtt.RespObject<T> toWarn(boolean successful, String resultHint) { |
||||
|
return new com.hxjt.dataupload.mqtt.RespObject(successful, resultHint, 1002, null); |
||||
|
} |
||||
|
|
||||
|
public static <T> com.hxjt.dataupload.mqtt.RespObject<T> toError(boolean successful, String resultHint) { |
||||
|
return new com.hxjt.dataupload.mqtt.RespObject(successful, resultHint, 1003, null); |
||||
|
} |
||||
|
|
||||
|
public static <T> com.hxjt.dataupload.mqtt.RespObject<T> toError(boolean successful, String resultHint, Integer code, T resultValue) { |
||||
|
return new com.hxjt.dataupload.mqtt.RespObject(successful, resultHint, code, resultValue); |
||||
|
} |
||||
|
|
||||
|
public RespObject() { |
||||
|
} |
||||
|
|
||||
|
public RespObject(boolean successful, String resultHint, Integer resultCode, T resultValue) { |
||||
|
this.successful = successful; |
||||
|
this.resultHint = resultHint; |
||||
|
this.resultCode = resultCode; |
||||
|
this.resultValue = resultValue; |
||||
|
} |
||||
|
|
||||
|
public boolean isSuccessful() { |
||||
|
return this.successful; |
||||
|
} |
||||
|
|
||||
|
public void setSuccessful(boolean successful) { |
||||
|
this.successful = successful; |
||||
|
} |
||||
|
|
||||
|
public String getResultHint() { |
||||
|
return this.resultHint; |
||||
|
} |
||||
|
|
||||
|
public void setResultHint(String resultHint) { |
||||
|
this.resultHint = resultHint; |
||||
|
} |
||||
|
|
||||
|
public Integer getResultCode() { |
||||
|
return this.resultCode; |
||||
|
} |
||||
|
|
||||
|
public void setResultCode(Integer resultCode) { |
||||
|
this.resultCode = resultCode; |
||||
|
} |
||||
|
|
||||
|
public T getResultValue() { |
||||
|
return this.resultValue; |
||||
|
} |
||||
|
|
||||
|
public void setResultValue(T resultValue) { |
||||
|
this.resultValue = resultValue; |
||||
|
} |
||||
|
|
||||
|
public interface Result { |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue