Skip to content

Commit

Permalink
MQTT: handle more devices not found
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorsmari committed Nov 7, 2019
1 parent 64ae871 commit 64fe335
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
14 changes: 5 additions & 9 deletions app/lib/mqtt_messages_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ def self.handle_readings(topic, message)
end
rescue Exception => e
Raven.capture_exception(e)
puts e.message
puts message
#Airbrake.notify(e, {payload: e.message + " - payload: " + message})
#puts e.inspect
#puts message
end

def self.handle_hello(topic, message)
Expand All @@ -55,12 +54,9 @@ def self.handle_hello(topic, message)

def self.handle_hardware_info(topic, message)
device_token = self.device_token(topic)
device = Device.where(device_token: device_token).first
if device.present?
device.update_attributes hardware_info: JSON.parse(message)
else
# Device with this device_token likely not found
end
device = Device.find_by(device_token: device_token)
return if device.blank?
device.update_attributes hardware_info: JSON.parse(message)
end

def self.handle_inventory(topic, message)
Expand Down
22 changes: 18 additions & 4 deletions spec/lib/mqtt_messages_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
topic: "device/sck/#{device.device_token}/info",
payload: '{"id":48,"uuid":"7d45fead-defd-4482-bc6a-a1b711879e2d"}'
)

@hardware_info_packet_bad = MQTT::Packet::Publish.new(
topic: "device/sck/BAD_TOPIC/info",
payload: '{"id":32,"uuid":"7d45fead-defd-4482-bc6a-a1b711879e2d"}'
)
end

describe '#device_token' do
Expand Down Expand Up @@ -78,12 +83,11 @@
end

context 'invalid packet' do
it 'it notifies Airbrake(has been removed)' do
it 'it notifies Raven' do
allow(Raven).to receive(:capture_exception)
expect(Kairos).not_to receive(:http_post_to)
#TODO: Do we need to disable all, after removing Airbrake?
#expect(Airbrake).to receive(:notify).with(RuntimeError, 'device not found - payload: '\
#'{"data": [{"recorded_at": "2016-06-08 10:30:00","sensors": [{"id": 1,"value": 21}]}]}')
MqttMessagesHandler.handle(@invalid_packet)
expect(Raven).to have_received(:capture_exception).with(RuntimeError)
end
end
end
Expand All @@ -106,9 +110,19 @@

describe '#hardware_info' do
it 'hardware info has been received and id changed from 47 -> 48' do
expect(device.hardware_info["id"]).to eq(47)
MqttMessagesHandler.handle(@hardware_info_packet)
device.reload
expect(device.hardware_info["id"]).to eq(48)
expect(@hardware_info_packet.payload).to eq((device.hardware_info.to_json))
end

it 'does not handle bad topic' do
expect(device.hardware_info["id"]).to eq(47)
MqttMessagesHandler.handle(@hardware_info_packet_bad)
device.reload
expect(device.hardware_info["id"]).to eq(47)
expect(@hardware_info_packet_bad.payload).to_not eq((device.hardware_info.to_json))
end
end
end

0 comments on commit 64fe335

Please sign in to comment.