修改了次级配电协议,优化消息结构
This commit is contained in:
+122
-68
@@ -763,210 +763,264 @@ int Driver::SafeReadUInt(const Json::Value& root, const std::string& key, unsign
|
||||
return 0; // 成功读取
|
||||
}
|
||||
|
||||
vector<DriverCommand> Driver::loadDriverIdFromJson(const std::string &strJson) {
|
||||
Json::CharReaderBuilder builder;
|
||||
Json::Value root;
|
||||
std::string errs;
|
||||
vector<DriverCommand> driverCmd;
|
||||
|
||||
// 解析JSON字符串
|
||||
std::istringstream s(strJson);
|
||||
if (!Json::parseFromStream(builder, s, &root, &errs)) {
|
||||
std::cerr << "JSON解析失败: " << errs << std::endl;
|
||||
return driverCmd;
|
||||
}
|
||||
// 设备字段名到ID的映射
|
||||
const std::map<std::string, int> fieldToId = {
|
||||
{"depth1", 18}, {"depth2", 19}, {"depth3", 20},
|
||||
{"overDeep1", 21}, {"overDeep2", 22},
|
||||
{"ctd1", 23}, {"ctd2", 24}, {"soundVeloc", 25},
|
||||
{"commMast", 40}, {"ins", 57}, {"ntp", 58},
|
||||
{"inputWaterSensor1", 59}, {"inputWaterSensor2", 60}, {"inputWaterSensor3", 61},
|
||||
{"singleBeamSonar1", 0}, {"singleBeamSonar2", 1}, {"singleBeamSonar3", 2},
|
||||
{"singleBeamSonar4", 3}, {"singleBeamSonar5", 4}, {"singleBeamSonar6", 5},
|
||||
{"singleBeamSonar7", 6}, {"singleBeamSonar8", 7}, {"forwardSonar", 8},
|
||||
{"DVL", 43}, {"lowFreqSonar", 36}, {"USBL", 42},
|
||||
{"bowCover1", 9}, {"bowCover2", 10}, {"bowCover3", 11},
|
||||
{"bowCover4", 12}, {"bowCover5", 13}, {"bowCover6", 14},
|
||||
{"bowRudderDeploy", 15}, {"bowRudderServo1", 16}, {"bowRudderServo2", 17},
|
||||
{"bowWaterlnet1", 27}, {"bowWaterlnet2", 28}, {"bowWaterlnet3", 29},
|
||||
{"bowWaterlnet4", 30}, {"bowWaterlnet5", 31}, {"bowWaterlnet6", 32},
|
||||
{"bowWaterlnet7", 33}, {"bowWaterlnet8", 34}, {"waterInject", 26},
|
||||
{"mastLiftingServo", 41}, {"buoyancyAdjust1", 44}, {"buoyancyAdjust2", 45},
|
||||
{"buoyancyAdjust3", 46}, {"buoyancyAdjust4", 47}, {"buoyancyAdjust5", 48},
|
||||
{"sternRudderServo1", 49}, {"sternRudderServo2", 50}, {"sternRudderServo3", 51},
|
||||
{"sternRudderServo4", 52}, {"thruster", 53}, {"bowThrow", 54},
|
||||
{"sternThrow", 55}, {"photoSensor", 38}, {"electSensor", 39},
|
||||
{"launchControler", 37}, {"sideSonar", 35}, {"buoyage", 56}
|
||||
};
|
||||
|
||||
// 检查每个字段,如果存在且值发生变化,则添加到返回向量中
|
||||
for (const auto& [field, id] : fieldToId) {
|
||||
if (root.isMember(field)) {
|
||||
unsigned char newValue = root[field].asUInt();
|
||||
// unsigned char& currentValue = *((unsigned char*)&table + offsetof(DriverTable, depth1) +
|
||||
// (fieldToId.at(field) - 18) * sizeof(unsigned char));
|
||||
|
||||
DriverCommand cmd;
|
||||
cmd.id = id;
|
||||
cmd.cmd = newValue;
|
||||
driverCmd.push_back(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
return driverCmd;
|
||||
}
|
||||
|
||||
int Driver::loadDriverTableFromJson(const std::string &strJson, DriverTable& table)
|
||||
{
|
||||
Json::CharReaderBuilder builder;
|
||||
Json::Value root;
|
||||
std::string errs;
|
||||
|
||||
|
||||
// 解析JSON字符串
|
||||
std::istringstream s(strJson);
|
||||
if (!Json::parseFromStream(builder, s, &root, &errs))
|
||||
{
|
||||
std::cerr << "JSON解析失败: " << errs << std::endl;
|
||||
return -3; // 解析失败
|
||||
|
||||
}
|
||||
|
||||
// 读取 JSON 数据并填充结构体,逐个字段检查
|
||||
int error_code = 0;
|
||||
|
||||
error_code = SafeReadUInt(root, "depth1", table.depth1);
|
||||
error_code = SafeReadUInt(root, "depth1_uint8", table.depth1);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "depth2", table.depth2);
|
||||
error_code = SafeReadUInt(root, "depth2_uint8", table.depth2);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "depth3", table.depth3);
|
||||
error_code = SafeReadUInt(root, "depth3_uint8", table.depth3);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "overDeep1", table.overDeep1);
|
||||
// error_code = SafeReadUInt(root, "overDeep1_uint8", table.overDeep1);
|
||||
// if (error_code != 0) return error_code;
|
||||
|
||||
// error_code = SafeReadUInt(root, "overDeep2_uint8", table.overDeep2);
|
||||
// if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "ctd1_uint8", table.ctd1);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "overDeep2", table.overDeep2);
|
||||
error_code = SafeReadUInt(root, "ctd2_uint8", table.ctd2);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "ctd1", table.ctd1);
|
||||
error_code = SafeReadUInt(root, "soundVeloc_uint8", table.soundVeloc);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "ctd2", table.ctd2);
|
||||
error_code = SafeReadUInt(root, "commDev_uint8", table.commMast);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "soundVeloc", table.soundVeloc);
|
||||
error_code = SafeReadUInt(root, "ins_uint8", table.ins);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "commMast", table.commMast);
|
||||
error_code = SafeReadUInt(root, "ntp_uint8", table.ntp);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "ins", table.ins);
|
||||
error_code = SafeReadUInt(root, "inputWaterSensor1_uint8", table.inputWaterSensor1);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "ntp", table.ntp);
|
||||
error_code = SafeReadUInt(root, "inputWaterSensor2_uint8", table.inputWaterSensor2);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "inputWaterSensor1", table.inputWaterSensor1);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "inputWaterSensor2", table.inputWaterSensor2);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "inputWaterSensor3", table.inputWaterSensor3);
|
||||
error_code = SafeReadUInt(root, "inputWaterSensor3_uint8", table.inputWaterSensor3);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
// 水声设备
|
||||
error_code = SafeReadUInt(root, "singleBeamSonar1", table.singleBeamSonar1);
|
||||
error_code = SafeReadUInt(root, "singleBeamSonar1_uint8", table.singleBeamSonar1);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "singleBeamSonar2", table.singleBeamSonar2);
|
||||
error_code = SafeReadUInt(root, "singleBeamSonar2_uint8", table.singleBeamSonar2);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "singleBeamSonar3", table.singleBeamSonar3);
|
||||
error_code = SafeReadUInt(root, "singleBeamSonar3_uint8", table.singleBeamSonar3);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "singleBeamSonar4", table.singleBeamSonar4);
|
||||
error_code = SafeReadUInt(root, "singleBeamSonar4_uint8", table.singleBeamSonar4);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "singleBeamSonar5", table.singleBeamSonar5);
|
||||
error_code = SafeReadUInt(root, "singleBeamSonar5_uint8", table.singleBeamSonar5);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "singleBeamSonar6", table.singleBeamSonar6);
|
||||
error_code = SafeReadUInt(root, "singleBeamSonar6_uint8", table.singleBeamSonar6);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "singleBeamSonar7", table.singleBeamSonar7);
|
||||
error_code = SafeReadUInt(root, "singleBeamSonar7_uint8", table.singleBeamSonar7);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "singleBeamSonar8", table.singleBeamSonar8);
|
||||
error_code = SafeReadUInt(root, "singleBeamSonar8_uint8", table.singleBeamSonar8);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "forwardSonar", table.forwardSonar);
|
||||
error_code = SafeReadUInt(root, "forwardSonar_uint8", table.forwardSonar);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "DVL", table.DVL);
|
||||
error_code = SafeReadUInt(root, "DVL_uint8", table.DVL);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "lowFreqSonar", table.lowFreqSonar);
|
||||
error_code = SafeReadUInt(root, "lowFreqSonar_uint8", table.lowFreqSonar);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "USBL", table.USBL);
|
||||
error_code = SafeReadUInt(root, "USBL_uint8", table.USBL);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
// 执行机构
|
||||
error_code = SafeReadUInt(root, "bowWaterlnet1", table.bowWaterlnet1);
|
||||
error_code = SafeReadUInt(root, "bowWaterlnet1_uint8", table.bowWaterlnet1);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "bowWaterlnet2", table.bowWaterlnet2);
|
||||
error_code = SafeReadUInt(root, "bowWaterlnet2_uint8", table.bowWaterlnet2);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "bowWaterlnet3", table.bowWaterlnet3);
|
||||
error_code = SafeReadUInt(root, "bowWaterlnet3_uint8", table.bowWaterlnet3);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "bowWaterlnet4", table.bowWaterlnet4);
|
||||
error_code = SafeReadUInt(root, "bowWaterlnet4_uint8", table.bowWaterlnet4);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "bowWaterlnet5", table.bowWaterlnet5);
|
||||
error_code = SafeReadUInt(root, "bowWaterlnet5_uint8", table.bowWaterlnet5);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "bowWaterlnet6", table.bowWaterlnet6);
|
||||
error_code = SafeReadUInt(root, "bowWaterlnet6_uint8", table.bowWaterlnet6);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "bowWaterlnet7", table.bowWaterlnet7);
|
||||
error_code = SafeReadUInt(root, "bowWaterlnet7_uint8", table.bowWaterlnet7);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "bowWaterlnet8", table.bowWaterlnet8);
|
||||
error_code = SafeReadUInt(root, "bowWaterlnet8_uint8", table.bowWaterlnet8);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "bowRudderDeploy", table.bowRudderDeploy);
|
||||
error_code = SafeReadUInt(root, "bowRudderDeploy_uint8", table.bowRudderDeploy);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "bowRudderServo1", table.bowRudderServo1);
|
||||
error_code = SafeReadUInt(root, "bowRudderServo1_uint8", table.bowRudderServo1);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "bowRudderServo2", table.bowRudderServo2);
|
||||
error_code = SafeReadUInt(root, "bowRudderServo2_uint8", table.bowRudderServo2);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "bowCover1", table.bowCover1);
|
||||
error_code = SafeReadUInt(root, "bowCover1_uint8", table.bowCover1);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "bowCover2", table.bowCover2);
|
||||
error_code = SafeReadUInt(root, "bowCover2_uint8", table.bowCover2);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "bowCover3", table.bowCover3);
|
||||
error_code = SafeReadUInt(root, "bowCover3_uint8", table.bowCover3);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "bowCover4", table.bowCover4);
|
||||
error_code = SafeReadUInt(root, "bowCover4_uint8", table.bowCover4);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "bowCover5", table.bowCover5);
|
||||
error_code = SafeReadUInt(root, "bowCover5_uint8", table.bowCover5);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "bowCover6", table.bowCover6);
|
||||
error_code = SafeReadUInt(root, "bowCover6_uint8", table.bowCover6);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "waterInject", table.waterInject);
|
||||
error_code = SafeReadUInt(root, "waterInject_uint8", table.waterInject);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "mastLiftingServo", table.mastLiftingServo);
|
||||
error_code = SafeReadUInt(root, "mastLiftingServo_uint8", table.mastLiftingServo);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "buoyancyAdjust1", table.buoyancyAdjust1);
|
||||
error_code = SafeReadUInt(root, "buoyancyAdjust1_uint8", table.buoyancyAdjust1);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "buoyancyAdjust2", table.buoyancyAdjust2);
|
||||
error_code = SafeReadUInt(root, "buoyancyAdjust2_uint8", table.buoyancyAdjust2);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "buoyancyAdjust3", table.buoyancyAdjust3);
|
||||
error_code = SafeReadUInt(root, "buoyancyAdjust3_uint8", table.buoyancyAdjust3);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "buoyancyAdjust4", table.buoyancyAdjust4);
|
||||
error_code = SafeReadUInt(root, "buoyancyAdjust4_uint8", table.buoyancyAdjust4);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "buoyancyAdjust5", table.buoyancyAdjust5);
|
||||
error_code = SafeReadUInt(root, "buoyancyAdjust5_uint8", table.buoyancyAdjust5);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "sternRudderServo1", table.sternRudderServo1);
|
||||
error_code = SafeReadUInt(root, "sternRudderServo1_uint8", table.sternRudderServo1);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "sternRudderServo2", table.sternRudderServo2);
|
||||
error_code = SafeReadUInt(root, "sternRudderServo2_uint8", table.sternRudderServo2);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "sternRudderServo3", table.sternRudderServo3);
|
||||
error_code = SafeReadUInt(root, "sternRudderServo3_uint8", table.sternRudderServo3);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "sternRudderServo4", table.sternRudderServo4);
|
||||
error_code = SafeReadUInt(root, "sternRudderServo4_uint8", table.sternRudderServo4);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "thruster", table.thruster);
|
||||
error_code = SafeReadUInt(root, "thruster_uint8", table.thruster);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "bowThrow", table.bowThrow);
|
||||
error_code = SafeReadUInt(root, "bowThrow_uint8", table.bowThrow);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "sternThrow", table.sternThrow);
|
||||
error_code = SafeReadUInt(root, "sternThrow_uint8", table.sternThrow);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
// 负载
|
||||
error_code = SafeReadUInt(root, "photoSensor", table.photoSensor);
|
||||
error_code = SafeReadUInt(root, "photoSensor_uint8", table.photoSensor);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "electSensor", table.electSensor);
|
||||
error_code = SafeReadUInt(root, "electSensor_uint8", table.electSensor);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "launchControler", table.launchControler);
|
||||
error_code = SafeReadUInt(root, "launchControler_uint8", table.launchControler);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "sideSonar", table.sideSonar);
|
||||
error_code = SafeReadUInt(root, "sideSonar_uint8", table.sideSonar);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
error_code = SafeReadUInt(root, "buoyage", table.buoyage);
|
||||
error_code = SafeReadUInt(root, "buoyage_uint8", table.buoyage);
|
||||
if (error_code != 0) return error_code;
|
||||
|
||||
return 0; // 返回0表示所有数据成功读取
|
||||
|
||||
Reference in New Issue
Block a user