Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor mqtt handler #162

Merged
merged 3 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 18 additions & 35 deletions app/lib/mqtt_messages_handler.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
class MqttMessagesHandler
def self.handle(packet)
handle_topic(packet.topic, packet.payload)
end
topic = packet.topic
message = packet.payload
return if topic.nil?

# handle_inventory is the only one that does NOT need a device
if topic.to_s.include?('inventory')
DeviceInventory.create({report: (message rescue nil)})
end

device = Device.find_by(device_token: self.device_token(topic))
return if device.nil?

def self.handle_topic(topic, message)
if topic.to_s.include?('readings')
self.handle_readings(topic, message)
self.handle_readings(device, message)
elsif topic.to_s.include?('hello')
self.handle_hello(topic, message)
self.handle_hello(device, message)
elsif topic.to_s.include?('info')
self.handle_hardware_info(topic, message)
else
self.handle_inventory(topic, message)
device.update hardware_info: JSON.parse(message)
end
end

# takes a packet and stores data
def self.handle_readings(topic, message)
device = Device.find_by(device_token: self.device_token(topic))
raise "device not found #{topic}" if device.nil?

def self.handle_readings(device, message)
data = self.data(message)
data.each do |reading|
Storer.new(device, reading)
Expand All @@ -30,20 +33,11 @@ def self.handle_readings(topic, message)
#puts message
end

def self.handle_hello(topic, message)
def self.handle_hello(device, message)
payload = {}
device_token = self.device_token(topic)
payload[:device_id] = device.id

return if device_token.blank?

device = Device.find_by(device_token: device_token)
if device.present?
payload[:device_id] = device.id
end

payload[:device_token] = device_token # TODO: remove after migration

orphan_device = OrphanDevice.find_by(device_token: device_token)
orphan_device = OrphanDevice.find_by(device_token: device.device_token)
if orphan_device
orphan_device.update(device_handshake: true)
payload[:onboarding_session] = orphan_device.onboarding_session
Expand All @@ -52,17 +46,6 @@ def self.handle_hello(topic, message)
Redis.current.publish('token-received', payload.to_json)
end

def self.handle_hardware_info(topic, message)
device_token = self.device_token(topic)
device = Device.find_by(device_token: device_token)
return if device.blank?
device.update hardware_info: JSON.parse(message)
end

def self.handle_inventory(topic, message)
DeviceInventory.create({report: (message rescue nil)})
end

# takes a packet and returns 'device token' from topic
def self.device_token(topic)
topic[/device\/sck\/(.*?)\//m, 1].to_s
Expand Down
19 changes: 17 additions & 2 deletions spec/lib/mqtt_messages_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
payload: '{"random_property":"random_result"}'
)

@inventory_packet_without_topic = MQTT::Packet::Publish.new(
payload: '{"random_property":"random_result2"}'
)

@hardware_info_packet = MQTT::Packet::Publish.new(
topic: "device/sck/#{device.device_token}/info",
payload: '{"id":48,"uuid":"7d45fead-defd-4482-bc6a-a1b711879e2d"}'
Expand Down Expand Up @@ -86,24 +90,35 @@
allow(Raven).to receive(:capture_exception)
expect(Kairos).not_to receive(:http_post_to)
MqttMessagesHandler.handle(@invalid_packet)
expect(Raven).to have_received(:capture_exception).with(RuntimeError)
#expect(Raven).to have_received(:capture_exception).with(RuntimeError)
end
end
end

describe '#hello' do
it 'logs device_token has been received' do
expect(Redis.current).to receive(:publish).with(
'token-received', { device_id: device.id, device_token: device.device_token }.to_json
'token-received', { device_id: device.id }.to_json
)
MqttMessagesHandler.handle(@hello_packet)
end
end

describe '#inventory' do
it 'logs inventory has been received' do
expect(DeviceInventory.count).to eq(0)
# This creates a new device_inventory item
expect(@inventory_packet.payload).to eq((device_inventory.report.to_json))
expect(DeviceInventory.count).to eq(1)
MqttMessagesHandler.handle(@inventory_packet)
expect(DeviceInventory.last.report["random_property"]).to eq('random_result')
expect(DeviceInventory.count).to eq(2)
end

it 'does not log inventory without a correct topic' do
expect(DeviceInventory.count).to eq(0)
MqttMessagesHandler.handle(@inventory_packet_without_topic)
expect(DeviceInventory.count).to eq(0)
end
end

Expand Down