Skip to content

Commit

Permalink
forward rendered device represenatation (after save) rather than raw …
Browse files Browse the repository at this point in the history
…message
  • Loading branch information
timcowlishaw committed May 6, 2024
1 parent 6b5363e commit 9457093
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/controllers/v0/readings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def legacy_create

if request.headers['X-SmartCitizenData']
MQTTClientFactory.create_client({clean_session: true, client_id: nil}) do |mqtt_client|
storer = RawStorer.new(mqtt_client)
storer = RawStorer.new(mqtt_client, self)
JSON.parse(request.headers['X-SmartCitizenData']).each do |raw_reading|
mac = request.headers['X-SmartCitizenMacADDR']
version = request.headers['X-SmartCitizenVersion']
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/send_to_datastore_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def perform(data_param, device_id)
end

def storer
@storer ||= Storer.new(mqtt_client)
@storer ||= Storer.new(mqtt_client, ActionController::Base.new.view_context)
end

def mqtt_client
Expand Down
2 changes: 1 addition & 1 deletion app/lib/mqtt_messages_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,6 @@ def data(message)


def storer
@storer ||= Storer.new(mqtt_client)
@storer ||= Storer.new(mqtt_client, ActionController::Base.new.view_context)
end
end
18 changes: 17 additions & 1 deletion app/models/concerns/message_forwarding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@ module MessageForwarding

def forward_reading(device, reading)
forwarder = MQTTForwarder.new(mqtt_client)
forwarder.forward_reading(device.forwarding_token, device.id, reading) if device.forward_readings?
payload = payload_for(device, reading)
forwarder.forward_reading(device.forwarding_token, device.id, payload) if device.forward_readings?
end

def payload_for(device, reading)
renderer.render(
partial: "v0/devices/device",
locals: {
device: device.reload,
current_user: nil,
slim_owner: true
}
)
end

private

def mqtt_client
raise NotImplementedError
end

def renderer
raise NotImplementedError
end
end
7 changes: 4 additions & 3 deletions app/models/raw_storer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
class RawStorer
include MessageForwarding

def initialize(mqtt_client)
def initialize(mqtt_client, renderer)
@mqtt_client = mqtt_client
@renderer = renderer
end

def store data, mac, version, ip, raise_errors=false
Expand Down Expand Up @@ -75,14 +76,14 @@ def store data, mac, version, ip, raise_errors=false

if !Rails.env.test? and device
begin
Redis.current.publish("data-received", ActionController::Base.new.view_context.render( partial: "v0/devices/device", locals: {device: @device, current_user: nil}))
Redis.current.publish("data-received", renderer.render( partial: "v0/devices/device", locals: {device: @device, current_user: nil}))
rescue
end
end
end

private

attr_reader :mqtt_client
attr_reader :mqtt_client, :renderer

end
7 changes: 4 additions & 3 deletions app/models/storer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ class Storer
include DataParser::Storer
include MessageForwarding

def initialize(mqtt_client)
def initialize(mqtt_client, renderer)
@mqtt_client = mqtt_client
@renderer = renderer
end

def store device, reading, do_update = true
Expand Down Expand Up @@ -51,13 +52,13 @@ def kairos_publish(reading_data)
def ws_publish(device)
return if Rails.env.test? or device.blank?
begin
Redis.current.publish("data-received", ActionController::Base.new.view_context.render( partial: "v0/devices/device", locals: {device: device, current_user: nil}))
Redis.current.publish("data-received", renderer.render( partial: "v0/devices/device", locals: {device: device, current_user: nil}))
rescue
end
end

private

attr_reader :mqtt_client
attr_reader :mqtt_client, :renderer

end
22 changes: 18 additions & 4 deletions spec/models/raw_storer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ def to_ts(time)
end
}

let(:renderer) {
# TODO: refactor these tests so they don't depend on the actual rendering,
# then replace this with a mock, to reduce brittleness.
ActionController::Base.new.view_context
}

subject(:storer) {
RawStorer.new(mqtt_client)
RawStorer.new(mqtt_client, renderer)
}

let(:json) {
Expand Down Expand Up @@ -96,15 +102,23 @@ def to_ts(time)
end

context "when the device allows forwarding" do
# TODO Tim Refactor this now you're passing in the MQTT client
let(:device_json) {
double(:device_json)
}

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

it "forwards the message with the forwarding token and the device's id" do
forwarding_token = double(:forwarding_token)
allow_any_instance_of(Device).to receive(:forwarding_token).and_return(forwarding_token)
allow_any_instance_of(Device).to receive(:forward_readings?).and_return(true)

forwarder = double(:mqtt_forwarder)
allow(MQTTForwarder).to receive(:new).and_return(forwarder)
expect(forwarder).to receive(:forward_reading).with(forwarding_token, device.id, 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
end
Expand Down
19 changes: 15 additions & 4 deletions spec/models/storer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
end
}

let(:renderer) {
double(:renderer).tap do |renderer|
allow(renderer).to receive(:render)
end
}

subject(:storer) {
Storer.new(mqtt_client)
Storer.new(mqtt_client, renderer)
}

context 'when receiving good data' do
Expand Down Expand Up @@ -80,14 +86,19 @@
end

context "when the device allows forwarding" do
# TODO tim refactor this now you're injecting the MQTT client

let(:device_json) {
double(:device_json)
}

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)
forwarder = double(:mqtt_forwarder)
allow(renderer).to receive(:render).and_return(device_json)
allow(MQTTForwarder).to receive(:new).and_return(forwarder)
expect(forwarder).to receive(:forward_reading).with(forwarding_token, device.id, @data)
expect(forwarder).to receive(:forward_reading).with(forwarding_token, device.id, device_json)
storer.store(device, @data)
end
end
Expand Down

0 comments on commit 9457093

Please sign in to comment.