mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
完成系统管理web端功能,实现系统管理服务端接口,实现登录功能
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
#include "Device.h"
|
||||
|
||||
#include "common/Logger.h"
|
||||
#include "common/Utils.h"
|
||||
#include "protocol/Communicator.h"
|
||||
#include "common/JsonN.h"
|
||||
|
||||
//int DeviceEntity::getAttrInt(std::string key)
|
||||
//{
|
||||
// auto iter = mapAttrs.find(key);
|
||||
// if (iter == mapAttrs.end()) { return 0; }
|
||||
// return Utils::toInt(iter->second);
|
||||
//}
|
||||
//
|
||||
//float DeviceEntity::getAttrFloat(std::string key)
|
||||
//{
|
||||
// auto iter = mapAttrs.find(key);
|
||||
// if (iter == mapAttrs.end()) { return 0.0f; }
|
||||
// return Utils::toFloat(iter->second);
|
||||
//}
|
||||
//
|
||||
//double DeviceEntity::getAttrDouble(std::string key)
|
||||
//{
|
||||
// auto iter = mapAttrs.find(key);
|
||||
// if (iter == mapAttrs.end()) { return 0.0; }
|
||||
// return Utils::toDouble(iter->second);
|
||||
//}
|
||||
//
|
||||
//std::string DeviceEntity::getAttrStr(std::string key)
|
||||
//{
|
||||
// auto iter = mapAttrs.find(key);
|
||||
// if (iter == mapAttrs.end()) { return ""; }
|
||||
// return iter->second;
|
||||
//}
|
||||
|
||||
int DeviceEntity::startComm()
|
||||
{
|
||||
if (!isOpen)
|
||||
{
|
||||
if (commEntity && commEntity->isAlive())
|
||||
{
|
||||
commEntity->close();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 从属性列表中获取通讯方式和通讯地址、端口
|
||||
std::string commType = attrs.getStr("commType");
|
||||
|
||||
// 如果entity的通讯协议类型当前配置不一致,需要关闭连接删除通讯后创建新的通讯
|
||||
if (commEntity && commEntity->type != commType)
|
||||
{
|
||||
commEntity->close();
|
||||
commEntity = nullptr;
|
||||
}
|
||||
// 创建新的通讯
|
||||
if (!commEntity)
|
||||
{
|
||||
commEntity = Communicator::createEntity(attrs);
|
||||
if (!commEntity) { return -1; }
|
||||
}
|
||||
|
||||
commEntity->start();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// $$Device
|
||||
std::map<int, std::shared_ptr<DeviceEntity>> Device::mapDevices;
|
||||
|
||||
void Device::add(DataFields& fields)
|
||||
{
|
||||
auto entity = std::make_shared<DeviceEntity>();
|
||||
entity->deviceId = fields.getInt("device_id");
|
||||
entity->type = fields.getInt("type");
|
||||
entity->name = fields.getStr("name");
|
||||
entity->code = fields.getStr("code");
|
||||
entity->isOpen = fields.getInt("is_open");
|
||||
entity->attrsJson = fields.getStr("attrs");
|
||||
|
||||
// 解析属性的JSON字符串,转换成键值对
|
||||
NJson jsonroot;
|
||||
bool ret = NJsonParse(entity->attrsJson, jsonroot);
|
||||
if (!ret) // 解析错误
|
||||
{
|
||||
XLOGE() << "device attr json parse error, device_id=" << entity->deviceId;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (auto& [key, val] : jsonroot.items()) {
|
||||
std::string valType = val.type_name();
|
||||
if (valType == "string") {
|
||||
entity->attrs.set(key, val.get<std::string>());
|
||||
}
|
||||
else if (valType == "number") {
|
||||
entity->attrs.set(key, val.get<int>());
|
||||
}
|
||||
else {
|
||||
XLOGE() << key << ": [" << valType << "]";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 保存设备 entity 到 map
|
||||
if (entity->deviceId != -1)
|
||||
{
|
||||
mapDevices[entity->deviceId] = entity;
|
||||
}
|
||||
|
||||
// 启动通讯,该函数中会自动判断isOpen状态,选择是否进行通讯连接
|
||||
entity->startComm();
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<DeviceEntity>> Device::getDeviceByType(int type)
|
||||
{
|
||||
std::vector<std::shared_ptr<DeviceEntity>> vecDevice;
|
||||
for (auto iter = mapDevices.begin(); iter!=mapDevices.end(); ++iter)
|
||||
{
|
||||
auto device = iter->second;
|
||||
if (device && (type<=0 || device->type == type))
|
||||
{
|
||||
vecDevice.push_back(device);
|
||||
}
|
||||
}
|
||||
return vecDevice;
|
||||
}
|
||||
Reference in New Issue
Block a user