59 changed files with 2028 additions and 2165 deletions
@ -0,0 +1,100 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.entitiy.device.profile; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.DeviceProfile; |
|||
import org.thingsboard.server.common.data.User; |
|||
import org.thingsboard.server.common.data.audit.ActionType; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|||
import org.thingsboard.server.common.data.id.DeviceProfileId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; |
|||
import org.thingsboard.server.dao.device.DeviceProfileService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.entitiy.AbstractTbEntityService; |
|||
import org.thingsboard.server.service.ota.OtaPackageStateService; |
|||
|
|||
import java.util.Objects; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@AllArgsConstructor |
|||
@Slf4j |
|||
public class DefaultTbDeviceProfileService extends AbstractTbEntityService implements TbDeviceProfileService { |
|||
|
|||
private final DeviceProfileService deviceProfileService; |
|||
private final OtaPackageStateService otaPackageStateService; |
|||
|
|||
@Override |
|||
public DeviceProfile save(DeviceProfile deviceProfile, User user) throws ThingsboardException { |
|||
ActionType actionType = deviceProfile.getId() == null ? ActionType.ADDED : ActionType.UPDATED; |
|||
TenantId tenantId = deviceProfile.getTenantId(); |
|||
boolean isFirmwareChanged = false; |
|||
boolean isSoftwareChanged = false; |
|||
|
|||
if (actionType.equals(ActionType.UPDATED)) { |
|||
DeviceProfile oldDeviceProfile = deviceProfileService.findDeviceProfileById(tenantId, deviceProfile.getId()); |
|||
if (!Objects.equals(deviceProfile.getFirmwareId(), oldDeviceProfile.getFirmwareId())) { |
|||
isFirmwareChanged = true; |
|||
} |
|||
if (!Objects.equals(deviceProfile.getSoftwareId(), oldDeviceProfile.getSoftwareId())) { |
|||
isSoftwareChanged = true; |
|||
} |
|||
} |
|||
DeviceProfile savedDeviceProfile = checkNotNull(deviceProfileService.saveDeviceProfile(deviceProfile)); |
|||
|
|||
tbClusterService.onDeviceProfileChange(savedDeviceProfile, null); |
|||
tbClusterService.broadcastEntityStateChangeEvent(tenantId, savedDeviceProfile.getId(), |
|||
actionType.equals(ActionType.ADDED) ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED); |
|||
|
|||
otaPackageStateService.update(savedDeviceProfile, isFirmwareChanged, isSoftwareChanged); |
|||
|
|||
notificationEntityService.notifyCreateOrUpdateOrDelete(tenantId, null, savedDeviceProfile.getId(), |
|||
savedDeviceProfile, user, actionType, true, null); |
|||
return savedDeviceProfile; |
|||
} |
|||
|
|||
@Override |
|||
public void delete(DeviceProfile deviceProfile, User user) { |
|||
DeviceProfileId deviceProfileId = deviceProfile.getId(); |
|||
TenantId tenantId = deviceProfile.getTenantId(); |
|||
deviceProfileService.deleteDeviceProfile(tenantId, deviceProfileId); |
|||
|
|||
tbClusterService.onDeviceProfileDelete(deviceProfile, null); |
|||
tbClusterService.broadcastEntityStateChangeEvent(tenantId, deviceProfileId, ComponentLifecycleEvent.DELETED); |
|||
notificationEntityService.notifyCreateOrUpdateOrDelete(tenantId, null, deviceProfileId, deviceProfile, |
|||
user, ActionType.DELETED, true, null, deviceProfileId.toString()); |
|||
} |
|||
|
|||
@Override |
|||
public DeviceProfile setDefaultDeviceProfile(DeviceProfile deviceProfile, DeviceProfile previousDefaultDeviceProfile, User user) throws ThingsboardException { |
|||
TenantId tenantId = deviceProfile.getTenantId(); |
|||
if (deviceProfileService.setDefaultDeviceProfile(tenantId, deviceProfile.getId())) { |
|||
if (previousDefaultDeviceProfile != null) { |
|||
previousDefaultDeviceProfile = deviceProfileService.findDeviceProfileById(tenantId, previousDefaultDeviceProfile.getId()); |
|||
notificationEntityService.logEntityAction(tenantId, previousDefaultDeviceProfile.getId(), previousDefaultDeviceProfile, |
|||
ActionType.UPDATED, user); |
|||
} |
|||
deviceProfile = deviceProfileService.findDeviceProfileById(tenantId, deviceProfile.getId()); |
|||
|
|||
notificationEntityService.logEntityAction(tenantId, deviceProfile.getId(), deviceProfile, ActionType.UPDATED, user); |
|||
} |
|||
return deviceProfile; |
|||
} |
|||
} |
|||
@ -1,111 +0,0 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.entitiy.deviceProfile; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.DeviceProfile; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.audit.ActionType; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|||
import org.thingsboard.server.common.data.id.DeviceProfileId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.entitiy.AbstractTbEntityService; |
|||
import org.thingsboard.server.service.security.model.SecurityUser; |
|||
|
|||
import java.util.Objects; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@AllArgsConstructor |
|||
@Slf4j |
|||
public class DefaultTbDeviceProfileService extends AbstractTbEntityService implements TbDeviceProfileService { |
|||
@Override |
|||
public DeviceProfile save(DeviceProfile deviceProfile, SecurityUser user) throws ThingsboardException { |
|||
ActionType actionType = deviceProfile.getId() == null ? ActionType.ADDED : ActionType.UPDATED; |
|||
TenantId tenantId = deviceProfile.getTenantId(); |
|||
try { |
|||
boolean isFirmwareChanged = false; |
|||
boolean isSoftwareChanged = false; |
|||
|
|||
if (actionType.equals(ActionType.UPDATED)) { |
|||
DeviceProfile oldDeviceProfile = deviceProfileService.findDeviceProfileById(tenantId, deviceProfile.getId()); |
|||
if (!Objects.equals(deviceProfile.getFirmwareId(), oldDeviceProfile.getFirmwareId())) { |
|||
isFirmwareChanged = true; |
|||
} |
|||
if (!Objects.equals(deviceProfile.getSoftwareId(), oldDeviceProfile.getSoftwareId())) { |
|||
isSoftwareChanged = true; |
|||
} |
|||
} |
|||
DeviceProfile savedDeviceProfile = checkNotNull(deviceProfileService.saveDeviceProfile(deviceProfile)); |
|||
|
|||
tbClusterService.onDeviceProfileChange(savedDeviceProfile, null); |
|||
tbClusterService.broadcastEntityStateChangeEvent(tenantId, savedDeviceProfile.getId(), |
|||
actionType.equals(ActionType.ADDED) ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED); |
|||
|
|||
otaPackageStateService.update(savedDeviceProfile, isFirmwareChanged, isSoftwareChanged); |
|||
|
|||
notificationEntityService.notifyCreateOrUpdateOrDelete(tenantId, null, savedDeviceProfile.getId(), savedDeviceProfile, user, actionType, true, null); |
|||
return savedDeviceProfile; |
|||
} catch (Exception e) { |
|||
notificationEntityService.notifyCreateOrUpdateOrDelete(tenantId, null, emptyId(EntityType.DEVICE_PROFILE), deviceProfile, user, actionType, false, e); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void delete(DeviceProfile deviceProfile, SecurityUser user) throws ThingsboardException { |
|||
DeviceProfileId deviceProfileId = deviceProfile.getId(); |
|||
TenantId tenantId = deviceProfile.getTenantId(); |
|||
try { |
|||
deviceProfileService.deleteDeviceProfile(tenantId, deviceProfileId); |
|||
|
|||
tbClusterService.onDeviceProfileDelete(deviceProfile, null); |
|||
tbClusterService.broadcastEntityStateChangeEvent(tenantId, deviceProfileId, ComponentLifecycleEvent.DELETED); |
|||
notificationEntityService.notifyCreateOrUpdateOrDelete(tenantId, null, deviceProfileId, deviceProfile, user, ActionType.DELETED, true, null, deviceProfileId.toString()); |
|||
} catch (Exception e) { |
|||
notificationEntityService.notifyCreateOrUpdateOrDelete(tenantId, null, emptyId(EntityType.DEVICE_PROFILE), null, user, ActionType.DELETED, false, e, deviceProfileId.toString()); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public DeviceProfile setDefaultDeviceProfile(DeviceProfile deviceProfile, DeviceProfile previousDefaultDeviceProfile, SecurityUser user) throws ThingsboardException { |
|||
TenantId tenantId = deviceProfile.getTenantId(); |
|||
try { |
|||
|
|||
if (deviceProfileService.setDefaultDeviceProfile(tenantId, deviceProfile.getId())) { |
|||
if (previousDefaultDeviceProfile != null) { |
|||
previousDefaultDeviceProfile = deviceProfileService.findDeviceProfileById(tenantId, previousDefaultDeviceProfile.getId()); |
|||
notificationEntityService.notifyEntity(tenantId, previousDefaultDeviceProfile.getId(), previousDefaultDeviceProfile, null, |
|||
ActionType.UPDATED, user, null); |
|||
} |
|||
deviceProfile = deviceProfileService.findDeviceProfileById(tenantId, deviceProfile.getId()); |
|||
|
|||
notificationEntityService.notifyEntity(tenantId, deviceProfile.getId(), deviceProfile, null, |
|||
ActionType.UPDATED, user, null); |
|||
} |
|||
return deviceProfile; |
|||
} catch (Exception e) { |
|||
notificationEntityService.notifyEntity(tenantId, emptyId(EntityType.DEVICE_PROFILE), null, null, |
|||
ActionType.UPDATED, user, e, deviceProfile.getId().toString()); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.entitiy.entity.relation; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.User; |
|||
import org.thingsboard.server.common.data.audit.ActionType; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.relation.EntityRelation; |
|||
import org.thingsboard.server.dao.relation.RelationService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.entitiy.AbstractTbEntityService; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@AllArgsConstructor |
|||
@Slf4j |
|||
public class DefaultTbEntityRelationService extends AbstractTbEntityService implements TbEntityRelationService { |
|||
|
|||
private final RelationService relationService; |
|||
|
|||
@Override |
|||
public void save(TenantId tenantId, CustomerId customerId, EntityRelation relation, User user) throws ThingsboardException { |
|||
relationService.saveRelation(tenantId, relation); |
|||
notificationEntityService.notifyCreateOrUpdateRelation(tenantId, customerId, |
|||
relation, user, ActionType.RELATION_ADD_OR_UPDATE, relation); |
|||
} |
|||
|
|||
@Override |
|||
public void delete(TenantId tenantId, CustomerId customerId, EntityRelation relation, User user) throws ThingsboardException { |
|||
boolean found = relationService.deleteRelation(tenantId, relation.getFrom(), relation.getTo(), relation.getType(), relation.getTypeGroup()); |
|||
if (!found) { |
|||
throw new ThingsboardException("Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND); |
|||
} |
|||
notificationEntityService.notifyCreateOrUpdateRelation(tenantId, customerId, |
|||
relation, user, ActionType.RELATION_DELETED, relation); |
|||
} |
|||
|
|||
@Override |
|||
public void deleteRelations(TenantId tenantId, CustomerId customerId, EntityId entityId, User user) throws ThingsboardException { |
|||
relationService.deleteEntityRelations(tenantId, entityId); |
|||
notificationEntityService.logEntityAction(tenantId, entityId, null, customerId, ActionType.RELATIONS_DELETED, user); |
|||
} |
|||
} |
|||
@ -1,76 +0,0 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.entitiy.entityRelation; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.audit.ActionType; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.relation.EntityRelation; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.entitiy.AbstractTbEntityService; |
|||
import org.thingsboard.server.service.security.model.SecurityUser; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@AllArgsConstructor |
|||
@Slf4j |
|||
public class DefaultTbEntityRelationService extends AbstractTbEntityService implements TbEntityRelationService { |
|||
@Override |
|||
public void save(TenantId tenantId, CustomerId customerId, EntityRelation relation, SecurityUser user) throws ThingsboardException { |
|||
try { |
|||
relationService.saveRelation(tenantId, relation); |
|||
notificationEntityService.notifyCreateOrUpdateOrDeleteRelation (tenantId, customerId, |
|||
relation, user, ActionType.RELATION_ADD_OR_UPDATE, null, relation); |
|||
} catch (Exception e) { |
|||
notificationEntityService.notifyCreateOrUpdateOrDeleteRelation (tenantId, customerId, |
|||
relation, user, ActionType.RELATION_ADD_OR_UPDATE, e, relation); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void delete(TenantId tenantId, CustomerId customerId, EntityRelation relation, SecurityUser user) throws ThingsboardException { |
|||
try { |
|||
Boolean found = relationService.deleteRelation(tenantId, relation.getFrom(), relation.getTo(), relation.getType(), relation.getTypeGroup()); |
|||
if (!found) { |
|||
throw new ThingsboardException("Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND); |
|||
} |
|||
notificationEntityService.notifyCreateOrUpdateOrDeleteRelation (tenantId, customerId, |
|||
relation, user, ActionType.RELATION_DELETED, null, relation); |
|||
} catch (Exception e) { |
|||
notificationEntityService.notifyCreateOrUpdateOrDeleteRelation (tenantId, customerId, |
|||
relation, user, ActionType.RELATION_DELETED, e, relation); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void deleteRelations(TenantId tenantId, CustomerId customerId, EntityId entityId, SecurityUser user) throws ThingsboardException { |
|||
try { |
|||
relationService.deleteEntityRelations(tenantId, entityId); |
|||
notificationEntityService.notifyEntity(tenantId, entityId, null, customerId, ActionType.RELATIONS_DELETED, user, null); |
|||
} catch (Exception e) { |
|||
notificationEntityService.notifyEntity(tenantId, entityId, null, customerId, ActionType.RELATIONS_DELETED, user, e); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,91 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.entitiy.ota; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.OtaPackage; |
|||
import org.thingsboard.server.common.data.OtaPackageInfo; |
|||
import org.thingsboard.server.common.data.SaveOtaPackageInfoRequest; |
|||
import org.thingsboard.server.common.data.StringUtils; |
|||
import org.thingsboard.server.common.data.User; |
|||
import org.thingsboard.server.common.data.audit.ActionType; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|||
import org.thingsboard.server.common.data.id.OtaPackageId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.ota.ChecksumAlgorithm; |
|||
import org.thingsboard.server.dao.ota.OtaPackageService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.entitiy.AbstractTbEntityService; |
|||
|
|||
import java.nio.ByteBuffer; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@AllArgsConstructor |
|||
@Slf4j |
|||
public class DefaultTbOtaPackageService extends AbstractTbEntityService implements TbOtaPackageService { |
|||
|
|||
private final OtaPackageService otaPackageService; |
|||
|
|||
@Override |
|||
public OtaPackageInfo save(SaveOtaPackageInfoRequest saveOtaPackageInfoRequest, User user) throws ThingsboardException { |
|||
TenantId tenantId = saveOtaPackageInfoRequest.getTenantId(); |
|||
ActionType actionType = saveOtaPackageInfoRequest.getId() == null ? ActionType.ADDED : ActionType.UPDATED; |
|||
OtaPackageInfo savedOtaPackageInfo = checkNotNull(otaPackageService.saveOtaPackageInfo( |
|||
new OtaPackageInfo(saveOtaPackageInfoRequest), saveOtaPackageInfoRequest.isUsesUrl())); |
|||
notificationEntityService.logEntityAction(tenantId, savedOtaPackageInfo.getId(), savedOtaPackageInfo, actionType, user); |
|||
return savedOtaPackageInfo; |
|||
} |
|||
|
|||
@Override |
|||
public OtaPackageInfo saveOtaPackageData(OtaPackageInfo otaPackageInfo, String checksum, ChecksumAlgorithm checksumAlgorithm, |
|||
byte[] data, String filename, String contentType, User user) throws ThingsboardException { |
|||
TenantId tenantId = otaPackageInfo.getTenantId(); |
|||
OtaPackageId otaPackageId = otaPackageInfo.getId(); |
|||
if (StringUtils.isEmpty(checksum)) { |
|||
checksum = otaPackageService.generateChecksum(checksumAlgorithm, ByteBuffer.wrap(data)); |
|||
} |
|||
OtaPackage otaPackage = new OtaPackage(otaPackageId); |
|||
otaPackage.setCreatedTime(otaPackageInfo.getCreatedTime()); |
|||
otaPackage.setTenantId(tenantId); |
|||
otaPackage.setDeviceProfileId(otaPackageInfo.getDeviceProfileId()); |
|||
otaPackage.setType(otaPackageInfo.getType()); |
|||
otaPackage.setTitle(otaPackageInfo.getTitle()); |
|||
otaPackage.setVersion(otaPackageInfo.getVersion()); |
|||
otaPackage.setTag(otaPackageInfo.getTag()); |
|||
otaPackage.setAdditionalInfo(otaPackageInfo.getAdditionalInfo()); |
|||
otaPackage.setChecksumAlgorithm(checksumAlgorithm); |
|||
otaPackage.setChecksum(checksum); |
|||
otaPackage.setFileName(filename); |
|||
otaPackage.setContentType(contentType); |
|||
otaPackage.setData(ByteBuffer.wrap(data)); |
|||
otaPackage.setDataSize((long) data.length); |
|||
OtaPackageInfo savedOtaPackage = otaPackageService.saveOtaPackage(otaPackage); |
|||
notificationEntityService.logEntityAction(tenantId, savedOtaPackage.getId(), savedOtaPackage, ActionType.UPDATED, user); |
|||
return savedOtaPackage; |
|||
} |
|||
|
|||
@Override |
|||
public void delete(OtaPackageInfo otaPackageInfo, User user) throws ThingsboardException { |
|||
TenantId tenantId = otaPackageInfo.getTenantId(); |
|||
OtaPackageId otaPackageId = otaPackageInfo.getId(); |
|||
otaPackageService.deleteOtaPackage(tenantId, otaPackageId); |
|||
notificationEntityService.logEntityAction(tenantId, otaPackageId, otaPackageInfo, ActionType.DELETED, user, |
|||
otaPackageInfo.getId().toString()); |
|||
} |
|||
} |
|||
@ -1,107 +0,0 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.entitiy.otaPackageController; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.thingsboard.server.common.data.StringUtils; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.OtaPackage; |
|||
import org.thingsboard.server.common.data.OtaPackageInfo; |
|||
import org.thingsboard.server.common.data.SaveOtaPackageInfoRequest; |
|||
import org.thingsboard.server.common.data.audit.ActionType; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|||
import org.thingsboard.server.common.data.id.OtaPackageId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.ota.ChecksumAlgorithm; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.entitiy.AbstractTbEntityService; |
|||
import org.thingsboard.server.service.security.model.SecurityUser; |
|||
|
|||
import java.nio.ByteBuffer; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@AllArgsConstructor |
|||
@Slf4j |
|||
public class DefaultTbOtaPackageService extends AbstractTbEntityService implements TbOtaPackageService { |
|||
@Override |
|||
public OtaPackageInfo save(SaveOtaPackageInfoRequest saveOtaPackageInfoRequest, SecurityUser user) throws ThingsboardException { |
|||
TenantId tenantId = saveOtaPackageInfoRequest.getTenantId(); |
|||
ActionType actionType = saveOtaPackageInfoRequest.getId() == null ? ActionType.ADDED : ActionType.UPDATED; |
|||
try { |
|||
OtaPackageInfo savedOtaPackageInfo = otaPackageService.saveOtaPackageInfo(new OtaPackageInfo(saveOtaPackageInfoRequest), saveOtaPackageInfoRequest.isUsesUrl()); |
|||
notificationEntityService.notifyEntity(tenantId, savedOtaPackageInfo.getId(), savedOtaPackageInfo, null, |
|||
actionType, user, null); |
|||
return savedOtaPackageInfo; |
|||
} catch (Exception e) { |
|||
notificationEntityService.notifyEntity(tenantId, emptyId(EntityType.OTA_PACKAGE), saveOtaPackageInfoRequest, null, |
|||
actionType, user, e); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void delete(OtaPackageInfo otaPackageInfo, SecurityUser user) throws ThingsboardException { |
|||
TenantId tenantId = otaPackageInfo.getTenantId(); |
|||
OtaPackageId otaPackageId = otaPackageInfo.getId(); |
|||
try { |
|||
otaPackageService.deleteOtaPackage(tenantId, otaPackageId); |
|||
notificationEntityService.notifyEntity(tenantId, otaPackageId, otaPackageInfo, null, |
|||
ActionType.DELETED, user, null, otaPackageInfo.getId().toString()); |
|||
} catch (Exception e) { |
|||
notificationEntityService.notifyEntity(tenantId, emptyId(EntityType.OTA_PACKAGE), null, null, |
|||
ActionType.DELETED, user, e, otaPackageInfo.getId().toString()); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public OtaPackageInfo saveOtaPackageData(OtaPackageInfo otaPackageInfo, String checksum, ChecksumAlgorithm checksumAlgorithm, |
|||
byte[] data, String filename, String contentType, SecurityUser user) throws ThingsboardException { |
|||
TenantId tenantId = otaPackageInfo.getTenantId(); |
|||
OtaPackageId otaPackageId = otaPackageInfo.getId(); |
|||
try { |
|||
if (StringUtils.isEmpty(checksum)) { |
|||
checksum = otaPackageService.generateChecksum(checksumAlgorithm, ByteBuffer.wrap(data)); |
|||
} |
|||
OtaPackage otaPackage = new OtaPackage(otaPackageId); |
|||
otaPackage.setCreatedTime(otaPackageInfo.getCreatedTime()); |
|||
otaPackage.setTenantId(tenantId); |
|||
otaPackage.setDeviceProfileId(otaPackageInfo.getDeviceProfileId()); |
|||
otaPackage.setType(otaPackageInfo.getType()); |
|||
otaPackage.setTitle(otaPackageInfo.getTitle()); |
|||
otaPackage.setVersion(otaPackageInfo.getVersion()); |
|||
otaPackage.setTag(otaPackageInfo.getTag()); |
|||
otaPackage.setAdditionalInfo(otaPackageInfo.getAdditionalInfo()); |
|||
otaPackage.setChecksumAlgorithm(checksumAlgorithm); |
|||
otaPackage.setChecksum(checksum); |
|||
otaPackage.setFileName(filename); |
|||
otaPackage.setContentType(contentType); |
|||
otaPackage.setData(ByteBuffer.wrap(data)); |
|||
otaPackage.setDataSize((long) data.length); |
|||
OtaPackageInfo savedOtaPackage = otaPackageService.saveOtaPackage(otaPackage); |
|||
notificationEntityService.notifyEntity(tenantId, savedOtaPackage.getId(), savedOtaPackage, null, |
|||
ActionType.UPDATED, user, null); |
|||
return savedOtaPackage; |
|||
} catch (Exception e) { |
|||
notificationEntityService.notifyEntity(tenantId, emptyId(EntityType.OTA_PACKAGE), null, null, |
|||
ActionType.UPDATED, user, e, otaPackageId.toString()); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue