Prechádzať zdrojové kódy

feat(flutter): Add api of "get script_task"

huangrunheng 3 rokov pred
rodič
commit
8f689843aa

+ 66 - 2
module/config/config_model.py

@@ -1,7 +1,10 @@
 # This Python file uses the following encoding: utf-8
 # @author runhey
 # github https://github.com/runhey
+from typing import Dict, Any
+
 import re
+import inflection
 
 from pathlib import Path
 from pydantic import BaseModel, ValidationError, Field
@@ -265,6 +268,68 @@ class ConfigModel(ConfigBase):
         except (AttributeError, KeyError):
             return False
 
+# ----------------------------------- fastapi -----------------------------------
+    def script_task(self, task: str) -> dict:
+        """
+
+        :param task: 同gui_args函数
+        :return:
+        """
+        task = convert_to_underscore(task)
+        task = getattr(self, task, None)
+        if task is None:
+            logger.warning(f'{task} is no inexistence')
+            return {}
+
+        def extract_groups(sch):
+            # 从schema 中提取未解析的group的数据
+            properties = {}
+            for key, value in sch["properties"].items():
+                properties[key] = re.search(r"/([^/]+)$", value['$ref']).group(1)
+            print(f'输出的组是{properties}')
+
+            result = {}
+            for key, value in properties.items():
+                result[key] = sch["definitions"][value]
+
+            return result
+
+        def merge_value(groups, jsons, definitions) -> list[dict]:
+            # 将 groups的参数,同导出的json一起合并, 用于前端显示
+            result = []
+            for key, value in groups["properties"].items():
+                item = {}
+                item["title"] = value["title"] if "title" in value else inflection.humanize(key)
+                if "description" in value:
+                    item["description"] = value["description"]
+                item["default"] = value["default"]
+                item["value"] = jsons[key] if key in jsons else value["default"]
+                item["type"] = value["type"] if "type" in value else "enum"
+                if 'allOf' in value:
+                    # list
+                    item["enumEnum"] = definitions[inflection.camelize(key, uppercase_first_letter=True)]["enum"]
+                # TODO: 最大值最小值
+                result.append(item)
+            return result
+
+
+
+
+
+
+
+        schema = task.schema()
+        groups = extract_groups(schema)
+
+
+        result: dict[str, list] = {}
+        for key, value in task.dict().items():
+            result[inflection.humanize(key)] = merge_value(groups[key], value, schema["definitions"])
+
+        return result
+
+
+
 
 if __name__ == "__main__":
     try:
@@ -274,7 +339,6 @@ if __name__ == "__main__":
         c = ConfigModel()
 
     # c.save()
-    print(c.gui_args('Script'))
-    # print(c.deep_get(c, 'area_boss.scheduler.success_interval'))
+    print(c.script_task('Script'))
 
 

+ 1 - 1
module/server/home_router.py

@@ -12,7 +12,7 @@ home_app = APIRouter(
 @home_app.get('/test')
 async def home_test():
     return {'message':'test'}
-
+#  gcc -Wall -pedantic -shared -fPIC -o group_work.so group_work.c -lwiringPi
 @home_app.get('/home_menu')
 async def home_menu():
     return {'Home': [], 'Updater': [], 'Tool': []}

+ 2 - 2
module/server/script_router.py

@@ -60,9 +60,9 @@ async def script_stop(script_name: str):
 
 @script_app.get('/{script_name}/{task}/args')
 async def script_task(script_name: str, task: str):
-    if not mm.ensure_config_cache(task):
+    if not mm.ensure_config_cache(script_name):
         raise Exception(f'[{script_name}] script file does not exist')
-    return mm.config_cache.get_task_args(task)
+    return mm.config_cache.model.script_task(task)
 
 @script_app.put('/{script_name}/{task}/{group}/{argument}/value/{value}')
 async def script_task(script_name: str, task: str, group: str, argument: str, value):