committed by
GitHub
703 changed files with 24582 additions and 2815 deletions
@ -0,0 +1,41 @@ |
|||
{ |
|||
"widgetsBundle": { |
|||
"alias": "navigation_widgets", |
|||
"title": "Navigation widgets", |
|||
"image": null |
|||
}, |
|||
"widgetTypes": [ |
|||
{ |
|||
"alias": "navigation_cards", |
|||
"name": "Navigation cards", |
|||
"descriptor": { |
|||
"type": "static", |
|||
"sizeX": 7, |
|||
"sizeY": 6, |
|||
"resources": [], |
|||
"templateHtml": "<tb-navigation-cards-widget [ctx]=\"ctx\"></tb-navigation-cards-widget>", |
|||
"templateCss": "/*#widget-container {\n overflow-y: auto;\n box-sizing: content-box !important;\n cursor: auto;\n}*/\n\n#widget-container #container {\n overflow-y: auto;\n box-sizing: content-box;\n cursor: auto;\n}", |
|||
"controllerScript": "self.onInit = function() {\n self.ctx.$scope.navigationCardsWidget.resize();\n}\n\nself.onResize = function() {\n self.ctx.$scope.navigationCardsWidget.resize();\n}\n\nself.onDestroy = function() {\n}\n", |
|||
"settingsSchema": "{\n \"schema\": {\n \"type\": \"object\",\n \"title\": \"Settings\",\n \"properties\": {\n \"filterType\": {\n \"title\": \"Filter type\",\n \"type\": \"string\",\n \"default\": \"all\"\n },\n \"filter\": {\n \"title\": \"Items\",\n \"type\": \"array\"\n }\n },\n \"required\": []\n },\n \"form\": [\n {\n \"key\": \"filterType\",\n \"type\": \"radios\",\n \"direction\": \"row\",\n \"titleMap\": [\n {\n \"value\": \"all\",\n \"name\": \"All items\"\n },\n {\n \"value\": \"include\",\n \"name\": \"Include items\"\n },\n {\n \"value\": \"exclude\",\n \"name\": \"Exclude items\"\n }\n ]\n },\n {\n \"key\": \"filter\",\n \"type\": \"rc-select\",\n \"condition\": \"model.filterType !== 'all'\",\n \"tags\": true,\n \"placeholder\": \"Enter urls to filter\",\n \"items\": [{\"value\": \"/devices\", \"label\": \"/devices\"}, {\"value\": \"/assets\", \"label\": \"/assets\"}, {\"value\": \"/deviceProfies\", \"label\": \"/deviceProfies\"}]\n }\n ]\n}\n", |
|||
"dataKeySettingsSchema": "{}\n", |
|||
"defaultConfig": "{\"datasources\":[{\"type\":\"static\",\"name\":\"function\",\"dataKeys\":[{\"name\":\"f(x)\",\"type\":\"function\",\"label\":\"Random\",\"color\":\"#2196f3\",\"settings\":{},\"_hash\":0.15479322438769105,\"funcBody\":\"var value = prevValue + Math.random() * 100 - 50;\\nvar multiplier = Math.pow(10, 2 || 0);\\nvar value = Math.round(value * multiplier) / multiplier;\\nif (value < -1000) {\\n\\tvalue = -1000;\\n} else if (value > 1000) {\\n\\tvalue = 1000;\\n}\\nreturn value;\"}]}],\"timewindow\":{\"realtime\":{\"timewindowMs\":60000}},\"showTitle\":false,\"backgroundColor\":\"rgba(255,255,255,0)\",\"color\":\"rgba(0, 0, 0, 0.87)\",\"padding\":\"8px\",\"settings\":{\"filterType\":\"all\"},\"title\":\"Navigation cards\",\"dropShadow\":false,\"showTitleIcon\":false,\"iconColor\":\"rgba(0, 0, 0, 0.87)\",\"iconSize\":\"24px\",\"titleTooltip\":\"\",\"enableFullscreen\":false,\"widgetStyle\":{},\"titleStyle\":{\"fontSize\":\"16px\",\"fontWeight\":400},\"showLegend\":false}" |
|||
} |
|||
}, |
|||
{ |
|||
"alias": "navigation_card", |
|||
"name": "Navigation card", |
|||
"descriptor": { |
|||
"type": "static", |
|||
"sizeX": 2.5, |
|||
"sizeY": 2, |
|||
"resources": [], |
|||
"templateHtml": "<tb-navigation-card-widget [ctx]=\"ctx\"></tb-navigation-card-widget>", |
|||
"templateCss": "", |
|||
"controllerScript": "self.onInit = function() {\n\n}\n\n\nself.onDestroy = function() {\n}\n", |
|||
"settingsSchema": "{\n \"schema\": {\n \"type\": \"object\",\n \"title\": \"Settings\",\n \"properties\": {\n \"name\": {\n \"title\": \"Title\",\n \"type\": \"string\",\n \"default\": \"{i18n:device.devices}\"\n },\n \"icon\": {\n \"title\": \"icon\",\n \"type\": \"string\",\n \"default\": \"devices_other\"\n },\n \"path\": {\n \"title\": \"Navigation path\",\n \"type\": \"string\",\n \"default\": \"/devices\"\n }\n },\n \"required\": [\"name\", \"icon\", \"path\"]\n },\n \"form\": [\n \"name\",\n {\n \"key\": \"icon\",\n \"type\": \"icon\"\n },\n \"path\"\n ]\n}\n", |
|||
"dataKeySettingsSchema": "{}\n", |
|||
"defaultConfig": "{\"datasources\":[{\"type\":\"static\",\"name\":\"function\",\"dataKeys\":[{\"name\":\"f(x)\",\"type\":\"function\",\"label\":\"Random\",\"color\":\"#2196f3\",\"settings\":{},\"_hash\":0.15479322438769105,\"funcBody\":\"var value = prevValue + Math.random() * 100 - 50;\\nvar multiplier = Math.pow(10, 2 || 0);\\nvar value = Math.round(value * multiplier) / multiplier;\\nif (value < -1000) {\\n\\tvalue = -1000;\\n} else if (value > 1000) {\\n\\tvalue = 1000;\\n}\\nreturn value;\"}]}],\"timewindow\":{\"realtime\":{\"timewindowMs\":60000}},\"showTitle\":false,\"backgroundColor\":\"rgba(255,255,255,0)\",\"color\":\"rgba(255,255,255,0.87)\",\"padding\":\"8px\",\"settings\":{\"name\":\"{i18n:device.devices}\",\"icon\":\"devices_other\",\"path\":\"/devices\"},\"title\":\"Navigation card\",\"dropShadow\":false,\"showTitleIcon\":false,\"iconColor\":\"rgba(0, 0, 0, 0.87)\",\"iconSize\":\"24px\",\"titleTooltip\":\"\",\"enableFullscreen\":false,\"widgetStyle\":{},\"titleStyle\":{\"fontSize\":\"16px\",\"fontWeight\":400},\"showLegend\":false}" |
|||
} |
|||
} |
|||
] |
|||
} |
|||
@ -0,0 +1,42 @@ |
|||
/** |
|||
* Copyright © 2016-2021 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.actors.ruleChain; |
|||
|
|||
import lombok.EqualsAndHashCode; |
|||
import lombok.Getter; |
|||
import org.thingsboard.rule.engine.api.TbContext; |
|||
import org.thingsboard.server.common.msg.TbActorStopReason; |
|||
import org.thingsboard.server.common.msg.TbMsg; |
|||
import org.thingsboard.server.common.msg.TbRuleEngineActorMsg; |
|||
import org.thingsboard.server.common.msg.queue.RuleNodeException; |
|||
|
|||
@EqualsAndHashCode(callSuper = true) |
|||
public abstract class TbToRuleNodeActorMsg extends TbRuleEngineActorMsg { |
|||
|
|||
@Getter |
|||
private final TbContext ctx; |
|||
|
|||
public TbToRuleNodeActorMsg(TbContext ctx, TbMsg tbMsg) { |
|||
super(tbMsg); |
|||
this.ctx = ctx; |
|||
} |
|||
|
|||
@Override |
|||
public void onTbActorStopped(TbActorStopReason reason) { |
|||
String message = reason == TbActorStopReason.STOPPED ? "Rule node stopped" : "Failed to initialize rule node!"; |
|||
msg.getCallback().onFailure(new RuleNodeException(message, ctx.getRuleChainName(), ctx.getSelf())); |
|||
} |
|||
} |
|||
@ -0,0 +1,129 @@ |
|||
/** |
|||
* Copyright © 2016-2021 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.controller; |
|||
|
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.security.access.prepost.PreAuthorize; |
|||
import org.springframework.web.bind.annotation.PathVariable; |
|||
import org.springframework.web.bind.annotation.RequestBody; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RequestMethod; |
|||
import org.springframework.web.bind.annotation.RequestParam; |
|||
import org.springframework.web.bind.annotation.ResponseBody; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
import org.thingsboard.rule.engine.api.msg.DeviceNameOrTypeUpdateMsg; |
|||
import org.thingsboard.server.common.data.Device; |
|||
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.lwm2m.LwM2mObject; |
|||
import org.thingsboard.server.common.data.lwm2m.ServerSecurityConfig; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; |
|||
import org.thingsboard.server.common.data.security.DeviceCredentials; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.security.permission.Resource; |
|||
|
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
@Slf4j |
|||
@RestController |
|||
@TbCoreComponent |
|||
@RequestMapping("/api") |
|||
public class DeviceLwm2mController extends BaseController { |
|||
|
|||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") |
|||
@RequestMapping(value = "/lwm2m/deviceProfile", params = {"sortOrder", "sortProperty"}, method = RequestMethod.GET) |
|||
@ResponseBody |
|||
public List<LwM2mObject> getLwm2mListObjects(@RequestParam String sortOrder, |
|||
@RequestParam String sortProperty, |
|||
@RequestParam(required = false) int[] objectIds, |
|||
@RequestParam(required = false) String searchText) |
|||
throws ThingsboardException { |
|||
try { |
|||
return lwM2MModelsRepository.getLwm2mObjects(objectIds, searchText, sortProperty, sortOrder); |
|||
} catch (Exception e) { |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") |
|||
@RequestMapping(value = "/lwm2m/deviceProfile/objects", params = {"pageSize", "page"}, method = RequestMethod.GET) |
|||
@ResponseBody |
|||
public PageData<LwM2mObject> getLwm2mListObjects(@RequestParam int pageSize, |
|||
@RequestParam int page, |
|||
@RequestParam(required = false) String searchText, |
|||
@RequestParam(required = false) String sortProperty, |
|||
@RequestParam(required = false) String sortOrder) throws ThingsboardException { |
|||
try { |
|||
PageLink pageLink = createPageLink(pageSize, page, searchText, sortProperty, sortOrder); |
|||
return checkNotNull(lwM2MModelsRepository.findDeviceLwm2mObjects(getTenantId(), pageLink)); |
|||
} catch (Exception e) { |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") |
|||
@RequestMapping(value = "/lwm2m/deviceProfile/bootstrap/{securityMode}/{bootstrapServerIs}", method = RequestMethod.GET) |
|||
@ResponseBody |
|||
public ServerSecurityConfig getLwm2mBootstrapSecurityInfo(@PathVariable("securityMode") String securityMode, |
|||
@PathVariable("bootstrapServerIs") boolean bootstrapServerIs) throws ThingsboardException { |
|||
try { |
|||
return lwM2MModelsRepository.getBootstrapSecurityInfo(securityMode, bootstrapServerIs); |
|||
} catch (Exception e) { |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") |
|||
@RequestMapping(value = "/lwm2m/device-credentials", method = RequestMethod.POST) |
|||
@ResponseBody |
|||
public Device saveDeviceWithCredentials(@RequestBody (required=false) Map<Class<?>, Object> deviceWithDeviceCredentials) throws ThingsboardException { |
|||
ObjectMapper mapper = new ObjectMapper(); |
|||
Device device = checkNotNull(mapper.convertValue(deviceWithDeviceCredentials.get(Device.class), Device.class)); |
|||
DeviceCredentials credentials = checkNotNull(mapper.convertValue( deviceWithDeviceCredentials.get(DeviceCredentials.class), DeviceCredentials.class)); |
|||
try { |
|||
device.setTenantId(getCurrentUser().getTenantId()); |
|||
checkEntity(device.getId(), device, Resource.DEVICE); |
|||
Device savedDevice = deviceService.saveDeviceWithCredentials(device, credentials); |
|||
checkNotNull(savedDevice); |
|||
|
|||
tbClusterService.onDeviceChange(savedDevice, null); |
|||
tbClusterService.pushMsgToCore(new DeviceNameOrTypeUpdateMsg(savedDevice.getTenantId(), |
|||
savedDevice.getId(), savedDevice.getName(), savedDevice.getType()), null); |
|||
tbClusterService.onEntityStateChange(savedDevice.getTenantId(), savedDevice.getId(), |
|||
device.getId() == null ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED); |
|||
|
|||
logEntityAction(savedDevice.getId(), savedDevice, |
|||
savedDevice.getCustomerId(), |
|||
device.getId() == null ? ActionType.ADDED : ActionType.UPDATED, null); |
|||
|
|||
if (device.getId() == null) { |
|||
deviceStateService.onDeviceAdded(savedDevice); |
|||
} else { |
|||
deviceStateService.onDeviceUpdated(savedDevice); |
|||
} |
|||
return savedDevice; |
|||
} catch (Exception e) { |
|||
logEntityAction(emptyId(EntityType.DEVICE), device, |
|||
null, device.getId() == null ? ActionType.ADDED : ActionType.UPDATED, e); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,90 @@ |
|||
/** |
|||
* Copyright © 2016-2021 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.controller; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.security.access.prepost.PreAuthorize; |
|||
import org.springframework.web.bind.annotation.PathVariable; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RequestMethod; |
|||
import org.springframework.web.bind.annotation.RequestParam; |
|||
import org.springframework.web.bind.annotation.ResponseBody; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.common.data.transport.resource.Resource; |
|||
import org.thingsboard.server.common.data.transport.resource.ResourceType; |
|||
import org.thingsboard.server.dao.resource.ResourceService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
|
|||
@Slf4j |
|||
@RestController |
|||
@TbCoreComponent |
|||
@RequestMapping("/api") |
|||
public class ResourceController extends BaseController { |
|||
|
|||
private final ResourceService resourceService; |
|||
|
|||
public ResourceController(ResourceService resourceService) { |
|||
this.resourceService = resourceService; |
|||
} |
|||
|
|||
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") |
|||
@RequestMapping(value = "/resource", method = RequestMethod.POST) |
|||
@ResponseBody |
|||
public Resource saveResource(Resource resource) throws ThingsboardException { |
|||
try { |
|||
resource.setTenantId(getTenantId()); |
|||
Resource savedResource = checkNotNull(resourceService.saveResource(resource)); |
|||
tbClusterService.onResourceChange(savedResource, null); |
|||
return savedResource; |
|||
} catch (Exception e) { |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") |
|||
@RequestMapping(value = "/resource", method = RequestMethod.GET) |
|||
@ResponseBody |
|||
public PageData<Resource> getResources(@RequestParam(required = false) boolean system, |
|||
@RequestParam int pageSize, |
|||
@RequestParam int page, |
|||
@RequestParam(required = false) String sortProperty, |
|||
@RequestParam(required = false) String sortOrder) throws ThingsboardException { |
|||
try { |
|||
PageLink pageLink = createPageLink(pageSize, page, null, sortProperty, sortOrder); |
|||
return checkNotNull(resourceService.findResourcesByTenantId(system ? TenantId.SYS_TENANT_ID : getTenantId(), pageLink)); |
|||
} catch (Exception e) { |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") |
|||
@RequestMapping(value = "/resource/{resourceType}/{resourceId}", method = RequestMethod.DELETE) |
|||
@ResponseBody |
|||
public void deleteResource(@PathVariable("resourceType") ResourceType resourceType, |
|||
@PathVariable("resourceId") String resourceId) throws ThingsboardException { |
|||
try { |
|||
Resource resource = checkNotNull(resourceService.getResource(getTenantId(), resourceType, resourceId)); |
|||
resourceService.deleteResource(getTenantId(), resourceType, resourceId); |
|||
tbClusterService.onResourceDeleted(resource, null); |
|||
} catch (Exception e) { |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,314 @@ |
|||
/** |
|||
* Copyright © 2016-2021 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.lwm2m; |
|||
|
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.eclipse.leshan.core.model.ObjectModel; |
|||
import org.eclipse.leshan.core.util.Hex; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; |
|||
import org.springframework.data.domain.PageImpl; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.lwm2m.LwM2mInstance; |
|||
import org.thingsboard.server.common.data.lwm2m.LwM2mObject; |
|||
import org.thingsboard.server.common.data.lwm2m.LwM2mResource; |
|||
import org.thingsboard.server.common.data.lwm2m.ServerSecurityConfig; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.common.transport.lwm2m.LwM2MTransportConfigBootstrap; |
|||
import org.thingsboard.server.common.transport.lwm2m.LwM2MTransportConfigServer; |
|||
import org.thingsboard.server.dao.service.Validator; |
|||
import org.thingsboard.server.queue.util.TbLwM2mTransportComponent; |
|||
import org.thingsboard.server.transport.lwm2m.secure.LwM2MSecurityMode; |
|||
|
|||
import java.math.BigInteger; |
|||
import java.security.AlgorithmParameters; |
|||
import java.security.GeneralSecurityException; |
|||
import java.security.KeyFactory; |
|||
import java.security.KeyStoreException; |
|||
import java.security.PublicKey; |
|||
import java.security.cert.CertificateEncodingException; |
|||
import java.security.cert.X509Certificate; |
|||
import java.security.spec.ECGenParameterSpec; |
|||
import java.security.spec.ECParameterSpec; |
|||
import java.security.spec.ECPoint; |
|||
import java.security.spec.ECPublicKeySpec; |
|||
import java.security.spec.KeySpec; |
|||
import java.util.ArrayList; |
|||
import java.util.Comparator; |
|||
import java.util.List; |
|||
import java.util.concurrent.atomic.AtomicInteger; |
|||
import java.util.function.Predicate; |
|||
import java.util.stream.Collector; |
|||
import java.util.stream.Collectors; |
|||
import java.util.stream.IntStream; |
|||
|
|||
import static org.thingsboard.server.dao.service.Validator.validateId; |
|||
|
|||
@Slf4j |
|||
@Service |
|||
@ConditionalOnExpression("('${service.type:null}'=='tb-transport' && '${transport.lwm2m.enabled:false}'=='true') || '${service.type:null}'=='monolith' || '${service.type:null}'=='tb-core'") |
|||
public class LwM2MModelsRepository { |
|||
|
|||
private static final String INCORRECT_TENANT_ID = "Incorrect tenantId "; |
|||
|
|||
@Autowired |
|||
LwM2MTransportConfigServer contextServer; |
|||
|
|||
|
|||
@Autowired |
|||
LwM2MTransportConfigBootstrap contextBootStrap; |
|||
|
|||
/** |
|||
* @param objectIds |
|||
* @param textSearch |
|||
* @return list of LwM2mObject |
|||
* Filter by Predicate (uses objectIds, if objectIds is null then it uses textSearch, |
|||
* if textSearch is null then it uses AllList from List<ObjectModel>) |
|||
*/ |
|||
public List<LwM2mObject> getLwm2mObjects(int[] objectIds, String textSearch, String sortProperty, String sortOrder) { |
|||
if (objectIds == null && textSearch != null && !textSearch.isEmpty()) { |
|||
objectIds = getObjectIdFromTextSearch(textSearch); |
|||
} |
|||
int[] finalObjectIds = objectIds; |
|||
return getLwm2mObjects((objectIds != null && objectIds.length > 0 && textSearch != null && !textSearch.isEmpty()) ? |
|||
(ObjectModel element) -> IntStream.of(finalObjectIds).anyMatch(x -> x == element.id) || element.name.toLowerCase().contains(textSearch.toLowerCase()) : |
|||
(objectIds != null && objectIds.length > 0) ? |
|||
(ObjectModel element) -> IntStream.of(finalObjectIds).anyMatch(x -> x == element.id) : |
|||
(textSearch != null && !textSearch.isEmpty()) ? |
|||
(ObjectModel element) -> element.name.contains(textSearch) : |
|||
null, |
|||
sortProperty, sortOrder); |
|||
} |
|||
|
|||
/** |
|||
* @param predicate |
|||
* @return list of LwM2mObject |
|||
*/ |
|||
private List<LwM2mObject> getLwm2mObjects(Predicate<? super ObjectModel> predicate, String sortProperty, String sortOrder) { |
|||
List<LwM2mObject> lwM2mObjects = new ArrayList<>(); |
|||
List<ObjectModel> listObjects = (predicate == null) ? this.contextServer.getModelsValue() : |
|||
contextServer.getModelsValue().stream() |
|||
.filter(predicate) |
|||
.collect(Collectors.toList()); |
|||
|
|||
listObjects.forEach(obj -> { |
|||
LwM2mObject lwM2mObject = new LwM2mObject(); |
|||
lwM2mObject.setId(obj.id); |
|||
lwM2mObject.setName(obj.name); |
|||
lwM2mObject.setMultiple(obj.multiple); |
|||
lwM2mObject.setMandatory(obj.mandatory); |
|||
LwM2mInstance instance = new LwM2mInstance(); |
|||
instance.setId(0); |
|||
List<LwM2mResource> resources = new ArrayList<>(); |
|||
obj.resources.forEach((k, v) -> { |
|||
if (!v.operations.isExecutable()) { |
|||
LwM2mResource resource = new LwM2mResource(k, v.name, false, false, false); |
|||
resources.add(resource); |
|||
} |
|||
}); |
|||
instance.setResources(resources.stream().toArray(LwM2mResource[]::new)); |
|||
lwM2mObject.setInstances(new LwM2mInstance[]{instance}); |
|||
lwM2mObjects.add(lwM2mObject); |
|||
}); |
|||
return lwM2mObjects.size() > 1 ? this.sortList (lwM2mObjects, sortProperty, sortOrder) : lwM2mObjects; |
|||
} |
|||
|
|||
private List<LwM2mObject> sortList (List<LwM2mObject> lwM2mObjects, String sortProperty, String sortOrder) { |
|||
switch (sortProperty) { |
|||
case "name": |
|||
switch (sortOrder) { |
|||
case "ASC": |
|||
lwM2mObjects.sort((o1, o2) -> o1.getName().compareTo(o2.getName())); |
|||
break; |
|||
case "DESC": |
|||
lwM2mObjects.stream().sorted(Comparator.comparing(LwM2mObject::getName).reversed()); |
|||
break; |
|||
} |
|||
case "id": |
|||
switch (sortOrder) { |
|||
case "ASC": |
|||
lwM2mObjects.sort((o1, o2) -> Long.compare(o1.getId(), o2.getId())); |
|||
break; |
|||
case "DESC": |
|||
lwM2mObjects.sort((o1, o2) -> Long.compare(o2.getId(), o1.getId())); |
|||
} |
|||
} |
|||
return lwM2mObjects; |
|||
} |
|||
|
|||
/** |
|||
* @param tenantId |
|||
* @param pageLink |
|||
* @return List of LwM2mObject in PageData format |
|||
*/ |
|||
public PageData<LwM2mObject> findDeviceLwm2mObjects(TenantId tenantId, PageLink pageLink) { |
|||
log.trace("Executing findDeviceProfileInfos tenantId [{}], pageLink [{}]", tenantId, pageLink); |
|||
validateId(tenantId, INCORRECT_TENANT_ID + tenantId); |
|||
Validator.validatePageLink(pageLink); |
|||
return this.findLwm2mListObjects(pageLink); |
|||
} |
|||
|
|||
/** |
|||
* @param pageLink |
|||
* @return List of LwM2mObject in PageData format, filter == TextSearch |
|||
* PageNumber = 1, PageSize = List<LwM2mObject>.size() |
|||
*/ |
|||
public PageData<LwM2mObject> findLwm2mListObjects(PageLink pageLink) { |
|||
PageImpl<LwM2mObject> page = new PageImpl<>(getLwm2mObjects(getObjectIdFromTextSearch(pageLink.getTextSearch()), |
|||
pageLink.getTextSearch(), |
|||
pageLink.getSortOrder().getProperty(), |
|||
pageLink.getSortOrder().getDirection().name())); |
|||
PageData<LwM2mObject> pageData = new PageData<>(page.getContent(), page.getTotalPages(), page.getTotalElements(), page.hasNext()); |
|||
return pageData; |
|||
} |
|||
|
|||
/** |
|||
* Filter for id Object |
|||
* @param textSearch - |
|||
* @return - return Object id only first chartAt in textSearch |
|||
*/ |
|||
private int[] getObjectIdFromTextSearch(String textSearch) { |
|||
String filtered = null; |
|||
if (textSearch !=null && !textSearch.isEmpty()) { |
|||
AtomicInteger a = new AtomicInteger(); |
|||
filtered = textSearch.chars () |
|||
.mapToObj(chr -> (char) chr) |
|||
.filter(i -> Character.isDigit(i) && textSearch.charAt(a.getAndIncrement()) == i) |
|||
.collect(Collector.of(StringBuilder::new, StringBuilder::append, StringBuilder::append, StringBuilder::toString)); |
|||
} |
|||
return (filtered != null && !filtered.isEmpty()) ? new int[]{Integer.parseInt(filtered)} : new int[0]; |
|||
} |
|||
|
|||
/** |
|||
* @param securityMode |
|||
* @param bootstrapServerIs |
|||
* @return ServerSecurityConfig more value is default: Important - port, host, publicKey |
|||
*/ |
|||
public ServerSecurityConfig getBootstrapSecurityInfo(String securityMode, boolean bootstrapServerIs) { |
|||
LwM2MSecurityMode lwM2MSecurityMode = LwM2MSecurityMode.fromSecurityMode(securityMode.toLowerCase()); |
|||
return getBootstrapServer(bootstrapServerIs, lwM2MSecurityMode); |
|||
} |
|||
|
|||
/** |
|||
* @param bootstrapServerIs |
|||
* @param mode |
|||
* @return ServerSecurityConfig more value is default: Important - port, host, publicKey |
|||
*/ |
|||
private ServerSecurityConfig getBootstrapServer(boolean bootstrapServerIs, LwM2MSecurityMode mode) { |
|||
ServerSecurityConfig bsServ = new ServerSecurityConfig(); |
|||
bsServ.setBootstrapServerIs(bootstrapServerIs); |
|||
if (bootstrapServerIs) { |
|||
bsServ.setServerId(contextBootStrap.getBootstrapServerId()); |
|||
switch (mode) { |
|||
case NO_SEC: |
|||
bsServ.setHost(contextBootStrap.getBootstrapHost()); |
|||
bsServ.setPort(contextBootStrap.getBootstrapPortNoSec()); |
|||
bsServ.setServerPublicKey(""); |
|||
break; |
|||
case PSK: |
|||
bsServ.setHost(contextBootStrap.getBootstrapHostSecurity()); |
|||
bsServ.setPort(contextBootStrap.getBootstrapPortSecurity()); |
|||
bsServ.setServerPublicKey(""); |
|||
break; |
|||
case RPK: |
|||
case X509: |
|||
bsServ.setHost(contextBootStrap.getBootstrapHostSecurity()); |
|||
bsServ.setPort(contextBootStrap.getBootstrapPortSecurity()); |
|||
bsServ.setServerPublicKey(getPublicKey (contextBootStrap.getBootstrapAlias(), this.contextBootStrap.getBootstrapPublicX(), this.contextBootStrap.getBootstrapPublicY())); |
|||
break; |
|||
default: |
|||
break; |
|||
} |
|||
} else { |
|||
bsServ.setServerId(contextServer.getServerId()); |
|||
switch (mode) { |
|||
case NO_SEC: |
|||
bsServ.setHost(contextServer.getServerHost()); |
|||
bsServ.setPort(contextServer.getServerPortNoSec()); |
|||
bsServ.setServerPublicKey(""); |
|||
break; |
|||
case PSK: |
|||
bsServ.setHost(contextServer.getServerHostSecurity()); |
|||
bsServ.setPort(contextServer.getServerPortSecurity()); |
|||
bsServ.setServerPublicKey(""); |
|||
break; |
|||
case RPK: |
|||
case X509: |
|||
bsServ.setHost(contextServer.getServerHostSecurity()); |
|||
bsServ.setPort(contextServer.getServerPortSecurity()); |
|||
bsServ.setServerPublicKey(getPublicKey (contextServer.getServerAlias(), this.contextServer.getServerPublicX(), this.contextServer.getServerPublicY())); |
|||
break; |
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
return bsServ; |
|||
} |
|||
|
|||
private String getPublicKey (String alias, String publicServerX, String publicServerY) { |
|||
String publicKey = getServerPublicKeyX509(alias); |
|||
return publicKey != null ? publicKey : getRPKPublicKey(publicServerX, publicServerY); |
|||
} |
|||
|
|||
/** |
|||
* @param alias |
|||
* @return PublicKey format HexString or null |
|||
*/ |
|||
private String getServerPublicKeyX509(String alias) { |
|||
try { |
|||
X509Certificate serverCertificate = (X509Certificate) contextServer.getKeyStoreValue().getCertificate(alias); |
|||
return Hex.encodeHexString(serverCertificate.getEncoded()); |
|||
} catch (CertificateEncodingException | KeyStoreException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
/** |
|||
* @param publicServerX |
|||
* @param publicServerY |
|||
* @return PublicKey format HexString or null |
|||
*/ |
|||
private String getRPKPublicKey(String publicServerX, String publicServerY) { |
|||
try { |
|||
/** Get Elliptic Curve Parameter spec for secp256r1 */ |
|||
AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC"); |
|||
algoParameters.init(new ECGenParameterSpec("secp256r1")); |
|||
ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class); |
|||
if (publicServerX != null && !publicServerX.isEmpty() && publicServerY != null && !publicServerY.isEmpty()) { |
|||
/** Get point values */ |
|||
byte[] publicX = Hex.decodeHex(publicServerX.toCharArray()); |
|||
byte[] publicY = Hex.decodeHex(publicServerY.toCharArray()); |
|||
/** Create key specs */ |
|||
KeySpec publicKeySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(publicX), new BigInteger(publicY)), |
|||
parameterSpec); |
|||
/** Get keys */ |
|||
PublicKey publicKey = KeyFactory.getInstance("EC").generatePublic(publicKeySpec); |
|||
if (publicKey != null && publicKey.getEncoded().length > 0) { |
|||
return Hex.encodeHexString(publicKey.getEncoded()); |
|||
} |
|||
} |
|||
} catch (GeneralSecurityException | IllegalArgumentException e) { |
|||
log.error("[{}] Failed generate Server RPK for profile", e.getMessage()); |
|||
throw new RuntimeException(e); |
|||
} |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,72 @@ |
|||
/** |
|||
* Copyright © 2016-2021 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.security.auth.oauth2; |
|||
|
|||
import org.springframework.util.SerializationUtils; |
|||
import javax.servlet.http.Cookie; |
|||
import javax.servlet.http.HttpServletRequest; |
|||
import javax.servlet.http.HttpServletResponse; |
|||
import java.util.Base64; |
|||
import java.util.Optional; |
|||
|
|||
public class CookieUtils { |
|||
|
|||
public static Optional<Cookie> getCookie(HttpServletRequest request, String name) { |
|||
Cookie[] cookies = request.getCookies(); |
|||
|
|||
if (cookies != null && cookies.length > 0) { |
|||
for (Cookie cookie : cookies) { |
|||
if (cookie.getName().equals(name)) { |
|||
return Optional.of(cookie); |
|||
} |
|||
} |
|||
} |
|||
|
|||
return Optional.empty(); |
|||
} |
|||
|
|||
public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) { |
|||
Cookie cookie = new Cookie(name, value); |
|||
cookie.setPath("/"); |
|||
cookie.setHttpOnly(true); |
|||
cookie.setMaxAge(maxAge); |
|||
response.addCookie(cookie); |
|||
} |
|||
|
|||
public static void deleteCookie(HttpServletRequest request, HttpServletResponse response, String name) { |
|||
Cookie[] cookies = request.getCookies(); |
|||
if (cookies != null && cookies.length > 0) { |
|||
for (Cookie cookie: cookies) { |
|||
if (cookie.getName().equals(name)) { |
|||
cookie.setValue(""); |
|||
cookie.setPath("/"); |
|||
cookie.setMaxAge(0); |
|||
response.addCookie(cookie); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
public static String serialize(Object object) { |
|||
return Base64.getUrlEncoder() |
|||
.encodeToString(SerializationUtils.serialize(object)); |
|||
} |
|||
|
|||
public static <T> T deserialize(Cookie cookie, Class<T> cls) { |
|||
return cls.cast(SerializationUtils.deserialize( |
|||
Base64.getUrlDecoder().decode(cookie.getValue()))); |
|||
} |
|||
} |
|||
@ -0,0 +1,55 @@ |
|||
/** |
|||
* Copyright © 2016-2021 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.security.auth.oauth2; |
|||
|
|||
import org.springframework.security.oauth2.client.web.AuthorizationRequestRepository; |
|||
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import javax.servlet.http.HttpServletRequest; |
|||
import javax.servlet.http.HttpServletResponse; |
|||
|
|||
@Component |
|||
public class HttpCookieOAuth2AuthorizationRequestRepository implements AuthorizationRequestRepository<OAuth2AuthorizationRequest> { |
|||
public static final String OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME = "oauth2_auth_request"; |
|||
private static final int cookieExpireSeconds = 180; |
|||
|
|||
@Override |
|||
public OAuth2AuthorizationRequest loadAuthorizationRequest(HttpServletRequest request) { |
|||
return CookieUtils.getCookie(request, OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME) |
|||
.map(cookie -> CookieUtils.deserialize(cookie, OAuth2AuthorizationRequest.class)) |
|||
.orElse(null); |
|||
} |
|||
|
|||
@Override |
|||
public void saveAuthorizationRequest(OAuth2AuthorizationRequest authorizationRequest, HttpServletRequest request, HttpServletResponse response) { |
|||
if (authorizationRequest == null) { |
|||
CookieUtils.deleteCookie(request, response, OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME); |
|||
return; |
|||
} |
|||
CookieUtils.addCookie(response, OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME, CookieUtils.serialize(authorizationRequest), cookieExpireSeconds); |
|||
} |
|||
|
|||
@SuppressWarnings("deprecation") |
|||
@Override |
|||
public OAuth2AuthorizationRequest removeAuthorizationRequest(HttpServletRequest request) { |
|||
return this.loadAuthorizationRequest(request); |
|||
} |
|||
|
|||
public void removeAuthorizationRequestCookies(HttpServletRequest request, HttpServletResponse response) { |
|||
CookieUtils.deleteCookie(request, response, OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME); |
|||
} |
|||
} |
|||
@ -0,0 +1,315 @@ |
|||
/** |
|||
* Copyright © 2016-2021 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.subscription; |
|||
|
|||
import com.google.common.util.concurrent.Futures; |
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import com.google.common.util.concurrent.MoreExecutors; |
|||
import lombok.Data; |
|||
import lombok.Getter; |
|||
import lombok.Setter; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
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.id.UserId; |
|||
import org.thingsboard.server.common.data.kv.AttributeKvEntry; |
|||
import org.thingsboard.server.common.data.query.ComplexFilterPredicate; |
|||
import org.thingsboard.server.common.data.query.DynamicValue; |
|||
import org.thingsboard.server.common.data.query.DynamicValueSourceType; |
|||
import org.thingsboard.server.common.data.query.EntityCountQuery; |
|||
import org.thingsboard.server.common.data.query.EntityKeyType; |
|||
import org.thingsboard.server.common.data.query.FilterPredicateType; |
|||
import org.thingsboard.server.common.data.query.KeyFilter; |
|||
import org.thingsboard.server.common.data.query.KeyFilterPredicate; |
|||
import org.thingsboard.server.common.data.query.SimpleKeyFilterPredicate; |
|||
import org.thingsboard.server.common.data.query.TsValue; |
|||
import org.thingsboard.server.dao.attributes.AttributesService; |
|||
import org.thingsboard.server.dao.entity.EntityService; |
|||
import org.thingsboard.server.service.telemetry.TelemetryWebSocketService; |
|||
import org.thingsboard.server.service.telemetry.TelemetryWebSocketSessionRef; |
|||
import org.thingsboard.server.service.telemetry.sub.TelemetrySubscriptionUpdate; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.Optional; |
|||
import java.util.Set; |
|||
import java.util.concurrent.ConcurrentHashMap; |
|||
import java.util.concurrent.ExecutionException; |
|||
import java.util.concurrent.ScheduledFuture; |
|||
|
|||
@Slf4j |
|||
@Data |
|||
public abstract class TbAbstractSubCtx<T extends EntityCountQuery> { |
|||
|
|||
protected final String serviceId; |
|||
protected final SubscriptionServiceStatistics stats; |
|||
protected final TelemetryWebSocketService wsService; |
|||
protected final EntityService entityService; |
|||
protected final TbLocalSubscriptionService localSubscriptionService; |
|||
protected final AttributesService attributesService; |
|||
protected final TelemetryWebSocketSessionRef sessionRef; |
|||
protected final int cmdId; |
|||
protected final Set<Integer> subToDynamicValueKeySet; |
|||
@Getter |
|||
protected final Map<DynamicValueKey, List<DynamicValue>> dynamicValues; |
|||
@Getter |
|||
@Setter |
|||
protected T query; |
|||
@Setter |
|||
protected volatile ScheduledFuture<?> refreshTask; |
|||
|
|||
public TbAbstractSubCtx(String serviceId, TelemetryWebSocketService wsService, |
|||
EntityService entityService, TbLocalSubscriptionService localSubscriptionService, |
|||
AttributesService attributesService, SubscriptionServiceStatistics stats, |
|||
TelemetryWebSocketSessionRef sessionRef, int cmdId) { |
|||
this.serviceId = serviceId; |
|||
this.wsService = wsService; |
|||
this.entityService = entityService; |
|||
this.localSubscriptionService = localSubscriptionService; |
|||
this.attributesService = attributesService; |
|||
this.stats = stats; |
|||
this.sessionRef = sessionRef; |
|||
this.cmdId = cmdId; |
|||
this.subToDynamicValueKeySet = ConcurrentHashMap.newKeySet(); |
|||
this.dynamicValues = new ConcurrentHashMap<>(); |
|||
} |
|||
|
|||
public void setAndResolveQuery(T query) { |
|||
dynamicValues.clear(); |
|||
this.query = query; |
|||
if (query != null && query.getKeyFilters() != null) { |
|||
for (KeyFilter filter : query.getKeyFilters()) { |
|||
registerDynamicValues(filter.getPredicate()); |
|||
} |
|||
} |
|||
resolve(getTenantId(), getCustomerId(), getUserId()); |
|||
} |
|||
|
|||
public void resolve(TenantId tenantId, CustomerId customerId, UserId userId) { |
|||
List<ListenableFuture<DynamicValueKeySub>> futures = new ArrayList<>(); |
|||
for (DynamicValueKey key : dynamicValues.keySet()) { |
|||
switch (key.getSourceType()) { |
|||
case CURRENT_TENANT: |
|||
futures.add(resolveEntityValue(tenantId, tenantId, key)); |
|||
break; |
|||
case CURRENT_CUSTOMER: |
|||
if (customerId != null && !customerId.isNullUid()) { |
|||
futures.add(resolveEntityValue(tenantId, customerId, key)); |
|||
} |
|||
break; |
|||
case CURRENT_USER: |
|||
if (userId != null && !userId.isNullUid()) { |
|||
futures.add(resolveEntityValue(tenantId, userId, key)); |
|||
} |
|||
break; |
|||
} |
|||
} |
|||
try { |
|||
Map<EntityId, Map<String, DynamicValueKeySub>> tmpSubMap = new HashMap<>(); |
|||
for (DynamicValueKeySub sub : Futures.successfulAsList(futures).get()) { |
|||
tmpSubMap.computeIfAbsent(sub.getEntityId(), tmp -> new HashMap<>()).put(sub.getKey().getSourceAttribute(), sub); |
|||
} |
|||
for (EntityId entityId : tmpSubMap.keySet()) { |
|||
Map<String, Long> keyStates = new HashMap<>(); |
|||
Map<String, DynamicValueKeySub> dynamicValueKeySubMap = tmpSubMap.get(entityId); |
|||
dynamicValueKeySubMap.forEach((k, v) -> keyStates.put(k, v.getLastUpdateTs())); |
|||
int subIdx = sessionRef.getSessionSubIdSeq().incrementAndGet(); |
|||
TbAttributeSubscription sub = TbAttributeSubscription.builder() |
|||
.serviceId(serviceId) |
|||
.sessionId(sessionRef.getSessionId()) |
|||
.subscriptionId(subIdx) |
|||
.tenantId(sessionRef.getSecurityCtx().getTenantId()) |
|||
.entityId(entityId) |
|||
.updateConsumer((s, subscriptionUpdate) -> dynamicValueSubUpdate(s, subscriptionUpdate, dynamicValueKeySubMap)) |
|||
.allKeys(false) |
|||
.keyStates(keyStates) |
|||
.scope(TbAttributeSubscriptionScope.SERVER_SCOPE) |
|||
.build(); |
|||
subToDynamicValueKeySet.add(subIdx); |
|||
localSubscriptionService.addSubscription(sub); |
|||
} |
|||
} catch (InterruptedException | ExecutionException e) { |
|||
log.info("[{}][{}][{}] Failed to resolve dynamic values: {}", tenantId, customerId, userId, dynamicValues.keySet()); |
|||
} |
|||
|
|||
} |
|||
|
|||
private void dynamicValueSubUpdate(String sessionId, TelemetrySubscriptionUpdate subscriptionUpdate, |
|||
Map<String, DynamicValueKeySub> dynamicValueKeySubMap) { |
|||
Map<String, TsValue> latestUpdate = new HashMap<>(); |
|||
subscriptionUpdate.getData().forEach((k, v) -> { |
|||
Object[] data = (Object[]) v.get(0); |
|||
latestUpdate.put(k, new TsValue((Long) data[0], (String) data[1])); |
|||
}); |
|||
|
|||
boolean invalidateFilter = false; |
|||
for (Map.Entry<String, TsValue> entry : latestUpdate.entrySet()) { |
|||
String k = entry.getKey(); |
|||
TsValue tsValue = entry.getValue(); |
|||
DynamicValueKeySub sub = dynamicValueKeySubMap.get(k); |
|||
if (sub.updateValue(tsValue)) { |
|||
invalidateFilter = true; |
|||
updateDynamicValuesByKey(sub, tsValue); |
|||
} |
|||
} |
|||
|
|||
if (invalidateFilter) { |
|||
update(); |
|||
} |
|||
} |
|||
|
|||
public abstract void fetchData(); |
|||
|
|||
protected abstract void update(); |
|||
|
|||
public void clearSubscriptions() { |
|||
clearDynamicValueSubscriptions(); |
|||
} |
|||
|
|||
@Data |
|||
private static class DynamicValueKeySub { |
|||
private final DynamicValueKey key; |
|||
private final EntityId entityId; |
|||
private long lastUpdateTs; |
|||
private String lastUpdateValue; |
|||
|
|||
boolean updateValue(TsValue value) { |
|||
if (value.getTs() > lastUpdateTs && (lastUpdateValue == null || !lastUpdateValue.equals(value.getValue()))) { |
|||
this.lastUpdateTs = value.getTs(); |
|||
this.lastUpdateValue = value.getValue(); |
|||
return true; |
|||
} else { |
|||
return false; |
|||
} |
|||
} |
|||
} |
|||
|
|||
private ListenableFuture<DynamicValueKeySub> resolveEntityValue(TenantId tenantId, EntityId entityId, DynamicValueKey key) { |
|||
ListenableFuture<Optional<AttributeKvEntry>> entry = attributesService.find(tenantId, entityId, |
|||
TbAttributeSubscriptionScope.SERVER_SCOPE.name(), key.getSourceAttribute()); |
|||
return Futures.transform(entry, attributeOpt -> { |
|||
DynamicValueKeySub sub = new DynamicValueKeySub(key, entityId); |
|||
if (attributeOpt.isPresent()) { |
|||
AttributeKvEntry attribute = attributeOpt.get(); |
|||
sub.setLastUpdateTs(attribute.getLastUpdateTs()); |
|||
sub.setLastUpdateValue(attribute.getValueAsString()); |
|||
updateDynamicValuesByKey(sub, new TsValue(attribute.getLastUpdateTs(), attribute.getValueAsString())); |
|||
} |
|||
return sub; |
|||
}, MoreExecutors.directExecutor()); |
|||
} |
|||
|
|||
@SuppressWarnings("unchecked") |
|||
protected void updateDynamicValuesByKey(DynamicValueKeySub sub, TsValue tsValue) { |
|||
DynamicValueKey dvk = sub.getKey(); |
|||
switch (dvk.getPredicateType()) { |
|||
case STRING: |
|||
dynamicValues.get(dvk).forEach(dynamicValue -> dynamicValue.setResolvedValue(tsValue.getValue())); |
|||
break; |
|||
case NUMERIC: |
|||
try { |
|||
Double dValue = Double.parseDouble(tsValue.getValue()); |
|||
dynamicValues.get(dvk).forEach(dynamicValue -> dynamicValue.setResolvedValue(dValue)); |
|||
} catch (NumberFormatException e) { |
|||
dynamicValues.get(dvk).forEach(dynamicValue -> dynamicValue.setResolvedValue(null)); |
|||
} |
|||
break; |
|||
case BOOLEAN: |
|||
Boolean bValue = Boolean.parseBoolean(tsValue.getValue()); |
|||
dynamicValues.get(dvk).forEach(dynamicValue -> dynamicValue.setResolvedValue(bValue)); |
|||
break; |
|||
} |
|||
} |
|||
|
|||
@SuppressWarnings("unchecked") |
|||
private void registerDynamicValues(KeyFilterPredicate predicate) { |
|||
switch (predicate.getType()) { |
|||
case STRING: |
|||
case NUMERIC: |
|||
case BOOLEAN: |
|||
Optional<DynamicValue> value = getDynamicValueFromSimplePredicate((SimpleKeyFilterPredicate) predicate); |
|||
if (value.isPresent()) { |
|||
DynamicValue dynamicValue = value.get(); |
|||
DynamicValueKey key = new DynamicValueKey( |
|||
predicate.getType(), |
|||
dynamicValue.getSourceType(), |
|||
dynamicValue.getSourceAttribute()); |
|||
dynamicValues.computeIfAbsent(key, tmp -> new ArrayList<>()).add(dynamicValue); |
|||
} |
|||
break; |
|||
case COMPLEX: |
|||
((ComplexFilterPredicate) predicate).getPredicates().forEach(this::registerDynamicValues); |
|||
} |
|||
} |
|||
|
|||
private Optional<DynamicValue<T>> getDynamicValueFromSimplePredicate(SimpleKeyFilterPredicate<T> predicate) { |
|||
if (predicate.getValue().getUserValue() == null) { |
|||
return Optional.ofNullable(predicate.getValue().getDynamicValue()); |
|||
} else { |
|||
return Optional.empty(); |
|||
} |
|||
} |
|||
|
|||
public String getSessionId() { |
|||
return sessionRef.getSessionId(); |
|||
} |
|||
|
|||
public TenantId getTenantId() { |
|||
return sessionRef.getSecurityCtx().getTenantId(); |
|||
} |
|||
|
|||
public CustomerId getCustomerId() { |
|||
return sessionRef.getSecurityCtx().getCustomerId(); |
|||
} |
|||
|
|||
public UserId getUserId() { |
|||
return sessionRef.getSecurityCtx().getId(); |
|||
} |
|||
|
|||
protected void clearDynamicValueSubscriptions() { |
|||
if (subToDynamicValueKeySet != null) { |
|||
for (Integer subId : subToDynamicValueKeySet) { |
|||
localSubscriptionService.cancelSubscription(sessionRef.getSessionId(), subId); |
|||
} |
|||
subToDynamicValueKeySet.clear(); |
|||
} |
|||
} |
|||
|
|||
public void setRefreshTask(ScheduledFuture<?> task) { |
|||
this.refreshTask = task; |
|||
} |
|||
|
|||
public void cancelTasks() { |
|||
if (this.refreshTask != null) { |
|||
log.trace("[{}][{}] Canceling old refresh task", sessionRef.getSessionId(), cmdId); |
|||
this.refreshTask.cancel(true); |
|||
} |
|||
} |
|||
|
|||
@Data |
|||
public static class DynamicValueKey { |
|||
@Getter |
|||
private final FilterPredicateType predicateType; |
|||
@Getter |
|||
private final DynamicValueSourceType sourceType; |
|||
@Getter |
|||
private final String sourceAttribute; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,55 @@ |
|||
/** |
|||
* Copyright © 2016-2021 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.subscription; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.thingsboard.server.common.data.query.EntityCountQuery; |
|||
import org.thingsboard.server.common.data.query.EntityKeyType; |
|||
import org.thingsboard.server.dao.attributes.AttributesService; |
|||
import org.thingsboard.server.dao.entity.EntityService; |
|||
import org.thingsboard.server.service.telemetry.TelemetryWebSocketService; |
|||
import org.thingsboard.server.service.telemetry.TelemetryWebSocketSessionRef; |
|||
import org.thingsboard.server.service.telemetry.cmd.v2.EntityCountUpdate; |
|||
import org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate; |
|||
import org.thingsboard.server.service.telemetry.sub.TelemetrySubscriptionUpdate; |
|||
|
|||
@Slf4j |
|||
public class TbEntityCountSubCtx extends TbAbstractSubCtx<EntityCountQuery> { |
|||
|
|||
private volatile int result; |
|||
|
|||
public TbEntityCountSubCtx(String serviceId, TelemetryWebSocketService wsService, EntityService entityService, |
|||
TbLocalSubscriptionService localSubscriptionService, AttributesService attributesService, |
|||
SubscriptionServiceStatistics stats, TelemetryWebSocketSessionRef sessionRef, int cmdId) { |
|||
super(serviceId, wsService, entityService, localSubscriptionService, attributesService, stats, sessionRef, cmdId); |
|||
} |
|||
|
|||
@Override |
|||
public void fetchData() { |
|||
result = (int) entityService.countEntitiesByQuery(getTenantId(), getCustomerId(), query); |
|||
wsService.sendWsMsg(sessionRef.getSessionId(), new EntityCountUpdate(cmdId, result)); |
|||
} |
|||
|
|||
@Override |
|||
protected void update() { |
|||
int newCount = (int) entityService.countEntitiesByQuery(getTenantId(), getCustomerId(), query); |
|||
if (newCount != result) { |
|||
result = newCount; |
|||
wsService.sendWsMsg(sessionRef.getSessionId(), new EntityCountUpdate(cmdId, result)); |
|||
} |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
/** |
|||
* Copyright © 2016-2021 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.telemetry.cmd.v2; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Data; |
|||
|
|||
@Data |
|||
@AllArgsConstructor |
|||
@JsonIgnoreProperties(ignoreUnknown = true) |
|||
public abstract class CmdUpdate { |
|||
|
|||
private final int cmdId; |
|||
private final int errorCode; |
|||
private final String errorMsg; |
|||
|
|||
public abstract CmdUpdateType getCmdUpdateType(); |
|||
|
|||
} |
|||
@ -0,0 +1,35 @@ |
|||
/** |
|||
* Copyright © 2016-2021 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.telemetry.cmd.v2; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonCreator; |
|||
import com.fasterxml.jackson.annotation.JsonProperty; |
|||
import lombok.Getter; |
|||
import org.thingsboard.server.common.data.query.EntityCountQuery; |
|||
import org.thingsboard.server.common.data.query.EntityDataQuery; |
|||
|
|||
public class EntityCountCmd extends DataCmd { |
|||
|
|||
@Getter |
|||
private final EntityCountQuery query; |
|||
|
|||
@JsonCreator |
|||
public EntityCountCmd(@JsonProperty("cmdId") int cmdId, |
|||
@JsonProperty("query") EntityCountQuery query) { |
|||
super(cmdId); |
|||
this.query = query; |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
/** |
|||
* Copyright © 2016-2021 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.telemetry.cmd.v2; |
|||
|
|||
import lombok.Data; |
|||
|
|||
@Data |
|||
public class EntityCountUnsubscribeCmd implements UnsubscribeCmd { |
|||
|
|||
private final int cmdId; |
|||
|
|||
} |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue