ruuvi_decoder 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a5a5c8e7c19d0f0baa600ff7c8c27e72c7dba7540de5bd40f048dd523f191c37
4
+ data.tar.gz: 7db878cfe01d36f8e00c673b12955dc564d5d45e57447593bb7037620aafc564
5
+ SHA512:
6
+ metadata.gz: 8a07a24f94e91ac1669a5801a8a553b48fb5426ea4333fa984ab5c532be568acaad62c4be4ed1ab0633777b7578a7135a2a090fd7c8989dc04c63c279b86f35d
7
+ data.tar.gz: 43dc03770ad562a31f0feafdb57d1d07f84587780af1bad06b4e4ac143200a43401b560f7cb2b1d1ea1b148e463e7dcede7d56f42163c3ebb06aa04afdc7f03d
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,18 @@
1
+ plugins:
2
+ - rubocop-rspec
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 3.1
6
+ NewCops: enable
7
+
8
+ RSpec/NamedSubject:
9
+ Enabled: false
10
+
11
+ RSpec/ExampleLength:
12
+ Enabled: false
13
+
14
+ RSpec/MultipleExpectations:
15
+ Enabled: false
16
+
17
+ Metrics/MethodLength:
18
+ Max: 20
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-03-29
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Simo Leone
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # RuuviDecoder
2
+ Decode data from Ruuvi sensors.
3
+
4
+ ## Installation
5
+
6
+ Install the gem and add to the application's Gemfile by executing:
7
+
8
+ ```bash
9
+ bundle add ruuvi_decoder
10
+ ```
11
+
12
+ If bundler is not being used to manage dependencies, install the gem by executing:
13
+
14
+ ```bash
15
+ gem install ruuvi_decoder
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ The easiest way to use this library is to call `RuuviDecoder.decode` with an array of bytes or
21
+ a binary-encoded string (from a bluetooth advertisement's vendor data). It will automatically detect the data
22
+ format and return an appropriate instance of a decoder class. If you know the data format
23
+ in advance you can also directly instantiate a decoder class such as `RuuviDecoder::V5Data`
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruuvi_decoder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/ruuvi_decoder/blob/main/CODE_OF_CONDUCT.md).
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuuviDecoder
4
+ # Decoder for V5 formatted data.
5
+ # https://docs.ruuvi.com/communication/bluetooth-advertisements/data-format-5-rawv2
6
+ class V5Data
7
+ MANUFACTURER_ID = 0x0499
8
+ DATA_LENGTH_BYTES = 24
9
+ VERSION_TAG = 5
10
+
11
+ def initialize(raw_data)
12
+ @raw_data = RuuviDecoder.normalize_raw_data(raw_data)
13
+
14
+ raise ArgumentError, 'data is not valid v5 data' unless self.class.detect(@raw_data)
15
+ end
16
+
17
+ def self.detect(raw_data)
18
+ raw_data.size == DATA_LENGTH_BYTES && raw_data[0] == VERSION_TAG
19
+ end
20
+
21
+ def mac_address
22
+ return @mac_address if defined?(@mac_address)
23
+
24
+ @mac_address =
25
+ begin
26
+ bytes = raw_data[18..24]
27
+ if bytes.all?(0xff)
28
+ nil
29
+ else
30
+ bytes.map { |b| format('%02x', b) }.join(':')
31
+ end
32
+ end
33
+ end
34
+
35
+ def sequence_number
36
+ return @sequence_number if defined?(@sequence_number)
37
+
38
+ @sequence_number = decode_16_bits_unsigned(raw_data[16..17], multiplier: 1, invalid: 65_535)
39
+ end
40
+
41
+ def movement_counter
42
+ return @movement_counter if defined?(@movement_counter)
43
+
44
+ @movement_counter = decode_16_bits_unsigned([0, raw_data[15]], multiplier: 1, invalid: 255)
45
+ end
46
+
47
+ def tx_power_dbm
48
+ return @tx_power_dbm if defined?(@tx_power_dbm)
49
+
50
+ @tx_power_dbm = decode_bitmasked_unsigned(raw_power_info, 0x001F, offset: -20, multiplier: 2, invalid: 31)
51
+ end
52
+
53
+ def battery_v
54
+ return @battery_v if defined?(@battery_v)
55
+
56
+ @battery_v = decode_bitmasked_unsigned(raw_power_info >> 5, 0x07FF, offset: 1600, multiplier: 0.001,
57
+ invalid: 2047)
58
+ end
59
+
60
+ def acceleration_x_g
61
+ return @acceleration_x_g if defined?(@acceleration_x_g)
62
+
63
+ @acceleration_x_g = decode_16_bits_signed(raw_data[7..8], invalid: 0x8000, multiplier: 0.001)
64
+ end
65
+
66
+ def acceleration_y_g
67
+ return @acceleration_y_g if defined?(@acceleration_y_g)
68
+
69
+ @acceleration_y_g = decode_16_bits_signed(raw_data[9..10], invalid: 0x8000, multiplier: 0.001)
70
+ end
71
+
72
+ def acceleration_z_g
73
+ return @acceleration_z_g if defined?(@acceleration_z_g)
74
+
75
+ @acceleration_z_g = decode_16_bits_signed(raw_data[11..12], invalid: 0x8000, multiplier: 0.001)
76
+ end
77
+
78
+ def pressure_hpa
79
+ return @pressure_hpa if defined?(@pressure_hpa)
80
+
81
+ @pressure_hpa = decode_16_bits_unsigned(raw_data[5..6], offset: 50_000, invalid: 65_535, multiplier: 0.01)
82
+ end
83
+
84
+ def humidity_pct
85
+ return @humidity_pct if defined?(@humidity_pct)
86
+
87
+ @humidity_pct = decode_16_bits_unsigned(raw_data[3..4], multiplier: 0.0025, invalid: 65_535)
88
+ end
89
+
90
+ def temperature_c
91
+ return @temperature_c if defined?(@temperature_c)
92
+
93
+ @temperature_c = decode_16_bits_signed(raw_data[1..2], multiplier: 0.005, invalid: 0x8000)
94
+ end
95
+
96
+ def inspect
97
+ <<~INSPECT.chomp
98
+ <#{self.class.name}
99
+ temperature: #{temperature_c} C,
100
+ humidity: #{humidity_pct} %,
101
+ pressure: #{pressure_hpa} hPa,
102
+ acceleration_x: #{acceleration_x_g} G,
103
+ acceleration_y: #{acceleration_y_g} G,
104
+ acceleration_z: #{acceleration_z_g} G,
105
+ battery: #{battery_v} V,
106
+ tx_power: #{tx_power_dbm} dBm,
107
+ movement_counter: #{movement_counter},
108
+ sequence_number: #{sequence_number},
109
+ mac_address: #{mac_address}
110
+ >
111
+ INSPECT
112
+ end
113
+
114
+ private
115
+
116
+ attr_reader :raw_data
117
+
118
+ def raw_power_info
119
+ @raw_power_info = decode_16_bits_unsigned(raw_data[13..14])
120
+ end
121
+
122
+ def decode_16_bits_signed(bytes, multiplier: 1, invalid: nil, offset: 0)
123
+ return if !invalid.nil? && bytes == [invalid].pack('s>').bytes
124
+
125
+ signed_int = bytes.pack('C*').unpack1('s>')
126
+ (signed_int + offset) * multiplier
127
+ end
128
+
129
+ def decode_16_bits_unsigned(bytes, multiplier: 1, invalid: nil, offset: 0)
130
+ return if !invalid.nil? && bytes == [invalid].pack('S>').bytes
131
+
132
+ signed_int = bytes.pack('C*').unpack1('S>')
133
+ (signed_int + offset) * multiplier
134
+ end
135
+
136
+ def decode_bitmasked_unsigned(value, mask, multiplier: 1, invalid: nil, offset: 0)
137
+ value &= mask
138
+ return if !invalid.nil? && value == invalid
139
+
140
+ (value + offset) * multiplier
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuuviDecoder
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'ruuvi_decoder/version'
4
+ require_relative 'ruuvi_decoder/v5_data'
5
+
6
+ # Decoders for various binary data formats emitted by Ruuvi bluetooth sensors.
7
+ # Use classes directly or the static decode method to automatically select one based
8
+ # on the version tag.
9
+ module RuuviDecoder
10
+ def self.decode(raw_data)
11
+ raw_data = normalize_raw_data(raw_data)
12
+
13
+ # TODO(simo): implement more formats :)
14
+ decoder_class = [V5Data].find { |data_format| data_format.detect(raw_data) }
15
+ raise 'no decoder found' if decoder_class.nil?
16
+
17
+ decoder_class.new(raw_data)
18
+ end
19
+
20
+ def self.normalize_raw_data(raw_data)
21
+ case raw_data
22
+ when Enumerable
23
+ raw_data
24
+ when String
25
+ unless raw_data.encoding == Encoding::ASCII_8BIT
26
+ raise ArgumentError,
27
+ 'raw_data must ASCII_8BIT (binary) encoded'
28
+ end
29
+
30
+ raw_data.bytes
31
+ else
32
+ raise ArgumentError, "raw_data must be an Enumerable of bytes or a binary-encoded String, got #{raw_data.class}"
33
+ end
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruuvi_decoder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Simo Leone
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2025-03-30 00:00:00.000000000 Z
11
+ dependencies: []
12
+ email:
13
+ - simo.leone@iki.fi
14
+ executables: []
15
+ extensions: []
16
+ extra_rdoc_files: []
17
+ files:
18
+ - ".rspec"
19
+ - ".rubocop.yml"
20
+ - CHANGELOG.md
21
+ - LICENSE.txt
22
+ - README.md
23
+ - Rakefile
24
+ - lib/ruuvi_decoder.rb
25
+ - lib/ruuvi_decoder/v5_data.rb
26
+ - lib/ruuvi_decoder/version.rb
27
+ homepage: https://github.com/simoleone/ruuvi_decoder
28
+ licenses:
29
+ - MIT
30
+ metadata:
31
+ homepage_uri: https://github.com/simoleone/ruuvi_decoder
32
+ rubygems_mfa_required: 'true'
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.1.0
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.6.2
48
+ specification_version: 4
49
+ summary: Decoder for Ruuvi sensor bluetooth data packets.
50
+ test_files: []