Exemplo n.º 1
0
def update():
    # setup server url
    config = ConfigParser()
    CONFIG_FILE = os.path.join(os.path.dirname(__file__), 'config.ini')
    config.read(CONFIG_FILE)
    ip = config.get('Host', 'ip')
    port = config.get('Host', 'port')
    url = 'https://' + ip + ':' + port + '/update'

    # setup car info
    vehicle_info = VehicleInfo()
    VEHICLE_INFO_FILE = os.path.join(os.path.dirname(__file__),
                                     'vehicle_info.pb.txt')
    try:
        proto_utils.get_pb_from_text_file(VEHICLE_INFO_FILE, vehicle_info)
    except IOError:
        print("vehicle_info.pb.txt cannot be open file.")
        exit()

    brand = VehicleInfo.Brand.Name(vehicle_info.brand)
    model = VehicleInfo.Model.Name(vehicle_info.model)
    vin = vehicle_info.vehicle_config.vehicle_id.vin
    car_info = {
        "car_type": brand + "." + model,
        "tag": sys.argv[1],
        "vin": vin,
    }

    urllib3.disable_warnings()
    CERT_FILE = os.path.join(os.path.dirname(__file__), 'ota.cert')
    r = requests.post(url, json=car_info, verify=CERT_FILE)
    if r.status_code == 200:
        print("Update successfully.")
        sys.exit(0)
    elif r.status_code == 400:
        print("Invalid Request.")
    else:
        print("Cannot connect to server.")
    sys.exit(1)
Exemplo n.º 2
0
def query():
    vehicle_info = VehicleInfo()
    VEHICLE_INFO_FILE = os.path.join(os.path.dirname(__file__),
                                     'vehicle_info.pb.txt')
    try:
        proto_utils.get_pb_from_text_file(VEHICLE_INFO_FILE, vehicle_info)
    except IOError:
        print "vehicle_info.pb.txt cannot be open file."
        sys.exit(1)

    # setup server url
    config = ConfigParser()
    CONFIG_FILE = os.path.join(os.path.dirname(__file__), 'config.ini')
    config.read(CONFIG_FILE)
    ip = config.get('Host', 'ip')
    port = config.get('Host', 'port')
    url = 'https://' + ip + ':' + port + '/query'

    # generat device token
    returnCode = sec_api.sec_upgrade_get_device_token()
    if returnCode[0] == False:
        print 'get device token failed!'
        sys.exit(1)
    dev_token = returnCode[1]

    # setup car info
    brand = VehicleInfo.Brand.Name(vehicle_info.brand)
    model = VehicleInfo.Model.Name(vehicle_info.model)
    vin = vehicle_info.license.vin
    META_FILE = '/apollo/meta.ini'
    config.read(META_FILE)
    car_info = {
        "car_type": brand + "." + model,
        "tag": config.get('Release', 'tag'),
        "vin": vin,
        "token": dev_token
    }

    urllib3.disable_warnings()
    CERT_FILE = os.path.join(os.path.dirname(__file__), 'ota.cert')
    r = requests.post(url, json=car_info, verify=CERT_FILE)
    if r.status_code == 200:
        auth_token = r.json().get("auth_token")
        if auth_token == "":
            print "Cannot get authorize token!"
            sys.exit(1)
        else:
            token_file_name = os.environ['HOME'] + \
                '/.cache/apollo_update/auth_token'
            apollo_update = os.path.dirname(token_file_name)
            if not os.path.exists(apollo_update):
                os.makedirs(apollo_update)
            with open(token_file_name, 'w') as token_file:
                token_file.write(auth_token)
        tag = r.json().get("tag")
        print tag
        sys.exit(0)
    elif r.status_code == 204:
        print "Release is up to date."
    elif r.status_code == 400:
        print "Invalid car type."
    else:
        print "Cannot connect to server."
    sys.exit(1)