import whatap.pack.tagcount_pack as tagcount_pack
import whatap.osinfo as osinfo
import whatap.agent.secure.security_master as secure


class TaskInfraNtpInfo:
    NTP_INFO = "infra_ntp_info"

    def __init__(self, interval=60):
        self.intervalValue = interval
        self.recType = 1

    def interval(self):
        return self.intervalValue

    def process(self, os_, pcode, oid, now):
        ntp_info = osinfo.GetNtpInfo()
        if not ntp_info:
            return None

        secu = secure.GetSecurityMaster()
        p = tagcount_pack.TagCountPack()
        p.pcode = pcode
        p.oid = oid
        p.time = now
        p.category = TaskInfraNtpInfo.NTP_INFO
        p.putTag("oname", str(secu.oname))
        p.putTag("!rectype", str(self.recType))

        p.put("checkTime", ntp_info.get("checkTime", 0))
        p.put("ntpInUse", ntp_info.get("ntpInUse", 0))
        p.put("ntpDaemon", str(ntp_info.get("ntpDaemon") or ""))
        p.put("timeSynced", ntp_info.get("timeSynced", 0))
        p.put("ntpServer", str(ntp_info.get("ntpServer") or ""))
        p.put("offset", ntp_info.get("offset", 0.0))
        p.put("offsetDirection", ntp_info.get("offsetDirection", 0))
        p.put("timeZone", str(ntp_info.get("timeZone") or ""))

        return p
