settings.py 11 KB

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