This repository has been archived by the owner on May 7, 2020. It is now read-only.
/
SampleJavaDemo.java
109 lines (97 loc) · 3.85 KB
/
SampleJavaDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Copyright (c) 2014,2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* 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.eclipse.smarthome.automation.sample.java.demo.internal;
import java.util.ArrayList;
import org.eclipse.smarthome.automation.Action;
import org.eclipse.smarthome.automation.Rule;
import org.eclipse.smarthome.automation.RuleRegistry;
import org.eclipse.smarthome.automation.Trigger;
import org.eclipse.smarthome.automation.Visibility;
import org.eclipse.smarthome.automation.core.util.ModuleBuilder;
import org.eclipse.smarthome.automation.core.util.RuleBuilder;
import org.eclipse.smarthome.config.core.Configuration;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
/**
* This class shows how to create a rule, using the Java API.It also shows how to add it to the rule engine via
* RuleRegistry interface.
*
* @author Plamen Peev - Initial contribution
*/
@Component(immediate = true)
public class SampleJavaDemo {
/**
* Reference to a {@link RuleRegistry} service. The demo uses the service to add and remove a rule.
*/
private static RuleRegistry ruleRegistry;
/**
* The UID of the added {@link Rule}.
*/
private static final String RULE_UID = "JavaDemoRule";
void addRule() {
final Configuration triggerConfig = new Configuration();
triggerConfig.put("itemName", "DemoSwitch");
final Trigger ruleTrigger = ModuleBuilder.createTrigger().withId("RuleTrigger")
.withTypeUID("ItemStateChangeTrigger").withConfiguration(triggerConfig).build();
final Configuration actionConfig = new Configuration();
actionConfig.put("itemName", "DemoDimmer");
actionConfig.put("command", "ON");
final Action ruleAction = ModuleBuilder.createAction().withId("RuleAction").withTypeUID("ItemPostCommandAction")
.withConfiguration(actionConfig).build();
final ArrayList<Trigger> triggers = new ArrayList<Trigger>();
triggers.add(ruleTrigger);
final ArrayList<Action> actions = new ArrayList<Action>();
actions.add(ruleAction);
final Rule r = RuleBuilder.create(RULE_UID).withTriggers(triggers).withActions(actions)
.withVisibility(Visibility.VISIBLE).withName("DemoRule").build();
ruleRegistry.add(r);
}
/**
* Called from DS when all of the required services are available.
*
* @param componentContext - the component's context.
*/
@Activate
protected void activate(ComponentContext componentContext) {
addRule();
}
/**
* Called from DS when one of the required services becomes unavailable.
*
* @param componentContext - the component's context.
*/
@Deactivate
protected void deactivate(ComponentContext componentContext) {
SampleJavaDemo.ruleRegistry.remove(RULE_UID);
}
/**
* Bind the {@link RuleRegistry} service - called from DS.
*
* @param ruleRegistry: RuleRegistry service.
*/
@Reference
protected void setRuleRegistry(RuleRegistry ruleRegistry) {
SampleJavaDemo.ruleRegistry = ruleRegistry;
}
/**
* Unbind the {@link RuleRegistry} service - called from DS.
*
* @param ruleRegistry: RuleRegistry service.
*/
protected void unsetRuleRegistry(RuleRegistry ruleRegistry) {
SampleJavaDemo.ruleRegistry = null;
}
}