diff --git a/app/lib/mqtt_messages_handler.rb b/app/lib/mqtt_messages_handler.rb index feb4006d..f75dccad 100644 --- a/app/lib/mqtt_messages_handler.rb +++ b/app/lib/mqtt_messages_handler.rb @@ -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) @@ -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 @@ -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 diff --git a/spec/lib/mqtt_messages_handler_spec.rb b/spec/lib/mqtt_messages_handler_spec.rb index f6e0dbb4..e6fcab7e 100644 --- a/spec/lib/mqtt_messages_handler_spec.rb +++ b/spec/lib/mqtt_messages_handler_spec.rb @@ -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"}' @@ -86,7 +90,7 @@ 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 @@ -94,7 +98,7 @@ 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 @@ -102,8 +106,19 @@ 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