fix(relays): {value} is the relay's label, not a per-relay value
It was built the other way round on a misreading of two screenshots: the pattern
held {value} and the per-relay boxes held numbers to drop into it. The ask was
simpler and better — {value} is the name typed in Relay labels, so a switch
addressed by antenna name is one pattern instead of eight URLs:
http://10.10.10.100/relay?on={value} relay 1 named Ant1 → ?on=Ant1
Renaming the antenna re-addresses it, and the name on the button and the name on
the wire cannot drift apart because they are the same string. It works in the
per-relay URLs and in the patterns alike, so the per-relay boxes go back to
holding URLs and nothing about them changes meaning any more.
The label is percent-encoded with %20 rather than "+" for a space: "+" is a
space only in a query string and a literal plus in a path, and this can land in
either half of a URL.
{value} on a relay with no label would send "?on=", an empty parameter that most
boards answer with a cheerful 200 and no movement. The driver refuses it and
names the label as what is missing; the editor warns while it is being typed,
beside the empty box rather than after an antenna fails to switch. The labels
also join the driver's cache key — they are part of the wire format now.
This commit is contained in:
@@ -22,7 +22,7 @@ func TestHTTPGenericPattern(t *testing.T) {
|
||||
|
||||
d := NewHTTPGeneric(nil, nil,
|
||||
srv.URL+"/relay?n={relay}&state=on",
|
||||
srv.URL+"/relay?n={relay}&state=off", "", "", 4)
|
||||
srv.URL+"/relay?n={relay}&state=off", "", "", 4, nil)
|
||||
if err := d.Set(context.Background(), 2, true); err != nil {
|
||||
t.Fatalf("Set on: %v", err)
|
||||
}
|
||||
@@ -53,7 +53,7 @@ func TestHTTPGenericPerRelayURLsWinOverThePattern(t *testing.T) {
|
||||
d := NewHTTPGeneric(
|
||||
[]string{srv.URL + "/FF0101", "", srv.URL + "/weird/on"},
|
||||
[]string{srv.URL + "/FF0100", "", ""},
|
||||
srv.URL+"/pattern/on/{relay}", srv.URL+"/pattern/off/{relay}", "", "", 3)
|
||||
srv.URL+"/pattern/on/{relay}", srv.URL+"/pattern/off/{relay}", "", "", 3, nil)
|
||||
|
||||
_ = d.Set(context.Background(), 1, true) // its own URL
|
||||
_ = d.Set(context.Background(), 2, true) // empty → falls back to the pattern
|
||||
@@ -66,36 +66,36 @@ func TestHTTPGenericPerRelayURLsWinOverThePattern(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The {value} form: one address, and the per-relay boxes hold the number that
|
||||
// goes into it. A bit-mask board (qro.cz) is the case — /Set0/1, /Set0/2,
|
||||
// /Set0/4, /Set0/8 — where four full URLs differ by one character.
|
||||
func TestHTTPGenericValueSubstitution(t *testing.T) {
|
||||
// {value} is the relay's LABEL: a switch addressed by antenna name rather than
|
||||
// by channel number is one pattern instead of eight URLs.
|
||||
func TestHTTPGenericValueIsTheRelayLabel(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
var got []string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
mu.Lock()
|
||||
got = append(got, r.URL.Path)
|
||||
got = append(got, r.URL.String())
|
||||
mu.Unlock()
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
d := NewHTTPGeneric(
|
||||
[]string{"1", "2", "4", "8"},
|
||||
[]string{"0", "0", "0", "0"},
|
||||
srv.URL+"/Set0/{value}", srv.URL+"/Set0/{value}", "", "", 4)
|
||||
_ = d.Set(context.Background(), 3, true)
|
||||
_ = d.Set(context.Background(), 1, false)
|
||||
[]string{srv.URL + "/relay?on={value}"}, // per-relay URL
|
||||
nil,
|
||||
"", srv.URL+"/relay?off={value}", // and the pattern, for the other direction
|
||||
"", "", 3, []string{"Ant1", "Beam 20m", ""})
|
||||
_ = d.Set(context.Background(), 1, true)
|
||||
_ = d.Set(context.Background(), 2, false)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
want := []string{"/Set0/4", "/Set0/0"}
|
||||
// The space in "Beam 20m" must go out as %20 — a "+" would be a literal plus
|
||||
// in a path, and this substitution can land in either half of a URL.
|
||||
want := []string{"/relay?on=Ant1", "/relay?off=Beam%2020m"}
|
||||
if strings.Join(got, " ") != strings.Join(want, " ") {
|
||||
t.Errorf("requested %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// {value} and {relay-1} together: the other API of the same board, whose
|
||||
// channels are numbered from zero. Without the offset the pattern has to be
|
||||
// abandoned for four hand-typed URLs.
|
||||
// {relay-1} for a board whose channels are numbered from zero.
|
||||
func TestHTTPGenericRelayOffset(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
var got []string
|
||||
@@ -106,10 +106,8 @@ func TestHTTPGenericRelayOffset(t *testing.T) {
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
d := NewHTTPGeneric(
|
||||
[]string{"1", "1", "1", "1"},
|
||||
[]string{"0", "0", "0", "0"},
|
||||
srv.URL+"/set0/{relay-1}/{value}", srv.URL+"/set0/{relay-1}/{value}", "", "", 4)
|
||||
d := NewHTTPGeneric(nil, nil,
|
||||
srv.URL+"/set0/{relay-1}/1", srv.URL+"/set0/{relay-1}/0", "", "", 4, nil)
|
||||
_ = d.Set(context.Background(), 1, true)
|
||||
_ = d.Set(context.Background(), 4, false)
|
||||
mu.Lock()
|
||||
@@ -120,14 +118,15 @@ func TestHTTPGenericRelayOffset(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// With {value} in the pattern the per-relay boxes hold values, so an empty one
|
||||
// must be reported as a missing VALUE. Telling the operator to enter a URL in a
|
||||
// box that wants "4" sends them to rewrite a configuration that was nearly right.
|
||||
func TestHTTPGenericNamesAMissingValue(t *testing.T) {
|
||||
d := NewHTTPGeneric(nil, nil, "http://x/Set0/{value}", "http://x/Set0/{value}", "", "", 2)
|
||||
// A URL that uses {value} on an unlabelled relay would go out as "?on=" — an
|
||||
// empty parameter, which most boards answer with a cheerful 200 and no
|
||||
// movement. It must be refused, and the message must say the label is what is
|
||||
// missing.
|
||||
func TestHTTPGenericRefusesValueWithoutALabel(t *testing.T) {
|
||||
d := NewHTTPGeneric(nil, nil, "http://x/relay?on={value}", "", "", "", 2, []string{"", ""})
|
||||
err := d.Set(context.Background(), 1, true)
|
||||
if err == nil || !strings.Contains(err.Error(), "value") {
|
||||
t.Errorf("err = %v, want it to name the missing value", err)
|
||||
if err == nil || !strings.Contains(err.Error(), "label") {
|
||||
t.Errorf("err = %v, want it to name the missing label", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,7 +149,7 @@ func TestHTTPGenericSuppliesTheScheme(t *testing.T) {
|
||||
// A switch with the ON URLs filled and OFF left empty latches. The error has to
|
||||
// name the direction, or the operator cannot tell which half is missing.
|
||||
func TestHTTPGenericNamesTheMissingDirection(t *testing.T) {
|
||||
d := NewHTTPGeneric([]string{"http://x/on"}, nil, "", "", "", "", 1)
|
||||
d := NewHTTPGeneric([]string{"http://x/on"}, nil, "", "", "", "", 1, nil)
|
||||
err := d.Set(context.Background(), 1, false)
|
||||
if err == nil || !strings.Contains(err.Error(), "OFF") {
|
||||
t.Errorf("err = %v, want it to name the OFF direction", err)
|
||||
@@ -161,7 +160,7 @@ func TestHTTPGenericNamesTheMissingDirection(t *testing.T) {
|
||||
func TestHTTPGenericRemembersWhatItCommanded(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
||||
defer srv.Close()
|
||||
d := NewHTTPGeneric(nil, nil, srv.URL+"/on/{relay}", srv.URL+"/off/{relay}", "", "", 3)
|
||||
d := NewHTTPGeneric(nil, nil, srv.URL+"/on/{relay}", srv.URL+"/off/{relay}", "", "", 3, nil)
|
||||
_ = d.Set(context.Background(), 2, true)
|
||||
st, err := d.Status(context.Background())
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user