Skip to content

Commit

Permalink
Prepare to store MAC address as tag to InfluxDB
Browse files Browse the repository at this point in the history
  • Loading branch information
ledermann committed Jan 17, 2025
1 parent d990b57 commit 2651bab
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 30 deletions.
2 changes: 2 additions & 0 deletions lib/flux_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def point(record)
name: influx_measurement,
time: record.time,
fields: record.to_hash,
# TODO: Add tags, so just ONE Measurement would be enough
# tags: { mac: record.mac }.compact,
)
end

Expand Down
9 changes: 8 additions & 1 deletion lib/shelly_response_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(json)
attr_reader :data

def solectrus_record(id: 1, response_duration: nil)
SolectrusRecord.new(id:, response_duration:, time:, payload: record_hash)
SolectrusRecord.new(id:, mac:, time:, payload: record_hash.merge(response_duration:))
end

private
Expand All @@ -22,6 +22,13 @@ def record_hash
}.compact
end

def mac
data.dig('sys', 'mac') ||
data['mac'] ||
device_status&.dig('mac') ||
device_status&.dig('sys', 'mac')
end

def time
data['unixtime'] ||
data.dig('sys', 'unixtime') ||
Expand Down
16 changes: 10 additions & 6 deletions lib/solectrus_record.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
class SolectrusRecord
def initialize(id:, time:, payload:, response_duration: nil)
def initialize(id:, time:, payload:, mac: nil, response_duration: nil)
@id = id
@time = time
@payload = payload
@mac = mac
@response_duration = response_duration
end

attr_reader :id, :time, :response_duration

def to_hash
@payload
end
attr_reader :id, :time, :mac

%i[
temp
power
power_a
power_b
power_c
response_duration
].each do |method|
define_method(method) do
@payload[method]
end
end

def to_hash
@payload.merge(
response_duration:,
).compact
end

def power?
!power.round.zero?
end
Expand Down
12 changes: 6 additions & 6 deletions spec/cassettes/influx-success.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions spec/cassettes/shelly-pro-3em.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions spec/lib/solectrus_record_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'solectrus_record'

describe SolectrusRecord do
subject(:record) { described_class.new(id: 1, response_duration: 20.5, time: Time.now, payload:) }
subject(:record) { described_class.new(id: 1, mac: 'deadbeef0001', time: Time.now, payload:) }

let(:payload) do
{
Expand All @@ -10,6 +10,7 @@
power_a: 10.2,
power_b: 20.5,
power_c: 30.3,
response_duration: 20.5,
}
end

Expand Down Expand Up @@ -42,13 +43,13 @@
end
end

describe '#response_duration' do
describe '#mac' do
it 'returns the value' do
expect(record.response_duration).to eq(20.5)
expect(record.mac).to eq('deadbeef0001')
end
end

%i[temp power power_a power_b power_c].each do |method|
%i[temp power power_a power_b power_c response_duration].each do |method|
describe "##{method}" do
it "returns the value of #{method} from the payload" do
expect(record.send(method)).to eq(payload[method])
Expand Down

0 comments on commit 2651bab

Please sign in to comment.