  class HeartbeatWorker
  include Sidekiq::Worker
  sidekiq_options :queue => :monitoring, :retry => false, :backtrace => true

  def perform(message)
    if message.is_a?(String) && message == "true"
      message = $redis.lpop('bravo')
      message = JSON.parse(message)
    end
    unless message.blank?
      p message
      #Add data to monitoring capped collection
      MonitoringDataCapped.save_hb(message.deep_dup)

      mc = MonitoringChild.save_hb(message.deep_dup)
      ap_details = $redis.hgetall "AP:#{mc.info['NASID']}"
      last_hb = ap_details["last_hb"]
      config_update = false
      if mc.info["FIRST_HB"] == 1
         hb_freq = ap_details['hb_freq']
         interval = RouterInventory.get_hb_intreval(hb_freq)
        if last_hb.present? && (mc.created_at.to_i - last_hb.to_i) >= interval
          ApDowntime.create( mac: mc.info['NASID'], st: Time.zone.at(last_hb.to_i), et: mc.created_at, sec: (mc.created_at.to_i - last_hb.to_i))
        end
        version=mc.info["PIAP_VERSION"]
        if (ap_details["version"].blank? && (version.present? && version.gsub('.','').to_i <= 20222)) || (ap_details["version"].present? && ap_details["version"].gsub('.','').to_i <= 20222 && version.present? && version.gsub('.','').to_i > 20222)
          config_update = true#RouterInventory.where(mac_id: mc.info['NASID']).last.try(:update_redis)
        end
        #Trigger init commands for Spoton devices
        InitCommandWorker.perform_async(mc.info['NASID'], ap_details)
      end
      #Set redis last HB time
      # ap_details = $redis.hgetall "AP:#{mc.info['NASID']}"
      curr_ap_details = []
      curr_ap_details += ["prev_last_hb", last_hb] 
      curr_ap_details += ["last_hb", mc.created_at.to_i] 
      curr_ap_details += ["version", mc.info["PIAP_VERSION"]] if ap_details["version"].blank? || ap_details["version"] != mc.info["PIAP_VERSION"] || mc.info["FIRST_HB"] == 1
      curr_ap_details += ["BN", mc.info["BN"]] if ap_details["BN"].blank? || ap_details["BN"] != mc.info["BN"] || mc.info["FIRST_HB"] == 1
      curr_ap_details += ["p_version", ap_details["version"]] if ap_details["version"] != mc.info["PIAP_VERSION"] && mc.info["FIRST_HB"] == 1
      curr_ap_details += ["pub_ip", mc.s_info["PUBLIC_IP"]] if ap_details["pub_ip"].blank? || (mc.s_info.present? && ap_details["pub_ip"] != mc.s_info["PUBLIC_IP"])  || (mc.info||{})["FIRST_HB"] == 1      #$redis.multi do
      # $redis.hset "AP:#{mc.info['NASID']}", "prev_last_hb", last_hb
      # $redis.hset "AP:#{mc.info['NASID']}", "last_hb", mc.created_at.to_i
      # $redis.hset "AP:#{mc.info['NASID']}", "version", mc.info["PIAP_VERSION"] if ap_details["version"].blank? || ap_details["version"] != mc.info["PIAP_VERSION"] || mc.info["FIRST_HB"] == 1
      # $redis.hset "AP:#{mc.info['NASID']}", "p_version", ap_details["version"] if ap_details["version"] != mc.info["PIAP_VERSION"] && mc.info["FIRST_HB"] == 1
      # $redis.hset "AP:#{mc.info['NASID']}", "pub_ip", mc.s_info["PUBLIC_IP"] if ap_details["pub_ip"].blank? || (mc.s_info.present? && ap_details["pub_ip"] != mc.s_info["PUBLIC_IP"])  || mc.info["FIRST_HB"] == 1
      if mc.radio != nil
        mc.radio.each do |radio|
          if radio['BAND'].to_s == '1'
            curr_ap_details += ["C_24GHZ" , radio['CHANNEL']]
            curr_ap_details += ["P_24GHZ" , radio['TXPOWER']]
            # $redis.hset "AP:#{mc.info['NASID']}", "C_24GHZ" , radio['CHANNEL']
            # $redis.hset "AP:#{mc.info['NASID']}", "P_24GHZ" , radio['TXPOWER']
          else
            curr_ap_details += ["C_5GHZ" , radio['CHANNEL']]
            curr_ap_details += ["P_5GHZ" , radio['TXPOWER']]
            # $redis.hset "AP:#{mc.info['NASID']}", "C_5GHZ" , radio['CHANNEL']
            # $redis.hset "AP:#{mc.info['NASID']}", "P_5GHZ" , radio['TXPOWER']
          end
        end
      end

      $redis.hmset "AP:#{mc.info['NASID']}", curr_ap_details
      
      if ap_details['vendor_type'] == '6' #&& ap_details['test_device'] != 'true'
        if (mc.info['CONSUME_ID'] != ap_details['CONSUME_ID']) && ap_details['test_device'] != 'true'
          ri=RouterInventory.where(mac_id: mc.info['NASID']).last
          p "[OPENWIFI][SWITCH][APPLY] Applying latest config for #{mc.info['NASID']}--- #{mc.id}"
          ri.update_redis(true,true,true) if ri.present?
        end

        if ap_details["pending_upgrade"].present? && ap_details["pending_upgrade"].to_i > 0
          pending_upgrade_id = ap_details["pending_upgrade"].to_i
          job_id = ap_details["upgrade_job_id"]
          unless ap_details['ongoing_upgrade'] == "true" && job_id.present?
            PushUpgradeWorker.perform_async(mc.info['NASID'], pending_upgrade_id)
          end
        end
      else
        RouterInventory.where(mac_id: mc.info['NASID']).last.try(:update_redis) if (mc.info["FIRST_HB"] == 1 && config_update)
      end
      
      if (mc.info["FIRST_HB"] == 1 || ap_details['hw_part_no'].blank?) && mc.info["HW_PART_NO"].present? && mc.info["HW_PART_NO"] != ""# && !mc.info["HW_PART_NO"].nil?
        ri = RouterInventory.find_by_mac_id mc.info['NASID']
        ri.set_hw_part_no mc.info["HW_PART_NO"] if ri.present? && ri.hardware_part_id.blank?
      end
      if ap_details['alert'] == "1" && JSON.parse($redis.get('PCC:ORG:ALERT:STATUS') || '[]').include?(ap_details['org_id']) #&& ["4551", "25", "273", "14", "433", "477"].include?(ap_details['org_id'])
        #Create Alerts worker
        jid=AlertsWorker.perform_async(message, mc.created_at.to_i)

        puts "Heartbeat worker completed --- Alerts Worker #{jid}"
      end

    end
  end
end
