Commit 35bde26334eead63ab01aa0f069aba00a067c125

Authored by 黄 x
1 parent 6670b868

revert: 回滚设备状态相关BUG

@@ -245,7 +245,11 @@ public class YtDeviceController extends BaseController { @@ -245,7 +245,11 @@ public class YtDeviceController extends BaseController {
245 queryMap.put("alarmStatus", alarmStatus); 245 queryMap.put("alarmStatus", alarmStatus);
246 queryMap.put("profileId", profileId); 246 queryMap.put("profileId", profileId);
247 if (deviceState != null) { 247 if (deviceState != null) {
248 - queryMap.put("deviceState", deviceState.name()); 248 + if(deviceState != DeviceState.INACTIVE){
  249 + queryMap.put("deviceState", deviceState == DeviceState.ONLINE);
  250 + }else{
  251 + queryMap.put("activeState",DeviceState.INACTIVE);
  252 + }
249 } 253 }
250 if (deviceType != null) { 254 if (deviceType != null) {
251 queryMap.put("deviceType", deviceType.name()); 255 queryMap.put("deviceType", deviceType.name());
@@ -22,14 +22,9 @@ public class YtDevice extends TenantBaseEntity { @@ -22,14 +22,9 @@ public class YtDevice extends TenantBaseEntity {
22 22
23 @TableField(typeHandler = JacksonTypeHandler.class) 23 @TableField(typeHandler = JacksonTypeHandler.class)
24 private JsonNode deviceInfo; 24 private JsonNode deviceInfo;
25 -  
26 - private LocalDateTime activeTime;  
27 - @TableField(typeHandler = EnumTypeHandler.class)  
28 - private DeviceState deviceState;  
29 private String profileId; 25 private String profileId;
30 private String tbDeviceId; 26 private String tbDeviceId;
31 private String deviceTypeId; 27 private String deviceTypeId;
32 - private LocalDateTime lastConnectTime;  
33 private String label; 28 private String label;
34 @TableField(typeHandler = EnumTypeHandler.class) 29 @TableField(typeHandler = EnumTypeHandler.class)
35 private DeviceTypeEnum deviceType; 30 private DeviceTypeEnum deviceType;
@@ -79,23 +79,21 @@ public class HomePageServiceImpl implements HomePageService { @@ -79,23 +79,21 @@ public class HomePageServiceImpl implements HomePageService {
79 LocalDateTime endTime = LocalDateTime.of(nowTime.toLocalDate(), LocalTime.MAX); 79 LocalDateTime endTime = LocalDateTime.of(nowTime.toLocalDate(), LocalTime.MAX);
80 HomePageTopMessage messageInfo = new HomePageTopMessage(FastIotConstants.MagicNumber.ZERO); 80 HomePageTopMessage messageInfo = new HomePageTopMessage(FastIotConstants.MagicNumber.ZERO);
81 BaseHomePageTop alarm = new BaseHomePageTop(FastIotConstants.MagicNumber.ZERO); 81 BaseHomePageTop alarm = new BaseHomePageTop(FastIotConstants.MagicNumber.ZERO);
82 - List<YtDevice> deviceList; 82 + Map<String,Object> queryMap = new HashMap<>();
  83 + List<DeviceDTO> deviceList;
83 if (isPtSysAdmin || isPtAdmin) { 84 if (isPtSysAdmin || isPtAdmin) {
84 - deviceList = deviceMapper.selectList(new LambdaQueryWrapper<>());  
85 setTenantInfoData(homePageLeftTopDTO, startTime, endTime); 85 setTenantInfoData(homePageLeftTopDTO, startTime, endTime);
86 setCustomerInfoData(homePageLeftTopDTO, startTime, endTime); 86 setCustomerInfoData(homePageLeftTopDTO, startTime, endTime);
87 } else if (isTenantAdmin) { 87 } else if (isTenantAdmin) {
88 - deviceList =  
89 - deviceMapper.selectList(  
90 - new LambdaQueryWrapper<YtDevice>().eq(YtDevice::getTenantId, tenantId)); 88 + queryMap.put("tenantId",tenantId);
91 setAlarmAndMessageInfo(tenantId, messageInfo, alarm); 89 setAlarmAndMessageInfo(tenantId, messageInfo, alarm);
92 90
93 } else { 91 } else {
94 - User user = userMapper.selectById(currentUserId);  
95 - deviceList =  
96 - ReflectUtils.sourceToTarget(  
97 - deviceMapper.getDeviceByCustomerTbUserId(user.getTbUser()), YtDevice.class); 92 + String customerId = userMapper.findCustomerIdByUserId(currentUserId);
  93 + //查询customerId
  94 + queryMap.put("customerId",customerId);
98 } 95 }
  96 + deviceList = deviceMapper.findDevices(queryMap);
99 homeDeviceInfo = new HomeDeviceInfoDTO(FastIotConstants.MagicNumber.ZERO); 97 homeDeviceInfo = new HomeDeviceInfoDTO(FastIotConstants.MagicNumber.ZERO);
100 if (null != deviceList) { 98 if (null != deviceList) {
101 setDeviceInfoData(deviceList, homeDeviceInfo); 99 setDeviceInfoData(deviceList, homeDeviceInfo);
@@ -223,7 +221,7 @@ public class HomePageServiceImpl implements HomePageService { @@ -223,7 +221,7 @@ public class HomePageServiceImpl implements HomePageService {
223 * @param deviceList 拥有的设备 221 * @param deviceList 拥有的设备
224 * @param homeDeviceInfoDTO 首页信息 222 * @param homeDeviceInfoDTO 首页信息
225 */ 223 */
226 - private void setDeviceInfoData(List<YtDevice> deviceList, HomeDeviceInfoDTO homeDeviceInfoDTO) { 224 + private void setDeviceInfoData(List<DeviceDTO> deviceList, HomeDeviceInfoDTO homeDeviceInfoDTO) {
227 homeDeviceInfoDTO.setSumCount(deviceList.size()); 225 homeDeviceInfoDTO.setSumCount(deviceList.size());
228 deviceList.forEach( 226 deviceList.forEach(
229 ytDevice -> { 227 ytDevice -> {
@@ -10,7 +10,6 @@ import org.apache.commons.lang3.StringUtils; @@ -10,7 +10,6 @@ import org.apache.commons.lang3.StringUtils;
10 import org.springframework.stereotype.Service; 10 import org.springframework.stereotype.Service;
11 import org.springframework.transaction.annotation.Transactional; 11 import org.springframework.transaction.annotation.Transactional;
12 import org.thingsboard.server.common.data.DeviceProfile; 12 import org.thingsboard.server.common.data.DeviceProfile;
13 -import org.thingsboard.server.common.data.id.DeviceId;  
14 import org.thingsboard.server.common.data.id.EntityId; 13 import org.thingsboard.server.common.data.id.EntityId;
15 import org.thingsboard.server.common.data.id.TenantId; 14 import org.thingsboard.server.common.data.id.TenantId;
16 import org.thingsboard.server.common.data.security.DeviceCredentials; 15 import org.thingsboard.server.common.data.security.DeviceCredentials;
@@ -51,14 +50,14 @@ public class YtDeviceServiceImpl extends AbstractBaseService<DeviceMapper, YtDev @@ -51,14 +50,14 @@ public class YtDeviceServiceImpl extends AbstractBaseService<DeviceMapper, YtDev
51 @Transactional 50 @Transactional
52 public DeviceDTO insertOrUpdate(String tenantId, DeviceDTO deviceDTO) { 51 public DeviceDTO insertOrUpdate(String tenantId, DeviceDTO deviceDTO) {
53 52
54 - if (StringUtils.isBlank(deviceDTO.getId())) {  
55 - return insert(deviceDTO);  
56 - } else {  
57 - return update(tenantId, deviceDTO);  
58 - } 53 + if (StringUtils.isBlank(deviceDTO.getId())) {
  54 + return insert(deviceDTO);
  55 + } else {
  56 + return update(deviceDTO);
59 } 57 }
  58 + }
60 59
61 - private DeviceDTO update(String tenantId, DeviceDTO deviceDTO) { 60 + private DeviceDTO update(DeviceDTO deviceDTO) {
62 61
63 validateUpdate(deviceDTO); 62 validateUpdate(deviceDTO);
64 63
@@ -84,64 +83,25 @@ public class YtDeviceServiceImpl extends AbstractBaseService<DeviceMapper, YtDev @@ -84,64 +83,25 @@ public class YtDeviceServiceImpl extends AbstractBaseService<DeviceMapper, YtDev
84 } 83 }
85 } 84 }
86 85
87 - private boolean detectDeviceChange(DeviceDTO deviceDTO, YtDevice device) {  
88 - boolean changed =  
89 - !deviceDTO.getName().equalsIgnoreCase(device.getName())  
90 - || !deviceDTO.getProfileId().equalsIgnoreCase(device.getProfileId());  
91 - if (changed) {  
92 - return true;  
93 - }  
94 - boolean dtoGateway =  
95 - Optional.ofNullable(deviceDTO.getDeviceType())  
96 - .map(deviceType -> deviceType.equals(DeviceTypeEnum.GATEWAY))  
97 - .orElse(false);  
98 - boolean entityGateway =  
99 - Optional.ofNullable(device.getDeviceType())  
100 - .map(deviceType -> deviceType.equals(DeviceTypeEnum.GATEWAY))  
101 - .orElse(false);  
102 - if (entityGateway != dtoGateway) {  
103 - changed = true;  
104 - }  
105 - if (changed) {  
106 - return true;  
107 - }  
108 - String dtoDesc =  
109 - Optional.ofNullable(deviceDTO.getDeviceInfo())  
110 - .map(deviceInfo -> deviceInfo.get("description"))  
111 - .map(JsonNode::asText)  
112 - .orElse("");  
113 - String dtoLabel = deviceDTO.getLabel() == null ? "" : deviceDTO.getLabel();  
114 - String entityDesc =  
115 - Optional.ofNullable(device.getDeviceInfo())  
116 - .map(deviceInfo -> deviceInfo.get("description"))  
117 - .map(JsonNode::asText)  
118 - .orElse("");  
119 - String entityLabel = device.getLabel() == null ? "" : device.getLabel();  
120 - if (!dtoDesc.equals(entityDesc) || !dtoLabel.equals(entityLabel)) {  
121 - changed = true;  
122 - }  
123 - return changed; 86 + @Override
  87 + public boolean validateFormdata(String currentTenantId, DeviceDTO deviceDTO) {
  88 + boolean insert = StringUtils.isBlank(deviceDTO.getId());
  89 + String deviceTenantId = deviceDTO.getTenantId();
  90 + if (StringUtils.isBlank(deviceDTO.getName())) {
  91 + throw new YtDataValidationException("device name cannot be blank");
  92 + }
  93 + // validate IOT DB
  94 + if (StringUtils.isBlank(deviceDTO.getProfileId())) {
  95 + throw new YtDataValidationException("device profile cannot be blank");
124 } 96 }
125 97
126 - @Override  
127 - public boolean validateFormdata(String currentTenantId, DeviceDTO deviceDTO) {  
128 - boolean insert = StringUtils.isBlank(deviceDTO.getId());  
129 - String deviceTenantId = deviceDTO.getTenantId();  
130 - if (StringUtils.isBlank(deviceDTO.getName())) {  
131 - throw new YtDataValidationException("device name cannot be blank");  
132 - }  
133 - // validate IOT DB  
134 - if (StringUtils.isBlank(deviceDTO.getProfileId())) {  
135 - throw new YtDataValidationException("device profile cannot be blank");  
136 - }  
137 -  
138 - // 验证设备名称是否已经存在 如果此处直接使用deviceDTO 将有误  
139 - if (insert) {  
140 - DeviceDTO check = new DeviceDTO();  
141 - check.setName(deviceDTO.getName());  
142 - if (findTbDeviceId(deviceTenantId, check).size() > 0) {  
143 - throw new YtDataValidationException(ErrorMessage.NAME_ALREADY_EXISTS.getMessage());  
144 - } 98 + // 验证设备名称是否已经存在 如果此处直接使用deviceDTO 将有误
  99 + if (insert) {
  100 + DeviceDTO check = new DeviceDTO();
  101 + check.setName(deviceDTO.getName());
  102 + if (findTbDeviceId(deviceTenantId, check).size() > 0) {
  103 + throw new YtDataValidationException(ErrorMessage.NAME_ALREADY_EXISTS.getMessage());
  104 + }
145 105
146 deviceTenantId = currentTenantId; 106 deviceTenantId = currentTenantId;
147 deviceDTO.setTenantId(currentTenantId); 107 deviceDTO.setTenantId(currentTenantId);
@@ -168,30 +128,23 @@ public class YtDeviceServiceImpl extends AbstractBaseService<DeviceMapper, YtDev @@ -168,30 +128,23 @@ public class YtDeviceServiceImpl extends AbstractBaseService<DeviceMapper, YtDev
168 return true; 128 return true;
169 } 129 }
170 130
171 - private DeviceDTO insert(DeviceDTO deviceDTO) { 131 + private DeviceDTO insert(DeviceDTO deviceDTO) {
172 132
173 - YtDevice device = new YtDevice();  
174 - deviceDTO.copyToEntity(  
175 - device,  
176 - ModelConstants.TablePropertyMapping.ACTIVE_TIME,  
177 - ModelConstants.TablePropertyMapping.DEVICE_STATE,  
178 - ModelConstants.TablePropertyMapping.CREATOR,  
179 - ModelConstants.TablePropertyMapping.UPDATER,  
180 - ModelConstants.TablePropertyMapping.CREATE_TIME,  
181 - ModelConstants.TablePropertyMapping.UPDATE,  
182 - ModelConstants.TablePropertyMapping.UPDATE_TIME); 133 + YtDevice device = new YtDevice();
  134 + deviceDTO.copyToEntity(
  135 + device,
  136 + ModelConstants.TablePropertyMapping.ACTIVE_TIME,
  137 + ModelConstants.TablePropertyMapping.DEVICE_STATE,
  138 + ModelConstants.TablePropertyMapping.CREATOR,
  139 + ModelConstants.TablePropertyMapping.UPDATER,
  140 + ModelConstants.TablePropertyMapping.CREATE_TIME,
  141 + ModelConstants.TablePropertyMapping.UPDATE,
  142 + ModelConstants.TablePropertyMapping.UPDATE_TIME);
183 143
184 - device.setAlarmStatus(0);  
185 - DeviceState deviceState;  
186 - if (null != deviceDTO.getDeviceState()) {  
187 - deviceState = deviceDTO.getDeviceState();  
188 - } else {  
189 - deviceState = DeviceState.INACTIVE;  
190 - }  
191 - device.setDeviceState(deviceState);  
192 - baseMapper.insert(device);  
193 - return device.getDTO(DeviceDTO.class);  
194 - } 144 + device.setAlarmStatus(0);
  145 + baseMapper.insert(device);
  146 + return device.getDTO(DeviceDTO.class);
  147 + }
195 148
196 @Override 149 @Override
197 public List<String> findTbDeviceId(String tenantId, Set<String> ids) { 150 public List<String> findTbDeviceId(String tenantId, Set<String> ids) {
@@ -5,7 +5,6 @@ import com.baomidou.mybatisplus.core.metadata.IPage; @@ -5,7 +5,6 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
5 import org.apache.ibatis.annotations.Mapper; 5 import org.apache.ibatis.annotations.Mapper;
6 import org.apache.ibatis.annotations.Param; 6 import org.apache.ibatis.annotations.Param;
7 import org.thingsboard.server.common.data.Device; 7 import org.thingsboard.server.common.data.Device;
8 -import org.thingsboard.server.common.data.id.DeviceId;  
9 import org.thingsboard.server.common.data.yunteng.dto.DeviceDTO; 8 import org.thingsboard.server.common.data.yunteng.dto.DeviceDTO;
10 import org.thingsboard.server.dao.yunteng.entities.YtDevice; 9 import org.thingsboard.server.dao.yunteng.entities.YtDevice;
11 10
@@ -21,14 +20,6 @@ public interface DeviceMapper extends BaseMapper<YtDevice> { @@ -21,14 +20,6 @@ public interface DeviceMapper extends BaseMapper<YtDevice> {
21 DeviceDTO selectDetail(@Param("tenantId") String tenantId, @Param("id") String id); 20 DeviceDTO selectDetail(@Param("tenantId") String tenantId, @Param("id") String id);
22 21
23 /** 22 /**
24 - * 通过客户的ID,查询改客户能查看的所有设备  
25 - *  
26 - * @param userId 客户ID  
27 - * @return 能查看的设备  
28 - */  
29 - List<DeviceDTO> getDeviceByCustomerTbUserId(@Param("userId") String userId);  
30 -  
31 - /**  
32 * 通过网关子设备的TB设备ID查询网关设备信息 23 * 通过网关子设备的TB设备ID查询网关设备信息
33 * @param tbDeviceId 网关子设备的TB设备ID 24 * @param tbDeviceId 网关子设备的TB设备ID
34 * @return 网关设备信息 25 * @return 网关设备信息
@@ -43,4 +34,11 @@ public interface DeviceMapper extends BaseMapper<YtDevice> { @@ -43,4 +34,11 @@ public interface DeviceMapper extends BaseMapper<YtDevice> {
43 * @return 34 * @return
44 */ 35 */
45 boolean freshAlarmStatus(@Param("tbDeviceId") String tbDeviceId,@Param("created") Integer created); 36 boolean freshAlarmStatus(@Param("tbDeviceId") String tbDeviceId,@Param("created") Integer created);
  37 +
  38 + /**
  39 + * 用于统计设备相关信息
  40 + * @param queryMap 查询条件
  41 + * @return 查询的设备信息
  42 + */
  43 + List<DeviceDTO> findDevices(@Param("queryMap") Map<String,Object> queryMap);
46 } 44 }
@@ -41,4 +41,6 @@ public interface UserMapper extends BaseMapper<User> { @@ -41,4 +41,6 @@ public interface UserMapper extends BaseMapper<User> {
41 List<UserDTO> getMyCustomers(@Param("tenantId") String tenantId, @Param("customerId") String customerId, @Param("userIds") Collection<String> userIds); 41 List<UserDTO> getMyCustomers(@Param("tenantId") String tenantId, @Param("customerId") String customerId, @Param("userIds") Collection<String> userIds);
42 42
43 List<UserDTO> findUsersAsyncByTs(@Param("startTs") String startTs,@Param("endTs") String endTs); 43 List<UserDTO> findUsersAsyncByTs(@Param("startTs") String startTs,@Param("endTs") String endTs);
  44 +
  45 + String findCustomerIdByUserId(@Param("id") String id);
44 } 46 }
@@ -72,8 +72,12 @@ @@ -72,8 +72,12 @@
72 <if test="queryMap.deviceType !=null and queryMap.deviceType !=''"> 72 <if test="queryMap.deviceType !=null and queryMap.deviceType !=''">
73 AND ifd.device_type = #{queryMap.deviceType} 73 AND ifd.device_type = #{queryMap.deviceType}
74 </if> 74 </if>
75 - <if test="queryMap.deviceState !=null and queryMap.deviceState !=''">  
76 - AND ifd.device_state = #{queryMap.deviceState} 75 + <if test="queryMap.deviceState !=null">
  76 + AND a.bool_v = #{queryMap.deviceState}
  77 + AND b.long_v IS NOT NULL
  78 + </if>
  79 + <if test="queryMap.activeState !=null">
  80 + AND b.long_v IS NULL
77 </if> 81 </if>
78 <if test="queryMap.customerId !=null"> 82 <if test="queryMap.customerId !=null">
79 AND d.customer_id::TEXT = #{queryMap.customerId} 83 AND d.customer_id::TEXT = #{queryMap.customerId}
@@ -107,14 +111,6 @@ @@ -107,14 +111,6 @@
107 111
108 </where> 112 </where>
109 </select> 113 </select>
110 -  
111 - <select id="getDeviceByCustomerTbUserId" resultMap="deviceMap">  
112 - SELECT <include refid="basicColumns"/> FROM iotfs_device ifd  
113 - LEFT JOIN device d ON d.ID :: TEXT = ifd.tb_device_id  
114 - LEFT JOIN tb_user tu ON d.customer_id = tu.customer_id  
115 - WHERE tu.id::TEXT = #{userId}  
116 - </select>  
117 -  
118 <select id="findGateWayDeviceByTbDeviceId" resultMap="deviceMap"> 114 <select id="findGateWayDeviceByTbDeviceId" resultMap="deviceMap">
119 SELECT 115 SELECT
120 <include refid="basicColumns"/> 116 <include refid="basicColumns"/>
@@ -131,4 +127,40 @@ @@ -131,4 +127,40 @@
131 SET alarm_status=#{created} 127 SET alarm_status=#{created}
132 WHERE tb_device_id = #{tbDeviceId} 128 WHERE tb_device_id = #{tbDeviceId}
133 </update> 129 </update>
  130 +
  131 + <select id="findDevices" resultMap="deviceMap">
  132 + SELECT
  133 + ifd.ID,
  134 + ifd.NAME,
  135 + ifd.tenant_id,
  136 + ifd.organization_id,
  137 + ifd.create_time,
  138 + ifdp.NAME AS profile_name,
  139 + io.NAME AS organization_name,
  140 + A.bool_v status,
  141 + b.long_v last_online_time,
  142 + d.customer_id :: TEXT AS customer_id
  143 + FROM
  144 + iotfs_device ifd
  145 + LEFT JOIN device_profile ifdp ON ifd.profile_id = ifdp.ID ::
  146 + TEXT LEFT JOIN iotfs_organization io ON io.ID = ifd.organization_id
  147 + LEFT JOIN attribute_kv A ON ifd.tb_device_id = A.entity_id :: TEXT
  148 + AND A.entity_type = 'DEVICE'
  149 + AND A.attribute_key = 'active'
  150 + LEFT JOIN attribute_kv b ON ifd.tb_device_id = b.entity_id :: TEXT
  151 + AND b.entity_type = 'DEVICE'
  152 + AND b.attribute_key = 'lastActivityTime'
  153 + LEFT JOIN attribute_kv C ON ifd.tb_device_id = C.entity_id :: TEXT
  154 + AND C.entity_type = 'DEVICE'
  155 + AND C.attribute_key = 'inactivityAlarmTime'
  156 + LEFT JOIN device d ON d.ID :: TEXT = ifd.tb_device_id
  157 + <where>
  158 + <if test="queryMap.tenantId !=null and queryMap.tenantId !=''">
  159 + AND ifd.tenant_id = #{queryMap.tenantId}
  160 + </if>
  161 + <if test="queryMap.customerId !=null and queryMap.customerId !=''">
  162 + AND d.customer_id = #{queryMap.customerId}
  163 + </if>
  164 + </where>
  165 + </select>
134 </mapper> 166 </mapper>
@@ -202,4 +202,8 @@ @@ -202,4 +202,8 @@
202 AND su.create_time &gt;= #{startTs} ::TIMESTAMP 202 AND su.create_time &gt;= #{startTs} ::TIMESTAMP
203 AND su.create_time &lt;#{endTs} ::TIMESTAMP 203 AND su.create_time &lt;#{endTs} ::TIMESTAMP
204 </select> 204 </select>
  205 +
  206 + <select id="findCustomerIdByUserId" resultType="java.lang.String">
  207 + SELECT tu.customer_id FROM sys_user su LEFT JOIN tb_user tu ON su.tb_user = tu.id WHERE su.id = #{id}
  208 + </select>
205 </mapper> 209 </mapper>