| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- import sys
- import argparse
- from pathlib import Path
- from lxml import etree
- node_project = {
- "name": "project",
- "attr": {
- "name": "测试",
- "version": "V1.0.0",
- "description": "自动化生成测试画面监控配置文件"
- }
- }
- node_canvas = {
- "name": "canvas",
- "attr": {
- "width": "1280",
- "height": "720",
- "backgroundColor": "#000000",
- "name": "",
- "version": "V1.0.0",
- "description": "",
- "lastModified": "",
- "lastModifiedBy": "",
- "default": "0"
- }
- }
- label = {
- "name": "Label",
- "property": {
- "x": 0,
- "y": 0,
- "width": 800,
- "height": 20,
- "text": "",
- "font": "SimSun,9,-1,5,50,0,0,0,0,0",
- "fontColor": "#a0a0a4",
- "backgroundColor": "#00000000",
- "borderVisiable": "true",
- "borderColor": "#a0a0a4",
- "borderWidth": 1
- },
- "parameter": {
- "parameterName": [
- ]
- }
- }
- number = {
- "name": "Number",
- "property": {
- "x": 0,
- "y": 0,
- "width": 400,
- "height": 20,
- "rangeFirst": "[0, 1)",
- "rangeSecond": "[1, 2)",
- "rangeThird": "[2, 3)",
- "rangeFourth": "[3, 4)",
- "rangeFifth": "[4, 5]",
- "colorFirst": "#ff0000",
- "colorSecond": "#ffff00",
- "colorThird": "#00ff00",
- "colorFourth": "#ffff00",
- "colorFifth": "#ff0000",
- "oorColor": "#ffffff",
- "currentValue": 0,
- "backgroundColor": "#00000000",
- "unit": None,
- "borderVisible": True,
- "borderWidth": 2,
- "font": "SimSum,10,-1,5,50,0,0,0,0,0",
- "decimals": 8
- },
- "parameter": {
- "parameterName": [
- ]
- }
- }
- binary = {
- "name": "Binary",
- "property": {
- "x": 0,
- "y": 0,
- "width": 400,
- "height": 20,
- "backgroundColor": "#00000000",
- "borderVisiable": True,
- "borderWidth": 1,
- "font": "SimSun,9,-1,5,50,0,0,0,0,0",
- "rangeFirst": "[0, 1)",
- "rangeSecond": "[1, 2]",
- "colorFirst": "#ff0000",
- "colorSecond": "#ffff00",
- "oorColor": "#ffffff",
- "currentBinary": "0b0"
- },
- "parameter": {
- "parameterName": [
- ]
- }
- }
- def control_dict_to_node(control_dict, parent=None):
- control_node = etree.Element(control_dict["name"])
- if parent is not None:
- parent.append(control_node)
- # property
- if "property" in control_dict:
- prop_node = etree.SubElement(control_node, "property")
- for k, v in control_dict["property"].items():
- child = etree.SubElement(prop_node, k)
- if v is None:
- continue
- elif isinstance(v, bool):
- child.text = "true" if v else "false"
- else:
- child.text = str(v)
- # parameter
- if "parameter" in control_dict:
- param_node = etree.SubElement(control_node, "parameter")
- for k, v in control_dict["parameter"].items():
- # 多个 parameterName
- if isinstance(v, list):
- for item in v:
- etree.SubElement(param_node, k).text = str(item)
- else:
- etree.SubElement(param_node, k).text = str(v)
- return control_node
- def get_parameters(filename: str) -> dict:
- # 读取 XML 文件
- tree = etree.parse(filename) # 替换为你的文件路径
- root = tree.getroot()
- # 将结果转为字典
- parameters = {msg.get('ParameterName'): msg.get('NormalizedDataType')
- for msg in root.xpath('//Message')}
- # 打印字典
- # for param, dtype in parameters.items():
- # print(f"{param}: {dtype}")
- for idx, (param, dtype) in enumerate(parameters.items(), start = 0):
- print(f"{idx}: param = {param}, dtype = {dtype}")
- return parameters
- def generate_monitor_xml(filename: str, output: str, cal_parameters: list):
- parameters = get_parameters(filename)
- # ========= project 根节点 =========
- project = etree.Element(
- node_project["name"],
- **node_project.get("attr", {})
- )
- canvas_index = 0
- for idx, (param, dtype) in enumerate(parameters.items(), start = 0):
- # 如果当前 canvas 已满 36 组,则创建新 canvas
- if idx % 36 == 0:
- node_canvas["attr"]["name"] = f"canvas-{canvas_index}"
- canvas = etree.SubElement(
- project,
- node_canvas["name"],
- **node_canvas.get("attr", {})
- )
- canvas_index += 1
- y_offset = (idx % 36) * 20
- # Label
- lbl_ctrl = label.copy()
- lbl_ctrl["property"] = label["property"].copy()
- lbl_ctrl["property"]["y"] = y_offset
- lbl_ctrl["property"]["text"] = param
- control_dict_to_node(lbl_ctrl, canvas)
- # 数据控件
- if dtype == "BINARY":
- bin_ctrl = binary.copy()
- bin_ctrl["property"] = binary["property"].copy()
- bin_ctrl["property"]["x"] = lbl_ctrl["property"]["width"] + 80
- bin_ctrl["property"]["y"] = y_offset
- bin_ctrl["parameter"]["parameterName"] = [param]
- control_dict_to_node(bin_ctrl, canvas)
- else:
- num_ctrl = number.copy()
- num_ctrl["property"] = number["property"].copy()
- num_ctrl["property"]["x"] = lbl_ctrl["property"]["width"] + 80
- num_ctrl["property"]["y"] = y_offset
- num_ctrl["parameter"]["parameterName"] = [param]
- control_dict_to_node(num_ctrl, canvas)
- for idx, param in enumerate(cal_parameters, start = 0):
- if idx % 36 == 0:
- node_canvas["attr"]["name"] = f"canvas-{canvas_index}"
- canvas = etree.SubElement(
- project,
- node_canvas["name"],
- **node_canvas.get("attr", {})
- )
- canvas_index += 1
- y_offset = (idx % 36) * 20
- # Label
- lbl_ctrl = label.copy()
- lbl_ctrl["property"] = label["property"].copy()
- lbl_ctrl["property"]["y"] = y_offset
- lbl_ctrl["property"]["text"] = param
- control_dict_to_node(lbl_ctrl, canvas)
- num_ctrl = number.copy()
- num_ctrl["property"] = number["property"].copy()
- num_ctrl["property"]["x"] = lbl_ctrl["property"]["width"] + 80
- num_ctrl["property"]["y"] = y_offset
- num_ctrl["parameter"]["parameterName"] = [param]
- control_dict_to_node(num_ctrl, canvas)
- # ========= 写入文件 =========
- tree = etree.ElementTree(project)
- tree.write(
- output,
- encoding="UTF-8",
- xml_declaration=True,
- pretty_print=True
- )
- print("canvas count: ", canvas_index)
- if __name__ == "__main__":
- parser = argparse.ArgumentParser(description="Auto generate test monitor configure file by data process configure file.")
- parser.add_argument("--dataprocess", type=str, required=True, help="data process configure file (XML file)")
- parser.add_argument("-o", "--output", type=str, required=True, help="output monitor configure file (XML file)")
- args = parser.parse_args()
- dataprocess_file = args.dataprocess
- output_file = args.output
- path = Path(dataprocess_file)
- if not path.exists():
- print("data process configure is not exist.")
- sys.exit(1)
- if not path.suffix.lower() == ".xml":
- print("the dataprocess requirement is an XML file")
- sys.exit(1)
- if not Path(output_file).suffix.lower() == ".xml":
- print("the output requirement is an XML file")
- sys.exit(1)
- cal_parameters = [
- "cal_parameter1",
- "cal_parameter2"
- ]
- generate_monitor_xml(dataprocess_file, output_file, cal_parameters)
|