controller.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import requests
  2. import os
  3. import json
  4. import logging
  5. import time
  6. ''' 准备数据 '''
  7. SCHEDULER_BASE_URL = os.getenv("SCHEDULER_BASE_URL")
  8. BACKEND_BASE_URL = os.getenv("BACKEND_BASE_URL")
  9. logger = logging.getLogger("algoA logger")
  10. missionId = os.getenv("missionId")
  11. planId = os.getenv("planId")
  12. headers = {
  13. "Content-Type": "application/json", # 明确声明数据格式
  14. "Accept": "application/json" # 声明期望的响应格式
  15. }
  16. params = {
  17. "missionId": missionId,
  18. "planId": planId,
  19. }
  20. print(json.dumps({'msg': 'started'}), flush=True)
  21. response = requests.get(SCHEDULER_BASE_URL + '/fetchData', params=params, headers=headers)
  22. data = response.json()
  23. if not data:
  24. quit()
  25. print(json.dumps({'msg': 'start', 'data': data}), flush=True)
  26. ''' 开始计算 '''
  27. progressData = {
  28. 'missionId': missionId,
  29. 'planId': planId,
  30. 'progress': 0,
  31. }
  32. print(json.dumps({'msg': 'progress', 'data': progressData}), flush=True)
  33. while progressData['progress'] < 100:
  34. start_time = time.perf_counter()
  35. count = 0
  36. while True:
  37. count += 1
  38. if time.perf_counter() - start_time >= 1.0:
  39. break
  40. progressData['progress'] += 5
  41. print(json.dumps({'msg': 'progress', 'data': progressData}), flush=True)
  42. start_time = time.perf_counter()
  43. count = 0
  44. while True:
  45. count += 1
  46. if time.perf_counter() - start_time >= 5.0:
  47. break
  48. ''' 完成计算 '''
  49. result = {
  50. 'missionId': missionId,
  51. 'planId': planId,
  52. 'progress': 100,
  53. 'nodes': [[1, 'S'], [2, 'D'], [3, 'D'], [4, 'I']],
  54. 'edges': [[1, 2], [1, 4], [2, 4], [3, 4]],
  55. }
  56. print(json.dumps({'msg': 'result', 'data': result}), flush=True)
  57. # if response:
  58. # if response.json()['code'] == 'OK':
  59. # print("response is ok")
  60. # response = requests.post(BACKEND_BASE_URL + "/rawDataTrans/", json={
  61. # 'missionId': missionId,
  62. # 'planId': planId,
  63. # 'progress': 100,
  64. # 'nodes': [[1, 'S'], [2, 'D'], [3, 'D'], [4, 'I']],
  65. # 'edges': [[1, 2], [1, 4], [2, 4], [3, 4]],
  66. # })
  67. # print(f"算法控制程序推送结果完毕 MissionId: {missionId} PlanId: {planId} Message: {response.json()}")
  68. # else:
  69. # print(f"算法控制程序结果反馈未被识别 MissionId: {missionId} PlanId: {planId}")
  70. # else:
  71. # print(f"算法控制程序结果反馈失败 MissionId: {missionId} PlanId: {planId}")