-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mqtt_messages_handler_spec.rb
181 lines (160 loc) · 6.27 KB
/
mqtt_messages_handler_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
require 'rails_helper'
RSpec.describe MqttMessagesHandler do
let(:device) { create(:device, device_token: 'aA1234') }
let(:orphan_device) { create(:orphan_device, device_token: 'xX9876') }
let(:component) { build(:component, board: build(:kit), sensor: build(:sensor, id: 1)) }
let(:device_inventory) { create(:device_inventory, report: '{"random_property": "random_result"}') }
before do
device.components << component
@data = [{
"recorded_at"=>"2016-06-08 10:30:00",
"sensors"=>[{
"id"=>1,
"value"=>21
}]
}]
@packet = MQTT::Packet::Publish.new(
topic: "device/sck/#{device.device_token}/readings",
payload: '{"data": [{"recorded_at": "2016-06-08 10:30:00","sensors": [{"id": 1,"value": 21}]}]}'
)
@invalid_packet = MQTT::Packet::Publish.new(
topic: "device/sck/invalid_device_token/readings",
payload: '{"data": [{"recorded_at": "2016-06-08 10:30:00","sensors": [{"id": 1,"value": 21}]}]}'
)
@inventory_packet = MQTT::Packet::Publish.new(
topic: "device/sck/inventory",
payload: '{"random_property":"random_result"}'
)
@hardware_info_packet = MQTT::Packet::Publish.new(
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
it 'returns device_token from topic' do
expect(MqttMessagesHandler.device_token(@packet.topic)).to eq(device.device_token)
end
end
describe '#data' do
it 'returns parsed data from payload' do
expect(MqttMessagesHandler.data(@packet.payload)).to match_array(@data)
end
end
describe '#readings' do
before do
# storer data processing
value = component.normalized_value((Float(@data[0]['sensors'][0]['value'])))
@data_array = [{
name: device.find_sensor_key_by_id(1),
timestamp: Time.parse(@data[0]['recorded_at']).to_i * 1000,
value: value,
tags: {
device_id: device.id,
method: 'REST'
}
}]
end
context 'valid reading packet' do
# TODO this fails on GitHub Actions, but not locally! Why?
skip 'queues reading data in order to be stored' do
# model/storer.rb is not using Kairos, but Redis -> Telnet
#expect(Kairos).to receive(:http_post_to).with("/datapoints", @data_array)
#expect(Storer).to receive(:initialize).with('a', 'b')
expect(Redis.current).to receive(:publish).with(
'telnet_queue', [{
name: nil,
timestamp: 1465374600000,
value: 21.0,
tags: {
device_id: device.id,
method: 'REST'
}
}].to_json
)
MqttMessagesHandler.handle_topic(@packet.topic, @packet.payload)
end
it 'does not queue when there is no data' do
expect(Redis.current).not_to receive(:publish).with(
'telnet_queue', [{
name: nil,
timestamp: 1465374600000,
value: 21.0,
tags: {
device_id: device.id,
method: 'REST'
}
}].to_json
)
MqttMessagesHandler.handle_topic(@packet.topic, @hardware_info_packet.payload)
end
end
context 'invalid packet' do
it 'it notifies Raven' do
allow(Raven).to receive(:capture_exception)
expect(Kairos).not_to receive(:http_post_to)
MqttMessagesHandler.handle_topic(@invalid_packet.topic, @invalid_packet.payload)
#expect(Raven).to have_received(:capture_exception).with(RuntimeError)
end
end
end
describe '#handle_hello' do
it 'logs device_token has been received' do
expect(orphan_device.device_handshake).to be false
expect(Redis.current).to receive(:publish).with(
'token-received', {
onboarding_session: orphan_device.onboarding_session
}.to_json
)
MqttMessagesHandler.handle_topic(
"device/sck/#{orphan_device.device_token}/hello",
'content ignored by MqttMessagesHandler\#hello'
)
expect(orphan_device.reload.device_handshake).to be true
end
end
describe '#handle_raw' do
it 'processes raw data' do
the_data = "{ t:2017-03-24T13:35:14Z, 29:48.45, 13:66, 12:28, 10:4.45 }"
# TODO: we should expect that a new Storer object should contain the correct, processed readings
expect(Storer).to receive(:new)
MqttMessagesHandler.handle_raw_readings(device, the_data)
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_topic(@inventory_packet.topic, @inventory_packet.payload)
expect(DeviceInventory.last.report["random_property"]).to eq('random_result')
expect(DeviceInventory.count).to eq(2)
end
it 'does not log inventory with an incorrect / nil topic' do
expect(DeviceInventory.count).to eq(0)
MqttMessagesHandler.handle_topic('invenxxx','{"random_property":"random_result2"}')
MqttMessagesHandler.handle_topic(nil,'{"random_property":"random_result2"}')
expect(DeviceInventory.count).to eq(0)
end
end
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_topic(@hardware_info_packet.topic, @hardware_info_packet.payload)
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_topic(@hardware_info_packet_bad.topic, @hardware_info_packet_bad.payload)
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