Skip to content

Commit

Permalink
use new device presentation for forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
timcowlishaw committed Jul 10, 2024
1 parent 0b743a4 commit 7cc1bec
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
18 changes: 7 additions & 11 deletions app/lib/presenters/component_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,19 @@ def previous_value
def readings
readings = options[:readings]
if readings
readings.flat_map { |reading| format_reading(reading) }.compact
readings.map { |reading| format_reading(reading) }.compact
end
end

private

def format_reading(reading)
# TODO sort out the mess of multiple reading formats used ini
# DataParser, RawStorer, etc, etc.
reading.data.map { |entry|
timestamp = entry.timestamp
value = entry.sensors&.find { |sensor|
sensor["id"] == component.sensor_id
}.dig("value")
{ timestamp: timestamp, value: value } if value
}.compact

timestamp = reading[""]
value = reading[component.sensor_id.to_s]
raw_value = reading["#{component.sensor_id}_raw"]
if value || raw_value
{ timestamp: timestamp, value: value, raw_value: raw_value }.compact
end
end
end
end
11 changes: 3 additions & 8 deletions app/models/concerns/message_forwarding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ def forward_reading(device, reading)
end

def payload_for(device, reading)
renderer.render(
partial: "v0/devices/device",
locals: {
device: device.reload,
current_user: nil,
slim_owner: true
}
)
# We pass the device.owner as the current_user here as the
# MQTT forwarding topic is private and authenticated.
Presenters.present(device, device.owner, renderer, readings: [reading])
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/models/raw_storer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def store data, mac, version, ip, raise_errors=false
device.update_columns(last_reading_at: parsed_ts, data: sql_data, state: 'has_published')
end

forward_reading(device, data)
forward_reading(device, sql_data)
rescue Exception => e

success = false
Expand Down
2 changes: 1 addition & 1 deletion app/models/storer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def store device, reading, do_update = true
update_device(device, parsed_reading[:parsed_ts], parsed_reading[:sql_data])
end

forward_reading(device, reading)
forward_reading(device, parsed_reading[:sql_data])

rescue Exception => e
Sentry.capture_exception(e)
Expand Down
1 change: 1 addition & 0 deletions spec/models/raw_storer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def to_ts(time)
allow_any_instance_of(Device).to receive(:forward_readings?).and_return(true)
forwarder = double(:mqtt_forwarder)
allow(MQTTForwarder).to receive(:new).and_return(forwarder)
allow(Presenters).to receive(:present).and_return(device_json)
expect(forwarder).to receive(:forward_reading).with(forwarding_token, device.id, device_json)
storer.store(json, device.mac_address, "1.1-0.9.0-A", "127.0.0.1", true)
end
Expand Down
15 changes: 14 additions & 1 deletion spec/models/storer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,25 @@
double(:device_json)
}

it "creates a presentation of the device for the authorized user, passing the readings" do

forwarding_token = double(:forwarding_token)
forwarder = double(:mqtt_forwarder)
allow(device).to receive(:forwarding_token).and_return(forwarding_token)
allow(device).to receive(:forward_readings?).and_return(true)
allow(MQTTForwarder).to receive(:new).and_return(forwarder)
allow(forwarder).to receive(:forward_reading)
# TODO update this to test the actual arguments passed once we've refactored the mess of different reading formats and parsers.
expect(Presenters).to receive(:present).and_return(device_json)
storer.store(device, @data)
end

it "forwards the message with the forwarding token and the device's id" do
forwarding_token = double(:forwarding_token)
forwarder = double(:mqtt_forwarder)
allow(device).to receive(:forwarding_token).and_return(forwarding_token)
allow(device).to receive(:forward_readings?).and_return(true)
allow(renderer).to receive(:render).and_return(device_json)
allow(Presenters).to receive(:present).and_return(device_json)
allow(MQTTForwarder).to receive(:new).and_return(forwarder)
expect(forwarder).to receive(:forward_reading).with(forwarding_token, device.id, device_json)
storer.store(device, @data)
Expand Down

0 comments on commit 7cc1bec

Please sign in to comment.