#ifndef _LOCAL_DATA_LASTING_H__
#define _LOCAL_DATA_LASTING_H__
//#include "tag.h"
#include <sqlite3.h>
#include <stdio.h>
#include <string.h>
#include "common.h"
#ifdef EN_EMS
#define SHM_NAME "data_persistence"
#else
#define SHM_NAME "lc_data_persistence"
#endif

#define SYS_PERSISTENCE_DIR "/app/database"


typedef struct shm_tag_info
{
    int status; // 用于跟踪当前点位是否有使用，当前配置有使用的点设置为1，没有使用设置为0
    char dev_tag_name[96];
    int datatype;
    union {
        long to_int;
        double to_float;
    }tag_cache;//VALUE
    //
    void *tag_cache_p;
}shm_tag_info;


typedef struct shm_info
{
    int fd;
    int count;//用于记录总数
    int tags_index;//用于记录当前位置
    int shm_adress_version;
    shm_tag_info tags[0];
}shm_info_t;

typedef struct data_presistence_info
{
    int startup_type;
    int init_flag;
    shm_info_t *shm_info;
    sqlite3 *db_info;
    int db_update_interval;  //数据库写入周期
}data_presistence_info;

enum startup_type {
    STARTUP_TYPE_FIRST_START = 1,
    STARTUP_TYPE_POWER_DOWN,
    STARTUP_TYPE_RESTART,
    STARTUP_TYPE_CREATE_DB
};

void updata_data_persistence(void *par_tar);
data_presistence_info *new_data_presistence(const char *shm_name, int update_interval);
void get_last_data_from_data_presistence(data_presistence_info * presistence_ptr, void *param);
int data_presistence_start(data_presistence_info * presistence_ptr, const char *thread_name);
#endif
