Skip to content

Commit

Permalink
erlang: implemented CONTROLLERS dictionary lookup (cf. uhppoted/uhppo…
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Jul 30, 2024
1 parent fc89bb9 commit f1530d5
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 14 deletions.
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
- [x] Python
- [x] Zig
- [x] PHP
- [ ] Erlang
- [x] Erlang
- [x] udp-sendto
- [x] tcp-sendto
- [ ] controllers dictionary
- [x] controllers dictionary
- [ ] Lua
- [ ] models: rename 'device id' to controller and set type to 'controller'
- [ ] remove uhppote::resolve
Expand Down
2 changes: 1 addition & 1 deletion bindings/.models/erlang.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"datetime": "datetime",
"HHmm": "hhmm",
"pin": "pin",
"controller": "uint32",
"controller": "Controller",

"optional date": "date",
"optional datetime": "datetime"
Expand Down
6 changes: 1 addition & 5 deletions bindings/erlang/.templates/templates.erl
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
{{define "args"}}{{ range $index, $arg := .}}{{if ne .type "magic"}}{{if $index}}, {{end}}{{CamelCase .name}}{{end}}{{end}}{{end}}

{{define "params"}}{{ range $index, $param := .}}{{if ne .type "magic"}}{{if $index}}, {{end}}{{CamelCase .name}}{{end}}{{end}}{{end}}
{{define "params"}}{{ range $index, $param := .}}{{if ne .type "magic"}}, {{CamelCase .name}}{{end}}{{end}}{{end}}

{{define "values"}}{{ range $index, $param := .}}{{if $index}}, {{end}}{{template "var" .}}{{end}}{{end}}

{{define "type"}}{{lookup "erlang.types" . "???"}}{{end}}

{{define "var"}}to{{CamelCase .type}}("{{ .value }}"){{end}}




29 changes: 27 additions & 2 deletions bindings/erlang/src/commands.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

-export([commands/0, find/1, exec/3]).

-include("records.hrl").

-define(CONTROLLER, 405419896).
-define(DOOR, 3).
-define(MODE, 2).
Expand All @@ -18,7 +20,19 @@

-define(LOG_TAG, "commands").

-include("records.hrl").
-define(CONTROLLERS, #{
405419896 => #controller{
controller = 405419896,
address = "192.168.1.100",
transport = "tcp"
},

303986753 => #controller{
controller = 303986753,
address = "192.168.1.100:60000",
transport = "udp"
}
}).

commands() ->
[
Expand Down Expand Up @@ -79,7 +93,7 @@ execute(get_all_controllers, _Options, Config) ->
uhppoted:get_all_controllers(Config);

execute(get_controller, _Options, Config) ->
Controller = ?CONTROLLER,
Controller = resolve(?CONTROLLER),
uhppoted:get_controller(Config, Controller);

execute(set_ip, _Options, Config) ->
Expand Down Expand Up @@ -333,6 +347,17 @@ listen() ->
log:infof(?LOG_TAG, closed)
end.

resolve(Controller) ->
case maps:find(Controller, ?CONTROLLERS) of
{ok, V} -> V;
_ ->
#controller{
controller = Controller,
address = "",
transport = "udp"
}
end.

parse_addr(S) ->
[A, P] = string:tokens(S, ":"),
{ok, Addr} = inet:parse_address(A),
Expand Down
3 changes: 2 additions & 1 deletion bindings/erlang/src/records.hrl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-record(controller, {controller, address, transport}).

{{range .model.responses}}
{{ template "response" . -}}
{{end}}
Expand All @@ -17,4 +19,3 @@
{{range (last .fields)}}{{snakeCase .name}}{{end}}
}).
{{end}}

13 changes: 10 additions & 3 deletions bindings/erlang/src/uhppoted.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

-define(LOG_TAG, "uhppoted").

-record(controller, {controller, address, transport}).
-include("records.hrl").

get_all_controllers(Config) ->
Request = encoder:get_controller_request(0),
Expand Down Expand Up @@ -63,7 +63,7 @@ listen(Handler) ->
{{define "function"}}
{{snakeCase .name}}(Config, {{template "args" .args}}) ->
C = resolve(Controller),
Request = encoder:{{snakeCase .request.name}}({{template "params" .args}}),
Request = encoder:{{snakeCase .request.name}}(C#controller.controller{{template "params" slice .args 1}}),

case ut0311:send(Config, C, Request) of
{ok, none} ->
Expand All @@ -77,9 +77,16 @@ listen(Handler) ->
end.
{{end}}

resolve(Controller) ->
resolve(Controller) when is_integer(Controller) ->
#controller{
controller = Controller,
address = "",
transport = "udp"
};

resolve({controller, Controller, Address, Transport}) ->
#controller{
controller = Controller,
address = Address,
transport = Transport
}.
1 change: 1 addition & 0 deletions bindings/go/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var LISTENER = netip.MustParseAddrPort("192.168.1.100:60001")

var controllers = map[uint32]uhppote.Controller{
405419896: uhppote.Controller{405419896, "192.168.1.100:60000", "tcp"},
303986753: uhppote.Controller{303986753,"192.168.1.100:60000","udp"},
}

type command struct {
Expand Down

0 comments on commit f1530d5

Please sign in to comment.