diff --git a/prog/sensors/chips.c b/prog/sensors/chips.c index 272e7092..b66e5904 100644 --- a/prog/sensors/chips.c +++ b/prog/sensors/chips.c @@ -99,11 +99,27 @@ void print_chip_json(const sensors_chip_name *name) } if (cnt++) printf(","); - printf("\"%s\":{", label); + subCnt = 0; + if (new_json) { + printf("\"%s\":{", feature->name); + if (strcmp(label, feature->name)) { + if (subCnt++) + printf(","); + printf("\"label\":\"%s\"", label); + } + } else + printf("\"%s\":{", label); b = 0; - subCnt = 0; while ((sub = sensors_get_all_subfeatures(name, feature, &b))) { + char *fname = sub->name; + if (new_json) { + if (strlen(feature->name) < strlen(sub->name)) { + fname += strlen(feature->name); + if (*fname == '_') /* FIXME some sensors use other separator */ + fname++; + } + } if (sub->flags & SENSORS_MODE_R) { is_temp = !strcmp("°C", sensors_get_quantity(sub->type)->unit); @@ -118,13 +134,50 @@ void print_chip_json(const sensors_chip_name *name) printf(","); if (is_temp && fahrenheit) val = deg_ctof(val); - printf("\"%s\":%.3f", sub->name, val); + if (new_json) { + const char *unit = sensors_get_quantity(sub->type)->unit; + const char *quantity = sensors_get_quantity(sub->type)->quantity; + int quaCnt = 0; + + printf("\"%s\":{", fname); + + if (is_temp && fahrenheit) + unit = "°F"; + if (strrchr(quantity, ' ')) + quantity = strrchr(quantity, ' ') + 1; + + if (sub->type == SENSORS_SUBFEATURE_TEMP_TYPE) { + if (quaCnt++) + printf(","); + printf("\"label\":\"%s\"", sensors_temp_type_name(val)); + } + + if (strlen(quantity)) { + if (quaCnt++) + printf(","); + printf("\"quantity\":\"%s\"", quantity); + } + + if (strlen(unit)) { + if (quaCnt++) + printf(","); + printf("\"unit\":\"%s\"", unit); + } + + if (quaCnt++) + printf(","); + printf("\"value\":%.16g", val); + printf("}"); + + } else { + printf("\"%s\":%f", fname, val); + } } } else { if (subCnt++) printf(","); - printf("\"%s\":NaN", sub->name); + printf("\"%s\":NaN", fname); } } free(label); diff --git a/prog/sensors/main.c b/prog/sensors/main.c index a68fe446..0031eb66 100644 --- a/prog/sensors/main.c +++ b/prog/sensors/main.c @@ -42,6 +42,7 @@ #define VERSION LM_VERSION static int do_sets, do_raw, do_json, hide_adapter; +int new_json; int fahrenheit; char degstr[5]; /* store the correct string to print degrees */ @@ -296,12 +297,13 @@ int main(int argc, char *argv[]) do_raw = 0; do_json = 0; + new_json = 0; do_sets = 0; do_bus_list = 0; hide_adapter = 0; allow_no_sensors = 0; while (1) { - c = getopt_long(argc, argv, "hsvfAc:ujn", long_opts, NULL); + c = getopt_long(argc, argv, "hsvfAc:ujJn", long_opts, NULL); if (c == EOF) break; switch(c) { @@ -333,6 +335,10 @@ int main(int argc, char *argv[]) case 'j': do_json = 1; break; + case 'J': + do_json = 1; + new_json = 1; + break; case 'B': do_bus_list = 1; break; diff --git a/prog/sensors/main.h b/prog/sensors/main.h index 7b608f0c..d53f1e64 100644 --- a/prog/sensors/main.h +++ b/prog/sensors/main.h @@ -22,6 +22,7 @@ #define PROG_SENSORS_MAIN_H extern int fahrenheit; +extern int new_json; extern char degstr[5]; #endif /* PROG_SENSORS_MAIN_H */ diff --git a/prog/sensors/sensors.1 b/prog/sensors/sensors.1 index d207aa1b..67c1b97b 100644 --- a/prog/sensors/sensors.1 +++ b/prog/sensors/sensors.1 @@ -67,7 +67,9 @@ of the output by scripts. It is also useful when writing a configuration file because it shows the raw input names which must be referenced in the configuration file. .IP -j -Json output. This mode is suitable for post-processing of the output by scripts. +Json output. +.IP -J +New json output. This mode is suitable for post-processing of the output by scripts. .IP "-v, --version" Print the program version and exit. .IP "-f, --fahrenheit"