123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import requests
- import os
- import logging
- from time import sleep
- ''' 准备数据 '''
- SCHEDULER_BASE_URL = os.getenv("SCHEDULER_BASE_URL")
- BACKEND_BASE_URL = os.getenv("BACKEND_BASE_URL")
- logger = logging.getLogger("algoA logger")
- missionId = os.getenv("missionId")
- planId = os.getenv("planId")
- headers = {
- "Content-Type": "application/json", # 明确声明数据格式
- "Accept": "application/json" # 声明期望的响应格式
- }
- params = {
- "missionId": missionId,
- "planId": planId,
- }
- print("THIS is a algo program")
- response = requests.get(SCHEDULER_BASE_URL + '/fetchData', params=params, headers=headers)
- data = response.json()
- print("data is")
- print(data)
- if not data:
- quit()
- else:
- print(data['nodes'])
- print(data['edges'])
- ''' 开始计算 '''
- for i in range(5):
- response = requests.post(BACKEND_BASE_URL + "/rawDataTrans/", json={
- 'missionId': missionId,
- 'planId': planId,
- 'progress': i * 20,
- })
- sleep(3)
- ''' 完成计算 '''
- try:
- response = requests.post(SCHEDULER_BASE_URL + '/report', json={
- 'missionId': missionId,
- 'planId': planId,
- 'state': 'DONE',
- 'results': {
- 'nodes': [[1, 'S'], [2, 'D'], [3, 'D'], [4, 'I']],
- 'edges': [[1, 2], [1, 4], [2, 4], [3, 4]],
- },
- })
- except Exception as error:
- print("ERROR is", error)
- if response:
- if response.json()['code'] == 'OK':
- print("response is ok")
- response = requests.post(BACKEND_BASE_URL + "/rawDataTrans/", json={
- 'missionId': missionId,
- 'planId': planId,
- 'progress': 100,
- 'nodes': [[1, 'S'], [2, 'D'], [3, 'D'], [4, 'I']],
- 'edges': [[1, 2], [1, 4], [2, 4], [3, 4]],
- })
- print(f"算法控制程序推送结果完毕 MissionId: {missionId} PlanId: {planId} Message: {response.json()}")
- else:
- print(f"算法控制程序结果反馈未被识别 MissionId: {missionId} PlanId: {planId}")
- else:
- print(f"算法控制程序结果反馈失败 MissionId: {missionId} PlanId: {planId}")
|