2025-07-18 09:08:09 +08:00
|
|
|
|
#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;
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
2025-08-20 19:00:22 +08:00
|
|
|
|
int Device::startComm()
|
2025-07-18 09:08:09 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (!isOpen)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (commEntity && commEntity->isAlive())
|
|
|
|
|
|
{
|
|
|
|
|
|
commEntity->close();
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 从属性列表中获取通讯方式和通讯地址、端口
|
2025-08-22 19:06:50 +08:00
|
|
|
|
std::string commType = attrs.value("commType");
|
2025-07-18 09:08:09 +08:00
|
|
|
|
|
|
|
|
|
|
// 如果entity的通讯协议类型当前配置不一致,需要关闭连接删除通讯后创建新的通讯
|
|
|
|
|
|
if (commEntity && commEntity->type != commType)
|
|
|
|
|
|
{
|
|
|
|
|
|
commEntity->close();
|
|
|
|
|
|
commEntity = nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 创建新的通讯
|
|
|
|
|
|
if (!commEntity)
|
|
|
|
|
|
{
|
|
|
|
|
|
commEntity = Communicator::createEntity(attrs);
|
|
|
|
|
|
if (!commEntity) { return -1; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
commEntity->start();
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-22 19:06:50 +08:00
|
|
|
|
std::shared_ptr<Device> Device::create(Fields& fields)
|
2025-07-18 09:08:09 +08:00
|
|
|
|
{
|
2025-08-20 19:00:22 +08:00
|
|
|
|
auto device = std::make_shared<Device>();
|
2025-08-26 18:36:25 +08:00
|
|
|
|
device->deviceId = fields.get<int>("device_id");
|
|
|
|
|
|
device->type = fields.get<int>("type");
|
2025-08-22 19:06:50 +08:00
|
|
|
|
device->name = fields.value("name");
|
|
|
|
|
|
device->code = fields.value("code");
|
2025-08-26 18:36:25 +08:00
|
|
|
|
device->isOpen = fields.get<int>("is_open");
|
2025-08-22 19:06:50 +08:00
|
|
|
|
device->attrsJson = fields.value("attrs");
|
2025-07-18 09:08:09 +08:00
|
|
|
|
|
|
|
|
|
|
// 解析属性的JSON字符串,转换成键值对
|
2025-08-26 18:36:25 +08:00
|
|
|
|
NJsonNode jsonroot;
|
|
|
|
|
|
bool ret = NJson::parse(device->attrsJson, jsonroot);
|
2025-07-18 09:08:09 +08:00
|
|
|
|
if (!ret) // 解析错误
|
|
|
|
|
|
{
|
2025-08-20 19:00:22 +08:00
|
|
|
|
XLOGE() << "device attr json parse error, device_id=" << device->deviceId;
|
2025-07-18 09:08:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
for (auto& [key, val] : jsonroot.items()) {
|
|
|
|
|
|
std::string valType = val.type_name();
|
|
|
|
|
|
if (valType == "string") {
|
2025-08-20 19:00:22 +08:00
|
|
|
|
device->attrs.set(key, val.get<std::string>());
|
2025-07-18 09:08:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (valType == "number") {
|
2025-08-20 19:00:22 +08:00
|
|
|
|
device->attrs.set(key, val.get<int>());
|
2025-07-18 09:08:09 +08:00
|
|
|
|
}
|
2025-08-20 19:00:22 +08:00
|
|
|
|
else {
|
2025-07-18 09:08:09 +08:00
|
|
|
|
XLOGE() << key << ": [" << valType << "]";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 启动通讯,该函数中会自动判断isOpen状态,选择是否进行通讯连接
|
2025-08-20 19:00:22 +08:00
|
|
|
|
device->startComm();
|
|
|
|
|
|
return device;
|
2025-07-18 09:08:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-20 19:00:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
//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;
|
|
|
|
|
|
//}
|