|
|
@ -17,6 +17,7 @@ package org.thingsboard.server.controller; |
|
|
|
|
|
|
|
|
import io.swagger.annotations.ApiOperation; |
|
|
import io.swagger.annotations.ApiOperation; |
|
|
import io.swagger.annotations.ApiParam; |
|
|
import io.swagger.annotations.ApiParam; |
|
|
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
@ -32,21 +33,17 @@ import org.springframework.web.bind.annotation.ResponseStatus; |
|
|
import org.springframework.web.bind.annotation.RestController; |
|
|
import org.springframework.web.bind.annotation.RestController; |
|
|
import org.thingsboard.server.common.data.DeviceProfile; |
|
|
import org.thingsboard.server.common.data.DeviceProfile; |
|
|
import org.thingsboard.server.common.data.DeviceProfileInfo; |
|
|
import org.thingsboard.server.common.data.DeviceProfileInfo; |
|
|
import org.thingsboard.server.common.data.EntityType; |
|
|
|
|
|
import org.thingsboard.server.common.data.audit.ActionType; |
|
|
|
|
|
import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
|
|
|
|
|
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|
|
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|
|
import org.thingsboard.server.common.data.id.DeviceProfileId; |
|
|
import org.thingsboard.server.common.data.id.DeviceProfileId; |
|
|
import org.thingsboard.server.common.data.page.PageData; |
|
|
import org.thingsboard.server.common.data.page.PageData; |
|
|
import org.thingsboard.server.common.data.page.PageLink; |
|
|
import org.thingsboard.server.common.data.page.PageLink; |
|
|
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; |
|
|
|
|
|
import org.thingsboard.server.dao.timeseries.TimeseriesService; |
|
|
import org.thingsboard.server.dao.timeseries.TimeseriesService; |
|
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
|
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
|
|
|
|
|
import org.thingsboard.server.service.entitiy.deviceProfile.TbDeviceProfileService; |
|
|
import org.thingsboard.server.service.security.permission.Operation; |
|
|
import org.thingsboard.server.service.security.permission.Operation; |
|
|
import org.thingsboard.server.service.security.permission.Resource; |
|
|
import org.thingsboard.server.service.security.permission.Resource; |
|
|
|
|
|
|
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.Objects; |
|
|
|
|
|
import java.util.UUID; |
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
|
import static org.thingsboard.server.controller.ControllerConstants.DEVICE_PROFILE_DATA; |
|
|
import static org.thingsboard.server.controller.ControllerConstants.DEVICE_PROFILE_DATA; |
|
|
@ -70,9 +67,11 @@ import static org.thingsboard.server.controller.ControllerConstants.UUID_WIKI_LI |
|
|
@RestController |
|
|
@RestController |
|
|
@TbCoreComponent |
|
|
@TbCoreComponent |
|
|
@RequestMapping("/api") |
|
|
@RequestMapping("/api") |
|
|
|
|
|
@RequiredArgsConstructor |
|
|
@Slf4j |
|
|
@Slf4j |
|
|
public class DeviceProfileController extends BaseController { |
|
|
public class DeviceProfileController extends BaseController { |
|
|
|
|
|
|
|
|
|
|
|
private final TbDeviceProfileService tbDeviceProfileService; |
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
private TimeseriesService timeseriesService; |
|
|
private TimeseriesService timeseriesService; |
|
|
@ -201,44 +200,11 @@ public class DeviceProfileController extends BaseController { |
|
|
public DeviceProfile saveDeviceProfile( |
|
|
public DeviceProfile saveDeviceProfile( |
|
|
@ApiParam(value = "A JSON value representing the device profile.") |
|
|
@ApiParam(value = "A JSON value representing the device profile.") |
|
|
@RequestBody DeviceProfile deviceProfile) throws ThingsboardException { |
|
|
@RequestBody DeviceProfile deviceProfile) throws ThingsboardException { |
|
|
try { |
|
|
deviceProfile.setTenantId(getTenantId()); |
|
|
boolean created = deviceProfile.getId() == null; |
|
|
|
|
|
deviceProfile.setTenantId(getTenantId()); |
|
|
|
|
|
|
|
|
|
|
|
checkEntity(deviceProfile.getId(), deviceProfile, Resource.DEVICE_PROFILE); |
|
|
|
|
|
|
|
|
|
|
|
boolean isFirmwareChanged = false; |
|
|
|
|
|
boolean isSoftwareChanged = false; |
|
|
|
|
|
|
|
|
|
|
|
if (!created) { |
|
|
|
|
|
DeviceProfile oldDeviceProfile = deviceProfileService.findDeviceProfileById(getTenantId(), 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); |
|
|
checkEntity(deviceProfile.getId(), deviceProfile, Resource.DEVICE_PROFILE); |
|
|
tbClusterService.broadcastEntityStateChangeEvent(deviceProfile.getTenantId(), savedDeviceProfile.getId(), |
|
|
|
|
|
created ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED); |
|
|
|
|
|
|
|
|
|
|
|
logEntityAction(savedDeviceProfile.getId(), savedDeviceProfile, |
|
|
return tbDeviceProfileService.save(deviceProfile, getCurrentUser()); |
|
|
null, |
|
|
|
|
|
created ? ActionType.ADDED : ActionType.UPDATED, null); |
|
|
|
|
|
|
|
|
|
|
|
otaPackageStateService.update(savedDeviceProfile, isFirmwareChanged, isSoftwareChanged); |
|
|
|
|
|
|
|
|
|
|
|
sendEntityNotificationMsg(getTenantId(), savedDeviceProfile.getId(), |
|
|
|
|
|
deviceProfile.getId() == null ? EdgeEventActionType.ADDED : EdgeEventActionType.UPDATED); |
|
|
|
|
|
return savedDeviceProfile; |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
logEntityAction(emptyId(EntityType.DEVICE_PROFILE), deviceProfile, |
|
|
|
|
|
null, deviceProfile.getId() == null ? ActionType.ADDED : ActionType.UPDATED, e); |
|
|
|
|
|
throw handleException(e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ApiOperation(value = "Delete device profile (deleteDeviceProfile)", |
|
|
@ApiOperation(value = "Delete device profile (deleteDeviceProfile)", |
|
|
@ -252,27 +218,10 @@ public class DeviceProfileController extends BaseController { |
|
|
@ApiParam(value = DEVICE_PROFILE_ID_PARAM_DESCRIPTION) |
|
|
@ApiParam(value = DEVICE_PROFILE_ID_PARAM_DESCRIPTION) |
|
|
@PathVariable(DEVICE_PROFILE_ID) String strDeviceProfileId) throws ThingsboardException { |
|
|
@PathVariable(DEVICE_PROFILE_ID) String strDeviceProfileId) throws ThingsboardException { |
|
|
checkParameter(DEVICE_PROFILE_ID, strDeviceProfileId); |
|
|
checkParameter(DEVICE_PROFILE_ID, strDeviceProfileId); |
|
|
try { |
|
|
DeviceProfileId deviceProfileId = new DeviceProfileId(toUUID(strDeviceProfileId)); |
|
|
DeviceProfileId deviceProfileId = new DeviceProfileId(toUUID(strDeviceProfileId)); |
|
|
DeviceProfile deviceProfile = checkDeviceProfileId(deviceProfileId, Operation.DELETE); |
|
|
DeviceProfile deviceProfile = checkDeviceProfileId(deviceProfileId, Operation.DELETE); |
|
|
tbDeviceProfileService.delete(deviceProfile, getCurrentUser()); |
|
|
deviceProfileService.deleteDeviceProfile(getTenantId(), deviceProfileId); |
|
|
} |
|
|
|
|
|
|
|
|
tbClusterService.onDeviceProfileDelete(deviceProfile, null); |
|
|
|
|
|
tbClusterService.broadcastEntityStateChangeEvent(deviceProfile.getTenantId(), deviceProfile.getId(), ComponentLifecycleEvent.DELETED); |
|
|
|
|
|
|
|
|
|
|
|
logEntityAction(deviceProfileId, deviceProfile, |
|
|
|
|
|
null, |
|
|
|
|
|
ActionType.DELETED, null, strDeviceProfileId); |
|
|
|
|
|
|
|
|
|
|
|
sendEntityNotificationMsg(getTenantId(), deviceProfile.getId(), EdgeEventActionType.DELETED); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
logEntityAction(emptyId(EntityType.DEVICE_PROFILE), |
|
|
|
|
|
null, |
|
|
|
|
|
null, |
|
|
|
|
|
ActionType.DELETED, e, strDeviceProfileId); |
|
|
|
|
|
throw handleException(e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ApiOperation(value = "Make Device Profile Default (setDefaultDeviceProfile)", |
|
|
@ApiOperation(value = "Make Device Profile Default (setDefaultDeviceProfile)", |
|
|
notes = "Marks device profile as default within a tenant scope." + TENANT_AUTHORITY_PARAGRAPH, |
|
|
notes = "Marks device profile as default within a tenant scope." + TENANT_AUTHORITY_PARAGRAPH, |
|
|
@ -284,30 +233,10 @@ public class DeviceProfileController extends BaseController { |
|
|
@ApiParam(value = DEVICE_PROFILE_ID_PARAM_DESCRIPTION) |
|
|
@ApiParam(value = DEVICE_PROFILE_ID_PARAM_DESCRIPTION) |
|
|
@PathVariable(DEVICE_PROFILE_ID) String strDeviceProfileId) throws ThingsboardException { |
|
|
@PathVariable(DEVICE_PROFILE_ID) String strDeviceProfileId) throws ThingsboardException { |
|
|
checkParameter(DEVICE_PROFILE_ID, strDeviceProfileId); |
|
|
checkParameter(DEVICE_PROFILE_ID, strDeviceProfileId); |
|
|
try { |
|
|
DeviceProfileId deviceProfileId = new DeviceProfileId(toUUID(strDeviceProfileId)); |
|
|
DeviceProfileId deviceProfileId = new DeviceProfileId(toUUID(strDeviceProfileId)); |
|
|
DeviceProfile deviceProfile = checkDeviceProfileId(deviceProfileId, Operation.WRITE); |
|
|
DeviceProfile deviceProfile = checkDeviceProfileId(deviceProfileId, Operation.WRITE); |
|
|
DeviceProfile previousDefaultDeviceProfile = deviceProfileService.findDefaultDeviceProfile(getTenantId()); |
|
|
DeviceProfile previousDefaultDeviceProfile = deviceProfileService.findDefaultDeviceProfile(getTenantId()); |
|
|
return tbDeviceProfileService.setDefaultDeviceProfile(deviceProfile, previousDefaultDeviceProfile, getCurrentUser()); |
|
|
if (deviceProfileService.setDefaultDeviceProfile(getTenantId(), deviceProfileId)) { |
|
|
|
|
|
if (previousDefaultDeviceProfile != null) { |
|
|
|
|
|
previousDefaultDeviceProfile = deviceProfileService.findDeviceProfileById(getTenantId(), previousDefaultDeviceProfile.getId()); |
|
|
|
|
|
|
|
|
|
|
|
logEntityAction(previousDefaultDeviceProfile.getId(), previousDefaultDeviceProfile, |
|
|
|
|
|
null, ActionType.UPDATED, null); |
|
|
|
|
|
} |
|
|
|
|
|
deviceProfile = deviceProfileService.findDeviceProfileById(getTenantId(), deviceProfileId); |
|
|
|
|
|
|
|
|
|
|
|
logEntityAction(deviceProfile.getId(), deviceProfile, |
|
|
|
|
|
null, ActionType.UPDATED, null); |
|
|
|
|
|
} |
|
|
|
|
|
return deviceProfile; |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
logEntityAction(emptyId(EntityType.DEVICE_PROFILE), |
|
|
|
|
|
null, |
|
|
|
|
|
null, |
|
|
|
|
|
ActionType.UPDATED, e, strDeviceProfileId); |
|
|
|
|
|
throw handleException(e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ApiOperation(value = "Get Device Profiles (getDeviceProfiles)", |
|
|
@ApiOperation(value = "Get Device Profiles (getDeviceProfiles)", |
|
|
|