use of org.openhab.core.library.items.NumberItem in project openhab1-addons by openhab.
the class PLCLogoBinding method createState.
private State createState(Item item, Object value) {
DecimalType number = null;
if (value instanceof Number) {
number = new DecimalType(value.toString());
}
State state = null;
if (item instanceof StringType) {
state = new StringType((String) value);
} else if (item instanceof NumberItem) {
if (number != null) {
return number;
} else if (value instanceof String) {
state = new DecimalType(((String) value).replaceAll("[^\\d|.]", ""));
}
} else if (item instanceof SwitchItem && (number != null)) {
state = (number.intValue() > 0) ? OnOffType.ON : OnOffType.OFF;
} else if (item instanceof ContactItem && (number != null)) {
state = (number.intValue() > 0) ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
}
return state;
}
use of org.openhab.core.library.items.NumberItem in project openhab1-addons by openhab.
the class PLCLogoBinding method internalReceiveCommand.
@Override
protected void internalReceiveCommand(String itemName, Command command) {
// the code being executed when a command was sent on the openHAB
// event bus goes here. This method is only called if one of the
// BindingProviders provide a binding for the given 'itemName'.
// Note itemname is the item name not the controller name/instance!
//
super.internalReceiveCommand(itemName, command);
logger.debug("internalReceiveCommand() is called!");
for (PLCLogoBindingProvider provider : providers) {
if (!provider.providesBindingFor(itemName)) {
continue;
}
PLCLogoBindingConfig config = provider.getBindingConfig(itemName);
if (!controllers.containsKey(config.getController())) {
logger.warn("Invalid write requested for controller {}", config.getController());
continue;
}
PLCLogoConfig controller = controllers.get(config.getController());
PLCLogoMemoryConfig wr = config.getWR();
int address = -1;
try {
address = wr.getAddress(controller.getModel());
} catch (BindingConfigParseException exception) {
logger.error("Invalid address for block {} on {}", wr.getBlockName(), controller);
continue;
}
int bit = -1;
try {
bit = wr.getBit(controller.getModel());
} catch (BindingConfigParseException exception) {
logger.error("Invalid bit for block {} on {}", wr.getBlockName(), controller);
continue;
}
if (!wr.isInRange(controller.getModel())) {
logger.warn("Invalid write request for block {} at address {}", wr.getBlockName(), address);
continue;
}
// Send command to the LOGO! controller memory
S7Client LogoS7Client = controller.getS7Client();
if (LogoS7Client == null) {
logger.debug("No S7client for controller {} found", config.getController());
continue;
}
final byte[] buffer = new byte[2];
int size = wr.isDigital() ? 1 : 2;
lock.lock();
int result = LogoS7Client.ReadArea(S7.S7AreaDB, 1, address, size, buffer);
logger.debug("Read word from logo memory: at {} {} bytes, result = {}", address, size, result);
if (result == 0) {
Item item = config.getItem();
if (item instanceof NumberItem && !wr.isDigital()) {
if (command instanceof DecimalType) {
int oldValue = S7.GetShortAt(buffer, 0);
int newValue = ((DecimalType) command).intValue();
S7.SetWordAt(buffer, 0, newValue);
logger.debug("Changed word at {} from {} to {}", address, oldValue, newValue);
result = LogoS7Client.WriteArea(S7.S7AreaDB, 1, address, size, buffer);
logger.debug("Wrote to memory at {} two bytes: [{}, {}]", address, buffer[0], buffer[1]);
}
} else if (item instanceof SwitchItem && wr.isDigital()) {
if (command instanceof OnOffType) {
boolean oldValue = S7.GetBitAt(buffer, 0, bit) ? true : false;
boolean newValue = command == OnOffType.ON ? true : false;
S7.SetBitAt(buffer, 0, bit, newValue);
logger.debug("Changed bit {}.{} from {} to {}", address, bit, oldValue, newValue);
result = LogoS7Client.WriteArea(S7.S7AreaDB, 1, address, size, buffer);
logger.debug("Wrote to memory at {} one byte: [{}]", address, buffer[0]);
}
}
// If nothing was written and read was ok, nothing will happen here
if (result != 0) {
logger.warn("Failed to write memory: {}. Reconnecting...", S7Client.ErrorText(result));
ReconnectLogo(LogoS7Client);
}
} else {
logger.warn("Failed to read memory: {}. Reconnecting...", S7Client.ErrorText(result));
ReconnectLogo(LogoS7Client);
}
lock.unlock();
}
}
use of org.openhab.core.library.items.NumberItem in project openhab1-addons by openhab.
the class SappBinding method updateState.
/**
* updates item repository for a single item
*/
private void updateState(String pnmasId, SappAddressType sappAddressType, int addressToUpdate, int newState, SappBindingProvider provider) {
logger.debug("Updating {} {} with new value {}", sappAddressType, addressToUpdate, newState);
for (String itemName : provider.getItemNames()) {
try {
Item item = itemRegistry.getItem(itemName);
if (item instanceof SwitchItem && !(item instanceof DimmerItem)) {
SappBindingConfigSwitchItem sappBindingConfigSwitchItem = (SappBindingConfigSwitchItem) provider.getBindingConfig(itemName);
if (!sappBindingConfigSwitchItem.isPollerSuspender()) {
SappAddressOnOffStatus statusAddress = sappBindingConfigSwitchItem.getStatus();
if (statusAddress.getAddressType() == sappAddressType && statusAddress.getPnmasId().equals(pnmasId) && addressToUpdate == statusAddress.getAddress()) {
logger.debug("found binding to update {}", sappBindingConfigSwitchItem);
int result = SappBindingConfigUtils.maskWithSubAddress(statusAddress.getSubAddress(), newState);
State stateToSet = result == statusAddress.getOnValue() ? OnOffType.ON : OnOffType.OFF;
if (!stateToSet.equals(item.getState())) {
eventPublisher.postUpdate(itemName, stateToSet);
}
}
}
} else if (item instanceof ContactItem) {
SappBindingConfigContactItem sappBindingConfigContactItem = (SappBindingConfigContactItem) provider.getBindingConfig(itemName);
SappAddressOpenClosedStatus statusAddress = sappBindingConfigContactItem.getStatus();
if (statusAddress.getAddressType() == sappAddressType && statusAddress.getPnmasId().equals(pnmasId) && addressToUpdate == statusAddress.getAddress()) {
logger.debug("found binding to update {}", sappBindingConfigContactItem);
int result = SappBindingConfigUtils.maskWithSubAddress(statusAddress.getSubAddress(), newState);
State stateToSet = result == statusAddress.getOpenValue() ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
if (!stateToSet.equals(item.getState())) {
eventPublisher.postUpdate(itemName, stateToSet);
}
}
} else if (item instanceof NumberItem) {
SappBindingConfigNumberItem sappBindingConfigNumberItem = (SappBindingConfigNumberItem) provider.getBindingConfig(itemName);
SappAddressDecimal address = sappBindingConfigNumberItem.getStatus();
if (address.getAddressType() == sappAddressType && address.getPnmasId().equals(pnmasId) && addressToUpdate == address.getAddress()) {
logger.debug("found binding to update {}", sappBindingConfigNumberItem);
int result = SappBindingConfigUtils.maskWithSubAddress(address.getSubAddress(), newState);
State stateToSet = new DecimalType(address.scaledValue(result, address.getSubAddress()));
if (!stateToSet.equals(item.getState())) {
eventPublisher.postUpdate(itemName, stateToSet);
}
}
} else if (item instanceof RollershutterItem) {
SappBindingConfigRollershutterItem sappBindingConfigRollershutterItem = (SappBindingConfigRollershutterItem) provider.getBindingConfig(itemName);
SappAddressRollershutterStatus statusAddress = sappBindingConfigRollershutterItem.getStatus();
if (statusAddress.getAddressType() == sappAddressType && statusAddress.getPnmasId().equals(pnmasId) && addressToUpdate == statusAddress.getAddress()) {
logger.debug("found binding to update {}", sappBindingConfigRollershutterItem);
int result = SappBindingConfigUtils.maskWithSubAddress(statusAddress.getSubAddress(), newState);
State stateToSet = result == statusAddress.getOpenValue() ? PercentType.HUNDRED : (result == statusAddress.getClosedValue() ? PercentType.ZERO : PercentType.valueOf("50"));
if (!stateToSet.equals(item.getState())) {
eventPublisher.postUpdate(itemName, stateToSet);
}
}
} else if (item instanceof DimmerItem) {
SappBindingConfigDimmerItem sappBindingConfigDimmerItem = (SappBindingConfigDimmerItem) provider.getBindingConfig(itemName);
SappAddressDimmer statusAddress = sappBindingConfigDimmerItem.getStatus();
if (statusAddress.getAddressType() == sappAddressType && statusAddress.getPnmasId().equals(pnmasId) && addressToUpdate == statusAddress.getAddress()) {
logger.debug("found binding to update {}", sappBindingConfigDimmerItem);
int result = statusAddress.scaledValue(SappBindingConfigUtils.maskWithSubAddress(statusAddress.getSubAddress(), newState), statusAddress.getSubAddress()).round(new MathContext(0, RoundingMode.HALF_EVEN)).intValue();
State stateToSet;
if (result <= PercentType.ZERO.intValue()) {
stateToSet = PercentType.ZERO;
} else if (result >= PercentType.HUNDRED.intValue()) {
stateToSet = PercentType.HUNDRED;
} else {
stateToSet = PercentType.valueOf(String.valueOf(result));
}
if (!stateToSet.equals(item.getState())) {
eventPublisher.postUpdate(itemName, stateToSet);
}
}
} else {
logger.error("unimplemented item type: {}", item.getClass().getSimpleName());
}
} catch (ItemNotFoundException e) {
logger.error("Item {} not found", itemName);
}
}
}
use of org.openhab.core.library.items.NumberItem in project openhab1-addons by openhab.
the class SappBinding method queryAndSendActualState.
/**
* reads state from device and updates item repository
*/
private void queryAndSendActualState(SappBindingProvider provider, String itemName) {
logger.debug("querying and sending item {}", itemName);
try {
Item item = itemRegistry.getItem(itemName);
if (item instanceof SwitchItem && !(item instanceof DimmerItem)) {
SappBindingConfigSwitchItem sappBindingConfigSwitchItem = (SappBindingConfigSwitchItem) provider.getBindingConfig(itemName);
SappAddressOnOffStatus statusAddress = sappBindingConfigSwitchItem.getStatus();
if (!sappBindingConfigSwitchItem.isPollerSuspender()) {
updateOnOffItem(provider, statusAddress, itemName, item);
}
} else if (item instanceof ContactItem) {
SappBindingConfigContactItem sappBindingConfigContactItem = (SappBindingConfigContactItem) provider.getBindingConfig(itemName);
SappAddressOpenClosedStatus statusAddress = sappBindingConfigContactItem.getStatus();
updateOpenClosedItem(provider, statusAddress, itemName, item);
} else if (item instanceof NumberItem) {
SappBindingConfigNumberItem sappBindingConfigNumberItem = (SappBindingConfigNumberItem) provider.getBindingConfig(itemName);
SappAddressDecimal statusAddress = sappBindingConfigNumberItem.getStatus();
updateDecimalItem(provider, statusAddress, itemName, item);
} else if (item instanceof RollershutterItem) {
SappBindingConfigRollershutterItem sappBindingConfigRollershutterItem = (SappBindingConfigRollershutterItem) provider.getBindingConfig(itemName);
SappAddressRollershutterStatus statusAddress = sappBindingConfigRollershutterItem.getStatus();
updateRollershutterItem(provider, statusAddress, itemName, item);
} else if (item instanceof DimmerItem) {
SappBindingConfigDimmerItem sappBindingConfigDimmerItem = (SappBindingConfigDimmerItem) provider.getBindingConfig(itemName);
SappAddressDimmer statusAddress = sappBindingConfigDimmerItem.getStatus();
updateDimmerItem(provider, statusAddress, itemName, item);
} else {
logger.error("unimplemented item type: {}", item.getClass().getSimpleName());
}
} catch (ItemNotFoundException e) {
logger.error("Item {} not found", itemName);
}
}
use of org.openhab.core.library.items.NumberItem in project openhab1-addons by openhab.
the class SysteminfoGenericBindingProviderTest method testProcessBindingConfig_FileCount02.
@Test
public /* Verify a relative path file count configuration (*nix): "DirFiles:60000:../resources/" */
void testProcessBindingConfig_FileCount02() throws BindingConfigParseException {
NumberItem testItem = new NumberItem("DirFiles02");
String simpleConfig = "DirFiles:60000:../resources/";
provider.processBindingConfiguration("systeminfo", testItem, simpleConfig);
Assert.assertEquals("DirFiles", provider.getCommandType("DirFiles02").toString());
Assert.assertNull(provider.getItemType("DirFiles02"));
Assert.assertEquals(60000, provider.getRefreshInterval("DirFiles02"));
Assert.assertEquals("../resources/", provider.getTarget("DirFiles02"));
}
Aggregations