# update: 2024-3-20
"""
车辆控制器API
"""
from urllib import parse
import requests
import time
import traceback

import sys
import importlib

sys.path.append(r'E:\casper\repositories\repositories\casperz.py-project\module-py')  # for pc
# sys.path.append(r'D:\casper\gitee\casperz.py-project\module-py')  # for pc
methods = importlib.import_module(f"xlib")


class API(object):

    def __init__(self, address='192.168.191.91', port=8000):
        """
        start_plan 启动任务
        abort_plan 终止任务
        start_engine 远程打火
        stall_engine 远程熄火
        apply_use_by_ip 申请远程操作模式 1 申请远程操作模式 2 申请自动作业模式
        """
        # --- define ---
        self.port = port
        # self.url = f"http://{address}:{port}"

    def cmd1001(self, address='192.168.191.91', navigation=[]):
        """
        启动行驶
        data = {
            # 指令类型
            "cmdid": 1001,  # 执行行驶任务
            # 指令参数
            "param": {
                # 导航数据
                "navigation": [
                    {
                        'type': 'forward',  # 方向类型 forward 正向 backward 反向
                        'nav_coordinates': [
                            # 直行轨迹坐标参数
                            {
                                "start_point_x": 37.7749,
                                "start_point_y": 37.7749,
                                "end_point_x": 40,
                                "end_point_y": -120
                            },
                            # 转弯轨迹坐标参数
                            {
                                "start_point_x": 40.7128,
                                "start_point_y": -74.0060,
                                "center_point_x": 10,
                                "center_point_y": 20,
                                "end_point_x": 50.7128,
                                "end_point_y": -70.0060
                            },
                        ]
                    },
                    {
                        'type': 'backward',
                        'nav_coordinates': [
                            # 直行轨迹坐标参数
                            {
                                "start_point_x": 37.7749,
                                "start_point_y": -122.4194,
                                "end_point_x": 40,
                                "end_point_y": -120
                            },
                            # 转弯轨迹坐标参数
                            {
                                "start_point_x": 40.7128,
                                "start_point_y": -74.0060,
                                "center_point_x": 10,
                                "center_point_y": 20,
                                "end_point_x": 50.7128,
                                "end_point_y": -70.0060
                            },
                        ]
                    },
                ],
            },
        }
        """
        data = {
            'cmdid': 1001,
            'param': {
                'navigation': navigation,
            }
        }
        url = f"http://{address}:{self.port}"
        response = requests.post(self.url, json=data)
        print('API.start_plan.url:', self.url)
        print('API.start_plan.data:', data)
        print('API.start_plan.code:', response.status_code)
        print('API.start_plan.text:', response.text)

    def cmd100101(self):
        """
        启动作业
        """
        data = {
            # 指令类型
            "cmdid": 100101,  # 启动作业
            # 指令参数
            "param": {
                # 任务类型 102 叉包 103 放包 104 倒渣
                "task_type": 102,
                # 102
                "task_coordinates": {
                    "pot_name": "M.24",
                    "pot_x": 40,
                    "pot_y": 50,
                    "mark_pot_pose": 0.3,
                    "e_point_x": 50,
                    "e_point_y": 60
                },
                # 103
                # "task_coordinates": {
                #     "pot_name": "M.24",
                #     "pot_x": 40,
                #     "pot_y": 50,
                #     "goal_pot_pose": 0.3,
                #     "e_point_x": 50,
                #     "e_point_y": 60
                # },
                # 104
                # "task_coordinates": {},
            }
        }

    def cmd1002(self):
        """
        中止行驶任务
        """
        data = {
            # 指令类型
            "cmdid": 1002,  # 中止行驶任务
            # 指令参数
            "param": {}
        }

    def cmd1003(self):
        data = {
            # 指令类型
            "cmdid": 1003,  # 远程打火
            # 指令参数
            "param": {}
        }

    def cmd1004(self):
        data = {
            # 指令类型
            "cmdid": 1004,  # 远程熄火
            # 指令参数
            "param": {}
        }

    def cmd1005(self):
        data = {
            # 指令类型
            "cmdid": 1005,  # 申请远程操作模式
            # 指令参数
            "param": {}
        }

    def cmd1006(self):
        data = {
            # 指令类型
            "cmdid": 1006,  # 申请自动作业模式
            # 指令参数
            "param": {}
        }

    def test002(self, address):
        """
        """
        data = {
            # 指令类型
            "cmdid": 1001,  # 执行行驶任务
            # 指令参数
            "param": {
                # 导航数据
                "navigation": [
                    {
                        'type': 'forward',
                        'nav_coordinates': [
                            {
                                'start_point_x': 2.495,  # 当前位置
                                'start_point_y': 173.1666142856,  # 当前位置
                                'end_point_x': 182.6588877142857,
                                'end_point_y': 169.8418064285714
                            },
                            {
                                'start_point_x': 182.6588877142857,
                                'start_point_y': 169.8418064285714,
                                'center_point_x': 182.92185485714285,
                                'center_point_y': 183.5769582142857,
                                'end_point_x': 196.65700664285714,
                                'end_point_y': 183.31399107142855
                            },
                            {
                                'start_point_x': 196.65700664285714,
                                'start_point_y': 183.31399107142855,
                                'center_point_x': 182.92185485714285,
                                'center_point_y': 183.5769582142857,
                                'end_point_x': 183.184822,
                                'end_point_y': 197.31211
                            },
                            {
                                'start_point_x': 183.184822,
                                'start_point_y': 197.31211,
                                'end_point_x': 178.045984 - 50,
                                'end_point_y': 197.219726
                            }
                        ]
                    }
                ]
            },
        }
        url = f"http://{address}:{self.port}"
        response = requests.post(url, json=data)
        print('API.start_plan.url:', url)
        print('API.start_plan.data:', data)
        print('API.start_plan.code:', response.status_code)

    def test004(self, address):
        data = {
            # 指令类型
            "cmdid": 100101,  # 启动作业
            # 指令参数
            "param": {

                "task_type": 103,  # 放包
                "task_coordinates": {
                    "pot_name": "N.20",
                    "pot_x": 122.755687,
                    "pot_y": 210.019149,
                    "mark_pot_pose": -1.570920,
                    "e_point_x": 0,
                    "e_point_y": 0
                },

            }
        }
        url = f"http://{address}:{self.port}"
        response = requests.post(url, json=data)
        print('API.start_plan.url:', url)
        print('API.start_plan.data:', data)
        print('API.start_plan.code:', response.status_code)


if __name__ == '__main__':
    # --- init ---
    api = API(port=8000)

    # --- test ---
    # out = api.test002(address='192.168.131.180')  # 行驶
    out = api.test004(address='192.168.131.180')  # 作业
    print(out)