#include "Device.h" #include "common/Spdlogger.h" #include "common/Utils.h" #include "protocol/CommEntity.h" #include "common/JsonN.h" #include static std::unordered_set g_setCacheDeviceType = {3, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110}; static bool CheckCacheType(int type) { return g_setCacheDeviceType.find(type) != g_setCacheDeviceType.end(); } std::shared_ptr Device::create(Fields& fields) { auto device = std::make_shared(); device->deviceId = fields.get("device_id"); device->type = fields.get("type"); device->name = fields.value("name"); device->code = fields.value("code"); device->isOpen = fields.get("is_open"); device->attrsJson = fields.value("attrs"); device->category = fields.get("category"); // 解析属性的JSON字符串,转换成键值对 njson jsonroot; bool ret = JSON::parse(device->attrsJson, jsonroot); if (!ret) // 解析错误 { spdlog::error("[device] device attr json parse error, device_id={}", device->deviceId); } else { for (auto& [key, val] : jsonroot.items()) { std::string valType = val.type_name(); if (valType == "string") { device->attrs.set(key, val.get()); } else if (valType == "number") { device->attrs.set(key, val.get()); } else { spdlog::error("[device] device attr unknown type: key={}, valtype={}", key, valType); } } } //int step = 600; //for (int i = 0; i*600<86400; ++i) //{ // double voltage = double(Utils::random(20000, 30000))*0.01; // double current = double(Utils::random(1000, 2000))*0.01; // device->mapCacheVoltage[i*step] = voltage; // device->mapCacheCurrent[i*step] = current; // device->mapCachePower[i*step] = voltage * current; //} // 启动通讯,该函数中会自动判断isOpen状态,选择是否进行通讯连接 //device->startComm(); return device; } int Device::startComm() { if (!isOpen) { if (commEntity && commEntity->alive) { commEntity->close(); } return 0; } //// 从属性列表中获取通讯方式和通讯地址、端口 //std::string commType = attrs.value("commType"); // //// 如果entity的通讯协议类型当前配置不一致,需要关闭连接删除通讯后创建新的通讯 //if (commEntity && commEntity->type != commType) //{ // commEntity->close(); // commEntity = nullptr; //} //// 创建新的通讯 //if (!commEntity) //{ // commEntity = CommEntity::create(attrs); // if (!commEntity) { return -1; } //} //commEntity->start(); return 0; } void Device::getCacheVoltage(std::vector& vec) { vec.resize(mapCacheVoltage.size()); int i = 0; for (auto iter = mapCacheVoltage.begin(); iter != mapCacheVoltage.end(); ++iter) { vec[i] = Utils::toStr(iter->second); i++; } } void Device::getCacheCurrent(std::vector& vec) { vec.resize(mapCacheCurrent.size()); int i = 0; for (auto iter = mapCacheCurrent.begin(); iter != mapCacheCurrent.end(); ++iter) { vec[i] = Utils::toStr(iter->second); i++; } } void Device::getCachePower(std::vector& vec) { vec.resize(mapCachePower.size()); int i = 0; for (auto iter = mapCachePower.begin(); iter != mapCachePower.end(); ++iter) { vec[i] = Utils::toStr(iter->second); i++; } } int64_t GetCurrentTimePos(int step) { auto tp = chrono::system_clock::now(); int64_t tTime = chrono::time_point_cast(tp).time_since_epoch().count(); std::time_t t = chrono::system_clock::to_time_t(tp); std::tm* tmlocal = localtime(&t); tmlocal->tm_hour = 0; tmlocal->tm_min = 0; tmlocal->tm_sec = 0; int64_t tDate = chrono::time_point_cast(chrono::system_clock::from_time_t(mktime(tmlocal))).time_since_epoch().count(); return (tTime - tDate) / step; } void Device::setCache(int datatype, std::vector& vec) { std::map* mapptr = NULL; if (datatype == 1) { mapptr = &mapCacheVoltage; } else if (datatype == 2) { mapptr = &mapCacheCurrent; } else if (datatype == 3) { mapptr = &mapCachePower; } if (mapptr) { const int step = 600; const int N = 86400/step; int n = GetCurrentTimePos(step); for (int i = 0; itype)) { return false; } if (npos == 0) { mapCacheVoltage.clear(); mapCacheCurrent.clear(); mapCachePower.clear(); } // 根据设备类型从参数(寄存器地址)中读取实时数据进行保存 mapCacheVoltage[npos] = Utils::random(100, 200); mapCacheCurrent[npos] = Utils::random(100, 200); mapCachePower[npos] = Utils::random(100, 200); return true; } void Device::storeDB(int npos) { } void Device::setParam(std::string k, std::string v) { mapParams[k] = v; if (type == 3 ) // 电表 { if (k == "") this->err = Utils::toInt(v); } else if (type == 101) // EMS { } else if (type == 102) // PCS { if (k == "0x1003") err = Utils::toInt(v); // 故障状态 R uint16 1故障,0正常 0 0x1003 if (k == "0x1005") online = Utils::toInt(v); // 设备在线 R uint16 1在线,0无效 1 0x1005 if (k == "0x1009") running = (v=="1" || v=="2"); //充放状态 R uint16 0:待机, 1:充电, 2:放电, 3:搁置 0 0x1009 } else if (type == 103) // PCU { if (k == "0x1002") err = Utils::toInt(v); //故障状态 R uint16 1故障,0正常 0 0x1002 if (k == "0x1004") online = Utils::toInt(v); //设备在线 R uint16 1在线,0无效 1 0x1004 if (k == "0x1006") running = Utils::toInt(v); //启停状态 R uint16 1开机,0关机 1 0x1006 } else if (type == 104) // BMS { if (k == "0x004A") { err = (v=="1"); online = 1; } //运行状态 R uint16 0 运行状态 0-正常 1-告警 2-保护 0x004A if (k == "0x004B") running = (v=="1" || v=="2"); //充放电状态 R uint16 0 0-待机 1-充电 2-放电 0x004B } else if (type == 105) // BCU { if (k == "0xA003") running = (v=="51" || v=="68"); //蓄电池充放电状态 R uint16 "0x11开路,0x22待机,0x33充电,0x44放电" 34 0xA003 if (k == "0xA004") err = (v=="85"); online=1; //电池组运行状态 R uint16 "0x11跳机,0x22待机,0x33放空,0x44充满,0x55预警,0x66正常" 102 0xA004 } else if (type == 106) // 充电桩 { } else if (type == 109) // 光伏板 { } } std::string Device::getParam(std::string k, std::string defaultVal) { auto iter = mapParams.find(k); if (iter != mapParams.end()) { return iter->second; } return defaultVal; } void Device::getRuntimeParams(std::vector>& params) { // 3 电表 // 101 EMS // 102 PCS // 103 PCU // 104 BMS // 105 BCU // 106 充电桩 // 109 光伏板 if (this->type == 3) { params.push_back({"A相电压", getParam("0x000B", "0.0") + " V"}); params.push_back({"A相电流", getParam("0x000D", "0.0") + " A"}); params.push_back({"B相电压", getParam("0x000F", "0.0") + " V"}); params.push_back({"B相电流", getParam("0x0011", "0.0") + " A"}); params.push_back({"C相电压", getParam("0x0013", "0.0") + " V"}); params.push_back({"C相电流", getParam("0x0015", "0.0") + " A"}); } else if (this->type == 101) // EMS { params.push_back({"A相电压", getParam("0x107E", "0.0") + " V"}); params.push_back({"A相电流", getParam("0x1084", "0.0") + " A"}); params.push_back({"B相电压", getParam("0x1080", "0.0") + " V"}); params.push_back({"B相电流", getParam("0x1086", "0.0") + " A"}); params.push_back({"C相电压", getParam("0x1082", "0.0") + " V"}); params.push_back({"C相电流", getParam("0x1088", "0.0") + " A"}); } else if (this->type == 102) // PCS { params.push_back({"A相电压", getParam("0x0010", "0.0") + " V"}); params.push_back({"A相电流", getParam("0x0019", "0.0") + " A"}); params.push_back({"B相电压", getParam("0x0011", "0.0") + " V"}); params.push_back({"B相电流", getParam("0x001A", "0.0") + " A"}); params.push_back({"C相电压", getParam("0x0011", "0.0") + " V"}); params.push_back({"C相电流", getParam("0x001B", "0.0") + " A"}); } else if (this->type == 103) // PCU { params.push_back({"A相电压", getParam("0x0013", "0.0") + " V"}); params.push_back({"A相电流", getParam("0x001C", "0.0") + " A"}); params.push_back({"B相电压", getParam("0x0014", "0.0") + " V"}); params.push_back({"B相电流", getParam("0x001D", "0.0") + " A"}); params.push_back({"C相电压", getParam("0x0015", "0.0") + " V"}); params.push_back({"C相电流", getParam("0x001E", "0.0") + " A"}); } else if (this->type == 104) // BMS { params.push_back({"SOC", getParam("0x0001", "0") + " %"}); params.push_back({"SOH", getParam("0x0002", "0") + " %"}); params.push_back({"电压", getParam("0x0003", "0.0") + " V"}); params.push_back({"电流", getParam("0x0005", "0.0") + " A"}); params.push_back({"单体最大电压", getParam("0x0021", "0.0") + " V"}); params.push_back({"单体最小电压", getParam("0x0024", "0.0") + " V"}); params.push_back({"单体最大温度", getParam("0x0029", "0.0") + " ℃"}); params.push_back({"单体最小温度", getParam("0x002C", "0.0") + " ℃"}); } else if (this->type == 105) // BCU { params.push_back({"簇电压", getParam("0x0003", "0.0") + " V"}); params.push_back({"簇电流", getParam("0x0005", "0") + " A"}); params.push_back({"簇温度", getParam("0x0007", "0.0") + " ℃"}); params.push_back({"簇电阻", getParam("0x0009", "0.0") + " Ω"}); params.push_back({"簇SOC", getParam("0x000B", "0") + " %"}); params.push_back({"簇SOH", getParam("0x000C", "0") + " %"}); } else { params.push_back({"额定电压", getParam("0x0001", "0.0") + " V"}); params.push_back({"实时电压", getParam("0x0001", "0.0") + " V"}); params.push_back({"额定电流", getParam("0x0001", "0.0") + " A"}); params.push_back({"实时电流", getParam("0x0001", "0.0") + " A"}); params.push_back({"额定功率", getParam("0x0001", "0.0") + " W"}); params.push_back({"实时功率", getParam("0x0001", "0.0") + " W"}); } }