Initial commit
This commit is contained in:
+160
@@ -0,0 +1,160 @@
|
||||
From 3079d7621187cb966f19726f2f31fb03f0080eed Mon Sep 17 00:00:00 2001
|
||||
From: roly <Rolyli.Li@luxshare-ict.com>
|
||||
Date: Wed, 11 Dec 2024 17:08:44 +0800
|
||||
Subject: [PATCH] add sensor config file
|
||||
|
||||
---
|
||||
scripts/meson.build | 19 +++++-
|
||||
scripts/sensor_data_record_gen.py | 104 ++++++++++++++++++++++++++++++
|
||||
2 files changed, 120 insertions(+), 3 deletions(-)
|
||||
create mode 100755 scripts/sensor_data_record_gen.py
|
||||
|
||||
diff --git a/scripts/meson.build b/scripts/meson.build
|
||||
index b739e6f..ae5358c 100644
|
||||
--- a/scripts/meson.build
|
||||
+++ b/scripts/meson.build
|
||||
@@ -3,11 +3,11 @@ python_exe = find_program('python3', 'python')
|
||||
|
||||
sensor_gen = custom_target('sensor-gen',
|
||||
output: 'sensor-gen.cpp',
|
||||
- input: [ 'sensor_gen.py', get_option('sensor-yaml-gen')],
|
||||
+ input: files(get_option('sensor-yaml-gen')),
|
||||
command: [
|
||||
python_exe,
|
||||
- '@INPUT0@',
|
||||
- '-i', '@INPUT1@',
|
||||
+ files('sensor_gen.py'),
|
||||
+ '-i', '@INPUT@',
|
||||
'-o', meson.current_build_dir(),
|
||||
'generate-cpp',
|
||||
],
|
||||
@@ -39,3 +39,16 @@ fru_gen = custom_target('fru-gen',
|
||||
],
|
||||
)
|
||||
generated_src += fru_gen
|
||||
+
|
||||
+sensor_data_gen = custom_target('sensor-json-gen',
|
||||
+ output: 'sensor-data-record.json',
|
||||
+ input: files(get_option('sensor-yaml-gen')),
|
||||
+ command: [
|
||||
+ files('sensor_data_record_gen.py'),
|
||||
+ '-i', '@INPUT@',
|
||||
+ '-o', meson.current_build_dir(),
|
||||
+ ],
|
||||
+ install: true,
|
||||
+ install_dir: get_option('datadir') / 'ipmi-providers',
|
||||
+)
|
||||
+generated_src += sensor_data_gen
|
||||
diff --git a/scripts/sensor_data_record_gen.py b/scripts/sensor_data_record_gen.py
|
||||
new file mode 100755
|
||||
index 0000000..2ef58b7
|
||||
--- /dev/null
|
||||
+++ b/scripts/sensor_data_record_gen.py
|
||||
@@ -0,0 +1,104 @@
|
||||
+#!/usr/bin/env python3
|
||||
+
|
||||
+import argparse
|
||||
+import json
|
||||
+import os
|
||||
+
|
||||
+import yaml
|
||||
+
|
||||
+
|
||||
+def getSensorParams(sensor, sensor_params):
|
||||
+ path = sensor["path"]
|
||||
+ sensorName = sensor.get("sensorName", path[path.rfind("/") + 1 :])
|
||||
+ sensorReadingType = sensor.get("sensorReadingType", 0)
|
||||
+ sensorType = sensor.get("sensorType", 0)
|
||||
+ sensor_params[path] = {
|
||||
+ "sensorName": sensorName,
|
||||
+ "sensorReadingType": sensorReadingType,
|
||||
+ "sensorType": sensorType,
|
||||
+ }
|
||||
+
|
||||
+ overridePaths = sensor.get("overridePaths", [])
|
||||
+ for item in overridePaths:
|
||||
+ sensorName = item[item.rfind("/") + 1 :]
|
||||
+ sensor_params[item] = {
|
||||
+ "sensorName": sensorName,
|
||||
+ "sensorReadingType": sensorReadingType,
|
||||
+ "sensorType": sensorType,
|
||||
+ }
|
||||
+ return sensor_params
|
||||
+
|
||||
+
|
||||
+def main():
|
||||
+ app_description = "Convert sensor yaml to json."
|
||||
+
|
||||
+ parser = argparse.ArgumentParser(
|
||||
+ description=app_description,
|
||||
+ formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
+ )
|
||||
+ parser.add_argument(
|
||||
+ "-i",
|
||||
+ "--sensor_yaml",
|
||||
+ dest="sensor_yaml",
|
||||
+ nargs="*",
|
||||
+ type=str,
|
||||
+ default=[],
|
||||
+ help="input sensor yaml file list to parse",
|
||||
+ )
|
||||
+ parser.add_argument(
|
||||
+ "-o",
|
||||
+ "--output-dir",
|
||||
+ dest="outputdir",
|
||||
+ help="The directory of json file",
|
||||
+ )
|
||||
+ parser.add_argument(
|
||||
+ "-f",
|
||||
+ "--override-flag",
|
||||
+ dest="override",
|
||||
+ default="",
|
||||
+ help="override flag",
|
||||
+ )
|
||||
+ args = parser.parse_args()
|
||||
+
|
||||
+ if not args.override and len(args.sensor_yaml) > 1:
|
||||
+ raise RuntimeError(
|
||||
+ "Not support multi machine, but there are multi sensor yamls"
|
||||
+ )
|
||||
+ total_sensor_params = {}
|
||||
+
|
||||
+ for yaml_file in args.sensor_yaml:
|
||||
+ with open(yaml_file, "r") as y:
|
||||
+ sensors = yaml.safe_load(y)
|
||||
+ sensors = [i for i in sensors.values()]
|
||||
+ sensor_params = {}
|
||||
+ for s in sensors:
|
||||
+ sensor_params = getSensorParams(s, sensor_params)
|
||||
+ for sensor_param in sensor_params:
|
||||
+ # if the sensor is already in the total_sensor_params, check the value is must same
|
||||
+ if sensor_param in total_sensor_params:
|
||||
+ if (
|
||||
+ total_sensor_params[sensor_param]
|
||||
+ != sensor_params[sensor_param]
|
||||
+ ):
|
||||
+ raise RuntimeError(
|
||||
+ "The sensor is already in the total_sensor_params, but the value is different"
|
||||
+ )
|
||||
+ else:
|
||||
+ total_sensor_params[sensor_param] = sensor_params[
|
||||
+ sensor_param
|
||||
+ ]
|
||||
+ output_json = os.path.join(args.outputdir, "sensor-data-record.json")
|
||||
+ with open(output_json, "w") as j:
|
||||
+ j.write(
|
||||
+ json.dumps(
|
||||
+ total_sensor_params,
|
||||
+ indent=4,
|
||||
+ sort_keys=True,
|
||||
+ separators=(",", ": "),
|
||||
+ )
|
||||
+ )
|
||||
+ j.write("\n")
|
||||
+
|
||||
+
|
||||
+if __name__ == "__main__":
|
||||
+ main()
|
||||
--
|
||||
2.25.1
|
||||
|
||||
Reference in New Issue
Block a user