namespace :set_redis do
  desc "Setting up redis default info details for existing device"
  task aps_last_info: :environment do
    aps = LocationNetwork.includes(:router_inventories).map(&:router_inventories).flatten.map(&:mac_id)
    ts = Time.zone.now
    aps.each do |ap|
      unless $redis.hkeys("AP:#{ap}").present?
        puts "Set redis for AP: #{ap}"
        mc = MonitoringChild.where("monitoring_data.INFO.NASID" => ap, "created_at" => {"$lt" => ts}).desc("created_at").first #ap_last_hb ap, ts
        $redis.hmset "AP:#{ap}", "last_hb", mc["created_at"].to_i, "version", mc.monitoring_data['INFO']['PIAP_VERSION'] unless mc.blank?
      end
    end
  end
end
