Skip to content

Resources

Jon Hall edited this page May 17, 2018 · 11 revisions

Top-Level Resources

Below are the details of the resources that Swill SDK manages as well as links to pages where you can learn how to interact with them.

Property Wiki Description
actors Actors Heating elements, pumps, etc.
fermenters Fermenters Each Fermenter references a Sensor and applies an algorithm to turn an Actor on or off.
kettles Kettles Each Kettle references a Sensor and applies an algorithm to turn an Actor on or off.
sensors Sensors A sensor/probe that reports its temperature.
steps Steps The steps that you want CraftBeerPi to follow.

Interacting with Resources

Getting a List of Resources

Each top-level resource has some basic methods that can be called. One getter function in particular on each resource will return a list of all the instances of that type. For the Actors resource, it's getActors(), for the Kettles resource it's getKettles(), etc. This will be the primary way to control and interact with CraftBeerPi resources.

An example of how to get all resources of a given type:

const actors = await sdk.resources.actors.getActors();

Interacting with Instances of Resources

Once you have a list of a resources instances, you can operate on them. All functions are async and return a Promise that resolves when the resource is officially updated in CraftBeerPi. Below are the common methods available to all resources.

Method Param Description
setState Object Merges a given object with the instance's own properties and submits it to CBPi to save. Resolves with the updated resource, and also edits the resource that setState was called on in-memory.
remove n/a Removes the instance of the resource from CBPi.

An example of setting a new state on a resource:

const actors = await sdk.resources.actors.getActors();

// Set a single state
await actors[0].setState({name: 'NewActorName'});

console.log(actors[0].name); // NewActorName

// Set multiple states at the same time
await actors[0].setState({name: 'NewActorNameTwo', power: 50});

console.log(`${actors[0].name}, ${actors[0].power}`); // NewActorNameTwo, 50

Receiving Updates to Instances of Resources

Each Top-Level Resource exposes an onUpdate function that allows clients to register event listeners with the resource.

Example event handler:

const unsubcribe = sdk.resources.actors.onUpdate((event_name, updated_resource) => 
  console.log(`${event_name}: ${JSON.stringify(updated_resource)}`));
// When the resource is changed in any way, this log occurs:
// UPDATE_ACTOR: { ... }
unsubcribe (); // No more events

Clone this wiki locally