Cookbook sync

How a device receives a cookbook sync from Things5: the request, the two ways to get the recipes, and the answer.

A cookbook is a set of recipe templates prepared in Things5 for a machine model; a cookbook sync sends it to devices of that model. If you are new to these terms, start from the guide Cookbooks and recipe templates.

When a user sends a cookbook to a device, Things5 asks the device to replace its recipes with the recipe templates of that cookbook. Things5 only sends the request and makes the recipes available: writing them on the machine is up to the device, which then tells Things5 how it went. This page describes the device side of a sync: which messages arrive, how the device gets the recipes and how it answers.

How a device receives a cookbook

One sync, several topics

A single sync uses several MQTT topics. This is expected:

  1. Things5 sends the command cmd/sync_recipe_templates_v2_req, with the id of the sync and a link (manifest_url) to the cookbook files.
  2. The device gets the recipes in one of two ways: over HTTPS from the links, or with an MQTT pull: one request/response pair for the manifest and one per recipe, on the req/ topics.
  3. The device answers once on cmd/sync_recipe_templates_res.

So in the MQTT log of a device that uses the MQTT pull you see, for a cookbook of 3 recipes: the command from Things5, 1 manifest request and its answer, 3 recipe requests and their answers, and the final answer of the device: 10 messages. See A real sync, message by message.

TopicDirectionUsed by
things5-production/v1/devices/{device_id}/cmd/sync_recipe_templates_v2_reqThings5 → deviceHTTPS and MQTT pull
things5-production/v1/devices/{device_id}/req/get_sync_manifest_reqDevice → Things5MQTT pull
things5-production/v1/devices/{device_id}/req/get_sync_manifest_resThings5 → deviceMQTT pull
things5-production/v1/devices/{device_id}/req/get_sync_recipe_reqDevice → Things5MQTT pull
things5-production/v1/devices/{device_id}/req/get_sync_recipe_resThings5 → deviceMQTT pull
things5-production/v1/devices/{device_id}/cmd/sync_recipe_templates_resDevice → Things5HTTPS and MQTT pull

Which transport to choose

HTTPS downloadMQTT pull
StatusStandard, recommendedAlternative
The device needsan HTTPS client (TLS, trusted CA certificates)only the MQTT connection it already has
Cookbook sizeanyany, but one round trip per recipe: best for small and medium cookbooks
Memory on the deviceone file at a timeone recipe at a time
Skip unchanged recipes (hash)yesyes
Speedfastest (downloads can run in parallel)one request at a time

No configuration is needed on Things5: the device decides at every sync which transport to use. The content is the same either way: same recipes, same order (index), the same JSON byte for byte with the same hash.

When a cookbook is sent to many devices, Things5 sends the command to a few devices at a time and moves on when a device answers. Answer on cmd/sync_recipe_templates_res as soon as the sync is done or has failed.

Things5 → device: cmd/sync_recipe_templates_v2_req

This command asks the device to replace all its recipes with the recipe templates of the cookbook. How the recipes are written on the machine is up to the device: Things5 only waits for its answer.

things5-production/v1/devices/<device-id>/cmd/sync_recipe_templates_v2_req

Payload

{
  "request_id": "4f0437f1-d038-4a54-96e6-f353caa1223e",
  "cookbook_sync_log_id": "3980d532-3174-4c03-8ab9-eee7a87b7ad6",
  "manifest_url": "https://downloads.example.com/cookbook-syncs/182683fe-44d5-4315-b9ee-65d6e89d05bf/manifest.json?X-Amz-Algorithm=...&X-Amz-Signature=...",
  "expires_at": 1795089000000
}

Payload Parameters

namedescription
request_idid of this command: send it back in cmd/sync_recipe_templates_res
cookbook_sync_log_idid of this sync on this device: send it back in the answer and use it in the MQTT pull requests
manifest_urlpre-signed HTTPS URL of the manifest (HTTPS transport only)
expires_atUnix time in milliseconds after which manifest_url and the recipe URLs no longer work (HTTPS transport only)

Transport 1 (standard): HTTPS download

Cookbook sync over HTTPS

  1. Download the manifest from manifest_url with a plain HTTPS GET. The URL is pre-signed: send it exactly as received, with no extra headers or credentials.
  2. For each entry of recipes[], compare hash with the recipe already on the device and download url only when it changed (see Skipping unchanged recipes).
  3. Replace the recipes on the machine.
  4. Answer on cmd/sync_recipe_templates_res.

🚧

Download before expires_at

The links stop working at expires_at. If they expired before the device could download the files, answer with an error: the user can send the sync again from Things5 and the device gets new links.

Manifest

The manifest lists every recipe of the cookbook.

{
  "manifest_version": 1,
  "cookbook_sync_id": "182683fe-44d5-4315-b9ee-65d6e89d05bf",
  "cookbook_metadata": {
    "example_key": "example_value"
  },
  "recipes": [
    {
      "index": 0,
      "id": "ff34e06c-09bd-4b93-8701-96aa82fcfb25",
      "hash": "67076c3831e3bb8fe9c9368a8bb0721558f4b0335b51fed9c52a8ba8a7b5bc2c",
      "hash_version": 1,
      "url": "https://downloads.example.com/cookbook-syncs/182683fe-44d5-4315-b9ee-65d6e89d05bf/recipes/0.json?X-Amz-Signature=..."
    }
  ]
}
namedescriptionexample
manifest_versionversion of the manifest format1
cookbook_sync_idid of this sync (the same for every device of the sync)
cookbook_metadatacookbook-level metadata, applied during the sync (arbitrary JSON object defined by the cookbook)
recipesarray of recipe entries, in cookbook order
recipes[].indexposition of the recipe in the cookbook0
recipes[].idrecipe template id
recipes[].hashcontent fingerprint of the recipe (lowercase hex SHA-256)
recipes[].hash_versionalgorithm version of hash1
recipes[].urlHTTPS URL of the recipe template (not present over MQTT pull)

Skipping unchanged recipes

hash is a content fingerprint of the recipe. Before downloading a recipe, compare it with the recipe already stored on the device:

  • same hash → the recipe is unchanged, skip it;
  • different or missing hash → download it;
  • hash_version the device does not implement → treat the recipe as changed and download it.

Only name, description, machine_model_id, phases and metadata contribute to the hash: editing who last changed a recipe, or when, does not change it. A device that keeps no hashes simply downloads every recipe.

Recipe template

Each recipe file contains one recipe template of the cookbook.

{
  "id": "ff34e06c-09bd-4b93-8701-96aa82fcfb25",
  "index": 0,
  "name": "Brioche",
  "description": "Recipe description",
  "machine_model_id": "0f7b403b-1748-437a-9cf7-3f79abd33083",
  "hash": "67076c3831e3bb8fe9c9368a8bb0721558f4b0335b51fed9c52a8ba8a7b5bc2c",
  "hash_version": 1,
  "phases": [
    {
      "dosages": [
        [
          {
            "name": "yeast",
            "quantity": "5.5",
            "metadata": [
              { "name": "type", "value": "1", "type": "integer" }
            ]
          }
        ]
      ],
      "values": [
        { "name": "cell_temperature", "value": "260", "type": "integer" },
        { "name": "time", "value": "90", "type": "integer" }
      ]
    }
  ],
  "metadata": [
    { "name": "image_path", "value": "../images/brioche.jpg", "type": "string" }
  ]
}
namedescriptionexample
idrecipe template id
indexposition of the recipe in the cookbook0
namerecipe nameBrioche
descriptionrecipe description
machine_model_idmachine model the recipe targets
hashcontent fingerprint (lowercase hex SHA-256)
hash_versionalgorithm version of hash1
phasesthe program: for each phase, values (name, value, type) and optional dosages
metadataarbitrary recipe metadata (name, value, type)

Transport 2 (alternative): MQTT pull

For constrained devices without an HTTPS client, or without the memory to hold the manifest and its recipes. The device ignores manifest_url and expires_at and asks Things5 for the manifest and then for each recipe it needs, over the MQTT connection it already has.

Cookbook sync over MQTT pull

The requests use the device-initiated req/ topics: the device chooses a new request_id for every request, and Things5 answers on the matching _res topic with the same request_id.

🚧

One request at a time

Send the next request only after the answer to the previous one. A request sent while another request of the same device is still waiting for its answer gets the busy error. If no answer arrives within 15 seconds, send the request again with a new request_id (within those 15 seconds a lost request still counts as in flight, so an early retry gets busy).

Device → Things5: req/get_sync_manifest_req

things5-production/v1/devices/<device-id>/req/get_sync_manifest_req
{
  "request_id": "238c53bd-03ef-4e42-b8af-fef3aefbf2cb",
  "cookbook_sync_log_id": "3980d532-3174-4c03-8ab9-eee7a87b7ad6"
}
namedescription
request_idchosen by the device, sent back in the answer
cookbook_sync_log_idthe cookbook_sync_log_id of cmd/sync_recipe_templates_v2_req

Things5 → device: req/get_sync_manifest_res

things5-production/v1/devices/<device-id>/req/get_sync_manifest_res
{
  "request_id": "238c53bd-03ef-4e42-b8af-fef3aefbf2cb",
  "cookbook_sync_log_id": "3980d532-3174-4c03-8ab9-eee7a87b7ad6",
  "errors": [],
  "manifest": {
    "manifest_version": 1,
    "cookbook_sync_id": "182683fe-44d5-4315-b9ee-65d6e89d05bf",
    "cookbook_metadata": {},
    "recipes": [
      { "index": 0, "id": "ff34e06c-09bd-4b93-8701-96aa82fcfb25", "hash": "67076c3831e3bb8fe9c9368a8bb0721558f4b0335b51fed9c52a8ba8a7b5bc2c", "hash_version": 1 },
      { "index": 1, "id": "4ce79aab-88b4-4a7e-97e3-7dbe7423fbf0", "hash": "269525538c16cfe0f946300081b56f5a6365e4ecd7c73ff89067c5b748aae46b", "hash_version": 1 },
      { "index": 2, "id": "e041ef0b-547f-4d53-9f22-07c53cb83561", "hash": "b40b8b340c4eb352159233da0c5ae1177396c7440720ff8c01ce704a17b91621", "hash_version": 1 }
    ]
  }
}

manifest is the manifest without recipes[].url. It is missing when errors is not empty.

Device → Things5: req/get_sync_recipe_req

things5-production/v1/devices/<device-id>/req/get_sync_recipe_req
{
  "request_id": "e9275c0f-8e73-4508-b900-ed4391ab52e2",
  "cookbook_sync_log_id": "3980d532-3174-4c03-8ab9-eee7a87b7ad6",
  "index": 0
}
namedescription
request_idchosen by the device, sent back in the answer
cookbook_sync_log_idas above
indexrecipes[].index of the recipe in the manifest

As over HTTPS, the device can compare hash with the recipe it already has and skip the request for unchanged recipes.

Things5 → device: req/get_sync_recipe_res

things5-production/v1/devices/<device-id>/req/get_sync_recipe_res
{
  "request_id": "e9275c0f-8e73-4508-b900-ed4391ab52e2",
  "cookbook_sync_log_id": "3980d532-3174-4c03-8ab9-eee7a87b7ad6",
  "index": 0,
  "errors": [],
  "recipe": {
    "id": "ff34e06c-09bd-4b93-8701-96aa82fcfb25",
    "index": 0,
    "name": "Brioche",
    "description": "Recipe description",
    "machine_model_id": "0f7b403b-1748-437a-9cf7-3f79abd33083",
    "hash": "67076c3831e3bb8fe9c9368a8bb0721558f4b0335b51fed9c52a8ba8a7b5bc2c",
    "hash_version": 1,
    "phases": [
      {
        "dosages": [],
        "values": [
          { "name": "cell_temperature", "type": "integer", "value": "260" },
          { "name": "time", "type": "integer", "value": "90" }
        ]
      }
    ],
    "metadata": [
      { "name": "image_path", "type": "string", "value": "../images/brioche.jpg" }
    ]
  }
}

recipe is the recipe template, identical to the file of the HTTPS transport. It is missing when errors is not empty.

Errors

errormeaningwhat to do
busyanother request of this device is still waiting for its answerwait for that answer (or 15 seconds), then retry
invalid_requestrequest_id, cookbook_sync_log_id or index missing or not validfix the request
sync_not_foundunknown cookbook_sync_log_id, or one of another devicestop: answer the sync with an error
sync_closedthe sync was already answered or cancelledstop: nothing to do
sync_expiredthe sync was sent more than 24 hours agostop: answer the sync with an error
index_not_foundno recipe with this index in the cookbookcheck the index against the manifest
too_largethe answer does not fit in one MQTT messagestop: answer the sync with an error; use HTTPS for this cookbook
internalerror on the Things5 sideretry later

Timing

  • The device can pull a sync for 24 hours after Things5 sent it; after that every request gets sync_expired.
  • Each request counts as activity on the sync. If a device makes no requests and does not answer for 3 minutes, Things5 stops waiting for it and moves on to the next devices of the same sync; the sync stays open on the device, and a later answer still completes it.

When the device has every recipe it needs, it answers the sync as usual.

Device → Things5: cmd/sync_recipe_templates_res

Publish on this topic once, when the sync is done or has failed, whatever transport the device used.

things5-production/v1/devices/<device-id>/cmd/sync_recipe_templates_res
{
  "request_id": "4f0437f1-d038-4a54-96e6-f353caa1223e",
  "cookbook_sync_log_id": "3980d532-3174-4c03-8ab9-eee7a87b7ad6",
  "errors": [],
  "synced_at": 1795086266936
}
namedescription
request_idthe request_id of cmd/sync_recipe_templates_v2_req
cookbook_sync_log_idthe same cookbook_sync_log_id sent in the command
errorsempty when the sync succeeded; otherwise one or more error messages. The first one is shown to the user as the result of the sync on this device
synced_atUnix time in milliseconds when the recipes were written

A real sync, message by message

The messages of a sync of a 3-recipe cookbook to a device that uses the MQTT pull, as they appear in the device's MQTT log in Things5 (payloads shortened, topic prefix …/v1/devices/{device_id}/ omitted). The whole sync took about 1.5 seconds.

#TopicDirectionPayload
1cmd/sync_recipe_templates_v2_reqThings5 → devicerequest_id: "4f0437f1…", cookbook_sync_log_id: "3980d532…", manifest_url, expires_at
2req/get_sync_manifest_reqDevice → Things5request_id: "238c53bd…", cookbook_sync_log_id: "3980d532…"
3req/get_sync_manifest_resThings5 → devicerequest_id: "238c53bd…", manifest: {recipes: [index 0, 1, 2]}, errors: []
4req/get_sync_recipe_reqDevice → Things5request_id: "e9275c0f…", index: 0
5req/get_sync_recipe_resThings5 → devicerequest_id: "e9275c0f…", index: 0, recipe: {…}, errors: []
6req/get_sync_recipe_reqDevice → Things5request_id: "209c75f3…", index: 1
7req/get_sync_recipe_resThings5 → devicerequest_id: "209c75f3…", index: 1, recipe: {…}, errors: []
8req/get_sync_recipe_reqDevice → Things5request_id: "00179b94…", index: 2
9req/get_sync_recipe_resThings5 → devicerequest_id: "00179b94…", index: 2, recipe: {…}, errors: []
10cmd/sync_recipe_templates_resDevice → Things5request_id: "4f0437f1…" (the command), cookbook_sync_log_id: "3980d532…", errors: [], synced_at

📘

Reading the MQTT log

The MQTT log shows messages in the order Things5 received or sent them, which can differ by a few milliseconds from the order on the device: an answer may appear just before its request, or the final answer before the last recipe answer. Match requests and answers by request_id.

A device that uses HTTPS shows only messages 1 and 10: the downloads do not go through MQTT.