PowerAdapter: JSON format support

This commit is contained in:
李通洲
2023-09-06 16:23:57 +08:00
parent a7c6e1165c
commit eb4636b947
2 changed files with 31 additions and 1 deletions
+30 -1
View File
@@ -62,7 +62,7 @@ void ffPrintPowerAdapter(FFPowerAdapterOptions* options)
void ffInitPowerAdapterOptions(FFPowerAdapterOptions* options)
{
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_POWERADAPTER_MODULE_NAME, ffParsePowerAdapterCommandOptions, ffParsePowerAdapterJsonObject, ffPrintPowerAdapter, NULL);
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_POWERADAPTER_MODULE_NAME, ffParsePowerAdapterCommandOptions, ffParsePowerAdapterJsonObject, ffPrintPowerAdapter, ffGeneratePowerAdapterJson);
ffOptionInitModuleArg(&options->moduleArgs);
}
@@ -97,3 +97,32 @@ void ffParsePowerAdapterJsonObject(FFPowerAdapterOptions* options, yyjson_val* m
ffPrintError(FF_POWERADAPTER_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
}
}
void ffGeneratePowerAdapterJson(FF_MAYBE_UNUSED FFPowerAdapterOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(PowerAdapterResult));
const char* error = ffDetectPowerAdapterImpl(&results);
if (error)
{
yyjson_mut_obj_add_str(doc, module, "error", error);
}
else if(results.length == 0)
{
yyjson_mut_obj_add_str(doc, module, "error", "No power adapters found");
}
else
{
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
FF_LIST_FOR_EACH(PowerAdapterResult, item, results)
{
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
yyjson_mut_obj_add_strbuf(doc, obj, "description", &item->description);
yyjson_mut_obj_add_strbuf(doc, obj, "manufacturer", &item->manufacturer);
yyjson_mut_obj_add_strbuf(doc, obj, "modelName", &item->modelName);
yyjson_mut_obj_add_strbuf(doc, obj, "name", &item->name);
yyjson_mut_obj_add_int(doc, obj, "watts", item->watts);
}
}
}
+1
View File
@@ -9,3 +9,4 @@ void ffInitPowerAdapterOptions(FFPowerAdapterOptions* options);
bool ffParsePowerAdapterCommandOptions(FFPowerAdapterOptions* options, const char* key, const char* value);
void ffDestroyPowerAdapterOptions(FFPowerAdapterOptions* options);
void ffParsePowerAdapterJsonObject(FFPowerAdapterOptions* options, yyjson_val* module);
void ffGeneratePowerAdapterJson(FFPowerAdapterOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module);