You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2018/02/07 17:31:30 UTC

[incubator-plc4x] branch feature/Beckhoff_ADS_protocol updated: introduced UnsignedShortLEByteValue

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch feature/Beckhoff_ADS_protocol
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git


The following commit(s) were added to refs/heads/feature/Beckhoff_ADS_protocol by this push:
     new 5bfbcff  introduced UnsignedShortLEByteValue
5bfbcff is described below

commit 5bfbcff0566c728426865f65d0fb2fe831098108
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Wed Feb 7 18:31:25 2018 +0100

    introduced UnsignedShortLEByteValue
---
 .../java/ads/api/commands/types/ADSState.java      | 29 ++++++++++++----
 .../java/ads/api/commands/types/DeviceState.java   | 28 ++++++++++++----
 .../plc4x/java/ads/api/commands/types/Version.java | 28 ++++++++++++----
 .../UnsignedShortLEByteValue.java}                 | 39 +++++++++++++++++-----
 4 files changed, 97 insertions(+), 27 deletions(-)

diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/ADSState.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/ADSState.java
index 86e2354..f2536cb 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/ADSState.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/ADSState.java
@@ -19,23 +19,38 @@
 package org.apache.plc4x.java.ads.api.commands.types;
 
 import io.netty.buffer.ByteBuf;
-import org.apache.plc4x.java.ads.api.util.ByteValue;
+import org.apache.plc4x.java.ads.api.util.UnsignedShortLEByteValue;
 
-public class ADSState extends ByteValue {
+public class ADSState extends UnsignedShortLEByteValue {
 
-    public static final int NUM_BYTES = 2;
+    public static final int NUM_BYTES = UnsignedShortLEByteValue.NUM_BYTES;
 
-    ADSState(byte... values) {
+    protected ADSState(byte... values) {
         super(values);
-        assertLength(NUM_BYTES);
+    }
+
+    protected ADSState(int value) {
+        super(value);
+    }
+
+    protected ADSState(ByteBuf byteBuf) {
+        super(byteBuf);
     }
 
     public static ADSState of(byte... values) {
         return new ADSState(values);
     }
 
+    public static ADSState of(int value) {
+        checkUnsignedBounds(value, NUM_BYTES);
+        return new ADSState(value);
+    }
+
     public static ADSState of(ByteBuf byteBuf) {
-        // TODO: could be transformed to readUnsignedShortLE someday
-        return of(byteBuf.readBytes(NUM_BYTES).array());
+        return new ADSState(byteBuf);
+    }
+
+    public static ADSState of(String length) {
+        return of(Integer.parseInt(length));
     }
 }
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/DeviceState.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/DeviceState.java
index 5e8b64f..9fe6eee 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/DeviceState.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/DeviceState.java
@@ -19,22 +19,38 @@
 package org.apache.plc4x.java.ads.api.commands.types;
 
 import io.netty.buffer.ByteBuf;
-import org.apache.plc4x.java.ads.api.util.ByteValue;
+import org.apache.plc4x.java.ads.api.util.UnsignedShortLEByteValue;
 
-public class DeviceState extends ByteValue {
+public class DeviceState extends UnsignedShortLEByteValue {
 
-    public static final int NUM_BYTES = 2;
+    public static final int NUM_BYTES = UnsignedShortLEByteValue.NUM_BYTES;
 
-    DeviceState(byte... value) {
+    protected DeviceState(byte... values) {
+        super(values);
+    }
+
+    protected DeviceState(int value) {
         super(value);
-        assertLength(NUM_BYTES);
+    }
+
+    protected DeviceState(ByteBuf byteBuf) {
+        super(byteBuf);
     }
 
     public static DeviceState of(byte... values) {
         return new DeviceState(values);
     }
 
+    public static DeviceState of(int value) {
+        checkUnsignedBounds(value, NUM_BYTES);
+        return new DeviceState(value);
+    }
+
     public static DeviceState of(ByteBuf byteBuf) {
-        return of(byteBuf.readBytes(NUM_BYTES).array());
+        return new DeviceState(byteBuf);
+    }
+
+    public static DeviceState of(String length) {
+        return of(Integer.parseInt(length));
     }
 }
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/Version.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/Version.java
index 8518fdf..e1e4cf4 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/Version.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/Version.java
@@ -19,22 +19,38 @@
 package org.apache.plc4x.java.ads.api.commands.types;
 
 import io.netty.buffer.ByteBuf;
-import org.apache.plc4x.java.ads.api.util.ByteValue;
+import org.apache.plc4x.java.ads.api.util.UnsignedShortLEByteValue;
 
-public class Version extends ByteValue {
+public class Version extends UnsignedShortLEByteValue {
 
-    public static final int NUM_BYTES = 2;
+    public static final int NUM_BYTES = UnsignedShortLEByteValue.NUM_BYTES;
 
-    Version(byte... values) {
+    protected Version(byte... values) {
         super(values);
-        assertLength(NUM_BYTES);
+    }
+
+    protected Version(int value) {
+        super(value);
+    }
+
+    protected Version(ByteBuf byteBuf) {
+        super(byteBuf);
     }
 
     public static Version of(byte... values) {
         return new Version(values);
     }
 
+    public static Version of(int value) {
+        checkUnsignedBounds(value, NUM_BYTES);
+        return new Version(value);
+    }
+
     public static Version of(ByteBuf byteBuf) {
-        return of(byteBuf.readBytes(NUM_BYTES).array());
+        return new Version(byteBuf);
+    }
+
+    public static Version of(String length) {
+        return of(Integer.parseInt(length));
     }
 }
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/DeviceState.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/UnsignedShortLEByteValue.java
similarity index 50%
copy from plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/DeviceState.java
copy to plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/UnsignedShortLEByteValue.java
index 5e8b64f..89f2522 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/DeviceState.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/UnsignedShortLEByteValue.java
@@ -16,25 +16,48 @@
  specific language governing permissions and limitations
  under the License.
  */
-package org.apache.plc4x.java.ads.api.commands.types;
+package org.apache.plc4x.java.ads.api.util;
 
 import io.netty.buffer.ByteBuf;
-import org.apache.plc4x.java.ads.api.util.ByteValue;
 
-public class DeviceState extends ByteValue {
+import java.nio.ByteBuffer;
+
+public abstract class UnsignedShortLEByteValue extends ByteValue {
 
     public static final int NUM_BYTES = 2;
 
-    DeviceState(byte... value) {
+    protected final int intValue;
+
+    public UnsignedShortLEByteValue(byte... value) {
         super(value);
         assertLength(NUM_BYTES);
+        intValue = getBytes()[1] << 8 | getBytes()[0];
+    }
+
+    public UnsignedShortLEByteValue(int value) {
+        super(ofLong(value));
+        checkUnsignedBounds(value, NUM_BYTES);
+        intValue = value;
+    }
+
+    public UnsignedShortLEByteValue(ByteBuf byteBuf) {
+        this(byteBuf.readUnsignedShortLE());
+    }
+
+    protected static byte[] ofLong(long value) {
+        return ByteBuffer.allocate(NUM_BYTES)
+            // LE
+            .put((byte) (value & 0xff))
+            .put((byte) (value >> 8 & 0xff))
+            .array();
     }
 
-    public static DeviceState of(byte... values) {
-        return new DeviceState(values);
+    public int getAsInt() {
+        return intValue;
     }
 
-    public static DeviceState of(ByteBuf byteBuf) {
-        return of(byteBuf.readBytes(NUM_BYTES).array());
+    @Override
+    public String toString() {
+        return String.valueOf(getAsInt());
     }
 }

-- 
To stop receiving notification emails like this one, please contact
sruehl@apache.org.