Skip to content

A clock back from repair

The rule

A clock that drops off for a few hours had a network outage: it needs nothing more (a ZK clock takes its time from the server). A clock away for days was most likely sent for repair, and may come back with a new board, new firmware or its settings reset. It is checked, and every punch it holds is read again, in case a repair interrupted an upload, without holding up the morning’s punches.

Which clocks

ZK clocks on push 2.4 or 3.4. Push 2.0 clocks cannot be asked for their punches again, so a job for one would fail; the repository tests that a recipe asks only for actions the packages it is for offer.

How the recipe does it

The back hook (the clock was heard from after more than its endpoint’s back_after_minutes) is given how long it was away (absent_for, in seconds):

  • under 3 days: nothing;
  • 3 days or more: get_info, and a job: the query_punches action on this clock alone, in the bulk lane, in a window from 22:00 to 05:00, each attempt given 2 hours. A job made by a hook is made once for the moment, however often the hook runs.

A punch read again is the same punch (its id is the same), so nothing is kept twice. The people the clock should hold need nothing from the hook: its sync group checks what the clock holds (the count of users it reports, its inventory) and sends again whoever it lacks.

What the scenarios show

tests/01-back.toml: back after a 6-hour outage, nothing done; back after a week, checked, with the overnight job.

Get the recipe

The recipe, its Lua and its scenarios are in the example packages repository: policy/repaired-clock. 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

repaired-clock/package.toml
name = "repaired-clock"
version = "1.0.0"
kind = "policy"
description = "A clock back after days away (a repair) has its information read, and every punch it holds read again overnight; back from a short outage, nothing is done."
host_api = 2
repaired-clock/main.lua
-- A ZK clock (push 2.4 or 3.4: 2.0 cannot be asked for its punches) heard
-- from again after being away. A short absence is a network
-- outage: nothing is needed (a ZK clock takes its time from the server). Away for days, it
-- was most likely sent for repair, and may come back with a new board,
-- new firmware or settings reset:
--
-- * its information is read again, so what it reports shows what changed;
-- * every punch it holds is read again, overnight, as a job in the bulk
-- lane, so that punches a repair interrupted the upload of reach Connect
-- without holding up the morning's punches. A punch read again is the
-- same punch: nothing is kept twice.
--
-- The people it should hold need nothing here: its sync group checks what
-- the clock holds (the count of users it reports, its inventory) and sends
-- again whoever it lacks.
local REPAIR = 3 * 24 * 3600
return {
back = function(device, absent_for)
if absent_for < REPAIR then
return nil
end
local name = device.endpoint .. "/" .. device.device
return {
actions = {
{ action = "get_info" },
},
jobs = {
{
name = "punches after repair: " .. name,
action = "query_punches",
lane = "bulk",
targets = { devices = { name } },
window = { hours = "22:00-05:00" },
timeout = "2h",
},
},
}
end,
}

Scenarios

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

Back from a short outage, a clock is left alone; back after a repair, it is checked and its punches read again overnight
tests/01-back.toml
description = "Back from a short outage, a clock is left alone; back after a repair, it is checked and its punches read again overnight"
[[step]]
name = "back after a 6-hour outage: nothing to do"
hook = "back"
device = { endpoint = "plant", device = "C7", group = "plant-7" }
absent_for = 21600
[step.expect]
effects = {}
[[step]]
name = "back after a week at the repair shop: checked, and its punches read again overnight in the bulk lane"
hook = "back"
device = { endpoint = "plant", device = "C7", group = "plant-7" }
absent_for = 604800
[step.expect]
effects = { actions = [{ action = "get_info" }], jobs = [{ name = "punches after repair: plant/C7", action = "query_punches", lane = "bulk", targets = { devices = ["plant/C7"] }, window = { hours = "22:00-05:00" }, timeout = "2h" }] }