Skip to content

Instantly share code, notes, and snippets.

@madaarya
Created January 21, 2020 15:47
Show Gist options
  • Save madaarya/34000ec9f66015681c3e5229dd8e226c to your computer and use it in GitHub Desktop.
Save madaarya/34000ec9f66015681c3e5229dd8e226c to your computer and use it in GitHub Desktop.
require 'time'
require 'csv'
require 'date'
require './services/lib/motes'
require "#{Dir.pwd}/services/lib/udp_log"
class PayloadDetail
def self.generate(data, port)
final = "no match on payload detail"
begin
splitted_record = data[:udptocsv].split(',')
records = {
"first_record" => splitted_record[0],
"second_record" => splitted_record[1],
"third_record" => splitted_record[2],
"rssi_dr" => {"rssi" => data[:rssi], "dr" => data[:dr]},
"seqno" => data[:seqno]
}
csv_value_present = false
mote_ui_record = records["first_record"].to_s
mote_ui_record_length = mote_ui_record.length
if mote_ui_record_length < 16
mote_ui_record = generate_formatted_mote(mote_ui_record_length) + mote_ui_record
else
mote_ui_record = mote_ui_record.gsub("-", "")
end
CSV.foreach('Table_motes.txt') do |row|
row_match = row[2].to_s.upcase
motes_match = (generate_formatted_mote(row_match.length) + row_match)
if mote_ui_record.upcase == motes_match
records["first_record"] = row_match
if Motes.respond_to?("get_final_data#{row[0].to_s}")
final = Motes.send("get_final_data#{row[0].to_s}", records, row, port)
else
final = "autre"
end
csv_value_present = true
end #end of if
end #end of CSV.foreach
unless csv_value_present
log = UdpLog.new("mote_not_declared_error")
log.write_log(records.merge!({time: Time.now, first_record_upcase: records["first_record"].to_s.upcase}))
end
rescue Exception => error
log = UdpLog.new("general_error")
log.write_log({port: port, possible_couse: 'parse csv data', file_location: "services/payload_detail.rb", error: error.backtrace})
end
final
end
def self.generate_formatted_mote(length)
required_length = 16 - length
str = ""
(1..required_length).each { |i| str << "0" }
return str
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment