יצירת רכב מסירה למשימות מתוזמנות

במסמך הזה מוסבר איך ליצור רכב מסביבת שרת באמצעות gRPC או REST. אפשר ליצור רכב מ-Driver SDK, בתנאי הקצאת את האפליקציה כסביבה מהימנה באמצעות פרטי הכניסה.

כדי להבין איך להשתמש ב-Driver SDK כדי ליצור כלי רכב:

כדי ליצור רכב חדש מסביבת שרת, צריך לבצע בקשת CreateDeliveryVehicle ל-Fleet Engine. משתמשים ב CreateDeliveryVehicleRequest להגדרת המאפיינים של הרכיב החדש רכב מסירה.

שדות לרכבים עם משימות מתוזמנות

כשיוצרים DeliveryVehicle, צריך להגדיר את השדות האופציונליים הבאים:

  • attributes
  • last_location
  • type

כדי ליצור רכב בלי להגדיר שדות אופציונליים, אפשר להשאיר את השדה DeliveryVehicle לא הוגדר בCreateDeliveryVehicleRequest.

יצירת דוגמה לרכב

אפשר להשתמש בספריית Java gRPC כדי ליצור רכב, או REST.

Java

  static final String PROJECT_ID = "my-delivery-co-gcp-project";
  static final String VEHICLE_ID = "vehicle-8241890"; // Avoid auto-incrementing IDs.

  DeliveryServiceBlockingStub deliveryService =
    DeliveryServiceGrpc.newBlockingStub(channel);

  // Vehicle settings
  String parent = "providers/" + PROJECT_ID;
  DeliveryVehicle vehicle = DeliveryVehicle.newBuilder()
    .addAttributes(DeliveryVehicleAttribute.newBuilder()
      .setKey("route_number").setValue("1"))  // Opaque to the Fleet Engine
    .build();

  // Vehicle request
  CreateDeliveryVehicleRequest createVehicleRequest =
    CreateDeliveryVehicleRequest.newBuilder()  // No need for the header
        .setParent(parent)
        .setDeliveryVehicleId(VEHICLE_ID)     // Vehicle ID assigned by the Provider
        .setDeliveryVehicle(vehicle)
        .build();

  // Error handling
  // If Fleet Engine does not have vehicle with that ID and the credentials of the
  // requestor pass, the service creates the vehicle successfully.

  try {
    DeliveryVehicle createdVehicle =
      deliveryService.createDeliveryVehicle(createVehicleRequest);
  } catch (StatusRuntimeException e) {
    Status s = e.getStatus();
    switch (s.getCode()) {
       case ALREADY_EXISTS:
         break;
       case PERMISSION_DENIED:
         break;
    }
    return;
  }

REST

כדי ליצור רכב מסביבת שרת, צריך לבצע קריאה ל-HTTP REST אל CreateDeliveryVehicle:

POST https://fleetengine.googleapis.com/v1/providers/<project_id>/deliveryVehicles?deliveryVehicleId=<id>

גוף ה-POST מייצג את הישות DeliveryVehicle שיש ליצור. אפשר מציינים את השדות האופציונליים הבאים:

  • attributes
  • lastLocation
  • type
  # Set $JWT, $PROJECT_ID, and $VEHICLE_ID in the local
  # environment
  curl -X POST "https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/deliveryVehicles?deliveryVehicleId=${VEHICLE_ID}" \
    -H "Content-type: application/json" \
    -H "Authorization: Bearer ${JWT}" \
  --data-binary @- << EOM
  {
    "attributes": [{"key": "model", "value": "sedan"}],
    "lastLocation": {"location": {"latitude": 12.1, "longitude": 14.5}}
  }
  EOM

כדי ליצור רכב בלי להגדיר שדות, צריך להשאיר את גוף ה-POST הבקשה ריקה. לאחר מכן, הרכב החדש שולף מזהה רכב deliveryVehicleId בכתובת ה-URL של POST.

דוגמה:

  # Set $JWT, $PROJECT_ID, and $VEHICLE_ID in the local
  # environment
  curl -X POST "https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/deliveryVehicles?deliveryVehicleId=${VEHICLE_ID}" \
    -H "Content-type: application/json" \
    -H "Authorization: Bearer ${JWT}"

המאמרים הבאים