Skip to content

Commit

Permalink
handshake devices on any mqtt message if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
timcowlishaw committed Dec 13, 2023
1 parent 7e01247 commit b55177c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
19 changes: 9 additions & 10 deletions app/lib/mqtt_messages_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ def self.handle_topic(topic, message)

return if topic.nil?

handshake_device(topic)

# The following do NOT need a device
if topic.to_s.include?('inventory')
DeviceInventory.create({ report: (message rescue nil) })
elsif topic.to_s.include?('hello')
orphan_device = OrphanDevice.find_by(device_token: device_token(topic))
return if orphan_device.nil?

handle_hello(orphan_device)
end

device = Device.find_by(device_token: device_token(topic))
Expand Down Expand Up @@ -89,11 +86,13 @@ def self.parse_raw_readings(message, device_id=nil)
JSON[reading]
end

def self.handle_hello(orphan_device)
payload = {}
orphan_device.update(device_handshake: true)
payload[:onboarding_session] = orphan_device.onboarding_session
Redis.current.publish('token-received', payload.to_json)
def self.handshake_device(topic)
orphan_device = OrphanDevice.find_by(device_token: device_token(topic))
return if orphan_device.nil?
orphan_device.update!(device_handshake: true)
Redis.current.publish('token-received', {
onboarding_session: orphan_device.onboarding_session
}.to_json)
end

# takes a packet and returns 'device token' from topic
Expand Down
55 changes: 52 additions & 3 deletions spec/lib/mqtt_messages_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@
MqttMessagesHandler.handle_topic(@packet.topic, @packet.payload)
end

it 'handshakes the device if an orphan device exists' do
orphan_device = create(:orphan_device, device_token: device.device_token)
expect(orphan_device.device_handshake).to be false
allow(Redis.current).to receive(:publish)
expect(Redis.current).to receive(:publish).with(
'token-received', {
onboarding_session: orphan_device.onboarding_session
}.to_json
)
MqttMessagesHandler.handle_topic(@packet.topic, @packet.payload)
expect(orphan_device.reload.device_handshake).to be true
end

it 'does not queue when there is no data' do
expect(Redis.current).not_to receive(:publish).with(
'telnet_queue', [{
Expand Down Expand Up @@ -120,9 +133,12 @@
end

describe '#handle_raw' do
it 'processes raw data' do
the_data = "{ t:2017-03-24T13:35:14Z, 1:48.45, 13:66, 12:28, 10:4.45 }"

let(:the_data) {
"{ t:2017-03-24T13:35:14Z, 1:48.45, 13:66, 12:28, 10:4.45 }"
}

it 'processes raw data' do
expect(Redis.current).to receive(:publish).with(
'telnet_queue', [{
name: nil,
Expand All @@ -148,10 +164,23 @@
# TODO: we should expect that a new Storer object should contain the correct, processed readings
#expect(Storer).to receive(:new)
end

it 'handshakes the device if an orphan device exists' do
orphan_device = create(:orphan_device, device_token: device.device_token)
expect(orphan_device.device_handshake).to be false
allow(Redis.current).to receive(:publish)
expect(Redis.current).to receive(:publish).with(
'token-received', {
onboarding_session: orphan_device.onboarding_session
}.to_json
)
MqttMessagesHandler.handle_topic("device/sck/#{device.device_token}/readings/raw", the_data)
expect(orphan_device.reload.device_handshake).to be true
end
end

describe '#handle_hello' do
it 'logs device_token has been received' do
it 'handshakes the device if an orphan device exists' do
expect(orphan_device.device_handshake).to be false
expect(Redis.current).to receive(:publish).with(
'token-received', {
Expand Down Expand Up @@ -183,6 +212,13 @@
MqttMessagesHandler.handle_topic(nil,'{"random_property":"random_result2"}')
expect(DeviceInventory.count).to eq(0)
end

it 'does not handshake any device' do
expect(Redis.current).not_to receive(:publish)
MqttMessagesHandler.handle_topic(
@inventory_packet.topic, @inventory_packet.payload
)
end
end

describe '#hardware_info' do
Expand All @@ -194,6 +230,19 @@
expect(@hardware_info_packet.payload).to eq((device.hardware_info.to_json))
end

it 'handshakes the device if an orphan device exists' do
orphan_device = create(:orphan_device, device_token: device.device_token)
expect(orphan_device.device_handshake).to be false
allow(Redis.current).to receive(:publish)
expect(Redis.current).to receive(:publish).with(
'token-received', {
onboarding_session: orphan_device.onboarding_session
}.to_json
)
MqttMessagesHandler.handle_topic(@hardware_info_packet.topic, @hardware_info_packet.payload)
expect(orphan_device.reload.device_handshake).to be true
end

it 'does not handle bad topic' do
expect(device.hardware_info["id"]).to eq(47)
MqttMessagesHandler.handle_topic(@hardware_info_packet_bad.topic, @hardware_info_packet_bad.payload)
Expand Down

0 comments on commit b55177c

Please sign in to comment.