19 changed files with 303 additions and 81 deletions
@ -0,0 +1,22 @@ |
|||
/** |
|||
* Copyright © 2016-2018 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.install; |
|||
|
|||
public interface DataUpdateService { |
|||
|
|||
void updateData(String fromVersion) throws Exception; |
|||
|
|||
} |
|||
@ -0,0 +1,106 @@ |
|||
/** |
|||
* Copyright © 2016-2018 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.install; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.context.annotation.Profile; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.Tenant; |
|||
import org.thingsboard.server.common.data.id.IdBased; |
|||
import org.thingsboard.server.common.data.page.TextPageLink; |
|||
import org.thingsboard.server.common.data.rule.RuleChain; |
|||
import org.thingsboard.server.dao.rule.RuleChainService; |
|||
import org.thingsboard.server.dao.tenant.TenantService; |
|||
|
|||
import java.util.List; |
|||
import java.util.UUID; |
|||
|
|||
@Service |
|||
@Profile("install") |
|||
@Slf4j |
|||
public class DefaultDataUpdateService implements DataUpdateService { |
|||
|
|||
@Autowired |
|||
private TenantService tenantService; |
|||
|
|||
@Autowired |
|||
private RuleChainService ruleChainService; |
|||
|
|||
@Autowired |
|||
private InstallScripts installScripts; |
|||
|
|||
@Override |
|||
public void updateData(String fromVersion) throws Exception { |
|||
switch (fromVersion) { |
|||
case "1.4.0": |
|||
log.info("Updating data from version 1.4.0 to 1.5.0 ..."); |
|||
tenantsDefaultRuleChainUpdater.updateEntities(null); |
|||
break; |
|||
default: |
|||
throw new RuntimeException("Unable to update data, unsupported fromVersion: " + fromVersion); |
|||
} |
|||
} |
|||
|
|||
private PaginatedUpdater<String, Tenant> tenantsDefaultRuleChainUpdater = |
|||
new PaginatedUpdater<String, Tenant>() { |
|||
|
|||
@Override |
|||
protected List<Tenant> findEntities(String region, TextPageLink pageLink) { |
|||
return tenantService.findTenants(pageLink).getData(); |
|||
} |
|||
|
|||
@Override |
|||
protected void updateEntity(Tenant tenant) { |
|||
try { |
|||
RuleChain ruleChain = ruleChainService.getRootTenantRuleChain(tenant.getId()); |
|||
if (ruleChain == null) { |
|||
installScripts.createDefaultRuleChains(tenant.getId()); |
|||
} |
|||
} catch (Exception e) { |
|||
log.error("Unable to update Tenant", e); |
|||
} |
|||
} |
|||
}; |
|||
|
|||
public abstract class PaginatedUpdater<I, D extends IdBased<?>> { |
|||
|
|||
private static final int DEFAULT_LIMIT = 100; |
|||
|
|||
public void updateEntities(I id) { |
|||
TextPageLink pageLink = new TextPageLink(DEFAULT_LIMIT); |
|||
boolean hasNext = true; |
|||
while (hasNext) { |
|||
List<D> entities = findEntities(id, pageLink); |
|||
for (D entity : entities) { |
|||
updateEntity(entity); |
|||
} |
|||
hasNext = entities.size() == pageLink.getLimit(); |
|||
if (hasNext) { |
|||
int index = entities.size() - 1; |
|||
UUID idOffset = entities.get(index).getUuidId(); |
|||
pageLink.setIdOffset(idOffset); |
|||
} |
|||
} |
|||
} |
|||
|
|||
protected abstract List<D> findEntities(I id, TextPageLink pageLink); |
|||
|
|||
protected abstract void updateEntity(D entity); |
|||
|
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,35 @@ |
|||
/** |
|||
* Copyright © 2016-2018 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.rule.engine.api; |
|||
|
|||
import lombok.Data; |
|||
import org.thingsboard.rule.engine.api.NodeConfiguration; |
|||
|
|||
import java.util.Arrays; |
|||
import java.util.Collections; |
|||
import java.util.List; |
|||
|
|||
@Data |
|||
public class EmptyNodeConfiguration implements NodeConfiguration<EmptyNodeConfiguration> { |
|||
|
|||
private int version; |
|||
|
|||
@Override |
|||
public EmptyNodeConfiguration defaultConfiguration() { |
|||
EmptyNodeConfiguration configuration = new EmptyNodeConfiguration(); |
|||
return configuration; |
|||
} |
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
/** |
|||
* Copyright © 2016-2018 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.rule.engine.filter; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.thingsboard.rule.engine.TbNodeUtils; |
|||
import org.thingsboard.rule.engine.api.*; |
|||
import org.thingsboard.server.common.data.plugin.ComponentType; |
|||
import org.thingsboard.server.common.msg.TbMsg; |
|||
import org.thingsboard.server.common.msg.session.SessionMsgType; |
|||
|
|||
@Slf4j |
|||
@RuleNode( |
|||
type = ComponentType.FILTER, |
|||
name = "message type switch", |
|||
configClazz = EmptyNodeConfiguration.class, |
|||
relationTypes = {"Post attributes", "Post telemetry", "RPC Request", "Other"}, |
|||
nodeDescription = "Route incoming messages by Message Type", |
|||
nodeDetails = "Sends messages with message types <b>\"Post attributes\", \"Post telemetry\", \"RPC Request\"</b> via corresponding chain, otherwise <b>Other</b> chain is used.", |
|||
uiResources = {"static/rulenode/rulenode-core-config.js"}, |
|||
configDirective = "tbNodeEmptyConfig") |
|||
public class TbMsgTypeSwitchNode implements TbNode { |
|||
|
|||
EmptyNodeConfiguration config; |
|||
|
|||
@Override |
|||
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException { |
|||
this.config = TbNodeUtils.convert(configuration, EmptyNodeConfiguration.class); |
|||
} |
|||
|
|||
@Override |
|||
public void onMsg(TbContext ctx, TbMsg msg) throws TbNodeException { |
|||
String relationType; |
|||
if (msg.getType().equals(SessionMsgType.POST_ATTRIBUTES_REQUEST.name())) { |
|||
relationType = "Post attributes"; |
|||
} else if (msg.getType().equals(SessionMsgType.POST_TELEMETRY_REQUEST.name())) { |
|||
relationType = "Post telemetry"; |
|||
} else if (msg.getType().equals(SessionMsgType.TO_SERVER_RPC_REQUEST.name())) { |
|||
relationType = "RPC Request"; |
|||
} else { |
|||
relationType = "Other"; |
|||
} |
|||
ctx.tellNext(msg, relationType); |
|||
} |
|||
|
|||
@Override |
|||
public void destroy() { |
|||
|
|||
} |
|||
} |
|||
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue