Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
IciaCarroBarallobre committed Sep 9, 2024
1 parent a7fa97c commit 49eadf7
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/grisp_connect_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ handle_rpc_messages([{internal_error, _, _} = E | Batch], Replies) ->
handle_rpc_messages(Batch,
[grisp_connect_jsonrpc:format_error(E)| Replies]).

handle_request(<<"post">>, #{type := <<"start_update">>} = Params, ID) ->
URL = maps:get(url, Params, 1),
case start_update(URL) of
% TODO: start_update error cast
{error, update_unavailable} -> {error,
-564095940,
<<"grisp_updater unavailable">>,
undefined,
ID};
{error, Reason} -> {error,
-32603,
iolist_to_binary(io_lib:format("~p", [Reason])), ID};
ok -> {result, undefined, ID}
end;
handle_request(<<"post">>, #{type := <<"flash">>} = Params, ID) ->
Led = maps:get(led, Params, 1),
Color = maps:get(color, Params, red),
Expand All @@ -64,6 +78,21 @@ flash(Led, Color) ->
end),
ok.

start_update(URL) ->
case is_running(grisp_updater) of
true -> grisp_updater:start_update(URL,
grisp_conntect_updater_progress,
#{client => self()}, #{});
false -> {error, update_unavailable}
end.

is_running(AppName) ->
Apps = application:which_applications(),
case [App || {App, _Desc, _VSN} <- Apps, App =:= AppName] of
[] -> false;
[_] -> true
end.

error_atom(-1) -> device_not_linked;
error_atom(-2) -> token_expired;
error_atom(-3) -> device_already_linked;
Expand Down
70 changes: 70 additions & 0 deletions src/grisp_connect_updater_progress.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
-module(grisp_connect_updater_progress).
-behaviour(grisp_updater_progress).

%--- Includes ------------------------------------------------------------------

-include_lib("kernel/include/logger.hrl").


%--- Types ---------------------------------------------------------------------


%--- Exports -------------------------------------------------------------------


% Behaviour grisp_updater_progress callbacks
-export([progress_init/1]).
-export([progress_update/2]).
-export([progress_warning/3]).
-export([progress_error/3]).
-export([progress_done/2]).


%--- records -------------------------------------------------------------------

-record(state, {
client :: pid(),
last_notification :: undefined | integer()
}).


%--- API Functions -------------------------------------------------------------


%--- Behavior grisp_updater_progress Callback ----------------------------------

progress_init(#{client := PID} = _Opts) ->
{ok, #state{
last_notification = erlang:system_time(millisecond),
client = PID
}}.

progress_update(#state{last_notification = LastLog} = State, Stats) ->
case (erlang:system_time(millisecond) - LastLog) > 1000 of
false -> {ok, State};
true ->
% Recheck log level when there is another way to check the progress update
?LOG_NOTICE("Update progress: ~b%", [progress_percent(Stats)]),
{ok, State#state{last_notification = erlang:system_time(millisecond)}}
end.

progress_warning(State, Msg, Reason) ->
?LOG_WARNING("Update warning; ~s: ~p", [Msg, Reason]),
{ok, State}.

progress_error(#state{}, Stats, Reason) ->
?LOG_ERROR("Update failed after ~b% : ~p",
[progress_percent(Stats), Reason]),
ok.

progress_done(#state{}, _Stats) ->
?LOG_NOTICE("Update done", []),
ok.


%--- Internal Functions --------------------------------------------------------

progress_percent(Stats) ->
#{data_total := Total, data_checked := Checked,
data_skipped := Skipped, data_written := Written} = Stats,
(Checked + Skipped + Written) * 100 div (Total * 2).

0 comments on commit 49eadf7

Please sign in to comment.