Skip to content

Commit

Permalink
Merge pull request #33 from gerrieg/items
Browse files Browse the repository at this point in the history
Optimized items
  • Loading branch information
seaside1 authored May 10, 2022
2 parents 80c0969 + 4877dda commit 3a3a574
Show file tree
Hide file tree
Showing 39 changed files with 902 additions and 502 deletions.
51 changes: 27 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ When the rule is triggered, the triggered value is stored in the event.
@JRuleName("MyEventValueTest")
@JRuleWhen(item = __MyTestSwitch2.ITEM, trigger = __MyTestSwitch2.TRIGGER_RECEIVED_COMMAND)
public void myEventValueTest(JRuleEvent event) {
logInfo("Got value from event: {}", event.getValue());
logInfo("Got value from event: {}", event.getState().getValue());
}
```
## Example 4
Expand All @@ -207,7 +207,7 @@ To add an OR statement we simply add multiple @JRuleWhen statements
@JRuleWhen(item = _MyTestNumber.ITEM, trigger = _MyTestNumber.TRIGGER_CHANGED, from = "14", to = "10")
@JRuleWhen(item = _MyTestNumber.ITEM, trigger = _MyTestNumber.TRIGGER_CHANGED, from = "10", to = "12")
public void myOrRuleNumber(JRuleEvent event) {
logInfo("Got change number: {}", event.getValue());
logInfo("Got change number: {}", event.getState().getValue());
}
```

Expand Down Expand Up @@ -352,13 +352,13 @@ Use case trigger a rule at 22:30 in the evening to set initial brightness for a

## Example 12

Use case: If temperature is below or equals to 20 degrees send command on to a heating fan
It is possible to use:
lte = less than or equals
lt = less than
gt = greater than
gte = greater than or equals
eq = equals
Use case: If temperature is below or equals to 20 degrees send command on to a heating fan
It is possible to use:
lte = less than or equals
lt = less than
gt = greater than
gte = greater than or equals
eq = equals
```java
@JRuleName("turnOnFanIfTemperatureIsLow")
@JRuleWhen(item = _MyTemperatureSensor.ITEM, trigger = _MyTemperatureSensor.TRIGGER_RECEIVED_UPDATE, lte = 20)
Expand Down Expand Up @@ -400,7 +400,7 @@ Use case: A group of switches, see if status is changed, and also which member i
@JRuleName("groupMySwitchesChanged")
@JRuleWhen(item = _gMySwitchGroup.ITEM, trigger = _gMySwitchGroup.TRIGGER_CHANGED)
public synchronized void groupMySwitchGroupChanged(JRuleEvent event) {
final boolean groupIsOnline = event.getValueAsOnOffValue() == ON;
final boolean groupIsOnline = event.getState().getValueAsOnOffValue() == ON;
final String memberThatChangedStatus = event.getMemberName();
logInfo("Member that changed the status of the Group of switches: {}", memberThatChangedStatus);
}
Expand All @@ -424,7 +424,7 @@ Use case: Listen for a Channel Trigger Event
@JRuleName("channelTriggered")
@JRuleWhen(channel = "binding:thing:buttonevent")
public synchronized void channelTriggered(JRuleEvent event) {
logInfo("Channel triggered with value: {}", event.getValue());
logInfo("Channel triggered with value: {}", event.getState().getValue());
}
```

Expand All @@ -435,30 +435,29 @@ Use case: Cron based expression to trigger rule
@JRuleName("testCron")
@JRuleWhen(cron = "4 * * * * *")
public void testCron(JRuleEvent event) {
logInfo("CRON: Running cron from string every minute when seconds is at 4: {}", event.getValue());
logInfo("CRON: Running cron from string every minute when seconds is at 4: {}", event.getState().getValue());
}
```


## Example 19

Use case: getLastUpdated for an item
Note that JRulePersistenceExtentions.getLastUpdate(_MyCoolItem.ITEM, "mapdb");
can be called without serviceId argument:
JRulePersistenceExtentions.getLastUpdate(_MyCoolItem.ITEM);
Use case: getLastUpdated for an item
Note that JRulePersistenceExtentions.getLastUpdate(_MyCoolItem.ITEM, "mapdb");
can be called without serviceId argument:
JRulePersistenceExtentions.getLastUpdate(_MyCoolItem.ITEM);
```java

@JRuleName("testLastUpdate")
@JRuleWhen(cron = "4 * * * * *")
public void testLastUpdate(JRuleEvent event) {
logInfo("CRON: Running cron from string: {}", event.getValue());
logInfo("CRON: Running cron from string: {}", event.getState().getValue());
ZonedDateTime lastUpdate = JRulePersistenceExtentions.getLastUpdate(_MyCoolItem.ITEM, "mapdb");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd - HH:mm:ss Z");
String lastUpdateFormatted = lastUpdate.format(formatter);
logInfo("Last Update: {}", lastUpdateFormatted);
```


## Example 20

Use case: Get the brigtness from a color item, set a color item to white (HSB 0, 0, 100)
Expand Down Expand Up @@ -518,8 +517,8 @@ Use case: Apply transformation using openHAB transformation service
@JRuleName("MyTransformation")
@JRuleWhen(item = _MyStringValue.ITEM, trigger = _MyStringValue.TRIGGER_RECEIVED_COMMAND)
public void applyTransformation(JRuleEvent event) {
String transformedValue = transform("MAP(my.map):%s", event.getValue());
logInfo("Transformed {} to {}", event.getValue(), transformedValue);
String transformedValue = transform("MAP(my.map):%s", event.getState().getValue());
logInfo("Transformed {} to {}", event.getState().getValue(), transformedValue);
_MyTransformationReceiver.sendCommand(transformedValue);
}
}
Expand All @@ -538,7 +537,7 @@ message is updated.
@JRuleName("MyTestPreConditionRule1")
@JRuleWhen(item = _MyMessageNotification.ITEM, trigger = _MyMessageNotification.TRIGGER_RECEIVED_COMMAND)
public void testPrecondition(JRuleEvent event) {
String notificationMessage = event.getValue();
String notificationMessage = event.getState().getValue();
logInfo("It is ok to send notification: {}", notificationMessage);
_MySendNoticationItemMqtt.sendCommand(notificationMessage);
}
Expand Down Expand Up @@ -571,8 +570,8 @@ Use case: Send Quantity type Watt (W) from rule.
@JRuleName("testQuantityPowerWatt")
@JRuleWhen(item=_MyTestMeterPower.ITEM, trigger=_MyTestMeterPower.TRIGGER_CHANGED)
public void testQuantityPower(JRuleEvent event) {
logInfo("TestQuantity power will send this value as Watt: {}", event.getValue());
_TestPowerQuantityType.sendCommand(event.getValueAsDouble(), "W");
logInfo("TestQuantity power will send this value as Watt: {}", event.getState().getValue());
_TestPowerQuantityType.sendCommand(event.getState().getValueAsDouble(), "W");
}
}
```
Expand Down Expand Up @@ -609,14 +608,18 @@ triggered the rule.
@JRuleWhen(item=_MyTestSwitch2.ITEM, trigger=_MyTestSwitch2.TRIGGER_CHANGED_TO_ON)
public void triggerNameExample(JRuleEvent event) {
logInfo("The rule was triggered by the following item: {}", event.getItemName());
logInfo("The rule was Old Value was: {} and new value: {}", event.getOldState(), event.getState());
logInfo("The rule was Old Value was: {} and new value: {}", event.getOldState().getValue(), event.getState().getValue());

}
}
```


# Changelog
## BETA10
- Optimized items by gerrieg https://github.com/seaside1/jrule/pull/33
- Syntax change: event.getValue(), event.getValuesAsDouble() etc replaced with event.getState().getValue() and event.getState().getValueAsDouble()
- Syntax change JRuleSwitchItem.sendCommand(myItem, ON) replaced with JRuleSwitchItem.forName(myItem).sendCommand(ON)
## BETA9
- Fixed bug with item generation and forName overloading
## BETA8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.openhab.automation.jrule.internal.events.JRuleEventSubscriber;
import org.openhab.automation.jrule.internal.watch.JRuleRulesWatcher;
import org.openhab.automation.jrule.items.JRuleItemClassGenerator;
import org.openhab.automation.jrule.items.JRuleItemRegistry;
import org.openhab.core.events.Event;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.items.Item;
Expand Down Expand Up @@ -246,6 +247,7 @@ private synchronized void createItemsJar() {
} else {
logError("Failed to create items due to config {}, compiler is null", config);
}
JRuleItemRegistry.clear();
}

public synchronized void dispose() {
Expand All @@ -263,6 +265,7 @@ public synchronized void dispose() {
}
}
eventSubscriber.removePropertyChangeListener(this);
JRuleItemRegistry.clear();
}

public synchronized void generateItemSources() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
import org.openhab.automation.jrule.rules.value.JRuleColorValue;
import org.openhab.automation.jrule.rules.value.JRuleIncreaseDecreaseValue;
import org.openhab.automation.jrule.rules.value.JRuleOnOffValue;
import org.openhab.automation.jrule.trigger.JRuleCommonTrigger;

/**
* The {@link JRuleColorItem} Items
*
* @author Joseph (Seaside) Hagberg - Initial contribution
*/
public class JRuleColorItem extends JRuleItem {
public class JRuleColorItem extends JRuleItem implements JRuleCommonTrigger {

private final String itemName;

Expand All @@ -31,86 +32,46 @@ protected JRuleColorItem(String itemName) {
}

public static JRuleColorItem forName(String itemName) {
return new JRuleColorItem(itemName);
return JRuleItemRegistry.get(itemName, JRuleColorItem.class);
}

public JRuleColorValue getItemState() {
public JRuleColorValue getState() {
return JRuleEventHandler.get().getColorValue(itemName);
}

public JRuleOnOffValue getItemOnOffState() {
public JRuleOnOffValue getOnOffState() {
return JRuleEventHandler.get().getOnOffValue(itemName);
}

public int getItemPercentState() {
public int getPercentState() {
return JRuleEventHandler.get().getStateFromItemAsInt(itemName);
}

public void sendItemCommand(JRuleColorValue colorValue) {
public void sendCommand(JRuleColorValue colorValue) {
JRuleEventHandler.get().sendCommand(itemName, colorValue);
}

public void sendItemCommand(JRuleOnOffValue command) {
public void sendCommand(JRuleOnOffValue command) {
JRuleEventHandler.get().sendCommand(itemName, command);
}

public void sendItemCommand(JRuleIncreaseDecreaseValue command) {
public void sendCommand(JRuleIncreaseDecreaseValue command) {
JRuleEventHandler.get().sendCommand(itemName, command);
}

public void sendItemCommand(int value) {
public void sendCommand(int value) {
JRuleEventHandler.get().sendCommand(itemName, new JRulePercentType(value));
}

public void postItemUpdate(JRuleColorValue colorValue) {
public void postUpdate(JRuleColorValue colorValue) {
JRuleEventHandler.get().postUpdate(itemName, colorValue);
}

public void postItemUpdate(JRuleOnOffValue state) {
public void postUpdate(JRuleOnOffValue state) {
JRuleEventHandler.get().postUpdate(itemName, state);
}

public void postItemUpdate(int value) {
JRuleEventHandler.get().postUpdate(itemName, new JRulePercentType(value));
}

public static JRuleColorValue getState(String itemName) {
return JRuleEventHandler.get().getColorValue(itemName);
}

public static JRuleOnOffValue getOnOffState(String itemName) {
return JRuleEventHandler.get().getOnOffValue(itemName);
}

public static int getPercentState(String itemName) {
return JRuleEventHandler.get().getStateFromItemAsInt(itemName);
}

public static void sendCommand(String itemName, JRuleColorValue colorValue) {
JRuleEventHandler.get().sendCommand(itemName, colorValue);
}

public static void sendCommand(String itemName, JRuleOnOffValue command) {
JRuleEventHandler.get().sendCommand(itemName, command);
}

public static void sendCommand(String itemName, JRuleIncreaseDecreaseValue command) {
JRuleEventHandler.get().sendCommand(itemName, command);
}

public static void sendCommand(String itemName, int value) {
JRuleEventHandler.get().sendCommand(itemName, new JRulePercentType(value));
}

public static void postUpdate(String itemName, JRuleColorValue colorValue) {
JRuleEventHandler.get().postUpdate(itemName, colorValue);
}

public static void postUpdate(String itemName, JRuleOnOffValue state) {
JRuleEventHandler.get().postUpdate(itemName, state);
}

public static void postUpdate(String itemName, int value) {
public void postUpdate(int value) {
JRuleEventHandler.get().postUpdate(itemName, new JRulePercentType(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

import org.openhab.automation.jrule.internal.handler.JRuleEventHandler;
import org.openhab.automation.jrule.rules.value.JRuleOpenClosedValue;
import org.openhab.automation.jrule.trigger.JRuleCommonTrigger;

/**
* The {@link JRuleContactItem} Items
*
* @author Timo Litzius - Initial contribution
*/
public class JRuleContactItem extends JRuleItem {
public class JRuleContactItem extends JRuleItem implements JRuleCommonTrigger {

private final String itemName;

Expand All @@ -29,14 +30,10 @@ protected JRuleContactItem(String itemName) {
}

public static JRuleContactItem forName(String itemName) {
return new JRuleContactItem(itemName);
return JRuleItemRegistry.get(itemName, JRuleContactItem.class);
}

public JRuleOpenClosedValue getItemState() {
return JRuleEventHandler.get().getOpenClosedValue(itemName);
}

public static JRuleOpenClosedValue getState(String itemName) {
public JRuleOpenClosedValue getState() {
return JRuleEventHandler.get().getOpenClosedValue(itemName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
import java.util.Date;

import org.openhab.automation.jrule.internal.handler.JRuleEventHandler;
import org.openhab.automation.jrule.trigger.JRuleCommonTrigger;

/**
* The {@link JRuleDateTimeItem} Items
*
* @author Joseph (Seaside) Hagberg - Initial contribution
*/
public class JRuleDateTimeItem extends JRuleItem {
public class JRuleDateTimeItem extends JRuleItem implements JRuleCommonTrigger {

private final String itemName;

Expand All @@ -30,30 +31,18 @@ protected JRuleDateTimeItem(String itemName) {
}

public static JRuleDateTimeItem forName(String itemName) {
return new JRuleDateTimeItem(itemName);
return JRuleItemRegistry.get(itemName, JRuleDateTimeItem.class);
}

public void sendItemCommand(Date date) {
public void sendCommand(Date date) {
JRuleEventHandler.get().sendCommand(itemName, date);
}

public void postItemUpdate(Date date) {
public void postUpdate(Date date) {
JRuleEventHandler.get().postUpdate(itemName, date);
}

public Date getItemState() {
return JRuleEventHandler.get().getStateFromItemAsDate(itemName);
}

public static void sendCommand(String itemName, Date date) {
JRuleEventHandler.get().sendCommand(itemName, date);
}

public static void postUpdate(String itemName, Date date) {
JRuleEventHandler.get().postUpdate(itemName, date);
}

public static Date getState(String itemName) {
public Date getState() {
return JRuleEventHandler.get().getStateFromItemAsDate(itemName);
}
}
Loading

0 comments on commit 3a3a574

Please sign in to comment.