controller.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import requests
  2. import os
  3. import logging
  4. from time import sleep
  5. ''' 准备数据 '''
  6. SCHEDULER_BASE_URL = os.getenv("SCHEDULER_BASE_URL")
  7. BACKEND_BASE_URL = os.getenv("BACKEND_BASE_URL")
  8. logger = logging.getLogger("algoA logger")
  9. missionId = os.getenv("missionId")
  10. planId = os.getenv("planId")
  11. headers = {
  12. "Content-Type": "application/json", # 明确声明数据格式
  13. "Accept": "application/json" # 声明期望的响应格式
  14. }
  15. params = {
  16. "missionId": missionId,
  17. "planId": planId,
  18. }
  19. print("THIS is a algo program")
  20. response = requests.get(SCHEDULER_BASE_URL + '/fetchData', params=params, headers=headers)
  21. data = response.json()
  22. print("data is")
  23. print(data)
  24. if not data:
  25. quit()
  26. else:
  27. print(data['nodes'])
  28. print(data['edges'])
  29. ''' 开始计算 '''
  30. for i in range(5):
  31. response = requests.post(BACKEND_BASE_URL + "/rawDataTrans/", json={
  32. 'missionId': missionId,
  33. 'planId': planId,
  34. 'progress': i * 20,
  35. })
  36. sleep(3)
  37. ''' 完成计算 '''
  38. try:
  39. response = requests.post(SCHEDULER_BASE_URL + '/report', json={
  40. 'missionId': missionId,
  41. 'planId': planId,
  42. 'state': 'DONE',
  43. 'results': {
  44. 'nodes': [[1, 'S'], [2, 'D'], [3, 'D'], [4, 'I']],
  45. 'edges': [[1, 2], [1, 4], [2, 4], [3, 4]],
  46. },
  47. })
  48. except Exception as error:
  49. print("ERROR is", error)
  50. if response:
  51. if response.json()['code'] == 'OK':
  52. print("response is ok")
  53. response = requests.post(BACKEND_BASE_URL + "/rawDataTrans/", json={
  54. 'missionId': missionId,
  55. 'planId': planId,
  56. 'progress': 100,
  57. 'nodes': [[1, 'S'], [2, 'D'], [3, 'D'], [4, 'I']],
  58. 'edges': [[1, 2], [1, 4], [2, 4], [3, 4]],
  59. })
  60. print(f"算法控制程序推送结果完毕 MissionId: {missionId} PlanId: {planId} Message: {response.json()}")
  61. else:
  62. print(f"算法控制程序结果反馈未被识别 MissionId: {missionId} PlanId: {planId}")
  63. else:
  64. print(f"算法控制程序结果反馈失败 MissionId: {missionId} PlanId: {planId}")