-- Copyright (c) 2024 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at:
--         http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

local log = require 'mc.logging'
local utils = require 'types.utils'
local m_enums = require 'types.enums'
local class = require 'mc.class'
local singleton = require 'mc.singleton'
local m_error_custom = require 'messages.custom'
local skynet = require 'skynet'

local c_system_poh = class()

function c_system_poh:ctor()
end

function c_system_poh:add_poh_callback(object, host_obj)
    self.obj = object
    self.host_obj = host_obj

    -- 判断使能通电时长计数
    if not self.obj.POHEnable then
        log:notice('POHEnable is false ')
        return
    end

    -- 数据校验
    if not self.obj.Counter or not self.obj.MinutesPerCount or self.obj.MinutesPerCount == 0 then
        log:error('The property of POH is invalid')
        return
    end

    log:notice('Begin update POH')
    -- 通电时长计数例测任务
    skynet.fork_loop({count = 0}, function()
        while true do
            -- 检查周期的单位为分钟
            skynet.sleep(self.obj.MinutesPerCount * 6000)
            for _, host in pairs(self.host_obj) do
                if host.fructrl and m_enums.PGSignalState[host.fructrl:get_PowerState()] == m_enums.PowerStatus.ON then
                    self.obj.Counter = self.obj.Counter + 1
                    break
                end
            end         
        end
    end)
end


return singleton(c_system_poh)