settings.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. from wsgiref import headers
  2. from dotenv import load_dotenv, find_dotenv
  3. from pathlib import Path # Python 3.6+ only
  4. import configparser, json, pytz
  5. import requests
  6. import pillow_avif
  7. from utils.common import message_queue
  8. TIME_ZONE = pytz.timezone("Asia/Shanghai")
  9. from numpy import true_divide
  10. from databases import (
  11. create_all_database,
  12. DeviceConfig,
  13. SysConfigs,
  14. CRUD,
  15. batch_insert_sys_configs,
  16. SqlQuery,
  17. batch_insert_device_configs,
  18. )
  19. # 初始化数据表
  20. create_all_database()
  21. session = SqlQuery()
  22. device_config_crud = CRUD(DeviceConfig)
  23. all_devices = device_config_crud.read_all(session)
  24. if len(all_devices) == 0:
  25. # 如果配置表中一条数据都没有,就将初始化数据全部插入到数据表中
  26. action_tabs = json.load(open("action_tabs.json", encoding="utf-8"))
  27. actions = json.load(open("action.json", encoding="utf-8"))
  28. batch_insert_device_configs(session, action_tabs, actions)
  29. sys_config_crud = CRUD(SysConfigs)
  30. all_sys_configs = sys_config_crud.read_all(session)
  31. if len(all_sys_configs) == 0:
  32. # 如果配置表中一条数据都没有,就将初始化数据全部插入到数据表中
  33. sys_config_json = json.load(open("sys_configs.json", encoding="utf-8"))
  34. batch_insert_sys_configs(session, sys_config_json)
  35. else:
  36. # 查询数据库中缺失哪个数据,进行补充
  37. sys_config_json = json.load(open("sys_configs.json", encoding="utf-8"))
  38. for sys_config in sys_config_json:
  39. photos = CRUD(SysConfigs)
  40. item = photos.read(session, conditions={"key": sys_config.get("key")})
  41. if item == None:
  42. config = SysConfigs(**sys_config)
  43. session.add(config)
  44. session.commit() # 合并事务提交
  45. # 初始化数据表---结束
  46. def get_config_by_items(config_dict):
  47. __config_dict = {}
  48. for i, k in config_dict:
  49. __config_dict[i] = k
  50. return __config_dict
  51. keys = ["面料", "里料"]
  52. def getSysConfigs(key, item, default=None):
  53. session = SqlQuery()
  54. crud = CRUD(SysConfigs)
  55. one_item = crud.read(session, conditions={"key": key})
  56. config = json.loads(one_item.value)
  57. if item == "image_out_format":
  58. default_format = config.get(item, default)
  59. if default_format == "" or default_format == None:
  60. return "png"
  61. return config.get(item, default)
  62. def get_dict_value(_dict, key, default=None):
  63. if key in _dict:
  64. return _dict[key]
  65. else:
  66. return default
  67. MACHINE_LEVEL = (
  68. "二档"
  69. if getSysConfigs("other_configs", "device_speed", "二档") == ""
  70. else getSysConfigs("other_configs", "device_speed", "二档")
  71. )
  72. IS_TEST = False
  73. IS_MCU = True
  74. IS_LIN_SHI_TEST = False
  75. PhotographSeconds = float(
  76. 0.5
  77. if getSysConfigs("take_photo_configs", "camera_delay", "0.5") == ""
  78. else getSysConfigs("take_photo_configs", "camera_delay", "0.5")
  79. ) # 拍照停留时间
  80. MAX_PIXIAN_SIZE = 12000000
  81. def moveSpeed(level: str = None):
  82. config = {
  83. "一档": {
  84. "camera_high_motor": {
  85. "max_speed": 10000,
  86. "up_speed": 800,
  87. "down_speed": 700,
  88. },
  89. "turntable_steering": {
  90. "max_speed": 6000,
  91. "up_speed": 500,
  92. "down_speed": 400,
  93. },
  94. },
  95. "二档": {
  96. "camera_high_motor": {
  97. "max_speed": 7000,
  98. "up_speed": 600,
  99. "down_speed": 500,
  100. },
  101. "turntable_steering": {
  102. "max_speed": 4500,
  103. "up_speed": 350,
  104. "down_speed": 300,
  105. },
  106. },
  107. "三档": {
  108. "camera_high_motor": {
  109. "max_speed": 3500,
  110. "up_speed": 400,
  111. "down_speed": 300,
  112. },
  113. "turntable_steering": {
  114. "max_speed": 3000,
  115. "up_speed": 200,
  116. "down_speed": 200,
  117. },
  118. },
  119. }
  120. if level is None:
  121. return config[MACHINE_LEVEL]
  122. else:
  123. return config[level]
  124. config = configparser.ConfigParser()
  125. config_name = "config.ini"
  126. config.read(config_name, encoding="utf-8")
  127. # 应用名称
  128. APP_NAME = config.get("app", "app_name")
  129. # 应用版本号
  130. APP_VERSION = config.get("app", "version")
  131. # 是否开启调试模式
  132. IS_DEBUG = config.get("app", "debug")
  133. IS_UPLOAD_HLM = True if config.get("app", "is_upload") == "true" else False
  134. # 应用端口号
  135. PORT = config.get("app", "port")
  136. # 应用线程数
  137. APP_WORKS = config.get("app", "works")
  138. # 应用host地址
  139. APP_HOST = config.get("app", "host")
  140. # 应用服务启动名称
  141. APP_RUN = config.get("app", "app_run")
  142. # 日志名称
  143. LOG_FILE_NAME = config.get("log", "log_file_name")
  144. # 最大字节数
  145. MAX_BYTES = config.get("log", "max_bytes")
  146. print("Max bytes is", MAX_BYTES)
  147. # 备份数量
  148. BACKUP_COUNTS = config.get("log", "backup_counts")
  149. # 远程服务器地址
  150. HLM_HOST = config.get("log", "hlm_host")
  151. PROJECT = config.get("app", "project")
  152. # ----------------------------------
  153. mcu_config_dict = config.items("mcu_config")
  154. _mcu_config_dict = {}
  155. for i, k in mcu_config_dict:
  156. _mcu_config_dict[i] = int(k)
  157. # print(_mcu_config_dict)
  158. _config_mcu_config = get_config_by_items(config.items("mcu_config"))
  159. LEFT_FOOT_ACTION = _mcu_config_dict["left_foot_action"]
  160. LEFT_FOOT_PHOTOGRAPH = _mcu_config_dict["left_foot_photograph"]
  161. LEFT_FOOT_ACTION_1 = _mcu_config_dict["left_foot_action_1"]
  162. LEFT_FOOT_ACTION_2 = _mcu_config_dict["left_foot_action_2"]
  163. RIGHT_FOOT_ACTION = _mcu_config_dict["right_foot_action"]
  164. RIGHT_FOOT_PHOTOGRAPH = _mcu_config_dict["right_foot_photograph"]
  165. RIGHT_FOOT_ACTION_1 = _mcu_config_dict["right_foot_action_1"]
  166. RIGHT_FOOT_ACTION_2 = _mcu_config_dict["right_foot_action_2"]
  167. NEXT_STEP = int(get_dict_value(_config_mcu_config, "next_step", 6)) # 下一步
  168. MOVE_UP = _mcu_config_dict["move_up"]
  169. MOVE_DOWN = _mcu_config_dict["move_down"]
  170. STOP = _mcu_config_dict["stop"]
  171. camera_config_dict = config.items("camera_config")
  172. _camera_config_dict = {}
  173. for i, k in mcu_config_dict:
  174. _camera_config_dict[i] = int(k)
  175. _camera_config_dict = get_config_by_items(camera_config_dict)
  176. # LOW_ISO = _camera_config_dict["low_iso"]
  177. # HIGH_ISO = _camera_config_dict["high_iso"]
  178. DOMAIN = (
  179. "https://dev2.valimart.net"
  180. if config.get("app", "env") != "dev"
  181. else "https://dev2.pubdata.cn"
  182. )
  183. Company = "惠利玛"
  184. is_test_plugins = true_divide
  185. OUT_PIC_MODE = "." + getSysConfigs("basic_configs", "image_out_format", "png") # ".png"
  186. OUT_PIC_SIZE = (
  187. [1600]
  188. if getSysConfigs("basic_configs", "main_image_size", [1600]) == ""
  189. else getSysConfigs("basic_configs", "main_image_size", [1600])
  190. ) # 主图大小
  191. Mode = getSysConfigs("other_configs", "product_type", "鞋类") # 程序执行类
  192. OUT_PIC_FACTOR = float(
  193. 1
  194. if getSysConfigs("basic_configs", "image_sharpening", "1") == ""
  195. else getSysConfigs("basic_configs", "image_sharpening", "1")
  196. ) # 图片锐化
  197. RESIZE_IMAGE_MODE = 1
  198. GRENERATE_MAIN_PIC_BRIGHTNESS = 254 # 色阶是否调整到位判断
  199. RUNNING_MODE = getSysConfigs("other_configs", "running_mode", "普通模式")
  200. DEFAULT_CUTOUT_MODE = getSysConfigs("other_configs", "cutout_mode", "普通抠图")
  201. CUTOUT_MODE = (
  202. 0 if getSysConfigs("other_configs", "cutout_mode", "普通抠图") == "普通抠图" else 1
  203. )
  204. import importlib
  205. # plugins = [
  206. # # "custom_plugins.plugins.detail_template.huilima.detail_huilima1.DetailPicGet",
  207. # ".custom_plugins.plugins.detail_template.huilima.detail_huilima1.DetailPicGet",
  208. # # "custom_plugins.plugins.detail_template.huilima.detail_huilima2.DetailPicGet",
  209. # # "custom_plugins.plugins.detail_template.huilima.detail_huilima3.DetailPicGet",
  210. # # "custom_plugins.plugins.detail_template.huilima.detail_huilima4.DetailPicGet",
  211. # # "custom_plugins.plugins.detail_template.xiaosushuoxie.detail_xiaosushuoxie1",
  212. # # "custom_plugins.plugins.detail_template.xiaosushuoxie.detail_xiaosushuoxie2",
  213. # # "custom_plugins.plugins.detail_template.xiaosushuoxie.detail_xiaosushuoxie3",
  214. # # "custom_plugins.plugins.detail_template.xiaosushuoxie.detail_xiaosushuoxie4",
  215. # # "custom_plugins.plugins.detail_template.xiaosushuoxie.detail_xiaosushuoxie5",
  216. # # "custom_plugins.plugins.detail_template.xiaosushuoxie.detail_xiaosushuoxie6",
  217. # # "custom_plugins.plugins.detail_template.hongqingting.detail_hongqingting1.DetailPicGet",
  218. # # "custom_plugins.plugins_mode.detail_generate_base.DetailBase",
  219. # # "custom_plugins.plugins_mode.pic_deal.PictureProcessing",
  220. # ]
  221. # def load_plugin(plugin_path):
  222. # module_name, class_name = plugin_path.rsplit(".", 1)
  223. # module = importlib.import_module(module_name)
  224. # return getattr(module, class_name)
  225. # loaded_plugins = [load_plugin(p.lstrip(".")) for p in plugins]
  226. OUT_PIC_QUALITY = "普通"
  227. GRENERATE_MAIN_PIC_BRIGHTNESS = int(
  228. getSysConfigs("other_configs", "grenerate_main_pic_brightness", 254)
  229. ) # 色阶是否调整到位判断
  230. SHADOW_PROCESSING = int(
  231. getSysConfigs("other_configs", "shadow_processing", 0)
  232. ) # 0表示要直线和曲线,1 表示只要直线
  233. LOWER_Y = int(
  234. getSysConfigs("other_configs", "lower_y", 4)
  235. ) # 鞋底以下多少距离作为阴影蒙版
  236. CHECK_LOWER_Y = int(
  237. getSysConfigs("other_configs", "check_lower_y", 4)
  238. ) # 检测亮度区域,倒数第几行
  239. IS_GET_GREEN_MASK = (
  240. True if getSysConfigs("other_configs", "is_get_green_mask", "否") == "是" else False
  241. ) # 是否进行绿幕抠图
  242. IMAGE_SAVE_MAX_WORKERS = int(
  243. getSysConfigs("other_configs", "image_save_max_workers", 4)
  244. ) # 批量保存的线程大小
  245. COLOR_GRADATION_CYCLES = int(
  246. getSysConfigs("other_configs", "color_gradation_cycles", 22)
  247. ) # 色阶处理循环次数
  248. def recordDataPoint(token=None, page="", uuid=None, data=""):
  249. """记录日志"""
  250. if token == None:
  251. return
  252. if page == "":
  253. return
  254. if uuid == None:
  255. return
  256. headers = {"Content-Type": "application/json", "authorization": "Bearer " + token}
  257. params = {
  258. "site": 1,
  259. "type": 5,
  260. "channel": "aigc-camera",
  261. "uuid": uuid,
  262. "page": page,
  263. "page_url": "/",
  264. "describe": {"action": page, "data": data},
  265. "time": 0,
  266. }
  267. return requests.post(
  268. headers=headers, data=json.dumps(params), url=DOMAIN + "/api/record/point"
  269. )
  270. async def sendSocketMessage(
  271. code=0, msg="", data=None, device_status=2, msg_type=""
  272. ):
  273. data = {
  274. "code": code,
  275. "msg": msg,
  276. "status": device_status,
  277. "data": data,
  278. "msg_type": msg_type,
  279. }
  280. await message_queue.put(data)