Add `Logs::Create.run!` to logs_service

pull/554/head
Rick Carlino 2017-12-06 13:40:52 -06:00
parent 88a0d64abb
commit 63a90f8888
2 changed files with 15 additions and 15 deletions

View File

@ -14,7 +14,7 @@ module Transport
@log_channel ||= self.connection
.create_channel
.queue("", exclusive: true)
.bind("amq.topic", routing_key: "bot.#")
.bind("amq.topic", routing_key: "bot.*.logs")
end
def self.topic

View File

@ -1,20 +1,20 @@
Transport
.log_channel
.subscribe(block: true) do |delivery_info, properties, payload|
# # Legacy FBOS still uses the API for log storage.
# headers = properties[:headers] || {}
# # Guess version
# not_legacy = !headers.keys.join(" ").include?("mqtt")
# if (not_legacy)
# Extract current user (if version is appropriate)
# device_id = delivery_info.routing_key.split(".")[1].gsub("device_", "").to_i
# device = Device.find(device_id)
# # Parse payload
# payload = JSON.parse(payload)
# { "meta"=>{"z"=>0, "y"=>0, "x"=>0, "type"=>"info", "major_version"=>6},
# "message"=>"HQ FarmBot TEST 123 Pin 13 is 0",
# "created_at"=>1512585641,
# "channels"=>[] }
log = JSON.parse(payload)
# # {"meta"=>{"z"=>0, "y"=>0, "x"=>0, "type"=>"info"}, "message"=>"HQ FarmBot TEST 123 Pin 13 is 0", "created_at"=>1512585641, "channels"=>[]}
# Legacy bots will double save logs if we don't do this:
major_version = log.dig("meta", "major_version") || 0
# puts "===== INCOMING LOG ====="
# puts payload
# end
if(major_version >= 6)
device_id = delivery_info.routing_key.split(".")[1].gsub("device_", "").to_i
log[:device] = Device.find(device_id)
Logs::Create.run!(log).save!
else
puts "Ignoring legacy log."
end
end