#pragma once

#include <set>
#include <map>
#include <vector>
#include <memory>
#include <string> 
#include <unordered_map>

#include "common/Fields.h"
#include "app/DataStruct.h"

class CommEntity;

class Device
{
public:
    static std::shared_ptr<Device> create(Fields& fields);

    Device();

    void setFields(Fields& fields);

    int startComm();

    //void getCacheVoltage(std::vector<std::string>& vec);
    //void getCacheCurrent(std::vector<std::string>& vec);
    //void getCachePower(std::vector<std::string>& vec);

    // int datatype: 1: 电压，2：电流，3：功率
    void setCache(int datatype, std::vector<float>& vec);

    bool cache(int npos);
    void storeDB(int npos);

    void setParam(std::string k, int v);
    std::string getParam(std::string k, std::string defaultVal = "");

    void getRuntimeParams(std::vector<std::pair<std::string, std::string>>& params);
    void getRuntimeParams1(std::vector<std::pair<std::string, std::string>>& params);

    void setBCUUnit(std::string k, int pos, int v, int count);

public:


    int stationId = {0};
    int deviceId = {0};
    int type = {0};
    std::string name;
    std::string code;
    int category = 0;
    bool isOpen = false;
    std::string attrsJson = "";
    int sortNo {0};

    int err = 0;
    int online = 0;
    int running = 0;

    int64_t ts {0};

    //std::map<std::string, std::string> mapAttrs;
    Fields attrs;

    // 通讯entity
    std::shared_ptr<CommEntity> commEntity;

    int64_t tsDataDate {};
    //std::map<int, float> mapCacheVoltage;
    //std::map<int, float> mapCacheCurrent;
    //std::map<int, float> mapCachePower;

    std::vector<float> cacheU;
    std::vector<float> cacheI;
    std::vector<float> cacheP;

    // 参数值（保存从设备读取的参数值）
    std::map<std::string, std::string> mapParams;

    // 参数定义（定义参数的基础信息，包括寄存器地址，名称，单位，倍率）
    std::map<std::string, DeviceParamAddr*> mapMyParams;

    std::vector<std::vector<float>> vecBCUUnit;
    int bcuCount {0};

};
