everlights 0.1.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +43 -7
- data/config/everlights.example.yml +105 -0
- data/exe/everlights +75 -26
- data/lib/everlights/cloud.rb +113 -0
- data/lib/everlights/sync.rb +93 -10
- data/lib/everlights/web.html +869 -0
- data/lib/everlights/web.rb +214 -0
- data/lib/everlights.rb +81 -10
- data/skills/everlights/SKILL.md +8 -5
- metadata +11 -5
data/lib/everlights/sync.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
require "yaml"
|
|
2
2
|
|
|
3
3
|
module Everlights
|
|
4
|
-
# Builds sequences and calendar entries on the bridge from a config (
|
|
4
|
+
# Builds sequences and calendar entries on the bridge from a config (`everlights init` writes one; format in config/everlights.example.yml).
|
|
5
5
|
#
|
|
6
6
|
# config = Everlights::Sync.load_config("config/everlights.yml")
|
|
7
7
|
# plan = Everlights::Sync.new(el, config, state_path: "config/.sync-state.yml").plan # raises Error listing every config problem
|
|
@@ -19,7 +19,8 @@ module Everlights
|
|
|
19
19
|
|
|
20
20
|
Action = Struct.new(:kind, :op, :name, :detail, :run)
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
# shows: {rule name => [[start, end], ...]}, the nights each rule plays, tonight's show under way included
|
|
23
|
+
Plan = Struct.new(:actions, :skips, :horizon, :sync, :shows) do
|
|
23
24
|
def empty? = actions.empty?
|
|
24
25
|
def apply! = sync.apply(self)
|
|
25
26
|
|
|
@@ -63,12 +64,55 @@ module Everlights
|
|
|
63
64
|
def self.load_config(path)
|
|
64
65
|
config = YAML.safe_load_file(path, permitted_classes: [Date]) || {}
|
|
65
66
|
# YAML 1.1 reads the key `on:` as boolean true; put it back
|
|
66
|
-
|
|
67
|
+
# (transform_keys keeps `on` where it was, so a saved file reads in the same order)
|
|
68
|
+
config["schedule"].map! { |r| r.is_a?(Hash) ? r.transform_keys { _1 == true ? "on" : _1 } : r } if config["schedule"].is_a?(Array)
|
|
67
69
|
config
|
|
68
70
|
rescue Psych::SyntaxError => e
|
|
69
71
|
raise Error, "#{path}: #{e.message}"
|
|
70
72
|
end
|
|
71
73
|
|
|
74
|
+
# What sync has made on the bridge, next to the config.
|
|
75
|
+
def self.state_path(config_path) = File.join(File.dirname(config_path), ".sync-state.yml")
|
|
76
|
+
|
|
77
|
+
# What's on the bridge that sync didn't make (the EverLights app, `sequence add`, `calendar add`), as config:
|
|
78
|
+
# [{"sequences" => {...}, "schedule" => [...]}, notes about what a config can't say].
|
|
79
|
+
# Shows become one rule each, from their nights still to come.
|
|
80
|
+
def self.export(client, state_path: nil, now: Time.now)
|
|
81
|
+
state = state_path && File.exist?(state_path) ? YAML.safe_load_file(state_path) || {} : {}
|
|
82
|
+
ours = (state["sequences"] || {}).values + (state["events"] || {}).values
|
|
83
|
+
seqs = client.sequence_list
|
|
84
|
+
names = seqs.to_h { [_1["id"], _1["alias"]] }
|
|
85
|
+
sequences = seqs.reject { _1["id"].start_with?(BUILTIN_PREFIX) || ours.include?(_1["id"]) }.to_h do |s|
|
|
86
|
+
effects = s["effects"].to_h { [_1["effectType"], _1["value"]] }
|
|
87
|
+
[s["alias"], effects.empty? ? {"colors" => s["pattern"]} : {"colors" => s["pattern"], "effects" => effects}]
|
|
88
|
+
end
|
|
89
|
+
notes = []
|
|
90
|
+
schedule = client.event_list.reject { ours.include?(_1["id"]) }.filter_map do |e|
|
|
91
|
+
nights = e["occurrences"].map { [Time.parse(_1["start"]).localtime, Time.parse(_1["end"]).localtime] }
|
|
92
|
+
.select { |_, finish| finish > now }.sort
|
|
93
|
+
days = nights.map { _1[0].to_date }
|
|
94
|
+
next if days.empty?
|
|
95
|
+
unless days.each_cons(2).all? { |a, b| b == a + 1 }
|
|
96
|
+
notes << "#{e["alias"]}: its nights aren't in a row (#{days.map(&:iso8601).join(", ")}); write rules for them by hand"
|
|
97
|
+
next
|
|
98
|
+
end
|
|
99
|
+
notes << "#{e["alias"]}: plays on some zones only; a rule plays on every zone" unless e["zoneSerials"] == [ALL_ZONES]
|
|
100
|
+
start, finish = nights[0]
|
|
101
|
+
rule = {"name" => e["alias"]}
|
|
102
|
+
rule.merge!(days.size == 1 ? {"on" => days[0]} : {"from" => days[0], "to" => days[-1]})
|
|
103
|
+
rule["start"] = e["flags"].include?("StartAtDusk") ? "dusk" : start.strftime("%H:%M")
|
|
104
|
+
rule["end"] = finish.strftime("%H:%M")
|
|
105
|
+
rule["sequences"] = e["sequences"].map do |q|
|
|
106
|
+
name = names.fetch(q["sequenceId"], q["sequenceId"])
|
|
107
|
+
q["duration"] == DEFAULT_SECONDS ? name : "#{name}:#{q["duration"]}"
|
|
108
|
+
end
|
|
109
|
+
rule
|
|
110
|
+
end
|
|
111
|
+
[{"sequences" => sequences, "schedule" => schedule}, notes]
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
attr_reader :bridge_seqs # the bridge's sequences, as read by the last #plan
|
|
115
|
+
|
|
72
116
|
def initialize(client, config, state_path: nil, adopt: false, now: Time.now)
|
|
73
117
|
@c, @cfg, @state_path, @adopt, @now = client, config || {}, state_path, adopt, now
|
|
74
118
|
end
|
|
@@ -76,7 +120,7 @@ module Everlights
|
|
|
76
120
|
def plan
|
|
77
121
|
@errors = []
|
|
78
122
|
@state = load_state
|
|
79
|
-
bridge_seqs = @c.sequence_list
|
|
123
|
+
bridge_seqs = @bridge_seqs = @c.sequence_list
|
|
80
124
|
bridge_events = @c.event_list
|
|
81
125
|
actions = @adopt ? adopt(bridge_seqs, bridge_events) : []
|
|
82
126
|
owned_seq_ids = @state["sequences"].values
|
|
@@ -150,11 +194,13 @@ module Everlights
|
|
|
150
194
|
|
|
151
195
|
# One calendar entry per rule. Nights already under way stay; ended ones are dropped.
|
|
152
196
|
live = ->(event) { event["occurrences"].select { |o| Time.parse(o["start"]) <= @now + MARGIN && Time.parse(o["end"]) > @now } }
|
|
197
|
+
shows = {}
|
|
153
198
|
rules.each do |r|
|
|
154
199
|
nights = wanted[r[:name]]
|
|
155
200
|
current = bridge_events.find { _1["id"] == @state["events"][r[:name]] }
|
|
156
|
-
kept = current ? live.(current).map { [Time.parse(_1["start"]), Time.parse(_1["end"])] } : []
|
|
201
|
+
kept = current ? live.(current).map { [Time.parse(_1["start"]).localtime, Time.parse(_1["end"]).localtime] } : []
|
|
157
202
|
occurrences = kept + nights
|
|
203
|
+
shows[r[:name]] = occurrences unless occurrences.empty?
|
|
158
204
|
flags = r[:start][:dusk] ? ["StartAtDusk"] : []
|
|
159
205
|
if current.nil?
|
|
160
206
|
next if nights.empty?
|
|
@@ -203,7 +249,7 @@ module Everlights
|
|
|
203
249
|
end
|
|
204
250
|
end
|
|
205
251
|
|
|
206
|
-
Plan.new(actions, skips, horizon, self)
|
|
252
|
+
Plan.new(actions, skips, horizon, self, shows)
|
|
207
253
|
end
|
|
208
254
|
|
|
209
255
|
def apply(plan)
|
|
@@ -214,6 +260,20 @@ module Everlights
|
|
|
214
260
|
|
|
215
261
|
# --- dates ---
|
|
216
262
|
|
|
263
|
+
# The dates of an iCalendar feed's events, in local time; `match` keeps events whose title contains it.
|
|
264
|
+
# ponytail: an event counts on its start date only: a recurring one (RRULE) on its first, a multi-day one
|
|
265
|
+
# on its first day. Team and school feeds list each event; expand RRULE if a feed needs it.
|
|
266
|
+
def self.ics_dates(text, match = nil)
|
|
267
|
+
text.gsub("\r\n", "\n").gsub(/\n[ \t]/, "") # unfold continued lines
|
|
268
|
+
.scan(/^BEGIN:VEVENT\n(.*?)^END:VEVENT$/m).filter_map do |(event)|
|
|
269
|
+
next if event.match?(/^STATUS:CANCELLED$/)
|
|
270
|
+
next if match && !event[/^SUMMARY[^:\n]*:(.*)$/, 1].to_s.downcase.include?(match.to_s.downcase)
|
|
271
|
+
m = event.match(/^DTSTART[^:\n]*:(\d{4})(\d\d)(\d\d)(?:T(\d\d)(\d\d)(\d\d)(Z)?)?/) or next
|
|
272
|
+
y, mo, d, h, mi, s = m.captures.first(6).map(&:to_i)
|
|
273
|
+
m[7] ? Time.utc(y, mo, d, h, mi, s).localtime.to_date : Date.new(y, mo, d) # floating and TZID times: their own date
|
|
274
|
+
end.uniq
|
|
275
|
+
end
|
|
276
|
+
|
|
217
277
|
# Anonymous Gregorian algorithm.
|
|
218
278
|
def self.easter(year)
|
|
219
279
|
a, b, c = year % 19, year / 100, year % 100
|
|
@@ -304,7 +364,7 @@ module Everlights
|
|
|
304
364
|
start = check(where) { parse_start(r.fetch("start", defaults["start"])) }
|
|
305
365
|
finish = check(where) { parse_hm(r.fetch("end", defaults["end"])) }
|
|
306
366
|
if start&.dig(:dusk) && !(@cfg.dig("location", "lat") && @cfg.dig("location", "lon"))
|
|
307
|
-
@errors << "
|
|
367
|
+
@errors << "rules that start at dusk need `location: {lat:, lon:}` in the config"
|
|
308
368
|
end
|
|
309
369
|
seconds = r.fetch("seconds", defaults["seconds"])
|
|
310
370
|
@errors << "#{where}: needs sequences" if Array(r["sequences"]).empty?
|
|
@@ -319,18 +379,41 @@ module Everlights
|
|
|
319
379
|
end
|
|
320
380
|
|
|
321
381
|
def matcher(r, where)
|
|
322
|
-
on, every, range = r["on"], r["every"], r.key?("from") || r.key?("to")
|
|
323
|
-
if [on, every].compact.size + (range && !every ? 1 : 0) != 1
|
|
324
|
-
@errors << "#{where}: needs exactly one of `on:`, `from:`/`to:`, or `
|
|
382
|
+
on, every, range, ics = r["on"], r["every"], r.key?("from") || r.key?("to"), r["ics"]
|
|
383
|
+
if [on, every, ics].compact.size + (range && !every ? 1 : 0) != 1
|
|
384
|
+
@errors << "#{where}: needs exactly one of `on:`, `from:`/`to:`, `every:`, or `ics:`"
|
|
325
385
|
return nil
|
|
326
386
|
end
|
|
327
387
|
return check(where) { on_matcher(on) } if on
|
|
388
|
+
if ics
|
|
389
|
+
dates = check(where) { Sync.ics_dates(fetch(ics), r["match"]) }
|
|
390
|
+
return dates && ->(d) { dates.include?(d) }
|
|
391
|
+
end
|
|
328
392
|
in_range = range ? check(where) { range_matcher(r["from"], r["to"]) } : ->(_) { true }
|
|
329
393
|
return in_range unless every && in_range
|
|
330
394
|
days = check(where) { Array(every).map { weekday(_1) } }
|
|
331
395
|
days && ->(d) { days.include?(d.wday) && in_range.(d) }
|
|
332
396
|
end
|
|
333
397
|
|
|
398
|
+
# ponytail: fetched on every plan, and `web` plans on each save; cache it if a slow feed makes the page drag
|
|
399
|
+
def fetch(source)
|
|
400
|
+
src = source.to_s.strip
|
|
401
|
+
raise Error, "`ics:` needs a URL or a file path" if src.empty?
|
|
402
|
+
return File.read(src) unless src.match?(%r{\A(https?|webcal)://}i)
|
|
403
|
+
uri = URI(src.sub(/\Awebcal/i, "https"))
|
|
404
|
+
3.times do
|
|
405
|
+
res = Net::HTTP.get_response(uri)
|
|
406
|
+
return res.body if res.is_a?(Net::HTTPSuccess)
|
|
407
|
+
raise Error, "#{src} -> #{res.code}" unless res.is_a?(Net::HTTPRedirection)
|
|
408
|
+
uri = URI.join(uri, res["location"])
|
|
409
|
+
end
|
|
410
|
+
raise Error, "#{src}: too many redirects"
|
|
411
|
+
rescue Error
|
|
412
|
+
raise
|
|
413
|
+
rescue StandardError => e # no network, no such file, bad TLS, ...
|
|
414
|
+
raise Error, "can't read #{src}: #{e.message}"
|
|
415
|
+
end
|
|
416
|
+
|
|
334
417
|
def on_matcher(v)
|
|
335
418
|
at = date_expr(v)
|
|
336
419
|
->(d) { (d.year - 1..d.year + 1).any? { at.(_1) == d } } # offsets can cross a year boundary
|