-
-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add file based rule provider and YAML rule parser
Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
- Loading branch information
Ravi Nadahar
committed
Feb 13, 2025
1 parent
07fbfd1
commit ba2b127
Showing
4 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
...ion/src/main/java/org/openhab/core/automation/internal/parser/jackson/RuleYAMLParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (c) 2010-2025 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.core.automation.internal.parser.jackson; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openhab.core.automation.Rule; | ||
import org.openhab.core.automation.dto.RuleDTO; | ||
import org.openhab.core.automation.dto.RuleDTOMapper; | ||
import org.openhab.core.automation.parser.Parser; | ||
import org.openhab.core.automation.parser.ParsingException; | ||
import org.openhab.core.automation.parser.ParsingNestedException; | ||
import org.osgi.service.component.annotations.Component; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
/** | ||
* This class can parse and serialize sets of {@link Rule}s. | ||
* | ||
* @author Ravi Nadahar - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
@Component(immediate = true, service = Parser.class, property = { "parser.type=parser.rule", "format=yaml" }) | ||
public class RuleYAMLParser extends AbstractJacksonYAMLParser<Rule> { | ||
|
||
@Override | ||
public Set<Rule> parse(InputStreamReader reader) throws ParsingException { | ||
try { | ||
Set<Rule> rules = new HashSet<>(); | ||
JsonNode rootNode = yamlMapper.readTree(reader); | ||
if (rootNode.isArray()) { | ||
List<RuleDTO> ruleDtos = yamlMapper.convertValue(rootNode, new TypeReference<List<RuleDTO>>() { | ||
}); | ||
for (RuleDTO ruleDTO : ruleDtos) { | ||
rules.add(RuleDTOMapper.map(ruleDTO)); | ||
} | ||
} else { | ||
RuleDTO ruleDto = yamlMapper.convertValue(rootNode, new TypeReference<RuleDTO>() { | ||
}); | ||
rules.add(RuleDTOMapper.map(ruleDto)); | ||
} | ||
return rules; | ||
} catch (Exception e) { | ||
throw new ParsingException(new ParsingNestedException(ParsingNestedException.RULE, null, e)); | ||
} finally { | ||
try { | ||
reader.close(); | ||
} catch (IOException e) { | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...on/src/main/java/org/openhab/core/automation/internal/provider/file/RuleFileProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) 2010-2025 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.core.automation.internal.provider.file; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openhab.core.automation.Rule; | ||
import org.openhab.core.automation.RuleProvider; | ||
|
||
/** | ||
* This class is a {@link RuleProvider} implementation that provides file-based rules. It extends the functionality | ||
* of {@link AbstractFileProvider} for importing the {@link Rule}s from local files. | ||
* | ||
* @author Ravi Nadahar - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public abstract class RuleFileProvider extends AbstractFileProvider<Rule> implements RuleProvider { | ||
|
||
/** | ||
* Creates a new instance. | ||
*/ | ||
public RuleFileProvider() { | ||
super("rules"); | ||
} | ||
|
||
@Override | ||
protected String getUID(Rule providedObject) { | ||
return providedObject.getUID(); | ||
} | ||
|
||
@Override | ||
public Collection<Rule> getAll() { | ||
return List.copyOf(providedObjectsHolder.values()); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...main/java/org/openhab/core/automation/internal/provider/file/RuleFileProviderWatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (c) 2010-2025 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.core.automation.internal.provider.file; | ||
|
||
import java.util.Map; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openhab.core.OpenHAB; | ||
import org.openhab.core.automation.Rule; | ||
import org.openhab.core.automation.RuleProvider; | ||
import org.openhab.core.automation.parser.Parser; | ||
import org.openhab.core.automation.parser.ValidationException; | ||
import org.openhab.core.automation.parser.ValidationException.ObjectType; | ||
import org.openhab.core.service.WatchService; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
import org.osgi.service.component.annotations.ReferenceCardinality; | ||
import org.osgi.service.component.annotations.ReferencePolicy; | ||
|
||
/** | ||
* This class is a wrapper of {@link RuleFileProvider}s, responsible for initializing the WatchService. | ||
* | ||
* @author Ravi Nadahar - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
@Component(immediate = true, service = RuleProvider.class) | ||
public class RuleFileProviderWatcher extends RuleFileProvider { | ||
|
||
private final WatchService watchService; | ||
|
||
/** | ||
* Creates a new instance using the specified watch service. | ||
* | ||
* @param watchService the {@link WatchService} to use. | ||
*/ | ||
@Activate | ||
public RuleFileProviderWatcher(@Reference(target = WatchService.CONFIG_WATCHER_FILTER) WatchService watchService) { | ||
this.watchService = watchService; | ||
configurationRoots = new String[] { OpenHAB.getConfigFolder() }; | ||
} | ||
|
||
@Override | ||
protected void initializeWatchService(String watchingDir) { | ||
WatchServiceUtil.initializeWatchService(watchingDir, this, watchService); | ||
} | ||
|
||
@Override | ||
protected void deactivateWatchService(String watchingDir) { | ||
WatchServiceUtil.deactivateWatchService(watchingDir, this); | ||
} | ||
|
||
@Reference(cardinality = ReferenceCardinality.AT_LEAST_ONE, policy = ReferencePolicy.DYNAMIC, target = "(parser.type=parser.rule)") | ||
@Override | ||
public void addParser(Parser<Rule> parser, Map<String, String> properties) { | ||
super.addParser(parser, properties); | ||
} | ||
|
||
@Override | ||
public void removeParser(Parser<Rule> parser, Map<String, String> properties) { | ||
super.removeParser(parser, properties); | ||
} | ||
|
||
@SuppressWarnings("null") | ||
@Override | ||
protected void validateObject(Rule rule) throws ValidationException { | ||
String s; | ||
if ((s = rule.getUID()) == null || s.isBlank()) { | ||
throw new ValidationException(ObjectType.RULE, null, "UID cannot be blank"); | ||
} | ||
if ((s = rule.getName()) == null || s.isBlank()) { | ||
throw new ValidationException(ObjectType.RULE, rule.getUID(), "Name cannot be blank"); | ||
} | ||
} | ||
} |