#include "alarm_init.h"
#include "alarm_control.h"
#include "../proto_forward.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syslog.h>
#include "alarm_define.h"
#include "../ems/vlc_deviceLater.h"
#include "../business_log/business_log.h"
#define ALARM_BUSINESS_LOG(_lev, _id, _no, _fmt, ...) BUSINESS_LOG(_lev, _id, _no, "[告警]" _fmt, ##__VA_ARGS__)
extern alarm_control_t alarm_control;

extern void alarm_active_callback(void* context, tag_t* tag);
// 创建默认告警触发条件, 值 >= 1 时触发
static alarm_info_t* make_alarm_info_with_file_def(alarm_info_t* alarm_info_def, tag_t* t)
{
    if (alarm_info_def == NULL || t == NULL) {
        return NULL;
    }

    alarm_info_t* alarm_info = malloc(sizeof(alarm_info_t));
    if (alarm_info == NULL)
    {
        ems_syslog(LOG_ERR, "malloc alarm_info failed");
        return NULL;
    }
    memcpy(alarm_info, alarm_info_def, sizeof(alarm_info_t));

    alarm_condition_t* condition = (alarm_condition_t*)malloc(sizeof(alarm_condition_t));
    if (condition == NULL)
    {
        ems_syslog(LOG_ERR, "malloc alarm_condition failed");
        free(alarm_info);
        return NULL;
    }
    memset(condition, 0, sizeof(alarm_condition_t));
    condition->tag       = t;
    condition->value     = 1;
    condition->condition = CONDITION_GE;

    for (int i = 0; i < alarm_info->condition_num; i++)
    {
        if (alarm_info->condition[i] != NULL)
        {
            free(alarm_info->condition[i]);
            alarm_info->condition[i] = NULL;
        }
    }

    alarm_info->condition[0]  = condition;
    alarm_info->condition_num = 1;

    // 设置告警附加信息参数
    if (alarm_info->para_list == NULL)
    {
        return alarm_info;
    }

    char* ptr = alarm_info->para_list;
    char  tag_name[TAG_NAME_LEN] = {0};
    tag_t * p_tag = NULL;
    while (ptr != NULL && *(ptr + 1) != '\0' )
    {
        if (alarm_info->additional_para->num >= ALARM_CHILD_CONDS_NUM_MAX)
        {
            ems_syslog(LOG_ERR, "additional_para num >= MAX_ADDITIONAL_PARA_NUM %d", ALARM_CHILD_CONDS_NUM_MAX);
            break;
        }

        char* next = strchr(ptr, ';');
        if (next == NULL)
        {
            break;
        }

        int len = next - ptr;
        strncpy(tag_name, ptr, len);
        tag_name[len] = '\0';
        int n = alarm_info->additional_para->num++;
        alarm_info->additional_para->spilit[n] = 1;
        alarm_info->additional_para->addtype[n] = 1;
        alarm_info->additional_para->tags[n] = find_tag_by_dev_and_node(get_proto_forward_var(), ((device_t*)(t->dev_ptr))->no, tag_name);
        if (p_tag == NULL)
        {
            ems_syslog(LOG_ERR, "find tag %s_%s failed", ((device_t*)(t->dev_ptr))->no, tag_name);
        }
        ptr = next + 1;
    }
    return alarm_info;
}

int get_category_from_string(const char* category)
{
    if (category == NULL || strlen(category) == 0) return -1;

    if (strcmp(category, "Communication") == 0 || strcmp(category, "通讯") == 0)
        return EMMS2_CATEGORY_COMMUNICATION;
    if (strcmp(category, "Cutomized") == 0 || strcmp(category, "自定义") == 0)
        return EMMS2_CATEGORY_CUTOMIZED;
    if (strcmp(category, "Equipments") == 0 || strcmp(category, "设备") == 0)
        return EMMS2_CATEGORY_EQUIPMENTS;
    if (strcmp(category, "Resources") == 0 || strcmp(category, "资源") == 0)
        return EMMS2_CATEGORY_RESOURCES;
    if (strcmp(category, "Services") == 0 || strcmp(category, "业务") == 0)
        return EMMS2_CATEGORY_SERVICES;
    return -1;
}

int get_severity_from_string(const char* severity)
{
    if (severity == NULL || strlen(severity) == 0) return -1;

    if (strcmp(severity, "清除") == 0 || strcmp(severity, "Clear") == 0)
        return EMMS2_SEVERITY_CLEAR;
    if (strcmp(severity, "提示") == 0 || strcmp(severity, "Warning") == 0)
        return EMMS2_SEVERITY_WARNING;
    if (strcmp(severity, "一般") == 0 || strcmp(severity, "Minor") == 0)
        return EMMS2_SEVERITY_MINOR;
    if (strcmp(severity, "重要") == 0 || strcmp(severity, "Major") == 0)
        return EMMS2_SEVERITY_MAJOR;
    if (strcmp(severity, "严重") == 0 || strcmp(severity, "Critical") == 0)
        return EMMS2_SEVERITY_CRITICAL;
    return -1;
}

// 解析json中的alarm信息
static alarm_info_t* create_alarm_info_struct(const cJSON* tag)
{

    cJSON* category    = cJSON_GetObjectItem(tag, "categoryDisplay");
    cJSON* severity    = cJSON_GetObjectItem(tag, "severityDisplay");
    cJSON* alarm_code  = cJSON_GetObjectItem(tag, "alarmCode");
    cJSON* reason_code = cJSON_GetObjectItem(tag, "reasonCode");
    cJSON* paras  = cJSON_GetObjectItem(tag, "additionalInfoParas");
    if (alarm_code == NULL || reason_code == NULL || category == NULL || severity == NULL)
    {
        ems_syslog(LOG_ERR, "read alarm info failed, no tag_name or info or entity_type");
        return NULL;
    }
    alarm_info_t* alarm_info = (alarm_info_t*)malloc(sizeof(alarm_info_t));
    if (alarm_info == NULL)
    {
        ems_syslog(LOG_ERR, "malloc alarm_info failed");
        goto ERR0;
    }
    memset(alarm_info, 0, sizeof(alarm_info_t));

    alarm_info->alarm_code = alarm_code->valueint;
    if (alarm_info->alarm_code <= 0)
    {
        ems_syslog(LOG_ERR, "alarm_code: %d is not support", alarm_code->valueint);
        goto ERR0;
    }

    alarm_info->reason_code = reason_code->valueint;
    alarm_info->category = get_category_from_string(category->valuestring);
    alarm_info->severity = get_severity_from_string(severity->valuestring);

    if (paras != NULL && strlen(paras->valuestring) > 0)
    {
        alarm_info->para_list = strdup(paras->valuestring);
    }
    else
    {
        alarm_info->para_list = NULL;
    }


    return alarm_info;
ERR0:
    if (alarm_info != NULL)
        destory_alarm_info(alarm_info);
    return NULL;
}
// 解析alarm_def.json文件
static int init_alarm_info_from_json(cJSON* root)
{
    if (root == NULL)
    {
        ems_syslog(LOG_ERR, "parse alarm info file: %s failed", ALARM_INFO_FILE);
        goto ERR;
    }

    cJSON* vendor_models = cJSON_GetObjectItem(root, "vendor_models");
    if (vendor_models == NULL || cJSON_IsArray(vendor_models) == false)
    {
        ems_syslog(LOG_ERR, "parse alarm info file: %s failed, no vendor_models", ALARM_INFO_FILE);
        goto ERR;
    }

    int  size     = cJSON_GetArraySize(vendor_models);
    char key[144] = {0};
    int  key_len  = 0;

    for (int i = 0; i < size; i++)
    {
        cJSON* vendor_model = cJSON_GetArrayItem(vendor_models, i);
        if (vendor_model == NULL) continue;

        cJSON* type   = cJSON_GetObjectItem(vendor_model, "type");
        cJSON* vendor = cJSON_GetObjectItem(vendor_model, "vendor");
        cJSON* model  = cJSON_GetObjectItem(vendor_model, "model");
        cJSON* tags   = cJSON_GetObjectItem(vendor_model, "tags");

        int tag_size = cJSON_GetArraySize(tags);
        for (int j = 0; j < tag_size; j++)
        {
            cJSON* tag = cJSON_GetArrayItem(tags, j);
            if (tag == NULL) continue;

            alarm_info_t* alarm_info = create_alarm_info_struct(tag);
            if (alarm_info == NULL) continue;

            key_len = snprintf(key, sizeof(key), "%d.%d", alarm_info->alarm_code, alarm_info->reason_code);
            if (hash_intptr_addptr(&alarm_control.alarm_code_hash, key, key_len, (void*)alarm_info) != 0)
            {
                ems_syslog(LOG_ERR, "insert alarm info to alarm_code_hash failed, key: %s", key);
                destory_alarm_info(alarm_info);
                continue;
            }

            cJSON* tag_name = cJSON_GetObjectItem(tag, "tag");
            if (tag_name == NULL || tag_name->valuestring == NULL || strlen(tag_name->valuestring) <= 0) continue;
            key_len = snprintf(key, sizeof(key), "%s.%s.%s.%s", type->valuestring, vendor->valuestring, model->valuestring, tag_name->valuestring);
            if (hash_intptr_addptr(&alarm_control.alarm_info_hash, key, key_len, (void*)alarm_info) != 0)
            {
                ems_syslog(LOG_ERR, "insert alarm info to alarm_info_hash failed, key: %s", key);
                continue;
            }
        }
    }
    return 0;
ERR:
    return -1;
}
// 注册告警回调
// int register_alarm_callback_to_tag(tag_t* tag);
int add_alarm_info_to_alarm_tag(tag_t* t, alarm_info_t* alarm_info)
{
    if (t == NULL || alarm_info == NULL)
    {
        ems_syslog(LOG_ERR, "add alarm info to tag hash failed, t or alarm_info is NULL");
        return -1;
    }
    alarm_tag_t* alarm_tag = NULL;
    char         key[144]  = {0};
    int          key_len   = snprintf(key, sizeof(key), "%s", t->dev_tag_name);
    if (hash_intptr_findptr(alarm_control.alarm_tag_hash, key, key_len, (void**)&alarm_tag) != 0)
    {
        alarm_tag = (alarm_tag_t*)malloc(sizeof(alarm_tag_t));
        if (alarm_tag == NULL)
        {
            ems_syslog(LOG_ERR, "malloc alarm tag failed");
            return -1;
        }
        memset(alarm_tag, 0, sizeof(alarm_tag_t));
        alarm_tag->tag = t;
        alarm_tag_add_alarm_info(alarm_tag, alarm_info);

        if (hash_intptr_addptr(&alarm_control.alarm_tag_hash, key, key_len, alarm_tag) < 0)
        {
            ems_syslog(LOG_ERR, "add alarm tag to hash failed");
            destory_alarm_tag(alarm_tag);
            return -1;
        }
        // register_alarm_callback_to_tag(t);
    }
    else
    {
        return alarm_tag_add_alarm_info(alarm_tag, alarm_info);
    }
    return 0;
}
// 将模板中的告警添加到告警hash中
static int add_template_alarm_to_hash(tag_t* t)
{
    char          key[144]   = {0};
    int           key_len    = 0;
    alarm_info_t* alarm_info = NULL;
    if (t == NULL)
    {
        ems_syslog(LOG_ERR, "add inused alarm tag to hash failed, param is NULL");
        return -1;
    }
    char* tag_name = t->name;
    char* type     = ((device_t*)t->dev_ptr)->dev_type;
    char* vendor   = ((device_t*)t->dev_ptr)->use_template->vendor;
    char* model    = ((device_t*)t->dev_ptr)->use_template->model;
    key_len        = snprintf(key, sizeof(key), "%s.%s.%s.%s", type, vendor, model, tag_name);

    if (hash_intptr_findptr(alarm_control.alarm_info_hash, key, key_len, (void**)&alarm_info) != 0 || alarm_info == NULL)
    {
        ALARM_BUSINESS_LOG(BLOG_ERR, ALARM_ID_0004, t->dev_tag_name, "获取告警信息失败, 确认文件是否定义了[%s]告警信息, tag:%s", key, t->dev_tag_name);
        return -1;
    }

    alarm_info = make_alarm_info_with_file_def(alarm_info, t);
    if(alarm_info == NULL)
    {
        ALARM_BUSINESS_LOG(BLOG_ERR, ALARM_ID_0004, t->dev_tag_name, "创建告警信息对象失败,key: %s, tag:%s", key, t->dev_tag_name);
        return -1;
    }
    int ret = add_alarm_info_to_alarm_tag(t, alarm_info);
    if (ret != 0)
    {
        ALARM_BUSINESS_LOG(BLOG_ERR, ALARM_ID_0004, t->dev_tag_name, "注册告警信息对象失败,key: %s, tag:%s", key, t->dev_tag_name);
        destory_alarm_info(alarm_info);
    }
    return ret;
}

static int init_template_alarm()
{
    proto_forward_t* forward = get_proto_forward_var();
    for (int i = 0; i < forward->channels_size; i++)
    {
        for (int j = 0; j < forward->channels[i]->devs_size; j++)
        {
            for (int k = 0; k < forward->channels[i]->devs[j]->tags.ro_tags_size; k++)
            {
                tag_t* t = forward->channels[i]->devs[j]->tags.ro_tags[k];
                if (t == NULL) continue;
                if (t->property == PROPERTY_ALARM)
                {
                    if (add_template_alarm_to_hash(t) == 0)
                    {
                        // ALARM_BUSINESS_LOG(BLOG_NOTICE, ALARM_ID_0004, t->dev_tag_name, "注册告警信息成功,tag:%s", t->dev_tag_name);
                    }
                }
            }
            for (int k = 0; k < forward->channels[i]->devs[j]->tags.rw_tags_size; k++)
            {
                tag_t* t = forward->channels[i]->devs[j]->tags.rw_tags[k];
                if (t == NULL) continue;
                if (t->property == PROPERTY_ALARM)
                {
                    if (add_template_alarm_to_hash(t) == 0)
                    {
                        // ALARM_BUSINESS_LOG(BLOG_NOTICE, ALARM_ID_0004, t->dev_tag_name, "注册告警信息成功,tag:%s", t->dev_tag_name);
                    }
                }
            }
            for (int k = 0; k < forward->channels[i]->devs[j]->tags.sys_info_tags_size; k++)
            {
                tag_t* t = forward->channels[i]->devs[j]->tags.sys_info_tags[k];
                if (t == NULL) continue;
                if (t->property == PROPERTY_ALARM)
                {
                    if (add_template_alarm_to_hash(t) == 0)
                    {
                        // ALARM_BUSINESS_LOG(BLOG_NOTICE, ALARM_ID_0004, t->dev_tag_name, "注册告警信息成功,tag:%s", t->dev_tag_name);
                    }
                }
            }
        }
    }
    return 0;
}



alarm_info_t* make_alarm_info_with_cond_para(alarm_info_t* alarm_info, alarm_info_cond_t* alarm, tag_t* self)
{
    if (alarm_info == NULL || alarm == NULL || self == NULL) return NULL;

    alarm_info_t* new_alarm_info = malloc(sizeof(alarm_info_t));
    if (new_alarm_info == NULL) return NULL;
    memcpy(new_alarm_info, alarm_info, sizeof(alarm_info_t));

    if (new_alarm_info->additional_para != NULL)
    {
        free(new_alarm_info->additional_para);
        new_alarm_info->additional_para = NULL;
    }
    new_alarm_info->additional_para = malloc(sizeof(alarm_val_t));
    memset(new_alarm_info->additional_para, 0, sizeof(alarm_val_t));
    for (int i = 0; i < ALARM_CHILD_CONDS_NUM_MAX; i++)
    {
        char* para = alarm->additional_para[i];
        if (para == NULL || para[0] == '\0') break;

        int n = new_alarm_info->additional_para->num++;

        if (strchr(para, '&'))
        {
            new_alarm_info->additional_para->addtype[n] = 1;
            para                                    = para + 1;
        }
        else if(strchr(para, '%')){
            new_alarm_info->additional_para->addtype[n] = 2;
            para                                        = para + 1;
        }
        else{
            new_alarm_info->additional_para->addtype[n] = 0;
        }
        char* addition_name = strrchr(para, ';');
        if (addition_name)
        {
            new_alarm_info->additional_para->spilit[n] = 1;
            addition_name[0]                       = '\0';
        }
        char* addition = para;
        if (new_alarm_info->additional_para->addtype[n] == 0 || new_alarm_info->additional_para->addtype[n] == 1)
        {
            if (strstr(addition, "$DEV_NO") != addition)
            {
                new_alarm_info->additional_para->tags[n] = find_tag_by_name(get_proto_forward_var(), addition);
                if (new_alarm_info->additional_para->tags[n] == NULL)
                {
                    ems_syslog(LOG_ERR, "find %s tag err", addition);
                }
            }
            else
            {
                device_t *cur_dev = (device_t*)(self->dev_ptr);
                tag_t *target_tag = find_tag_by_dev_and_node(get_proto_forward_var(), cur_dev->no, addition + 8); // 8等于strlen("$DEV_NO") + 1
                if ((target_tag == NULL) && (strcmp(cur_dev->dev_type, "LC") == 0) && (strstr(addition, "$DEV_NO.BMS.") == addition))
                {
                    char* bms_no = get_bms_no_by_lc_no(cur_dev->no);
                    if (bms_no != NULL)
                    {
                        target_tag = find_tag_by_dev_and_node(get_proto_forward_var(), bms_no, addition + 12); // 12等于strlen("$DEV_NO.BMS.") + 1
                        free(bms_no);
                    }
                    if (target_tag != NULL)
                    {
                        ems_syslog(LOG_NOTICE, "%s find %s tag err, try to find %s tag ok", cur_dev->no, addition, bms_no);
                        //free(bms_no);
                    }
    
                }
                new_alarm_info->additional_para->tags[n] = target_tag;
                if (new_alarm_info->additional_para->tags[n] == NULL)
                {
                    ems_syslog(LOG_ERR, "find %s.%s tag err", ((device_t*)(self->dev_ptr))->no, addition + 8);
                }
            }
        }
        else if( new_alarm_info->additional_para->addtype[n] == 2)
        {
            new_alarm_info->additional_para->tags[n] = strdup(addition);
        }
    }

    for (int i = 0; i < ALARM_CHILD_CONDS_NUM_MAX; i++)
    {
        if (new_alarm_info->condition[i] != NULL)
        {
            free(new_alarm_info->condition[i]);
            new_alarm_info->condition[i] = NULL;
        }
    }
    new_alarm_info->condition_num = 0;
    // 解析告警条件 自身条件
    alarm_condition_t* alarm_condition = malloc(sizeof(alarm_condition_t));
    alarm_condition->condition         = alarm->cond;
    alarm_condition->value             = alarm->value;
    alarm_condition->tag               = self;
    alarm_tag_add_condition(new_alarm_info, alarm_condition);

    // 解析告警条件 子条件
    tag_t* child_tag = NULL;
    for (int i = 0; i < ALARM_CHILD_CONDS_NUM_MAX; i++)
    {
        if (alarm->child_conds[i].tag_name != NULL && strlen(alarm->child_conds[i].tag_name) > 0 && alarm->child_conds[i].cond != CONDITION_ERROR)
        {
            if (strstr(alarm->child_conds[i].tag_name, "$DEV_NO") != alarm->child_conds[i].tag_name)
            {
                child_tag = find_tag_by_name(get_proto_forward_var(), alarm->child_conds[i].tag_name);
            }
            else
            {
                child_tag = find_tag_by_dev_and_node(get_proto_forward_var(), ((device_t*)self->dev_ptr)->no, alarm->child_conds[i].tag_name + 8); //8等于strlen("$DEV_NO") + 1
            }

            if (child_tag == NULL)
            {
                ems_syslog(LOG_NOTICE, "child tag not found, tag name: %s", alarm->child_conds[i].tag_name);
                ems_syslog(LOG_CRIT, "告警触发条件点位不存在，需要确认tag: %s", alarm->child_conds[i].tag_name);
                continue;
            }
            alarm_condition_t* child_condition = malloc(sizeof(alarm_condition_t));
            child_condition->condition         = alarm->child_conds[i].cond;
            child_condition->value             = alarm->child_conds[i].value;
            child_condition->tag               = child_tag;
            alarm_tag_add_condition(new_alarm_info, child_condition);
        }
    }
    return new_alarm_info;
}

static int add_alarm_to_hash_from_code(alarm_info_cond_t* alarm, tag_t* tag)
{
    char          key[DEV_NO_LEN + TAG_NAME_LEN];
    int           key_len;
    alarm_info_t* alarm_info = NULL;
    key_len                  = snprintf(key, sizeof(key), "%d.%d", alarm->alarm_code, alarm->reason_code);
    if (hash_intptr_findptr(alarm_control.alarm_code_hash, key, key_len, (void**)&alarm_info) != 0 || alarm_info == NULL)
    {
        ALARM_BUSINESS_LOG(BLOG_ERR, ALARM_ID_0004, tag->dev_tag_name, "代码中注册的点位未找到指定的告警信息, 注册失败, tag: %s, key: %s", tag->dev_tag_name, key);
        return -1;
    }
    // 更新alarm_info 的 告警触发条件 和附加参数,
    alarm_info = make_alarm_info_with_cond_para(alarm_info, alarm, tag);
    if(alarm_info == NULL)
    {
        ALARM_BUSINESS_LOG(BLOG_ERR, ALARM_ID_0004, tag->dev_tag_name, "生成告警条件错误, 注册失败, tag: %s, key: %s", tag->dev_tag_name, key);
        return -1;
    }
    return add_alarm_info_to_alarm_tag(tag, alarm_info);
}

static alarm_info_cond_t* get_alarm_info_from_list(char* dev_type)
{
    for (int i = 0; i < alarm_config_list_num; i++)
    {
        if (strcmp(alarm_config_list[i].dev_type, dev_type) == 0)
        {
            return alarm_config_list[i].tages;
        }
    }
    return NULL;
}

static int init_code_register_alarm()
{
    proto_forward_t* forward = get_proto_forward_var();
    for (int i = 0; i < forward->channels_size; i++)
    {
        for (int j = 0; j < forward->channels[i]->devs_size; j++)
        {
            char*              dev_type     = forward->channels[i]->devs[j]->dev_type;
            alarm_info_cond_t* alarms_infos = get_alarm_info_from_list(dev_type);
            if (alarms_infos == NULL)
            {
                continue;
            }
            for (int a = 0; a < DEV_DEFINE_ALARM_NUM; a++)
            {
                alarm_info_cond_t* alarm = &alarms_infos[a];
                if (alarm == NULL || alarm->alarm_code == 0)
                {
                    continue;
                }
                tag_t* tag = NULL;
                for (int t = 0; t < forward->channels[i]->devs[j]->tags.ro_tags_size; t++)
                {
                    if (strcmp(forward->channels[i]->devs[j]->tags.ro_tags[t]->name, alarms_infos[a].tag_name) == 0)
                    {
                        tag = forward->channels[i]->devs[j]->tags.ro_tags[t];
                        break;
                    }
                }
                if (tag == NULL)
                {
                    for (int t = 0; t < forward->channels[i]->devs[j]->tags.rw_tags_size; t++)
                    {
                        if (strcmp(forward->channels[i]->devs[j]->tags.rw_tags[t]->name, alarms_infos[a].tag_name) == 0)
                        {
                            tag = forward->channels[i]->devs[j]->tags.rw_tags[t];
                            break;
                        }
                    }
                }
                if (tag == NULL)
                {
                    for (int t = 0; t < forward->channels[i]->devs[j]->tags.sys_info_tags_size; t++)
                    {
                        if (strcmp(forward->channels[i]->devs[j]->tags.sys_info_tags[t]->name, alarms_infos[a].tag_name) == 0)
                        {
                            tag = forward->channels[i]->devs[j]->tags.sys_info_tags[t];
                            break;
                        }
                    }
                }
                if (tag == NULL)
                {
                    ALARM_BUSINESS_LOG(BLOG_ERR, ALARM_ID_0004, alarms_infos[a].tag_name, "代码中注册的点位未在设备中找到, 注册失败, tag: %s, dev_no: %s", alarms_infos[a].tag_name, forward->channels[i]->devs[j]->no);
                    continue;
                }
                if (add_alarm_to_hash_from_code(alarm, tag) == 0)
                {
                    //ALARM_BUSINESS_LOG(BLOG_NOTICE, ALARM_ID_0004, tag->dev_tag_name, "告警点注册成功, tag: %s", tag->dev_tag_name);
                }
                // register_alarm_callback_to_tag(tag);
            }
        }
    }
    return 0;
}

// 初始化代码中的告警点
int init_alarm_info(alarm_control_t* alarm_control)
{
    char*  file            = read_file_data(ALARM_INFO_FILE);
    cJSON* alarm_file_json = NULL;
    int ret = -1;

    if (file == NULL)
    {
        ALARM_BUSINESS_LOG(BLOG_ERR, ALARM_ID_0003, NULL, "读取告警信息文件: %s 失败，检查文件是否存在", ALARM_INFO_FILE);
        goto CLEANUP;
    }
    alarm_file_json = cJSON_Parse(file);
    if (alarm_file_json == NULL)
    {
        ALARM_BUSINESS_LOG(BLOG_ERR, ALARM_ID_0003, NULL, "解析告警文件：%s 失败, 检查文件格式是否是json", ALARM_INFO_FILE);
        goto CLEANUP;
    }
    else
    {
        // ALARM_BUSINESS_LOG(BLOG_ERR, ALARM_ID_0003, NULL, "解析告警文件：%s 成功", ALARM_INFO_FILE);
    }

    // 解析告警信息文件
    ret = init_alarm_info_from_json(alarm_file_json);
    if (ret != 0)
    {
        ems_syslog(LOG_ERR, "insert all alarm in temp hash failed");
        goto CLEANUP;
    }
    // 读取模板里的告警点位,注册告警
    init_template_alarm();
    // 读取代码里的告警点位,注册告警
    init_code_register_alarm();

    ret = 0;
CLEANUP:
    if (alarm_file_json != NULL)
        cJSON_Delete(alarm_file_json);
    if (file != NULL)
        free(file);
        
    return ret;
}
