Skip to content

Commit

Permalink
remove raw value from presenter, forward actual json
Browse files Browse the repository at this point in the history
  • Loading branch information
timcowlishaw committed Jan 12, 2025
1 parent 681fcd4 commit 508d89e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/jobs/mqtt_forwarding_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def perform(device_id, data)
private

def payload_for(device, readings)
Presenters.present(device, device.owner, nil, readings: readings)
Presenters.present(device, device.owner, nil, readings: readings).to_json
end

def mqtt_client
Expand Down
5 changes: 1 addition & 4 deletions app/lib/presenters/component_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ def readings
def format_reading(reading)
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
{ timestamp: timestamp, value: value } if value
end
end
end
8 changes: 7 additions & 1 deletion spec/jobs/mqtt_forwarding_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
double(:device_json)
}

let(:device_representation) {
double(:device_representation).tap do |device_representation|
allow(device_representation).to receive(:to_json).and_return(device_json)
end
}

let(:forwarder) {
double(:forwarder).tap do |forwarder|
allow(forwarder).to receive(:forward_readings)
Expand All @@ -26,7 +32,7 @@

before do
allow(MQTTClientFactory).to receive(:create_client).and_return(mqtt_client)
allow(Presenters).to receive(:present).and_return(device_json)
allow(Presenters).to receive(:present).and_return(device_representation)
allow(MQTTForwarder).to receive(:new).and_return(forwarder)
allow_any_instance_of(Device).to receive(:forwarding_token).and_return(forwarding_token)
end
Expand Down
6 changes: 2 additions & 4 deletions spec/presenters/component_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,22 @@
context "when readings are supplied" do
let(:reading_timestamp) { Time.now - 6.hours }
let(:reading_value) { 1234.1 }
let(:reading_raw_value) { 2468.1 }
let(:options) {
{
readings: [
#TODO this particular reading format needs to be refactored out
{
"" => reading_timestamp,
"#{component.sensor_id}" => reading_value,
"#{component.sensor_id}_raw" => reading_raw_value,
"#{component.sensor_id + 1}" => 54321.0
}
]
}
}

it "returns the readings formatted with timestamp, value and raw_value" do
it "returns the readings formatted with timestamp and value" do
expect(presenter.as_json[:readings]).to eq([
{ timestamp: reading_timestamp, value: reading_value, raw_value: reading_raw_value }
{ timestamp: reading_timestamp, value: reading_value }
])
end
end
Expand Down

0 comments on commit 508d89e

Please sign in to comment.