Keypad bindings in the UI (/keypad.html, after the Design canvas' GridBinding): the keypads drawn as can.json has them, a key or knob picked by a tap or by pressing it, what it drives (a screen action or an output), the output's mode, what it does in an open menu, its LEDs; saved per car in data/drive/bindings.json over the profile's (GET/PUT/DELETE /api/keypad/bindings, checked on the way in), drive mode takes them when it loads
This commit is contained in:
1 parent
d6af65f4ef
commit
e82b2236b5
7 files changed
+325
-8
No files matched your search
@@ -516,18 +516,38 @@ module Maptool
|
||||
# CANopen TIME while nobody else sends it (can.json's "time": false: never).
|
||||
CAN_TIME = CAN_CONFIG && CAN_CONFIG.fetch("time", true) && !test? ? Can::TimeProducer.new { |f| App.can_send(f) } : nil
|
||||
|
||||
# The screen profile's bindings, for the outputs (the browser has them
|
||||
# for the screen's functions); read again when the file changes.
|
||||
# The keypads' bindings: the ones saved in /keypad.html (data/drive/
|
||||
# bindings.json, this car's), else the screen profile's defaults; read
|
||||
# again when the file changes. The browser takes them from GET /api/keypad/bindings.
|
||||
DRIVE_PROFILE_PATH = File.join(__dir__, "public", "drive", "road.json")
|
||||
BINDINGS_PATH = File.join(DATA_DIR, "drive", "bindings.json")
|
||||
def self.bindings
|
||||
mtime = File.mtime(DRIVE_PROFILE_PATH)
|
||||
@bindings = [mtime, JSON.parse(File.read(DRIVE_PROFILE_PATH)).fetch("bindings", [])] unless @bindings&.first == mtime
|
||||
path = File.exist?(BINDINGS_PATH) ? BINDINGS_PATH : DRIVE_PROFILE_PATH
|
||||
key = [path, File.mtime(path)]
|
||||
@bindings = [key, JSON.parse(File.read(path)).fetch("bindings", [])] unless @bindings&.first == key
|
||||
@bindings.last
|
||||
rescue StandardError => e
|
||||
warn "drive profile: #{e.message}"
|
||||
warn "drive bindings: #{e.message}"
|
||||
[]
|
||||
end
|
||||
|
||||
BINDING_KEYS = %w[input action menu output mode pulse_s led led_on].freeze
|
||||
# A saved list's check: each an input, an action / menu or an output, known modes and colours.
|
||||
def self.check_bindings(list)
|
||||
raise ArgumentError, "bindings: a list" unless list.is_a?(Array)
|
||||
|
||||
list.map do |b|
|
||||
raise ArgumentError, "a binding: an object" unless b.is_a?(Hash)
|
||||
|
||||
b = b.slice(*BINDING_KEYS).reject { |_, v| v.nil? || v == "" }
|
||||
raise ArgumentError, "a binding without an input" unless b["input"].is_a?(String) && b["input"].include?(".")
|
||||
raise ArgumentError, "#{b['input']}: unknown mode #{b['mode']}" if b["mode"] && !Outputs::MODES.include?(b["mode"])
|
||||
%w[led led_on].each { |k| raise ArgumentError, "#{b['input']}: unknown colour #{b[k]}" if b[k] && !Keypad::COLORS.key?(b[k]) }
|
||||
b["pulse_s"] = Float(b["pulse_s"]) if b["pulse_s"]
|
||||
b
|
||||
end
|
||||
end
|
||||
|
||||
def self.keypads_present
|
||||
nodes = CAN_STORE.snapshot[:nodes]
|
||||
KEYPAD ? KEYPAD.devices.select { |d| (seen = nodes.dig(d.heartbeat_node, :heartbeat_at)) && Time.now.to_f - seen < Keypad::PRESENT_S } : []
|
||||
@@ -666,6 +686,11 @@ module Maptool
|
||||
erb :can, layout: false
|
||||
end
|
||||
|
||||
# The keypads' bindings, edited (after the Design canvas' GridBinding).
|
||||
get "/keypad.html" do
|
||||
erb :keypad, layout: false
|
||||
end
|
||||
|
||||
# Every CAN frame as it came, decoded (polls GET /api/can?since=).
|
||||
get "/trace.html" do
|
||||
erb :trace, layout: false
|
||||
@@ -1688,6 +1713,39 @@ module Maptool
|
||||
json ok: true
|
||||
end
|
||||
|
||||
# The bindings and what they can bind (/keypad.html, drive mode): the
|
||||
# keypads' channels, the outputs, the colours; saved: this car's own
|
||||
# (PUT {bindings}), else the profile's (DELETE goes back to them).
|
||||
get "/api/keypad/bindings" do
|
||||
require_login!
|
||||
keypads = (KEYPAD&.devices || []).map do |d|
|
||||
keys = d.protocol == "pkp" ? (1..d.keys).map { |k| "K#{k}" } : []
|
||||
{ name: d.name, protocol: d.protocol, node: d.node, channels: (keys + d.inputs.keys).uniq }
|
||||
end
|
||||
outputs = (OUTPUTS&.snapshot || []).flat_map { |m| m[:channels].map { |c| { output: "#{m[:name]}.#{c[:n]}", label: c[:label] } } }
|
||||
json bindings: App.bindings, saved: File.exist?(BINDINGS_PATH), keypads: keypads, outputs: outputs,
|
||||
modes: Outputs::MODES, colors: Keypad::COLORS.keys - ["off"]
|
||||
end
|
||||
|
||||
put "/api/keypad/bindings" do
|
||||
require_login!
|
||||
begin
|
||||
list = App.check_bindings(JSON.parse(request.body.read)["bindings"])
|
||||
rescue ArgumentError, JSON::ParserError => e
|
||||
halt 400, json(error: e.message)
|
||||
end
|
||||
FileUtils.mkdir_p(File.dirname(BINDINGS_PATH))
|
||||
File.write("#{BINDINGS_PATH}.tmp", JSON.pretty_generate(bindings: list))
|
||||
File.rename("#{BINDINGS_PATH}.tmp", BINDINGS_PATH)
|
||||
json bindings: App.bindings, saved: true
|
||||
end
|
||||
|
||||
delete "/api/keypad/bindings" do
|
||||
require_login!
|
||||
FileUtils.rm_f(BINDINGS_PATH)
|
||||
json bindings: App.bindings, saved: false
|
||||
end
|
||||
|
||||
# The outputs (can.json's "outputs"): each module's channels, their
|
||||
# labels and state, which bindings drive them.
|
||||
get "/api/outputs" do
|
||||
|
||||
Reference in new issue
Block a user