Skip to content

Commit

Permalink
fix(knx): writing integer 0
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach committed Aug 12, 2024
1 parent 1b30eb2 commit 7f0af8b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: knx
version: 1.2.3
version: 1.2.4
crystal: ">= 0.36.1"

dependencies:
Expand Down
10 changes: 10 additions & 0 deletions spec/knx_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,14 @@ describe "knx protocol helper" do
cemi = input.read_bytes(KNX::CEMI)
cemi.to_slice.should eq cemi_raw
end

it "should encode / decode a Group Write 0" do
datagram = knx.action("4/1/7", 0, source: "0.0.0")
cemi_raw = "1100bce000002107010080".hexbytes
datagram.cemi.to_slice.should eq(cemi_raw)

input = IO::Memory.new(cemi_raw)
cemi = input.read_bytes(KNX::CEMI)
cemi.to_slice.should eq cemi_raw
end
end
22 changes: 13 additions & 9 deletions src/knx.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,21 @@ class KNX
when Bool
data ? Bytes[1] : Bytes[0]
when Int
io = IO::Memory.new
io.write_bytes(data, IO::ByteFormat::BigEndian)
if data.zero?
Bytes[0]
else
io = IO::Memory.new
io.write_bytes(data, IO::ByteFormat::BigEndian)

bytes = io.to_slice
index = 0
bytes.each_with_index do |byte, i|
next if byte == 0
index = i
break
bytes = io.to_slice
index = 0
bytes.each_with_index do |byte, i|
next if byte == 0
index = i
break
end
bytes[index..-1]
end
bytes[index..-1]
when Float, String, Time
KNX.datapoint(data).to_bytes
when Datapoint
Expand Down

0 comments on commit 7f0af8b

Please sign in to comment.