Lab 3: Event-Query APIs and Pico State
- Due No Due Date
- Points 100
- Submitting a text entry box, a website url, a media recording, or a file upload
Objective
The objective of this lab is to understand
- the Event-Query API Links to an external site.
- the use of entity variables.
Reading and Reference
Read the following:
- Pico State Lesson Links to an external site.
- Persistent Compute Objects
- Pico APIs: Events and Queries Links to an external site.
The following video may be helpful:
Pico State and Array and Map Operators Video
Links to an external site.
You may find the following resources useful:
- Mutating Persistent Variables Links to an external site.
- KRL Manual
- Developer Tips for the Pico Engine
Prerequisites
You should have completed:
Implementation Notes
- The best way to use the KRL documentation is to search since you may not always appreciate how it's organized (or isn't).
- In this lab you will implement another ruleset for your pico that complements or adds functionality to the ruleset you built for Lab 3: Rules, Events, and Intermediaries. Note that this second ruleset depends on explicit events that the rules in the first ruleset raise. This is an important property of picos.
Do This
Do the following:
-
Write a new ruleset called
temperature_store
to serve as a temperature module. This ruleset should have three rules:- A rule named
collect_temperatures
that looks forwovyn:new_temperature_reading
events and stores thetemperature
andtimestamp
event attributes in an entity variable. The entity variable should contain all the temperatures that have been processed. Hint: look at the resources above for persistent variable help. - A rule named
collect_threshold_violations
that looks forwovyn:threshold_violation
events and stores the violation temperature and a timestamp in a different entity variable that collects threshold violations. - A rule named
clear_temeratures
that looks for asensor:reading_reset
event and resets both of the entity variables from the rules in (1) and (2).
- A rule named
-
Add three functions to the global block of the
temperature_store
ruleset. These functions should return an array of temperatures where each temperature is a timestamp and a temperature in degrees.-
A function called
temperatures
that returns the contents of the temperature entity variable. -
A function called
threshold_violations
that returns the contents of the threshold violation entity variable. -
A function called
inrange_temperatures
that returns all the temperatures in the temperature entity variable that aren't in the threshold violation entity variable. (Note: I expect you to solve this without adding a rule that collects in-range temperatures)
-
-
Add a
provides
pragma to the meta block Links to an external site. of thetemperature_store
ruleset that lists the three functions. Also add ashares
pragma to the meta block with the same list of functions. -
Install your
temperature_store
ruleset in the same pico as your modifiedwovyn_base
ruleset. Test your setup to ensure you are storing temperatures from the wovyn device. You can use curl, a program you write, or a browser app like the Advanced REST Client Links to an external site. to make calls to the functions.
Deliverables
Turn in the following:
- URLs for your ruleset(s)
- Short screencast (< 3 min with sound) showing your new functions and rules with a short deminstartion of the labs functionality.
- Answers to the questions, including any posed in the preceding "do this" section.
Questions
- Explain how the rule
collect_temperatures
and thetemperatures
function work as an event-query API. -
Explain your strategy for finding temperatures that are in range.
-
What happens if
provides
doesn't list the name of thetemperatures
function? -
What happens if
shares
doesn't list it?