Skip to content

A clock's life: put into service, back

The rule

A dealer installs and services clocks for its customers:

  • Put into service, a clock’s information (model, firmware, counts) is read, so the dealer sees what was installed.
  • Back after a long absence (a site closed for the season, a long outage), away longer than a week, its information is read again (its firmware may be new).

What the recipe leaves alone

  • Punches. A recipe never clears the punches a clock holds on a lifecycle moment. A clock allowed after being held may still carry punches that have not reached Connect yet, and clearing them would delete real punches. Bench punches (tests, a previous site) are deleted by an operator, once the clock’s upload is seen to be done.
  • Time. ZK clocks take their time from the server, and not every package offers sync_time; a hook that asks a package for an action it lacks fails (policy.hook_failed). A recipe asks only for actions every bundled package offers.
  • People. A clock moved to another group needs no hook: the group clears the old site’s people from it first (clear_users, ahead of anything it sends), never takes them in when the clock reports them meanwhile, and then sends its own, as it does for every clock that joins it carrying another group’s people. A hook that asked for clear_users itself would write behind the group’s back, and is refused (policy.hook_failed), as the admin API refuses it.

How the recipe does it

Two hooks, each returning the standard actions to start on the device:

  • joined (the device is allowed for the first time): get_info.
  • back (heard from after more than the endpoint’s back_after_minutes): get_info after more than a week away, nothing before.

The endpoint names the package (policy = "clock-lifecycle@1") and the absence that counts as away (back_after_minutes).

For a clock back from repair, see repaired-clock.

What the scenarios show

tests/01-moments.toml: a clock joining has its information read and nothing cleared; back after two days, nothing; back after a month, its information read again.

Get the recipe

The recipe, its Lua and its scenarios are in the example packages repository: policy/clock-lifecycle. Test it, or your own copy of it, with hourctl pkg test, and push it to a tenant as any policy package.

The policy package

clock-lifecycle/package.toml
name = "clock-lifecycle"
version = "1.0.0"
kind = "policy"
description = "A clock's information is read when it joins and again when it comes back from a long absence; its punches are never cleared, and its people stay the sync group's to write."
host_api = 2
clock-lifecycle/main.lua
-- What a dealer does with a clock at each moment of its life:
--
-- * joined: a clock put into service has its information read (model,
-- firmware, counts), so the dealer sees what was installed. Its time is
-- not set here: a ZK clock takes its time from the server on every
-- exchange, and not every package offers `sync_time` (a hook asking a
-- package for an action it lacks fails with policy.hook_failed). Nor are
-- its punches cleared: the clock may still hold punches that have not
-- reached Connect, and clearing them would delete them.
-- * moved: a clock moved to another group (another site) needs no hook.
-- Its people are the new group's to write: the group clears
-- the old site's people from it and sends it its own, as it does for
-- every clock that joins it carrying another group's people. A hook that
-- cleared them itself would write behind the group's back, and is
-- refused.
-- * back: a clock heard from again after a long absence (a repair, a
-- site closed for the season), gone longer than a week, has its
-- information read again (its firmware may be new).
local WEEK = 7 * 24 * 3600
return {
joined = function(device)
return { actions = { { action = "get_info" } } }
end,
back = function(device, absent_for)
if absent_for > WEEK then
return { actions = { { action = "get_info" } } }
end
return nil
end,
}

Scenarios

Each scenario calls the package’s hooks with what Connect would hand them, and checks what they decide (the format).

A clock's information is read when it joins, and again when it comes back from a long absence
tests/01-moments.toml
description = "A clock's information is read when it joins, and again when it comes back from a long absence"
[[step]]
name = "joined: its information read; nothing cleared, whatever punches it holds are still to be uploaded"
hook = "joined"
device = { endpoint = "office", device = "C1", group = "hq", labels = { site = "hq" } }
[step.expect]
effects = { actions = [{ action = "get_info" }] }
[[step]]
name = "back after two days: nothing to do"
hook = "back"
device = { endpoint = "office", device = "C1", group = "hq" }
absent_for = 172800
[step.expect]
effects = {}
[[step]]
name = "back after a month: information read again"
hook = "back"
device = { endpoint = "office", device = "C1", group = "hq" }
absent_for = 2592000
[step.expect]
effects = { actions = [{ action = "get_info" }] }