settings.py 11 KB

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