From 3cda9dc379139d066cd14cdb5db2f9caaea9510a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Dywicki?= Date: Thu, 29 Feb 2024 19:48:33 +0100 Subject: [PATCH 1/3] Add support for fatek data registers. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Łukasz Dywicki --- .../docs/examples/fatek/fatek.things | 3 + .../docs/examples/fatek/fatek.yaml | 10 ++ .../docs/fatek/descriptors/channel-types.adoc | 71 +++++++++ .../descriptors/config-descriptions.adoc | 51 ++++++ .../docs/fatek/fatek.adoc | 36 +++++ .../binding/fatek/FatekBindingConstants.java | 2 + .../config/channel/BaseChannelConfig.java | 40 +++++ .../channel/binary/DiscreteChannelConfig.java | 18 +-- .../channel/data/Data32ChannelConfig.java | 24 +++ .../channel/data/DataChannelConfig.java | 26 +++ .../channel/BinaryChannelHandler.java | 2 +- .../internal/channel/DataChannelHandler.java | 148 ++++++++++++++++++ .../channel/DefaultChannelHandlerFactory.java | 23 +++ .../handler/FatekPlcThingHandler.java | 55 ++++--- .../OH-INF/config/binary-channel-config.xml | 3 - .../OH-INF/config/data16-channel-config.xml | 57 +++++++ .../OH-INF/config/data32-channel-config.xml | 62 ++++++++ .../main/resources/OH-INF/thing/channels.xml | 15 +- .../src/main/resources/OH-INF/thing/thing.xml | 2 +- 19 files changed, 603 insertions(+), 45 deletions(-) create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/BaseChannelConfig.java create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/data/Data32ChannelConfig.java create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/data/DataChannelConfig.java create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DataChannelHandler.java create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/data16-channel-config.xml create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/data32-channel-config.xml diff --git a/bundles/org.connectorio.addons.binding.fatek/docs/examples/fatek/fatek.things b/bundles/org.connectorio.addons.binding.fatek/docs/examples/fatek/fatek.things index 9b434d52..b90df4b3 100644 --- a/bundles/org.connectorio.addons.binding.fatek/docs/examples/fatek/fatek.things +++ b/bundles/org.connectorio.addons.binding.fatek/docs/examples/fatek/fatek.things @@ -13,6 +13,9 @@ Bridge co7io-fatek:tcp:my-connection "My PLC#1" [ Type discrete : S1 [ register="S", index=1 ] Type discrete : T1 [ register="T", index=1 ] Type discrete : C1 [ register="C", index=1 ] + + Type data16 : R1x16 [ register="R", index=1 ] + Type data32 : R1x32 [ register="DR", index=1 ] } } diff --git a/bundles/org.connectorio.addons.binding.fatek/docs/examples/fatek/fatek.yaml b/bundles/org.connectorio.addons.binding.fatek/docs/examples/fatek/fatek.yaml index e624aefb..1d11e1df 100644 --- a/bundles/org.connectorio.addons.binding.fatek/docs/examples/fatek/fatek.yaml +++ b/bundles/org.connectorio.addons.binding.fatek/docs/examples/fatek/fatek.yaml @@ -43,3 +43,13 @@ things: configuration: index: 1 register: "C" + - id: "R1x16" + type: "data16" + configuration: + index: 1 + register: "R" + - id: "R1x32" + type: "data32" + configuration: + index: 1 + register: "DR" diff --git a/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/channel-types.adoc b/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/channel-types.adoc index b15acca1..17294d29 100644 --- a/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/channel-types.adoc +++ b/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/channel-types.adoc @@ -11,6 +11,18 @@ <> +| co7io-fatek:data16 +| Data register (16 bit) +| Data register of connected PLC encoded using 16 bit registers. + +<> + +| co7io-fatek:data32 +| Data register (32 bit) +| Data register of connected PLC encoded using 32 bit registers. + +<> + |=== @@ -41,4 +53,63 @@ Below table contain configuration parameters which can be assigned to channels o |=== +[[co7io-fatek:data16]] +== Configuration of `co7io-fatek:data16` + +Below table contain configuration parameters which can be assigned to channels of type `co7io-fatek:data16`. + +[width="100%",caption="Channel type data16 configuration",cols="1,1,1,2"] +|=== +|Name | Type | Label ^|Description + +| register +| TEXT +| Register symbol +| Symbol (kind) of register to read + +| index +| INTEGER +| Register index +| Index of register used to retrieve data. + +| unsigned +| BOOLEAN +| Unsigned +| Values retrieved from and written to device assume unsigned representation. + +|=== + + +[[co7io-fatek:data32]] +== Configuration of `co7io-fatek:data32` + +Below table contain configuration parameters which can be assigned to channels of type `co7io-fatek:data32`. + +[width="100%",caption="Channel type data32 configuration",cols="1,1,1,2"] +|=== +|Name | Type | Label ^|Description + +| register +| TEXT +| Register symbol +| Symbol (kind) of register to read + +| index +| INTEGER +| Register index +| Index of register used to retrieve data. + +| unsigned +| BOOLEAN +| Unsigned +| Values retrieved from and written to device assume unsigned representation. Does not apply if floating option is set. + +| floating +| BOOLEAN +| floating point +| Indicate that read/write value is a floating point number. Overrides unsigned flag. + +|=== + + diff --git a/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/config-descriptions.adoc b/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/config-descriptions.adoc index 420e0aa1..289cf495 100644 --- a/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/config-descriptions.adoc +++ b/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/config-descriptions.adoc @@ -2,6 +2,34 @@ = Config descriptions +[[channel-type:co7io-fatek:data32]] +== Config `channel-type:co7io-fatek:data32` +[width="100%",caption="channel-type:co7io-fatek:data32 configuration",cols="1,1,1,2"] +|=== +|Name | Type | Label ^|Description + +| register +| TEXT +| Register symbol +| Symbol (kind) of register to read + +| index +| INTEGER +| Register index +| Index of register used to retrieve data. + +| unsigned +| BOOLEAN +| Unsigned +| Values retrieved from and written to device assume unsigned representation. Does not apply if floating option is set. + +| floating +| BOOLEAN +| floating point +| Indicate that read/write value is a floating point number. Overrides unsigned flag. + +|=== + [[thing-type:co7io-fatek:tcp]] == Config `thing-type:co7io-fatek:tcp` [width="100%",caption="thing-type:co7io-fatek:tcp configuration",cols="1,1,1,2"] @@ -109,4 +137,27 @@ |=== +[[channel-type:co7io-fatek:data16]] +== Config `channel-type:co7io-fatek:data16` +[width="100%",caption="channel-type:co7io-fatek:data16 configuration",cols="1,1,1,2"] +|=== +|Name | Type | Label ^|Description + +| register +| TEXT +| Register symbol +| Symbol (kind) of register to read + +| index +| INTEGER +| Register index +| Index of register used to retrieve data. + +| unsigned +| BOOLEAN +| Unsigned +| Values retrieved from and written to device assume unsigned representation. + +|=== + diff --git a/bundles/org.connectorio.addons.binding.fatek/docs/fatek/fatek.adoc b/bundles/org.connectorio.addons.binding.fatek/docs/fatek/fatek.adoc index 484d6eca..b47facfb 100644 --- a/bundles/org.connectorio.addons.binding.fatek/docs/fatek/fatek.adoc +++ b/bundles/org.connectorio.addons.binding.fatek/docs/fatek/fatek.adoc @@ -57,8 +57,44 @@ There are several kinds of addresses defined by protocol. | `S` | Step relay | `ON` and `OFF` (`OnOffType`) | `T` | Timer contact | `ON` and `OFF` (`OnOffType`) | `C` | Counter contact | `ON` and `OFF` (`OnOffType`) + +| `R` | Data register (16 bit) | `Number` (INT16/UINT16) +| `D` | Data register (16 bit) | `Number` (INT16/UINT16) +| `F` | File register (16 bit) | `Number` (INT16/UINT16) + +| `DR` | Data Register (32 bit) | `Number` (INT32/UINT32) or `Float` +| `DD` | Data Register (32 bit) | `Number` (INT32/UINT32) or `Float` +| `DF` | File register (32 bit) | `Number` (INT32/UINT32) + +| `RT` | Timer register (16 bit) | `Number` (INT16/UINT16) +| `RC` | Counter register (16 bit) | `Number` (INT16/UINT16) +| `DRT` | Timer register (32 bit) | `Number` (INT32/UINT32) or `Float` +| `DRC` | Counter register (32 bit) | `Number` (INT32/UINT32) or `Float` + +| `WX` | Input discrete data register (16 bit) | `Number` (INT16/UINT16) +| `WY` | Output relay data register (16 bit) | `Number` (INT16/UINT16) +| `WM` | Internal relay data register (16 bit) | `Number` (INT16/UINT16) +| `WS` | Step relay data register (16 bit) | `Number` (INT16/UINT16) +| `WT` | Timer discrete data register (16 bit) | `Number` (INT16/UINT16) +| `WC` | Counter discrete data register (16 bit) | `Number` (INT16/UINT16) + +| `DWX` | Input discrete data register (32 bit) | `Number` (INT32/UINT32) or `Float` +| `DWY` | Output relay data register (32 bit) | `Number` (INT32/UINT32) or `Float` +| `DWM` | Internal relay data register (32 bit) | `Number` (INT32/UINT32) or `Float` +| `DWS` | Step relay data register (32 bit) | `Number` (INT32/UINT32) or `Float` +| `DWT` | Timer discrete data register (32 bit) | `Number` (INT32/UINT32) or `Float` +| `DWC` | Counter discrete data register (32 bit) | `Number` (INT32/UINT32) or `Float` + |=== +The discrete inputs might be commendable (outputs, relays) or read only (input, contact). +Binding does not verify read/write access. +For data registers (`R`, `D`, `F` etc.) encoding of contained data might vary. +In principle - all registers of 16 bit length will be read and written using integer representation. +Larger values might be interpreted as integer or floating point number. +In order to use floating point representation use `data32` channel with `floating=true` option. +The unsinged representation is always forced for `data16` and `data32` channels using `unsinged=true` option. + == Textual configuration Below is example of a text configuration which you can use as a started. diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/FatekBindingConstants.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/FatekBindingConstants.java index a8215350..97b0af50 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/FatekBindingConstants.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/FatekBindingConstants.java @@ -41,6 +41,8 @@ public interface FatekBindingConstants extends BaseBindingConstants { // common dynamic channel types ChannelTypeUID CHANNEL_TYPE_DISCRETE = new ChannelTypeUID(BINDING_ID, "discrete"); + ChannelTypeUID CHANNEL_TYPE_DATA16 = new ChannelTypeUID(BINDING_ID, "data16"); + ChannelTypeUID CHANNEL_TYPE_DATA32 = new ChannelTypeUID(BINDING_ID, "data32"); } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/BaseChannelConfig.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/BaseChannelConfig.java new file mode 100644 index 00000000..b3fbf877 --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/BaseChannelConfig.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2024-2024 ConnectorIO Sp. z o.o. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.connectorio.addons.binding.fatek.config.channel; + +import org.connectorio.addons.binding.fatek.config.channel.RegisterConfig; +import org.simplify4u.jfatek.registers.RegName; + +public abstract class BaseChannelConfig implements RegisterConfig { + + public int index; + + public RegName register; + public boolean invert; + + @Override + public RegName getRegister() { + return register; + } + + @Override + public int getIndex() { + return index; + } + +} diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/binary/DiscreteChannelConfig.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/binary/DiscreteChannelConfig.java index f58cf326..67b8ae8c 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/binary/DiscreteChannelConfig.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/binary/DiscreteChannelConfig.java @@ -17,24 +17,10 @@ */ package org.connectorio.addons.binding.fatek.config.channel.binary; -import org.connectorio.addons.binding.fatek.config.channel.RegisterConfig; -import org.simplify4u.jfatek.registers.RegName; +import org.connectorio.addons.binding.fatek.config.channel.BaseChannelConfig; -public class DiscreteChannelConfig implements RegisterConfig { +public class DiscreteChannelConfig extends BaseChannelConfig { - public int index; - - public RegName register; public boolean invert; - @Override - public RegName getRegister() { - return register; - } - - @Override - public int getIndex() { - return index; - } - } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/data/Data32ChannelConfig.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/data/Data32ChannelConfig.java new file mode 100644 index 00000000..9455154a --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/data/Data32ChannelConfig.java @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2024-2024 ConnectorIO Sp. z o.o. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.connectorio.addons.binding.fatek.config.channel.data; + +public class Data32ChannelConfig extends DataChannelConfig { + + public boolean floating; + +} diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/data/DataChannelConfig.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/data/DataChannelConfig.java new file mode 100644 index 00000000..d64fc173 --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/data/DataChannelConfig.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2024-2024 ConnectorIO Sp. z o.o. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.connectorio.addons.binding.fatek.config.channel.data; + +import org.connectorio.addons.binding.fatek.config.channel.BaseChannelConfig; + +public class DataChannelConfig extends BaseChannelConfig { + + public boolean unsigned; + +} diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/BinaryChannelHandler.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/BinaryChannelHandler.java index 9528e2b5..b333de51 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/BinaryChannelHandler.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/BinaryChannelHandler.java @@ -36,7 +36,7 @@ public class BinaryChannelHandler implements FatekChannelHandler { private final ChannelUID channel; private final boolean invert; - public BinaryChannelHandler(Channel channel, DiscreteChannelConfig config) { + public BinaryChannelHandler(Channel channel, DiscreteChannelConfig config) { this.channel = channel.getUID(); this.invert = config.invert; switch (config.getRegister()) { diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DataChannelHandler.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DataChannelHandler.java new file mode 100644 index 00000000..d0eda38f --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DataChannelHandler.java @@ -0,0 +1,148 @@ +/* + * Copyright (C) 2024-2024 ConnectorIO Sp. z o.o. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.connectorio.addons.binding.fatek.internal.channel; + +import static org.simplify4u.jfatek.registers.DataReg.*; + +import java.math.BigInteger; +import java.util.function.BiFunction; +import org.connectorio.addons.binding.fatek.config.channel.data.DataChannelConfig; +import org.openhab.core.library.types.DecimalType; +import org.openhab.core.thing.Channel; +import org.openhab.core.thing.ChannelUID; +import org.openhab.core.types.Command; +import org.openhab.core.types.State; +import org.simplify4u.jfatek.registers.DataReg; +import org.simplify4u.jfatek.registers.Reg; +import org.simplify4u.jfatek.registers.RegValue; +import org.simplify4u.jfatek.registers.RegValue16; +import org.simplify4u.jfatek.registers.RegValue32; + +public class DataChannelHandler implements FatekChannelHandler { + + private final DataReg register; + private final ChannelUID channel; + private final boolean unsigned; + private final BiFunction mapper; + + public DataChannelHandler(Channel channel, DataChannelConfig config, BiFunction mapper) { + this.channel = channel.getUID(); + this.unsigned = config.invert; + this.mapper = mapper; + switch (config.getRegister()) { + case R: + this.register = R(config.index); + break; + case D: + this.register = D(config.index); + break; + case F: + this.register = F(config.index); + break; + case DR: + this.register = DR(config.index); + break; + case DD: + this.register = DD(config.index); + break; + case DF: + this.register = DF(config.index); + break; + case RT: + this.register = RT(config.index); + break; + case RC: + this.register = RC(config.index); + break; + case DRT: + this.register = DRT(config.index); + break; + case DRC: + this.register = DRC(config.index); + break; + case WX: + this.register = WX(config.index); + break; + case WY: + this.register = WY(config.index); + break; + case WM: + this.register = WM(config.index); + break; + case WS: + this.register = WS(config.index); + break; + case WT: + this.register = WT(config.index); + break; + case WC: + this.register = WC(config.index); + break; + case DWX: + this.register = DWX(config.index); + break; + case DWY: + this.register = DWY(config.index); + break; + case DWM: + this.register = DWM(config.index); + break; + case DWS: + this.register = DWS(config.index); + break; + case DWT: + this.register = DWT(config.index); + break; + case DWC: + this.register = DWC(config.index); + break; + default: + throw new IllegalArgumentException("Unsupported register kind " + config.getRegister()); + } + } + + @Override + public Reg register() { + return register; + } + + @Override + public ChannelUID channel() { + return channel; + } + + @Override + public RegValue prepareWrite(Command command) { + if (!(command instanceof DecimalType)) { + return null; + } + + DecimalType value = (DecimalType) command; + return mapper.apply(register, value); + } + + @Override + public State state(RegValue value) { + if (register.is32Bits()) { + return new DecimalType(value.floatValue()); + } + + return new DecimalType(unsigned ? value.longValueUnsigned() : value.longValue()); + } + +} diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DefaultChannelHandlerFactory.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DefaultChannelHandlerFactory.java index 339cd178..03a93251 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DefaultChannelHandlerFactory.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DefaultChannelHandlerFactory.java @@ -17,9 +17,14 @@ */ package org.connectorio.addons.binding.fatek.internal.channel; +import java.math.BigInteger; import org.connectorio.addons.binding.fatek.FatekBindingConstants; +import org.connectorio.addons.binding.fatek.config.channel.data.Data32ChannelConfig; +import org.connectorio.addons.binding.fatek.config.channel.data.DataChannelConfig; import org.connectorio.addons.binding.fatek.config.channel.binary.DiscreteChannelConfig; import org.openhab.core.thing.Channel; +import org.simplify4u.jfatek.registers.RegValue16; +import org.simplify4u.jfatek.registers.RegValue32; public class DefaultChannelHandlerFactory implements FatekChannelHandlerFactory { @@ -29,6 +34,24 @@ public FatekChannelHandler create(Channel channel) { DiscreteChannelConfig config = channel.getConfiguration().as(DiscreteChannelConfig.class); return new BinaryChannelHandler(channel, config); } + if (FatekBindingConstants.CHANNEL_TYPE_DATA16.equals(channel.getChannelTypeUID())) { + DataChannelConfig config = channel.getConfiguration().as(DataChannelConfig.class); + return new DataChannelHandler(channel, config, (register, state) -> { + int intValue = state.toBigDecimal().intValue(); + return RegValue16.getForReg(register, config.unsigned ? Integer.toUnsignedLong(intValue) : intValue); + }); + } + if (FatekBindingConstants.CHANNEL_TYPE_DATA32.equals(channel.getChannelTypeUID())) { + Data32ChannelConfig config = channel.getConfiguration().as(Data32ChannelConfig.class); + return new DataChannelHandler(channel, config, (register, state) -> { + if (config.floating) { + return RegValue32.getForReg(register, state.toBigDecimal().floatValue()); + } + + long longValue = state.toBigDecimal().longValue(); + return RegValue32.getForReg(register, config.unsigned ? BigInteger.valueOf(longValue).longValue() : longValue); + }); + } return null; } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/handler/FatekPlcThingHandler.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/handler/FatekPlcThingHandler.java index 5fdf3b93..53454bf4 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/handler/FatekPlcThingHandler.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/handler/FatekPlcThingHandler.java @@ -37,6 +37,8 @@ import org.openhab.core.thing.ThingStatusDetail; import org.openhab.core.types.Command; import org.openhab.core.types.State; +import org.openhab.core.types.Type; +import org.simplify4u.jfatek.FatekCommand; import org.simplify4u.jfatek.FatekReadMixDataCmd; import org.simplify4u.jfatek.FatekWriteDataCmd; import org.simplify4u.jfatek.FatekWriteDiscreteCmd; @@ -108,32 +110,39 @@ public void handleCommand(ChannelUID channelUID, Command command) { if (handlerMap.containsKey(channelUID)) { FatekChannelHandler handler = handlerMap.get(channelUID); Reg register = handler.register(); + RegValue value = handler.prepareWrite(command); + if (value == null) { + logger.warn("Could not map command {} from channel {} to value supported by PLC. Ignoring this write.", command, channelUID); + return; + } + if (register instanceof DataReg) { FatekWriteDataCmd cmd = new FatekWriteDataCmd(null, (DataReg) register); - RegValue value = handler.prepareWrite(command); - if (value != null) { - cmd.addValue(value.longValue()); - connection.execute(stationNumber, cmd).whenComplete((r, e) -> { - if (e != null) { - logger.error("Could not write value {} to register {}", command, register, e); - return; - } - logger.debug("Successful write of value {} mapped from {} for channel {}", value, command, register); - }); - } + cmd.addValue(value.longValue()); + write(channelUID, command, register, value, cmd); } else if (register instanceof DisReg) { - RegValue value = handler.prepareWrite(command); - if (value != null) { - FatekWriteDiscreteCmd cmd = new FatekWriteDiscreteCmd(null, (DisReg) register, value.boolValue()); - connection.execute(stationNumber, cmd).whenComplete((r, e) -> { - if (e != null) { - logger.error("Could not write value {} to register {}", command, register, e); - return; - } - logger.debug("Successful write of value {} mapped from {} for channel {}", value, command, register); - }); - } + FatekWriteDiscreteCmd cmd = new FatekWriteDiscreteCmd(null, (DisReg) register, value.boolValue()); + write(channelUID, command, register, value, cmd); + } else { + logger.error("Unsupported value {} mapped from command {} for channel {}", value, command, channelUID); + } + } + } + + private void write(ChannelUID channelUID, Command command, Reg register, RegValue value, FatekCommand cmd) { + connection.execute(stationNumber, cmd).whenComplete((r, e) -> { + if (e != null) { + logger.error("Could not write value {} to register {}", command, register, e); + return; } + logger.debug("Successful write of value {} mapped from {} for channel {}", value, command, register); + update(channelUID, command); + }); + } + + private void update(ChannelUID channelUID, Type value) { + if (value instanceof State) { + getCallback().stateUpdated(channelUID, (State) value); } } @@ -161,7 +170,7 @@ public void run() { FatekChannelHandler handler = registerMap.get(entry.getKey()); State state = handler.state(entry.getValue()); if (state != null) { - getCallback().stateUpdated(handler.channel(), state); + update(handler.channel(), state); } } }); diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/binary-channel-config.xml b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/binary-channel-config.xml index 58880bc0..be00adb6 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/binary-channel-config.xml +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/binary-channel-config.xml @@ -28,9 +28,6 @@ - diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/data16-channel-config.xml b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/data16-channel-config.xml new file mode 100644 index 00000000..2d1f2fda --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/data16-channel-config.xml @@ -0,0 +1,57 @@ + + + + + + + + Symbol (kind) of register to read + + + + + + + + + + + + + + + + + + + + Index of register used to retrieve data. + 0 + + + + Values retrieved from and written to device assume unsigned representation. + false + + + + diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/data32-channel-config.xml b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/data32-channel-config.xml new file mode 100644 index 00000000..8752a7a5 --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/data32-channel-config.xml @@ -0,0 +1,62 @@ + + + + + + + + Symbol (kind) of register to read + + + + + + + + + + + + + + + + + + + + Index of register used to retrieve data. + 0 + + + + Values retrieved from and written to device assume unsigned representation. Does not apply if floating option is set. + false + + + + Indicate that read/write value is a floating point number. Overrides unsigned flag. + false + + + + diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/channels.xml b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/channels.xml index 0bb7999e..6bd2a58f 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/channels.xml +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/channels.xml @@ -21,7 +21,6 @@ xmlns:thing="https://openhab.org/schemas/thing-description/v1.0.0" xsi:schemaLocation="https://openhab.org/schemas/thing-description/v1.0.0 https://openhab.org/schemas/thing-description-1.0.0.xsd"> - Switch @@ -29,4 +28,18 @@ + + Number + + Data register of connected PLC encoded using 16 bit registers. + + + + + Number + + Data register of connected PLC encoded using 32 bit registers. + + + \ No newline at end of file diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/thing.xml b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/thing.xml index 2cff4232..c3de501b 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/thing.xml +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/thing.xml @@ -23,7 +23,7 @@ https://openhab.org/schemas/thing-description-1.0.0.xsd "> - + From faaf773bdc3d53f7c0671f2e2cec80a419f9c8c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Dywicki?= Date: Fri, 1 Mar 2024 19:33:01 +0100 Subject: [PATCH 2/3] Add support for rollershutter items. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The rollershutter rely on three registers - data,discrete,discrete for position, start and stop signal. Signed-off-by: Łukasz Dywicki --- .../docs/examples/fatek/fatek.things | 3 + .../docs/examples/fatek/fatek.yaml | 18 +++ .../docs/fatek/descriptors/channel-types.adoc | 148 +++++++++++++++++- .../descriptors/config-descriptions.adoc | 131 +++++++++++++++- .../binding/fatek/FatekBindingConstants.java | 2 + .../config/channel/BaseChannelConfig.java | 8 +- .../channel/binary/DiscreteChannelConfig.java | 7 + .../RollerShutter32ChannelConfig.java | 33 ++++ .../RollerShutterChannelConfig.java | 33 ++++ .../fatek/internal/RegisterParser.java | 89 +++++++++++ .../channel/BinaryChannelHandler.java | 53 ++----- .../internal/channel/DataChannelHandler.java | 111 ++----------- .../channel/DefaultChannelHandlerFactory.java | 51 ++++-- .../internal/channel/FatekChannelHandler.java | 3 +- .../channel/FatekChannelHandlerFactory.java | 1 + .../channel/RollerShutterChannelHandler.java | 110 +++++++++++++ .../internal/channel/converter/Converter.java | 13 ++ .../channel/converter/Data16Converter.java | 35 +++++ .../channel/converter/Data32Converter.java | 46 ++++++ .../channel/converter/DiscreteConverter.java | 39 +++++ .../handler/FatekPlcThingHandler.java | 23 +-- .../OH-INF/config/binary-channel-config.xml | 4 +- .../OH-INF/config/data16-channel-config.xml | 4 +- .../OH-INF/config/data32-channel-config.xml | 4 +- .../config/rollershutter16-channel-config.xml | 120 ++++++++++++++ .../config/rollershutter32-channel-config.xml | 120 ++++++++++++++ .../main/resources/OH-INF/thing/channels.xml | 14 +- .../src/main/resources/OH-INF/thing/thing.xml | 2 +- 28 files changed, 1034 insertions(+), 191 deletions(-) create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/rollershutter/RollerShutter32ChannelConfig.java create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/rollershutter/RollerShutterChannelConfig.java create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/RegisterParser.java create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/RollerShutterChannelHandler.java create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Converter.java create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Data16Converter.java create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Data32Converter.java create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/DiscreteConverter.java create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/rollershutter16-channel-config.xml create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/rollershutter32-channel-config.xml diff --git a/bundles/org.connectorio.addons.binding.fatek/docs/examples/fatek/fatek.things b/bundles/org.connectorio.addons.binding.fatek/docs/examples/fatek/fatek.things index b90df4b3..d3fdf0af 100644 --- a/bundles/org.connectorio.addons.binding.fatek/docs/examples/fatek/fatek.things +++ b/bundles/org.connectorio.addons.binding.fatek/docs/examples/fatek/fatek.things @@ -16,6 +16,9 @@ Bridge co7io-fatek:tcp:my-connection "My PLC#1" [ Type data16 : R1x16 [ register="R", index=1 ] Type data32 : R1x32 [ register="DR", index=1 ] + + Type rollershutter16 : rollx16 [ register="R", index=1, startRegister="M", startIndex=1, stopRegister="M", stopIndex=2 ] + Type rollershutter32 : rollx32 [ register="DR", index=1, startRegister="M", startIndex=1, stopRegister="M", stopIndex=2 ] } } diff --git a/bundles/org.connectorio.addons.binding.fatek/docs/examples/fatek/fatek.yaml b/bundles/org.connectorio.addons.binding.fatek/docs/examples/fatek/fatek.yaml index 1d11e1df..688e090d 100644 --- a/bundles/org.connectorio.addons.binding.fatek/docs/examples/fatek/fatek.yaml +++ b/bundles/org.connectorio.addons.binding.fatek/docs/examples/fatek/fatek.yaml @@ -53,3 +53,21 @@ things: configuration: index: 1 register: "DR" + - id: "rollx16" + type: "rollershutter16" + configuration: + stopIndex: 2 + startIndex: 1 + stopRegister: "M" + index: 1 + startRegister: "M" + register: "R" + - id: "rollx32" + type: "rollershutter32" + configuration: + stopIndex: 2 + startIndex: 1 + stopRegister: "M" + index: 1 + startRegister: "M" + register: "DR" diff --git a/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/channel-types.adoc b/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/channel-types.adoc index 17294d29..0b6ba4b7 100644 --- a/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/channel-types.adoc +++ b/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/channel-types.adoc @@ -23,6 +23,18 @@ <> +| co7io-fatek:rollershutter16 +| Rollershutter (16-bit) +| Data register for position will use 16 bit registers. + +<> + +| co7io-fatek:rollershutter32 +| Rollershutter (32-bit) +| Data register for position will use 32 bit registers. + +<> + |=== @@ -37,8 +49,8 @@ Below table contain configuration parameters which can be assigned to channels o | register | TEXT -| Register symbol -| Symbol (kind) of register to read +| Register +| Type of discrete register to read or write. | index | INTEGER @@ -64,8 +76,8 @@ Below table contain configuration parameters which can be assigned to channels o | register | TEXT -| Register symbol -| Symbol (kind) of register to read +| Register +| Type of data register to read or write. | index | INTEGER @@ -91,8 +103,102 @@ Below table contain configuration parameters which can be assigned to channels o | register | TEXT -| Register symbol -| Symbol (kind) of register to read +| Register +| Type of data register to read or write. + +| index +| INTEGER +| Register index +| Index of register used to retrieve data. + +| unsigned +| BOOLEAN +| Unsigned +| Values retrieved from and written to device assume unsigned representation. Does not apply if floating option is set. + +| floating +| BOOLEAN +| floating point +| Indicate that read/write value is a floating point number. Overrides unsigned flag. + +|=== + + +[[co7io-fatek:rollershutter16]] +== Configuration of `co7io-fatek:rollershutter16` + +Below table contain configuration parameters which can be assigned to channels of type `co7io-fatek:rollershutter16`. + +[width="100%",caption="Channel type rollershutter16 configuration",cols="1,1,1,2"] +|=== +|Name | Type | Label ^|Description + +| register +| TEXT +| Register +| Data register used to retrieve position of roller shutter. + +| index +| INTEGER +| Register index +| Index of register used to retrieve data. + +| unsigned +| BOOLEAN +| Unsigned +| Values retrieved from and written to device assume unsigned representation. Does not apply if floating option is set. + +| floating +| BOOLEAN +| floating point +| Indicate that read/write value is a floating point number. Overrides unsigned flag. + +| startRegister +| TEXT +| Register +| Type of discrete register to write UP command. + +| startIndex +| INTEGER +| Register index +| Index of register used to write data. + +| startInvert +| BOOLEAN +| Invert +| Invert logical representation of related register (0->1, 1->0). + +| stopRegister +| TEXT +| Register +| Type of discrete register to write DOWN command. + +| stopIndex +| INTEGER +| Register index +| Index of register used to retrieve data. + +| stopInvert +| BOOLEAN +| Invert +| Invert logical representation of related register (0->1, 1->0). + +|=== + + +[[co7io-fatek:rollershutter32]] +== Configuration of `co7io-fatek:rollershutter32` + +Below table contain configuration parameters which can be assigned to channels of type `co7io-fatek:rollershutter32`. + +[width="100%",caption="Channel type rollershutter32 configuration",cols="1,1,1,2"] +|=== +|Name | Type | Label ^|Description + +| register +| TEXT +| Register +| Data register used to retrieve position of roller shutter. | index | INTEGER @@ -109,6 +215,36 @@ Below table contain configuration parameters which can be assigned to channels o | floating point | Indicate that read/write value is a floating point number. Overrides unsigned flag. +| startRegister +| TEXT +| Register +| Type of discrete register to write UP command. + +| startIndex +| INTEGER +| Register index +| Index of register used to write data. + +| startInvert +| BOOLEAN +| Invert +| Invert logical representation of related register (0->1, 1->0). + +| stopRegister +| TEXT +| Register +| Type of discrete register to write DOWN command. + +| stopIndex +| INTEGER +| Register index +| Index of register used to retrieve data. + +| stopInvert +| BOOLEAN +| Invert +| Invert logical representation of related register (0->1, 1->0). + |=== diff --git a/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/config-descriptions.adoc b/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/config-descriptions.adoc index 289cf495..6d6c6785 100644 --- a/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/config-descriptions.adoc +++ b/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/config-descriptions.adoc @@ -10,8 +10,8 @@ | register | TEXT -| Register symbol -| Symbol (kind) of register to read +| Register +| Type of data register to read or write. | index | INTEGER @@ -122,8 +122,8 @@ | register | TEXT -| Register symbol -| Symbol (kind) of register to read +| Register +| Type of discrete register to read or write. | index | INTEGER @@ -137,6 +137,64 @@ |=== +[[channel-type:co7io-fatek:rollershutter32]] +== Config `channel-type:co7io-fatek:rollershutter32` +[width="100%",caption="channel-type:co7io-fatek:rollershutter32 configuration",cols="1,1,1,2"] +|=== +|Name | Type | Label ^|Description + +| register +| TEXT +| Register +| Data register used to retrieve position of roller shutter. + +| index +| INTEGER +| Register index +| Index of register used to retrieve data. + +| unsigned +| BOOLEAN +| Unsigned +| Values retrieved from and written to device assume unsigned representation. Does not apply if floating option is set. + +| floating +| BOOLEAN +| floating point +| Indicate that read/write value is a floating point number. Overrides unsigned flag. + +| startRegister +| TEXT +| Register +| Type of discrete register to write UP command. + +| startIndex +| INTEGER +| Register index +| Index of register used to write data. + +| startInvert +| BOOLEAN +| Invert +| Invert logical representation of related register (0->1, 1->0). + +| stopRegister +| TEXT +| Register +| Type of discrete register to write DOWN command. + +| stopIndex +| INTEGER +| Register index +| Index of register used to retrieve data. + +| stopInvert +| BOOLEAN +| Invert +| Invert logical representation of related register (0->1, 1->0). + +|=== + [[channel-type:co7io-fatek:data16]] == Config `channel-type:co7io-fatek:data16` [width="100%",caption="channel-type:co7io-fatek:data16 configuration",cols="1,1,1,2"] @@ -145,8 +203,8 @@ | register | TEXT -| Register symbol -| Symbol (kind) of register to read +| Register +| Type of data register to read or write. | index | INTEGER @@ -160,4 +218,65 @@ |=== +[[channel-type:co7io-fatek:rollershutter16]] +== Config `channel-type:co7io-fatek:rollershutter16` +[width="100%",caption="channel-type:co7io-fatek:rollershutter16 configuration",cols="1,1,1,2"] +|=== +|Name | Type | Label ^|Description + +| register +| TEXT +| Register +| Data register used to retrieve position of roller shutter. + +| index +| INTEGER +| Register index +| Index of register used to retrieve data. + +| unsigned +| BOOLEAN +| Unsigned +| Values retrieved from and written to device assume unsigned representation. Does not apply if floating option is set. + +| floating +| BOOLEAN +| floating point +| Indicate that read/write value is a floating point number. Overrides unsigned flag. + +| startRegister +| TEXT +| Register +| Type of discrete register to write UP command. + +| startIndex +| INTEGER +| Register index +| Index of register used to write data. + +| startInvert +| BOOLEAN +| Invert +| Invert logical representation of related register (0->1, 1->0). + +| stopRegister +| TEXT +| Register +| Type of discrete register to write DOWN command. + +| stopIndex +| INTEGER +| Register index +| Index of register used to retrieve data. + +| stopInvert +| BOOLEAN +| Invert +| Invert logical representation of related register (0->1, 1->0). +| BOOLEAN +| Invert +| Invert logical representation of related register (0->1, 1->0). + +|=== + diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/FatekBindingConstants.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/FatekBindingConstants.java index 97b0af50..e0eed54f 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/FatekBindingConstants.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/FatekBindingConstants.java @@ -43,6 +43,8 @@ public interface FatekBindingConstants extends BaseBindingConstants { ChannelTypeUID CHANNEL_TYPE_DISCRETE = new ChannelTypeUID(BINDING_ID, "discrete"); ChannelTypeUID CHANNEL_TYPE_DATA16 = new ChannelTypeUID(BINDING_ID, "data16"); ChannelTypeUID CHANNEL_TYPE_DATA32 = new ChannelTypeUID(BINDING_ID, "data32"); + ChannelTypeUID CHANNEL_TYPE_ROLLERSHUTTER16 = new ChannelTypeUID(BINDING_ID, "rollershutter16"); + ChannelTypeUID CHANNEL_TYPE_ROLLERSHUTTER32 = new ChannelTypeUID(BINDING_ID, "rollershutter32"); } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/BaseChannelConfig.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/BaseChannelConfig.java index b3fbf877..83f7eb22 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/BaseChannelConfig.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/BaseChannelConfig.java @@ -17,7 +17,6 @@ */ package org.connectorio.addons.binding.fatek.config.channel; -import org.connectorio.addons.binding.fatek.config.channel.RegisterConfig; import org.simplify4u.jfatek.registers.RegName; public abstract class BaseChannelConfig implements RegisterConfig { @@ -25,7 +24,12 @@ public abstract class BaseChannelConfig implements RegisterConfig { public int index; public RegName register; - public boolean invert; + + public BaseChannelConfig() {} + public BaseChannelConfig(RegName register, int index) { + this.register = register; + this.index = index; + } @Override public RegName getRegister() { diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/binary/DiscreteChannelConfig.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/binary/DiscreteChannelConfig.java index 67b8ae8c..f4b397be 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/binary/DiscreteChannelConfig.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/binary/DiscreteChannelConfig.java @@ -18,9 +18,16 @@ package org.connectorio.addons.binding.fatek.config.channel.binary; import org.connectorio.addons.binding.fatek.config.channel.BaseChannelConfig; +import org.simplify4u.jfatek.registers.RegName; public class DiscreteChannelConfig extends BaseChannelConfig { public boolean invert; + public DiscreteChannelConfig() {} + public DiscreteChannelConfig(RegName register, int index, boolean invert) { + super(register, index); + this.invert = invert; + } + } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/rollershutter/RollerShutter32ChannelConfig.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/rollershutter/RollerShutter32ChannelConfig.java new file mode 100644 index 00000000..e4c3f864 --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/rollershutter/RollerShutter32ChannelConfig.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2024-2024 ConnectorIO Sp. z o.o. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.connectorio.addons.binding.fatek.config.channel.rollershutter; + +import org.connectorio.addons.binding.fatek.config.channel.data.Data32ChannelConfig; +import org.simplify4u.jfatek.registers.RegName; + +public class RollerShutter32ChannelConfig extends Data32ChannelConfig { + + public int startIndex; + public RegName startRegister; + public boolean startInvert; + + public int stopIndex; + public RegName stopRegister; + public boolean stopInvert; + +} diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/rollershutter/RollerShutterChannelConfig.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/rollershutter/RollerShutterChannelConfig.java new file mode 100644 index 00000000..9c61c909 --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/rollershutter/RollerShutterChannelConfig.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2024-2024 ConnectorIO Sp. z o.o. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.connectorio.addons.binding.fatek.config.channel.rollershutter; + +import org.connectorio.addons.binding.fatek.config.channel.data.DataChannelConfig; +import org.simplify4u.jfatek.registers.RegName; + +public class RollerShutterChannelConfig extends DataChannelConfig { + + public int startIndex; + public RegName startRegister; + public boolean startInvert; + + public int stopIndex; + public RegName stopRegister; + public boolean stopInvert; + +} diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/RegisterParser.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/RegisterParser.java new file mode 100644 index 00000000..5900c95e --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/RegisterParser.java @@ -0,0 +1,89 @@ +package org.connectorio.addons.binding.fatek.internal; + +import static org.simplify4u.jfatek.registers.DisReg.*; +import static org.simplify4u.jfatek.registers.DataReg.*; + +import org.connectorio.addons.binding.fatek.config.channel.RegisterConfig; +import org.simplify4u.jfatek.registers.DataReg; +import org.simplify4u.jfatek.registers.DisReg; + +public class RegisterParser { + + public static DisReg parseDiscrete(RegisterConfig config) { + switch (config.getRegister()) { + case X: + return X(config.getIndex()); + case Y: + return Y(config.getIndex()); + case M: + return M(config.getIndex()); + case S: + return S(config.getIndex()); + case T: + return T(config.getIndex()); + case C: + return C(config.getIndex()); + default: + throw new IllegalArgumentException("Unsupported register kind " + config.getRegister()); + } + } + + public static DataReg parseData16(RegisterConfig config) { + switch (config.getRegister()) { + case R: + return R(config.getIndex()); + case D: + return D(config.getIndex()); + case F: + return F(config.getIndex()); + case RT: + return RT(config.getIndex()); + case RC: + return RC(config.getIndex()); + case WX: + return WX(config.getIndex()); + case WY: + return WY(config.getIndex()); + case WM: + return WM(config.getIndex()); + case WS: + return WS(config.getIndex()); + case WT: + return WT(config.getIndex()); + case WC: + return WC(config.getIndex()); + default: + throw new IllegalArgumentException("Unsupported register kind " + config.getRegister()); + } + } + + public static DataReg parseData32(RegisterConfig config) { + switch (config.getRegister()) { + case DR: + return DR(config.getIndex()); + case DD: + return DD(config.getIndex()); + case DF: + return DF(config.getIndex()); + case DRT: + return DRT(config.getIndex()); + case DRC: + return DRC(config.getIndex()); + case DWX: + return DWX(config.getIndex()); + case DWY: + return DWY(config.getIndex()); + case DWM: + return DWM(config.getIndex()); + case DWS: + return DWS(config.getIndex()); + case DWT: + return DWT(config.getIndex()); + case DWC: + return DWC(config.getIndex()); + default: + throw new IllegalArgumentException("Unsupported register kind " + config.getRegister()); + } + } + +} diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/BinaryChannelHandler.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/BinaryChannelHandler.java index b333de51..12e3e947 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/BinaryChannelHandler.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/BinaryChannelHandler.java @@ -17,15 +17,13 @@ */ package org.connectorio.addons.binding.fatek.internal.channel; -import static org.simplify4u.jfatek.registers.DisReg.*; - -import org.connectorio.addons.binding.fatek.config.channel.binary.DiscreteChannelConfig; -import org.openhab.core.library.types.OnOffType; -import org.openhab.core.library.types.OpenClosedType; +import org.connectorio.addons.binding.fatek.internal.channel.converter.Converter; import org.openhab.core.thing.Channel; import org.openhab.core.thing.ChannelUID; import org.openhab.core.types.Command; import org.openhab.core.types.State; +import org.simplify4u.jfatek.FatekCommand; +import org.simplify4u.jfatek.FatekWriteDiscreteCmd; import org.simplify4u.jfatek.registers.DisReg; import org.simplify4u.jfatek.registers.Reg; import org.simplify4u.jfatek.registers.RegValue; @@ -33,34 +31,13 @@ public class BinaryChannelHandler implements FatekChannelHandler { private final DisReg register; + private final Converter converter; private final ChannelUID channel; - private final boolean invert; - public BinaryChannelHandler(Channel channel, DiscreteChannelConfig config) { + public BinaryChannelHandler(Channel channel, DisReg register, Converter converter) { this.channel = channel.getUID(); - this.invert = config.invert; - switch (config.getRegister()) { - case X: - this.register = X(config.getIndex()); - break; - case Y: - this.register = Y(config.getIndex()); - break; - case M: - this.register = M(config.getIndex()); - break; - case S: - this.register = S(config.getIndex()); - break; - case T: - this.register = T(config.getIndex()); - break; - case C: - this.register = C(config.getIndex()); - break; - default: - throw new IllegalArgumentException("Unsupported register kind " + config.getRegister()); - } + this.register = register; + this.converter = converter; } @Override @@ -74,21 +51,17 @@ public ChannelUID channel() { } @Override - public RegValue prepareWrite(Command command) { - boolean value; - if (command instanceof OpenClosedType) { - value = OpenClosedType.CLOSED == command; - } else { - value = OnOffType.ON == command; + public FatekCommand prepareWrite(Command command) { + RegValue value = converter.toValue(command); + if (value != null) { + return new FatekWriteDiscreteCmd(null, register, value.boolValue()); } - - return RegValue.getForReg(register, !invert ? !value : value); + return null; } @Override public State state(RegValue value) { - boolean status = invert ? !value.boolValue() : value.boolValue(); - return status ? OnOffType.ON : OnOffType.OFF; + return converter.toState(value); } } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DataChannelHandler.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DataChannelHandler.java index d0eda38f..074ebc5a 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DataChannelHandler.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DataChannelHandler.java @@ -17,103 +17,29 @@ */ package org.connectorio.addons.binding.fatek.internal.channel; -import static org.simplify4u.jfatek.registers.DataReg.*; - -import java.math.BigInteger; -import java.util.function.BiFunction; -import org.connectorio.addons.binding.fatek.config.channel.data.DataChannelConfig; -import org.openhab.core.library.types.DecimalType; +import org.connectorio.addons.binding.fatek.internal.channel.converter.Converter; import org.openhab.core.thing.Channel; import org.openhab.core.thing.ChannelUID; import org.openhab.core.types.Command; import org.openhab.core.types.State; +import org.simplify4u.jfatek.FatekCommand; +import org.simplify4u.jfatek.FatekPLC; +import org.simplify4u.jfatek.FatekWriteDataCmd; import org.simplify4u.jfatek.registers.DataReg; import org.simplify4u.jfatek.registers.Reg; import org.simplify4u.jfatek.registers.RegValue; -import org.simplify4u.jfatek.registers.RegValue16; -import org.simplify4u.jfatek.registers.RegValue32; +import org.simplify4u.jfatek.registers.RegValueData; public class DataChannelHandler implements FatekChannelHandler { - private final DataReg register; private final ChannelUID channel; - private final boolean unsigned; - private final BiFunction mapper; + private final DataReg register; + private final Converter converter; - public DataChannelHandler(Channel channel, DataChannelConfig config, BiFunction mapper) { + public DataChannelHandler(Channel channel, DataReg register, Converter converter) { this.channel = channel.getUID(); - this.unsigned = config.invert; - this.mapper = mapper; - switch (config.getRegister()) { - case R: - this.register = R(config.index); - break; - case D: - this.register = D(config.index); - break; - case F: - this.register = F(config.index); - break; - case DR: - this.register = DR(config.index); - break; - case DD: - this.register = DD(config.index); - break; - case DF: - this.register = DF(config.index); - break; - case RT: - this.register = RT(config.index); - break; - case RC: - this.register = RC(config.index); - break; - case DRT: - this.register = DRT(config.index); - break; - case DRC: - this.register = DRC(config.index); - break; - case WX: - this.register = WX(config.index); - break; - case WY: - this.register = WY(config.index); - break; - case WM: - this.register = WM(config.index); - break; - case WS: - this.register = WS(config.index); - break; - case WT: - this.register = WT(config.index); - break; - case WC: - this.register = WC(config.index); - break; - case DWX: - this.register = DWX(config.index); - break; - case DWY: - this.register = DWY(config.index); - break; - case DWM: - this.register = DWM(config.index); - break; - case DWS: - this.register = DWS(config.index); - break; - case DWT: - this.register = DWT(config.index); - break; - case DWC: - this.register = DWC(config.index); - break; - default: - throw new IllegalArgumentException("Unsupported register kind " + config.getRegister()); - } + this.register = register; + this.converter = converter; } @Override @@ -127,22 +53,17 @@ public ChannelUID channel() { } @Override - public RegValue prepareWrite(Command command) { - if (!(command instanceof DecimalType)) { - return null; + public FatekCommand prepareWrite(Command command) { + RegValue value = converter.toValue(command); + if (value instanceof RegValueData) { + return new FatekWriteDataCmd((FatekPLC) null, register, (RegValueData) value); } - - DecimalType value = (DecimalType) command; - return mapper.apply(register, value); + return null; } @Override public State state(RegValue value) { - if (register.is32Bits()) { - return new DecimalType(value.floatValue()); - } - - return new DecimalType(unsigned ? value.longValueUnsigned() : value.longValue()); + return converter.toState(value); } } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DefaultChannelHandlerFactory.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DefaultChannelHandlerFactory.java index 03a93251..730e16b7 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DefaultChannelHandlerFactory.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DefaultChannelHandlerFactory.java @@ -17,14 +17,17 @@ */ package org.connectorio.addons.binding.fatek.internal.channel; -import java.math.BigInteger; import org.connectorio.addons.binding.fatek.FatekBindingConstants; +import org.connectorio.addons.binding.fatek.config.channel.binary.DiscreteChannelConfig; import org.connectorio.addons.binding.fatek.config.channel.data.Data32ChannelConfig; import org.connectorio.addons.binding.fatek.config.channel.data.DataChannelConfig; -import org.connectorio.addons.binding.fatek.config.channel.binary.DiscreteChannelConfig; +import org.connectorio.addons.binding.fatek.config.channel.rollershutter.RollerShutter32ChannelConfig; +import org.connectorio.addons.binding.fatek.config.channel.rollershutter.RollerShutterChannelConfig; +import org.connectorio.addons.binding.fatek.internal.RegisterParser; +import org.connectorio.addons.binding.fatek.internal.channel.converter.Data16Converter; +import org.connectorio.addons.binding.fatek.internal.channel.converter.Data32Converter; +import org.connectorio.addons.binding.fatek.internal.channel.converter.DiscreteConverter; import org.openhab.core.thing.Channel; -import org.simplify4u.jfatek.registers.RegValue16; -import org.simplify4u.jfatek.registers.RegValue32; public class DefaultChannelHandlerFactory implements FatekChannelHandlerFactory { @@ -32,25 +35,39 @@ public class DefaultChannelHandlerFactory implements FatekChannelHandlerFactory public FatekChannelHandler create(Channel channel) { if (FatekBindingConstants.CHANNEL_TYPE_DISCRETE.equals(channel.getChannelTypeUID())) { DiscreteChannelConfig config = channel.getConfiguration().as(DiscreteChannelConfig.class); - return new BinaryChannelHandler(channel, config); + return new BinaryChannelHandler(channel, RegisterParser.parseDiscrete(config), new DiscreteConverter(config)); } if (FatekBindingConstants.CHANNEL_TYPE_DATA16.equals(channel.getChannelTypeUID())) { DataChannelConfig config = channel.getConfiguration().as(DataChannelConfig.class); - return new DataChannelHandler(channel, config, (register, state) -> { - int intValue = state.toBigDecimal().intValue(); - return RegValue16.getForReg(register, config.unsigned ? Integer.toUnsignedLong(intValue) : intValue); - }); + return new DataChannelHandler(channel, RegisterParser.parseData16(config), new Data16Converter(config)); } if (FatekBindingConstants.CHANNEL_TYPE_DATA32.equals(channel.getChannelTypeUID())) { Data32ChannelConfig config = channel.getConfiguration().as(Data32ChannelConfig.class); - return new DataChannelHandler(channel, config, (register, state) -> { - if (config.floating) { - return RegValue32.getForReg(register, state.toBigDecimal().floatValue()); - } - - long longValue = state.toBigDecimal().longValue(); - return RegValue32.getForReg(register, config.unsigned ? BigInteger.valueOf(longValue).longValue() : longValue); - }); + return new DataChannelHandler(channel, RegisterParser.parseData32(config), new Data32Converter(config)); + } + if (FatekBindingConstants.CHANNEL_TYPE_ROLLERSHUTTER16.equals(channel.getChannelTypeUID())) { + RollerShutterChannelConfig config = channel.getConfiguration().as(RollerShutterChannelConfig.class); + DiscreteChannelConfig startCfg = new DiscreteChannelConfig(config.startRegister, config.startIndex, config.startInvert); + DiscreteChannelConfig stopCfg = new DiscreteChannelConfig(config.stopRegister, config.stopIndex, config.stopInvert); + return new RollerShutterChannelHandler(channel, RegisterParser.parseData16(config), + RegisterParser.parseDiscrete(startCfg), + RegisterParser.parseDiscrete(startCfg), + new Data16Converter(config), + new DiscreteConverter(startCfg), + new DiscreteConverter(stopCfg) + ); + } + if (FatekBindingConstants.CHANNEL_TYPE_ROLLERSHUTTER32.equals(channel.getChannelTypeUID())) { + RollerShutter32ChannelConfig config = channel.getConfiguration().as(RollerShutter32ChannelConfig.class); + DiscreteChannelConfig startCfg = new DiscreteChannelConfig(config.startRegister, config.startIndex, config.startInvert); + DiscreteChannelConfig stopCfg = new DiscreteChannelConfig(config.stopRegister, config.stopIndex, config.stopInvert); + return new RollerShutterChannelHandler(channel, RegisterParser.parseData16(config), + RegisterParser.parseDiscrete(startCfg), + RegisterParser.parseDiscrete(startCfg), + new Data32Converter(config), + new DiscreteConverter(startCfg), + new DiscreteConverter(stopCfg) + ); } return null; } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/FatekChannelHandler.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/FatekChannelHandler.java index e9719b9e..725da644 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/FatekChannelHandler.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/FatekChannelHandler.java @@ -20,6 +20,7 @@ import org.openhab.core.thing.ChannelUID; import org.openhab.core.types.Command; import org.openhab.core.types.State; +import org.simplify4u.jfatek.FatekCommand; import org.simplify4u.jfatek.registers.Reg; import org.simplify4u.jfatek.registers.RegValue; @@ -30,7 +31,7 @@ public interface FatekChannelHandler { ChannelUID channel(); // write - RegValue prepareWrite(Command command); + FatekCommand prepareWrite(Command command); // fetch State state(RegValue value); diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/FatekChannelHandlerFactory.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/FatekChannelHandlerFactory.java index 30ca6eb9..f14437a3 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/FatekChannelHandlerFactory.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/FatekChannelHandlerFactory.java @@ -22,4 +22,5 @@ public interface FatekChannelHandlerFactory { FatekChannelHandler create(Channel channel); + } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/RollerShutterChannelHandler.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/RollerShutterChannelHandler.java new file mode 100644 index 00000000..7011d373 --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/RollerShutterChannelHandler.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2023-2023 ConnectorIO Sp. z o.o. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.connectorio.addons.binding.fatek.internal.channel; + +import java.util.Map; +import org.connectorio.addons.binding.fatek.internal.channel.converter.Converter; +import org.openhab.core.library.types.OnOffType; +import org.openhab.core.library.types.StopMoveType; +import org.openhab.core.library.types.UpDownType; +import org.openhab.core.thing.Channel; +import org.openhab.core.thing.ChannelUID; +import org.openhab.core.types.Command; +import org.openhab.core.types.State; +import org.simplify4u.jfatek.FatekCommand; +import org.simplify4u.jfatek.FatekWriteDataCmd; +import org.simplify4u.jfatek.FatekWriteMixDataCmd; +import org.simplify4u.jfatek.registers.DataReg; +import org.simplify4u.jfatek.registers.DisReg; +import org.simplify4u.jfatek.registers.Reg; +import org.simplify4u.jfatek.registers.RegValue; +import org.simplify4u.jfatek.registers.RegValueData; +import org.simplify4u.jfatek.registers.RegValueDis; + +public class RollerShutterChannelHandler implements FatekChannelHandler { + + private final ChannelUID channel; + private final DataReg register; + private final DisReg startReg; + private final DisReg stopReg; + private final Converter positionConverter; + private final Converter startConverter; + private final Converter stopConverter; + + public RollerShutterChannelHandler(Channel channel, DataReg register, DisReg startReg, DisReg stopReg, + Converter positionConverter, Converter startConverter, Converter stopConverter) { + this.channel = channel.getUID(); + this.register = register; + this.startReg = startReg; + this.stopReg = stopReg; + this.positionConverter = positionConverter; + this.startConverter = startConverter; + this.stopConverter = stopConverter; + } + + @Override + public Reg register() { + return register; + } + + @Override + public ChannelUID channel() { + return channel; + } + + @Override + public FatekCommand prepareWrite(Command command) { + RegValue positionValue = positionConverter.toValue(command); + if (positionValue instanceof RegValueData) { + return new FatekWriteDataCmd(null, register, (RegValueData) positionValue); + } + + if (command == UpDownType.UP) { + RegValue start = startConverter.toValue(OnOffType.ON); + RegValue stop = stopConverter.toValue(OnOffType.OFF); + return new FatekWriteMixDataCmd(null, Map.of( + startReg, start, + stopReg, stop + )); + } + if (command == UpDownType.DOWN) { + RegValue start = startConverter.toValue(OnOffType.OFF); + RegValue stop = stopConverter.toValue(OnOffType.ON); + return new FatekWriteMixDataCmd(null, Map.of( + startReg, start, + stopReg, stop + )); + } + if (command == StopMoveType.STOP) { + RegValue start = startConverter.toValue(OnOffType.OFF); + RegValue stop = stopConverter.toValue(OnOffType.OFF); + return new FatekWriteMixDataCmd(null, Map.of( + startReg, start, + stopReg, stop + )); + } + + return null; + } + + @Override + public State state(RegValue value) { + return positionConverter.toState(value); + } + +} diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Converter.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Converter.java new file mode 100644 index 00000000..6c13ac7a --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Converter.java @@ -0,0 +1,13 @@ +package org.connectorio.addons.binding.fatek.internal.channel.converter; + +import org.openhab.core.types.Command; +import org.openhab.core.types.State; +import org.simplify4u.jfatek.registers.RegValue; + +public interface Converter { + + RegValue toValue(Command command); + + State toState(RegValue value); + +} diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Data16Converter.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Data16Converter.java new file mode 100644 index 00000000..72826704 --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Data16Converter.java @@ -0,0 +1,35 @@ +package org.connectorio.addons.binding.fatek.internal.channel.converter; + +import java.math.BigInteger; +import org.connectorio.addons.binding.fatek.config.channel.data.Data32ChannelConfig; +import org.connectorio.addons.binding.fatek.config.channel.data.DataChannelConfig; +import org.openhab.core.library.types.DecimalType; +import org.openhab.core.types.Command; +import org.openhab.core.types.State; +import org.simplify4u.jfatek.registers.RegValue; +import org.simplify4u.jfatek.registers.RegValue32; + +public class Data16Converter implements Converter { + + private final DataChannelConfig config; + + public Data16Converter(DataChannelConfig config) { + this.config = config; + } + + @Override + public RegValue toValue(Command command) { + if (!(command instanceof DecimalType)) { + return null; + } + + DecimalType decimalType = (DecimalType) command; + long longValue = decimalType.toBigDecimal().longValue(); + return new RegValue32(config.unsigned ? BigInteger.valueOf(longValue).longValue() : longValue); + } + + @Override + public State toState(RegValue value) { + return new DecimalType(config.unsigned ? value.longValueUnsigned() : value.longValue()); + } +} diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Data32Converter.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Data32Converter.java new file mode 100644 index 00000000..ffd30745 --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Data32Converter.java @@ -0,0 +1,46 @@ +package org.connectorio.addons.binding.fatek.internal.channel.converter; + +import java.math.BigInteger; +import org.connectorio.addons.binding.fatek.config.channel.binary.DiscreteChannelConfig; +import org.connectorio.addons.binding.fatek.config.channel.data.Data32ChannelConfig; +import org.openhab.core.library.types.DecimalType; +import org.openhab.core.library.types.OnOffType; +import org.openhab.core.library.types.OpenClosedType; +import org.openhab.core.types.Command; +import org.openhab.core.types.State; +import org.simplify4u.jfatek.registers.RegValue; +import org.simplify4u.jfatek.registers.RegValue32; +import org.simplify4u.jfatek.registers.RegValueDis; + +public class Data32Converter implements Converter { + + private final Data32ChannelConfig config; + + public Data32Converter(Data32ChannelConfig config) { + this.config = config; + } + + @Override + public RegValue toValue(Command command) { + if (!(command instanceof DecimalType)) { + return null; + } + + DecimalType decimalType = (DecimalType) command; + if (config.floating) { + return new RegValue32(Float.floatToIntBits(decimalType.toBigDecimal().floatValue())); + } + + long longValue = decimalType.toBigDecimal().longValue(); + return new RegValue32(config.unsigned ? BigInteger.valueOf(longValue).longValue() : longValue); + } + + @Override + public State toState(RegValue value) { + if (config.floating) { + return new DecimalType(value.floatValue()); + } + + return new DecimalType(config.unsigned ? value.longValueUnsigned() : value.longValue()); + } +} diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/DiscreteConverter.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/DiscreteConverter.java new file mode 100644 index 00000000..6256fea9 --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/DiscreteConverter.java @@ -0,0 +1,39 @@ +package org.connectorio.addons.binding.fatek.internal.channel.converter; + +import org.connectorio.addons.binding.fatek.config.channel.binary.DiscreteChannelConfig; +import org.openhab.core.library.types.OnOffType; +import org.openhab.core.library.types.OpenClosedType; +import org.openhab.core.types.Command; +import org.openhab.core.types.State; +import org.simplify4u.jfatek.registers.RegValue; +import org.simplify4u.jfatek.registers.RegValueDis; + +public class DiscreteConverter implements Converter { + + private final DiscreteChannelConfig config; + + public DiscreteConverter(DiscreteChannelConfig config) { + this.config = config; + } + + @Override + public RegValue toValue(Command command) { + boolean value; + if (command instanceof OpenClosedType) { + value = OpenClosedType.CLOSED == command; + } else { + value = OnOffType.ON == command; + } + + if (value) { + return config.invert ? RegValueDis.FALSE : RegValueDis.TRUE; + } + return config.invert ? RegValueDis.TRUE : RegValueDis.FALSE; + } + + @Override + public State toState(RegValue value) { + boolean status = config.invert ? !value.boolValue() : value.boolValue(); + return status ? OnOffType.ON : OnOffType.OFF; + } +} diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/handler/FatekPlcThingHandler.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/handler/FatekPlcThingHandler.java index 53454bf4..a72350fb 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/handler/FatekPlcThingHandler.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/handler/FatekPlcThingHandler.java @@ -110,33 +110,24 @@ public void handleCommand(ChannelUID channelUID, Command command) { if (handlerMap.containsKey(channelUID)) { FatekChannelHandler handler = handlerMap.get(channelUID); Reg register = handler.register(); - RegValue value = handler.prepareWrite(command); - if (value == null) { + FatekCommand fatekCmd = null; //handler.prepareWrite(command); + if (fatekCmd == null) { logger.warn("Could not map command {} from channel {} to value supported by PLC. Ignoring this write.", command, channelUID); return; } - if (register instanceof DataReg) { - FatekWriteDataCmd cmd = new FatekWriteDataCmd(null, (DataReg) register); - cmd.addValue(value.longValue()); - write(channelUID, command, register, value, cmd); - } else if (register instanceof DisReg) { - FatekWriteDiscreteCmd cmd = new FatekWriteDiscreteCmd(null, (DisReg) register, value.boolValue()); - write(channelUID, command, register, value, cmd); - } else { - logger.error("Unsupported value {} mapped from command {} for channel {}", value, command, channelUID); - } + write(channelUID, command, fatekCmd); } } - private void write(ChannelUID channelUID, Command command, Reg register, RegValue value, FatekCommand cmd) { + private void write(ChannelUID channel, Command command, FatekCommand cmd) { connection.execute(stationNumber, cmd).whenComplete((r, e) -> { if (e != null) { - logger.error("Could not write value {} to register {}", command, register, e); + logger.error("Could not signal channel {} with command {} by fatek command {}", channel, command, cmd, e); return; } - logger.debug("Successful write of value {} mapped from {} for channel {}", value, command, register); - update(channelUID, command); + logger.debug("Successful write of channel {} with command {} annd fatek command {}", channel, command, cmd); + update(channel, command); }); } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/binary-channel-config.xml b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/binary-channel-config.xml index be00adb6..74e12a87 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/binary-channel-config.xml +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/binary-channel-config.xml @@ -23,8 +23,8 @@ - - Symbol (kind) of register to read + + Type of discrete register to read or write. diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/data16-channel-config.xml b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/data16-channel-config.xml index 2d1f2fda..7f36fa59 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/data16-channel-config.xml +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/data16-channel-config.xml @@ -23,8 +23,8 @@ - - Symbol (kind) of register to read + + Type of data register to read or write. diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/data32-channel-config.xml b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/data32-channel-config.xml index 8752a7a5..a4ba1c0e 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/data32-channel-config.xml +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/data32-channel-config.xml @@ -23,8 +23,8 @@ - - Symbol (kind) of register to read + + Type of data register to read or write. diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/rollershutter16-channel-config.xml b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/rollershutter16-channel-config.xml new file mode 100644 index 00000000..fffde168 --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/rollershutter16-channel-config.xml @@ -0,0 +1,120 @@ + + + + + + + + Settings for discrete register used for sending start command. + + + + Settings for discrete register used for sending stop command. + + + + + Data register used to retrieve position of roller shutter. + + + + + + + + + + + + + + + + + + + + Index of register used to retrieve data. + 0 + + + + Values retrieved from and written to device assume unsigned representation. Does not apply if floating option is set. + false + + + + Indicate that read/write value is a floating point number. Overrides unsigned flag. + false + + + + + + Type of discrete register to write UP command. + + + + + + + + + + + + Index of register used to write data. + 0 + + + + Invert logical representation of related register (0->1, 1->0). + false + + + + + + Type of discrete register to write DOWN command. + + + + + + + + + + + + Index of register used to retrieve data. + 0 + + + + Invert logical representation of related register (0->1, 1->0). + false + + + + + diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/rollershutter32-channel-config.xml b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/rollershutter32-channel-config.xml new file mode 100644 index 00000000..098750c3 --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/rollershutter32-channel-config.xml @@ -0,0 +1,120 @@ + + + + + + + + Settings for discrete register used for sending start command. + + + + Settings for discrete register used for sending stop command. + + + + + Data register used to retrieve position of roller shutter. + + + + + + + + + + + + + + + + + + + + Index of register used to retrieve data. + 0 + + + + Values retrieved from and written to device assume unsigned representation. Does not apply if floating option is set. + false + + + + Indicate that read/write value is a floating point number. Overrides unsigned flag. + false + + + + + + Type of discrete register to write UP command. + + + + + + + + + + + + Index of register used to write data. + 0 + + + + Invert logical representation of related register (0->1, 1->0). + false + + + + + + Type of discrete register to write DOWN command. + + + + + + + + + + + + Index of register used to retrieve data. + 0 + + + + Invert logical representation of related register (0->1, 1->0). + false + + + + + diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/channels.xml b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/channels.xml index 6bd2a58f..61551972 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/channels.xml +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/channels.xml @@ -34,7 +34,6 @@ Data register of connected PLC encoded using 16 bit registers. - Number @@ -42,4 +41,17 @@ + + Rollershutter + + Data register for position will use 16 bit registers. + + + + Rollershutter + + Data register for position will use 32 bit registers. + + + \ No newline at end of file diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/thing.xml b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/thing.xml index c3de501b..8d96bf85 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/thing.xml +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/thing.xml @@ -23,7 +23,7 @@ https://openhab.org/schemas/thing-description-1.0.0.xsd "> - + From 139172baed4137e0105a71cac0e41f95ea0dad45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Dywicki?= Date: Sat, 2 Mar 2024 00:38:16 +0100 Subject: [PATCH 3/3] Add support for fatek color channel. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Łukasz Dywicki --- .../docs/fatek/descriptors/channel-types.adoc | 80 ++++++++++ .../descriptors/config-descriptions.adoc | 55 +++++++ .../binding/fatek/FatekBindingConstants.java | 2 + .../channel/color/ColorChannelConfig.java | 45 ++++++ .../channel/data/DataChannelConfig.java | 8 + .../RollerShutter32ChannelConfig.java | 4 +- .../channel/BinaryChannelHandler.java | 10 +- .../internal/channel/ColorChannelHandler.java | 137 +++++++++++++++++ .../internal/channel/DataChannelHandler.java | 10 +- .../channel/DefaultChannelHandlerFactory.java | 34 +++++ .../internal/channel/FatekChannelHandler.java | 5 +- .../channel/RollerShutterChannelHandler.java | 27 ++-- .../channel/converter/ColorConverter.java | 73 +++++++++ .../internal/channel/converter/Converter.java | 17 +++ .../channel/converter/Data16Converter.java | 17 +++ .../channel/converter/Data32Converter.java | 17 +++ .../channel/converter/DiscreteConverter.java | 17 +++ .../handler/FatekPlcThingHandler.java | 47 ++++-- .../OH-INF/config/color16-channel-config.xml | 142 ++++++++++++++++++ .../OH-INF/config/color32-channel-config.xml | 142 ++++++++++++++++++ .../main/resources/OH-INF/thing/channels.xml | 13 ++ .../src/main/resources/OH-INF/thing/thing.xml | 2 +- 22 files changed, 864 insertions(+), 40 deletions(-) create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/color/ColorChannelConfig.java create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/ColorChannelHandler.java create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/ColorConverter.java create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/color16-channel-config.xml create mode 100644 bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/color32-channel-config.xml diff --git a/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/channel-types.adoc b/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/channel-types.adoc index 0b6ba4b7..4fb41e2b 100644 --- a/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/channel-types.adoc +++ b/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/channel-types.adoc @@ -35,6 +35,18 @@ <> +| co7io-fatek:color16 +| Color (16-bit) +| Data register for position will use 16 bit registers. + +<> + +| co7io-fatek:color32 +| Color (32-bit) +| Data register for position will use 32 bit registers. + +<> + |=== @@ -248,4 +260,72 @@ Below table contain configuration parameters which can be assigned to channels o |=== +[[co7io-fatek:color16]] +== Configuration of `co7io-fatek:color16` + +Below table contain configuration parameters which can be assigned to channels of type `co7io-fatek:color16`. + +[width="100%",caption="Channel type color16 configuration",cols="1,1,1,2"] +|=== +|Name | Type | Label ^|Description + +| color1register +| TEXT +| Color 1 register +| Data register used to retrieve or write Red or Hue part of color information. + +| color1index +| INTEGER +| Register index +| Index of register used to retrieve data. + +| color2register +| TEXT +| Color 2 register +| Data register used to retrieve or write Green or Saturation part of color information. + +| color2index +| INTEGER +| Register index +| Index of register used to retrieve data. + +| color3register +| TEXT +| Color 2 register +| Data register used to retrieve or write Blue or Brightness part of color information. + +| color3index +| INTEGER +| Register index +| Index of register used to retrieve data. + +| rgb +| BOOLEAN +| RGB mode +| Write and interpret read data as RGB code instead of HSB. + +| switcherRegister +| TEXT +| Register +| Type of discrete register to write ON or OFF command. + +| switcherIndex +| INTEGER +| Register index +| Index of register used to write data. + +| switcherInvert +| BOOLEAN +| Invert +| Invert logical representation of related register (0->1, 1->0). + +|=== + + +[[co7io-fatek:color32]] +== Configuration of `co7io-fatek:color32` + +Below table contain configuration parameters which can be assigned to channels of type `co7io-fatek:color32`. + + diff --git a/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/config-descriptions.adoc b/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/config-descriptions.adoc index 6d6c6785..39c7f706 100644 --- a/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/config-descriptions.adoc +++ b/bundles/org.connectorio.addons.binding.fatek/docs/fatek/descriptors/config-descriptions.adoc @@ -273,6 +273,61 @@ | BOOLEAN | Invert | Invert logical representation of related register (0->1, 1->0). + +|=== + +[[channel-type:co7io-fatek:color16]] +== Config `channel-type:co7io-fatek:color16` +[width="100%",caption="channel-type:co7io-fatek:color16 configuration",cols="1,1,1,2"] +|=== +|Name | Type | Label ^|Description + +| color1register +| TEXT +| Color 1 register +| Data register used to retrieve or write Red or Hue part of color information. + +| color1index +| INTEGER +| Register index +| Index of register used to retrieve data. + +| color2register +| TEXT +| Color 2 register +| Data register used to retrieve or write Green or Saturation part of color information. + +| color2index +| INTEGER +| Register index +| Index of register used to retrieve data. + +| color3register +| TEXT +| Color 2 register +| Data register used to retrieve or write Blue or Brightness part of color information. + +| color3index +| INTEGER +| Register index +| Index of register used to retrieve data. + +| rgb +| BOOLEAN +| RGB mode +| Write and interpret read data as RGB code instead of HSB. + +| switcherRegister +| TEXT +| Register +| Type of discrete register to write ON or OFF command. + +| switcherIndex +| INTEGER +| Register index +| Index of register used to write data. + +| switcherInvert | BOOLEAN | Invert | Invert logical representation of related register (0->1, 1->0). diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/FatekBindingConstants.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/FatekBindingConstants.java index e0eed54f..ad8b043e 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/FatekBindingConstants.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/FatekBindingConstants.java @@ -45,6 +45,8 @@ public interface FatekBindingConstants extends BaseBindingConstants { ChannelTypeUID CHANNEL_TYPE_DATA32 = new ChannelTypeUID(BINDING_ID, "data32"); ChannelTypeUID CHANNEL_TYPE_ROLLERSHUTTER16 = new ChannelTypeUID(BINDING_ID, "rollershutter16"); ChannelTypeUID CHANNEL_TYPE_ROLLERSHUTTER32 = new ChannelTypeUID(BINDING_ID, "rollershutter32"); + ChannelTypeUID CHANNEL_TYPE_COLOR16 = new ChannelTypeUID(BINDING_ID, "color16"); + ChannelTypeUID CHANNEL_TYPE_COLOR32 = new ChannelTypeUID(BINDING_ID, "color32"); } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/color/ColorChannelConfig.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/color/ColorChannelConfig.java new file mode 100644 index 00000000..d9f38abe --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/color/ColorChannelConfig.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2024-2024 ConnectorIO Sp. z o.o. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.connectorio.addons.binding.fatek.config.channel.color; + +import org.connectorio.addons.binding.fatek.config.channel.data.DataChannelConfig; +import org.simplify4u.jfatek.registers.RegName; + +public class ColorChannelConfig extends DataChannelConfig { + + // RGB color control mode + public boolean rgb = false; + + public RegName color1register; + public int color1index; + + public RegName color2register; + public int color2index; + + public RegName color3register; + public int color3index; + + // optional + public RegName switcherRegister; + public int switcherIndex; + public boolean switcherInvert; + + // increase decrease step - optional + public int step; + +} diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/data/DataChannelConfig.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/data/DataChannelConfig.java index d64fc173..85e88d79 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/data/DataChannelConfig.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/data/DataChannelConfig.java @@ -18,9 +18,17 @@ package org.connectorio.addons.binding.fatek.config.channel.data; import org.connectorio.addons.binding.fatek.config.channel.BaseChannelConfig; +import org.simplify4u.jfatek.registers.RegName; public class DataChannelConfig extends BaseChannelConfig { public boolean unsigned; + public DataChannelConfig() {} + + public DataChannelConfig(RegName register, int index) { + this.register = register; + this.index = index; + } + } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/rollershutter/RollerShutter32ChannelConfig.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/rollershutter/RollerShutter32ChannelConfig.java index e4c3f864..bf7ef82d 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/rollershutter/RollerShutter32ChannelConfig.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/config/channel/rollershutter/RollerShutter32ChannelConfig.java @@ -22,12 +22,12 @@ public class RollerShutter32ChannelConfig extends Data32ChannelConfig { - public int startIndex; public RegName startRegister; + public int startIndex; public boolean startInvert; - public int stopIndex; public RegName stopRegister; + public int stopIndex; public boolean stopInvert; } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/BinaryChannelHandler.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/BinaryChannelHandler.java index 12e3e947..782f894e 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/BinaryChannelHandler.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/BinaryChannelHandler.java @@ -17,6 +17,8 @@ */ package org.connectorio.addons.binding.fatek.internal.channel; +import java.util.Collections; +import java.util.List; import org.connectorio.addons.binding.fatek.internal.channel.converter.Converter; import org.openhab.core.thing.Channel; import org.openhab.core.thing.ChannelUID; @@ -41,8 +43,8 @@ public BinaryChannelHandler(Channel channel, DisReg register, Converter converte } @Override - public Reg register() { - return register; + public List registers() { + return Collections.singletonList(register); } @Override @@ -60,8 +62,8 @@ public FatekCommand prepareWrite(Command command) { } @Override - public State state(RegValue value) { - return converter.toState(value); + public State state(List value) { + return converter.toState(value.get(0)); } } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/ColorChannelHandler.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/ColorChannelHandler.java new file mode 100644 index 00000000..b5ca4645 --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/ColorChannelHandler.java @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2024-2024 ConnectorIO Sp. z o.o. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.connectorio.addons.binding.fatek.internal.channel; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; +import org.connectorio.addons.binding.fatek.config.channel.color.ColorChannelConfig; +import org.connectorio.addons.binding.fatek.internal.channel.converter.ColorConverter; +import org.connectorio.addons.binding.fatek.internal.channel.converter.Converter; +import org.openhab.core.library.types.HSBType; +import org.openhab.core.library.types.IncreaseDecreaseType; +import org.openhab.core.library.types.OnOffType; +import org.openhab.core.library.types.PercentType; +import org.openhab.core.thing.Channel; +import org.openhab.core.thing.ChannelUID; +import org.openhab.core.types.Command; +import org.openhab.core.types.State; +import org.simplify4u.jfatek.FatekCommand; +import org.simplify4u.jfatek.FatekWriteDiscreteCmd; +import org.simplify4u.jfatek.FatekWriteMixDataCmd; +import org.simplify4u.jfatek.registers.DataReg; +import org.simplify4u.jfatek.registers.DisReg; +import org.simplify4u.jfatek.registers.Reg; +import org.simplify4u.jfatek.registers.RegValue; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ColorChannelHandler implements FatekChannelHandler { + + private final Logger logger = LoggerFactory.getLogger(ColorChannelHandler.class); + + private final ChannelUID channel; + private final ColorChannelConfig config; + private final DataReg color1; + private final DataReg color2; + private final DataReg color3; + private final DisReg switcher; + private final ColorConverter converter; + private final Converter switcherConverter; + + // a volatile state + private AtomicReference state = new AtomicReference<>(); + + public ColorChannelHandler(Channel channel, ColorChannelConfig config, DataReg color1, DataReg color2, DataReg color3, + ColorConverter converter, DisReg switcher, Converter switcherConverter) { + this.channel = channel.getUID(); + this.config = config; + this.color1 = color1; + this.color2 = color2; + this.color3 = color3; + this.switcher = switcher; + this.converter = converter; + this.switcherConverter = switcherConverter; + } + + @Override + public List registers() { + return Arrays.asList(color1, color2, color3); + } + + @Override + public ChannelUID channel() { + return channel; + } + + @Override + public FatekCommand prepareWrite(Command command) { + if (command instanceof IncreaseDecreaseType) { + if (state.get() == null) { + return null; + } + + HSBType hsb = state.get(); + if (IncreaseDecreaseType.INCREASE.equals(command)) { + int brightness = Math.min(hsb.getBrightness().intValue() + config.step, 100); + return colorCommand(new HSBType(hsb.getHue(), hsb.getSaturation(), new PercentType(brightness))); + } else if (IncreaseDecreaseType.DECREASE.equals(command)) { + int brightness = Math.max(hsb.getBrightness().intValue() - config.step, 0); + return colorCommand(new HSBType(hsb.getHue(), hsb.getSaturation(), new PercentType(brightness))); + } else { + return null; + } + } else if (command instanceof OnOffType) { + RegValue value = switcherConverter.toValue(command); + if (value != null) { + return new FatekWriteDiscreteCmd(null, switcher, value.boolValue()); + } + return null; + } else if (command instanceof HSBType) { + return colorCommand((HSBType) command); + } + + logger.warn("Unsupported command {} received by color channel {}", command, channel); + return null; + } + + private FatekCommand colorCommand(HSBType hsbType) { + List value = converter.toValue(hsbType); + if (value != null) { + return new FatekWriteMixDataCmd(null, Map.of( + color1, value.get(0), + color2, value.get(1), + color3, value.get(2) + )); + } + return null; + } + + @Override + public State state(List value) { + State convertedState = converter.toState(value.get(0), value.get(1), value.get(2)); + if (convertedState instanceof HSBType) { + // retain last read value of color state + state.set((HSBType) convertedState); + } + return convertedState; + } + + +} diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DataChannelHandler.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DataChannelHandler.java index 074ebc5a..2a9d51e7 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DataChannelHandler.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DataChannelHandler.java @@ -17,6 +17,8 @@ */ package org.connectorio.addons.binding.fatek.internal.channel; +import java.util.Collections; +import java.util.List; import org.connectorio.addons.binding.fatek.internal.channel.converter.Converter; import org.openhab.core.thing.Channel; import org.openhab.core.thing.ChannelUID; @@ -43,8 +45,8 @@ public DataChannelHandler(Channel channel, DataReg register, Converter converter } @Override - public Reg register() { - return register; + public List registers() { + return Collections.singletonList(register); } @Override @@ -62,8 +64,8 @@ public FatekCommand prepareWrite(Command command) { } @Override - public State state(RegValue value) { - return converter.toState(value); + public State state(List value) { + return converter.toState(value.get(0)); } } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DefaultChannelHandlerFactory.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DefaultChannelHandlerFactory.java index 730e16b7..a19e83a5 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DefaultChannelHandlerFactory.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/DefaultChannelHandlerFactory.java @@ -19,15 +19,20 @@ import org.connectorio.addons.binding.fatek.FatekBindingConstants; import org.connectorio.addons.binding.fatek.config.channel.binary.DiscreteChannelConfig; +import org.connectorio.addons.binding.fatek.config.channel.color.ColorChannelConfig; import org.connectorio.addons.binding.fatek.config.channel.data.Data32ChannelConfig; import org.connectorio.addons.binding.fatek.config.channel.data.DataChannelConfig; import org.connectorio.addons.binding.fatek.config.channel.rollershutter.RollerShutter32ChannelConfig; import org.connectorio.addons.binding.fatek.config.channel.rollershutter.RollerShutterChannelConfig; import org.connectorio.addons.binding.fatek.internal.RegisterParser; +import org.connectorio.addons.binding.fatek.internal.channel.converter.ColorConverter; +import org.connectorio.addons.binding.fatek.internal.channel.converter.Converter; import org.connectorio.addons.binding.fatek.internal.channel.converter.Data16Converter; import org.connectorio.addons.binding.fatek.internal.channel.converter.Data32Converter; import org.connectorio.addons.binding.fatek.internal.channel.converter.DiscreteConverter; import org.openhab.core.thing.Channel; +import org.simplify4u.jfatek.registers.DataReg; +import org.simplify4u.jfatek.registers.DisReg; public class DefaultChannelHandlerFactory implements FatekChannelHandlerFactory { @@ -69,7 +74,36 @@ public FatekChannelHandler create(Channel channel) { new DiscreteConverter(stopCfg) ); } + if (FatekBindingConstants.CHANNEL_TYPE_COLOR16.equals(channel.getChannelTypeUID())) { + ColorChannelConfig config = channel.getConfiguration().as(ColorChannelConfig.class); + DataReg color1cfg = RegisterParser.parseData16(new DataChannelConfig(config.color1register, config.color1index)); + DataReg color2cfg = RegisterParser.parseData16(new DataChannelConfig(config.color2register, config.color2index)); + DataReg color3cfg = RegisterParser.parseData16(new DataChannelConfig(config.color3register, config.color3index)); + return createColorHandler(channel, config, color1cfg, color2cfg, color3cfg); + } + if (FatekBindingConstants.CHANNEL_TYPE_COLOR32.equals(channel.getChannelTypeUID())) { + ColorChannelConfig config = channel.getConfiguration().as(ColorChannelConfig.class); + DataReg color1cfg = RegisterParser.parseData32(new DataChannelConfig(config.color1register, config.color1index)); + DataReg color2cfg = RegisterParser.parseData32(new DataChannelConfig(config.color2register, config.color2index)); + DataReg color3cfg = RegisterParser.parseData32(new DataChannelConfig(config.color3register, config.color3index)); + return createColorHandler(channel, config, color1cfg, color2cfg, color3cfg); + } return null; } + private FatekChannelHandler createColorHandler(Channel channel, ColorChannelConfig config, DataReg color1, DataReg color2, DataReg color3) { + DisReg switcher = null; + Converter switcherConverter = null; + if (config.switcherRegister != null && config.switcherIndex != 0) { + DiscreteChannelConfig switchCfg = new DiscreteChannelConfig(config.switcherRegister, config.switcherIndex, config.switcherInvert); + switcher = RegisterParser.parseDiscrete(switchCfg); + switcherConverter = new DiscreteConverter(switchCfg); + } + return new ColorChannelHandler(channel, config, + color1, color2, color3, + new ColorConverter(color1, color2, color3, config.rgb), + switcher, switcherConverter + ); + } + } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/FatekChannelHandler.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/FatekChannelHandler.java index 725da644..24e85de6 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/FatekChannelHandler.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/FatekChannelHandler.java @@ -17,6 +17,7 @@ */ package org.connectorio.addons.binding.fatek.internal.channel; +import java.util.List; import org.openhab.core.thing.ChannelUID; import org.openhab.core.types.Command; import org.openhab.core.types.State; @@ -27,12 +28,12 @@ public interface FatekChannelHandler { // identification - Reg register(); + List registers(); ChannelUID channel(); // write FatekCommand prepareWrite(Command command); // fetch - State state(RegValue value); + State state(List value); } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/RollerShutterChannelHandler.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/RollerShutterChannelHandler.java index 7011d373..b0ea4573 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/RollerShutterChannelHandler.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/RollerShutterChannelHandler.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2023 ConnectorIO Sp. z o.o. + * Copyright (C) 2024-2024 ConnectorIO Sp. z o.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,8 @@ */ package org.connectorio.addons.binding.fatek.internal.channel; +import java.util.Collections; +import java.util.List; import java.util.Map; import org.connectorio.addons.binding.fatek.internal.channel.converter.Converter; import org.openhab.core.library.types.OnOffType; @@ -34,10 +36,13 @@ import org.simplify4u.jfatek.registers.Reg; import org.simplify4u.jfatek.registers.RegValue; import org.simplify4u.jfatek.registers.RegValueData; -import org.simplify4u.jfatek.registers.RegValueDis; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class RollerShutterChannelHandler implements FatekChannelHandler { + private final Logger logger = LoggerFactory.getLogger(RollerShutterChannelHandler.class); + private final ChannelUID channel; private final DataReg register; private final DisReg startReg; @@ -58,8 +63,8 @@ public RollerShutterChannelHandler(Channel channel, DataReg register, DisReg sta } @Override - public Reg register() { - return register; + public List registers() { + return Collections.singletonList(register); } @Override @@ -74,23 +79,21 @@ public FatekCommand prepareWrite(Command command) { return new FatekWriteDataCmd(null, register, (RegValueData) positionValue); } - if (command == UpDownType.UP) { + if (UpDownType.UP.equals(command)) { RegValue start = startConverter.toValue(OnOffType.ON); RegValue stop = stopConverter.toValue(OnOffType.OFF); return new FatekWriteMixDataCmd(null, Map.of( startReg, start, stopReg, stop )); - } - if (command == UpDownType.DOWN) { + } else if (UpDownType.DOWN.equals(command)) { RegValue start = startConverter.toValue(OnOffType.OFF); RegValue stop = stopConverter.toValue(OnOffType.ON); return new FatekWriteMixDataCmd(null, Map.of( startReg, start, stopReg, stop )); - } - if (command == StopMoveType.STOP) { + } else if (StopMoveType.STOP.equals(command)) { RegValue start = startConverter.toValue(OnOffType.OFF); RegValue stop = stopConverter.toValue(OnOffType.OFF); return new FatekWriteMixDataCmd(null, Map.of( @@ -99,12 +102,14 @@ public FatekCommand prepareWrite(Command command) { )); } + logger.warn("Unsupported command {} received by rollershutter channel {}", command, channel); + return null; } @Override - public State state(RegValue value) { - return positionConverter.toState(value); + public State state(List value) { + return positionConverter.toState(value.get(0)); } } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/ColorConverter.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/ColorConverter.java new file mode 100644 index 00000000..75593ed6 --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/ColorConverter.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2024-2024 ConnectorIO Sp. z o.o. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.connectorio.addons.binding.fatek.internal.channel.converter; + +import java.util.Arrays; +import java.util.List; +import org.openhab.core.library.types.DecimalType; +import org.openhab.core.library.types.HSBType; +import org.openhab.core.library.types.PercentType; +import org.openhab.core.types.State; +import org.simplify4u.jfatek.registers.Reg; +import org.simplify4u.jfatek.registers.RegValue; + +public class ColorConverter { + + private final Reg color1; + private final Reg color2; + private final Reg color3; + private final boolean rgb; + + public ColorConverter(Reg color1, Reg color2, Reg color3, boolean rgb) { + this.color1 = color1; + this.color2 = color2; + this.color3 = color3; + this.rgb = rgb; + } + + public List toValue(HSBType command) { + if (rgb) { + PercentType[] rgb = command.toRGB(); + return Arrays.asList( + RegValue.getForReg(color1, rgb[0].toBigDecimal().intValue()), + RegValue.getForReg(color2, rgb[1].toBigDecimal().intValue()), + RegValue.getForReg(color3, rgb[2].toBigDecimal().intValue()) + ); + } + return Arrays.asList( + RegValue.getForReg(color1, command.getHue().intValue()), + RegValue.getForReg(color2, command.getSaturation().intValue()), + RegValue.getForReg(color3, command.getBrightness().intValue()) + ); + } + + public State toState(RegValue value1, RegValue value2, RegValue value3) { + if (rgb) { + return HSBType.fromRGB( + Math.min(value1.intValueUnsigned(), 255), + Math.min(value2.intValueUnsigned(), 255), + Math.min(value3.intValueUnsigned(), 255) + ); + } + return new HSBType( + new DecimalType(Math.min(value1.intValueUnsigned(), 360)), + new PercentType(Math.min(value2.intValueUnsigned(), 100)), + new PercentType(Math.min(value3.intValueUnsigned(), 100)) + ); + } +} diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Converter.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Converter.java index 6c13ac7a..be7a334b 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Converter.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Converter.java @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2024-2024 ConnectorIO Sp. z o.o. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + */ package org.connectorio.addons.binding.fatek.internal.channel.converter; import org.openhab.core.types.Command; diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Data16Converter.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Data16Converter.java index 72826704..974e941c 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Data16Converter.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Data16Converter.java @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2024-2024 ConnectorIO Sp. z o.o. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + */ package org.connectorio.addons.binding.fatek.internal.channel.converter; import java.math.BigInteger; diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Data32Converter.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Data32Converter.java index ffd30745..2a6d12be 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Data32Converter.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/Data32Converter.java @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2024-2024 ConnectorIO Sp. z o.o. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + */ package org.connectorio.addons.binding.fatek.internal.channel.converter; import java.math.BigInteger; diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/DiscreteConverter.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/DiscreteConverter.java index 6256fea9..832611e5 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/DiscreteConverter.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/channel/converter/DiscreteConverter.java @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2024-2024 ConnectorIO Sp. z o.o. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + */ package org.connectorio.addons.binding.fatek.internal.channel.converter; import org.connectorio.addons.binding.fatek.config.channel.binary.DiscreteChannelConfig; diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/handler/FatekPlcThingHandler.java b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/handler/FatekPlcThingHandler.java index a72350fb..bd05d8dd 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/handler/FatekPlcThingHandler.java +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/java/org/connectorio/addons/binding/fatek/internal/handler/FatekPlcThingHandler.java @@ -17,8 +17,12 @@ */ package org.connectorio.addons.binding.fatek.internal.handler; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; @@ -40,10 +44,6 @@ import org.openhab.core.types.Type; import org.simplify4u.jfatek.FatekCommand; import org.simplify4u.jfatek.FatekReadMixDataCmd; -import org.simplify4u.jfatek.FatekWriteDataCmd; -import org.simplify4u.jfatek.FatekWriteDiscreteCmd; -import org.simplify4u.jfatek.registers.DataReg; -import org.simplify4u.jfatek.registers.DisReg; import org.simplify4u.jfatek.registers.Reg; import org.simplify4u.jfatek.registers.RegValue; import org.slf4j.Logger; @@ -93,7 +93,9 @@ public void initialize() { FatekChannelHandler handler = channelHandlerFactory.create(channel); if (handler != null) { handlerMap.put(channel.getUID(), handler); - registerMap.put(handler.register(), handler); + for (Reg register : handler.registers()) { + registerMap.put(register, handler); + } } } Long interval = getThingConfig().map(cfg -> cfg.refreshInterval) @@ -109,14 +111,13 @@ public void initialize() { public void handleCommand(ChannelUID channelUID, Command command) { if (handlerMap.containsKey(channelUID)) { FatekChannelHandler handler = handlerMap.get(channelUID); - Reg register = handler.register(); - FatekCommand fatekCmd = null; //handler.prepareWrite(command); - if (fatekCmd == null) { + FatekCommand cmd = handler.prepareWrite(command); + if (cmd == null) { logger.warn("Could not map command {} from channel {} to value supported by PLC. Ignoring this write.", command, channelUID); return; } - write(channelUID, command, fatekCmd); + write(channelUID, command, cmd); } } @@ -152,18 +153,32 @@ protected Long getDefaultPollingInterval() { @Override public void run() { - connection.execute(stationNumber, new FatekReadMixDataCmd(null, registerMap.keySet())).whenComplete((r, e) -> { - if (e != null) { - logger.warn("Failed to retrieve data", e); + connection.execute(stationNumber, new FatekReadMixDataCmd(null, registerMap.keySet())).whenComplete((result, error) -> { + if (error != null) { + logger.warn("Failed to retrieve data", error); return; } - for (Entry entry : r.entrySet()) { - FatekChannelHandler handler = registerMap.get(entry.getKey()); - State state = handler.state(entry.getValue()); + callChannelHandlers(result); + }); + } + + private void callChannelHandlers(Map result) { + Set called = new HashSet<>(); + for (Entry entry : result.entrySet()) { + FatekChannelHandler handler = registerMap.get(entry.getKey()); + if (!called.contains(handler)) { + // handler have not been called yet, lets collect its registers and let it parse data + List values = new ArrayList<>(); + for (Reg register : handler.registers()) { + values.add(result.get(register)); + } + // convert values to OH state + State state = handler.state(values); if (state != null) { update(handler.channel(), state); } + called.add(handler); } - }); + } } } diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/color16-channel-config.xml b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/color16-channel-config.xml new file mode 100644 index 00000000..0b6c0be6 --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/color16-channel-config.xml @@ -0,0 +1,142 @@ + + + + + + + + Additional settings used for switching target source. + + + + + + Data register used to retrieve or write Red or Hue part of color information. + + + + + + + + + + + + + + + + + + + + Index of register used to retrieve data. + 0 + + + + + + Data register used to retrieve or write Green or Saturation part of color information. + + + + + + + + + + + + + + + + + + + + Index of register used to retrieve data. + 0 + + + + + + Data register used to retrieve or write Blue or Brightness part of color information. + + + + + + + + + + + + + + + + + + + + Index of register used to retrieve data. + 0 + + + + + Write and interpret read data as RGB code instead of HSB. + false + + + + + + Type of discrete register to write ON or OFF command. + + + + + + + + + + + + Index of register used to write data. + 0 + + + + Invert logical representation of related register (0->1, 1->0). + false + + + + diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/color32-channel-config.xml b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/color32-channel-config.xml new file mode 100644 index 00000000..cccb4e3a --- /dev/null +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/config/color32-channel-config.xml @@ -0,0 +1,142 @@ + + + + + + + + Additional settings used for switching target source. + + + + + + Data register used to retrieve or write Red or Hue part of color information. + + + + + + + + + + + + + + + + + + + + Index of register used to retrieve data. + 0 + + + + + + Data register used to retrieve or write Green or Saturation part of color information. + + + + + + + + + + + + + + + + + + + + Index of register used to retrieve data. + 0 + + + + + + Data register used to retrieve or write Blue or Brightness part of color information. + + + + + + + + + + + + + + + + + + + + Index of register used to retrieve data. + 0 + + + + + Write and interpret read data as RGB code instead of HSB. + false + + + + + + Type of discrete register to write ON or OFF command. + + + + + + + + + + + + Index of register used to write data. + 0 + + + + Invert logical representation of related register (0->1, 1->0). + false + + + + diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/channels.xml b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/channels.xml index 61551972..f580517f 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/channels.xml +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/channels.xml @@ -54,4 +54,17 @@ + + Color + + Data register for position will use 16 bit registers. + + + + Color + + Data register for position will use 32 bit registers. + + + \ No newline at end of file diff --git a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/thing.xml b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/thing.xml index 8d96bf85..4016efd5 100644 --- a/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/thing.xml +++ b/bundles/org.connectorio.addons.binding.fatek/src/main/resources/OH-INF/thing/thing.xml @@ -23,7 +23,7 @@ https://openhab.org/schemas/thing-description-1.0.0.xsd "> - +