Skip to content

Commit

Permalink
Rename grisp_io to grsip_connect
Browse files Browse the repository at this point in the history
  • Loading branch information
bchassoul committed Apr 4, 2024
1 parent bb869dc commit 15ac7b0
Show file tree
Hide file tree
Showing 30 changed files with 129 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ct.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: grisp_io CT
name: grisp_connect CT

on:
push:
Expand Down
14 changes: 7 additions & 7 deletions Board_Registration.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ For a hassle-free experience:

If you prefer a hands-on approach or need custom setup:

1. **Project Setup:** Follow the [GRiSP Wiki](https://github.com/grisp/grisp/wiki) for instructions on creating and deploying your GRiSP2 application. Before proceeding, ensure `grisp_io` is added to your project's dependencies. Make sure `grisp_io` is started by adding it under `applications` section in your `myapp.app.src` file; this is a crucial step for manual linking.
1. **Project Setup:** Follow the [GRiSP Wiki](https://github.com/grisp/grisp/wiki) for instructions on creating and deploying your GRiSP2 application. Before proceeding, ensure `grisp_connect` is added to your project's dependencies. Make sure `grisp_connect` is started by adding it under `applications` section in your `myapp.app.src` file; this is a crucial step for manual linking.

- **Optional Configuration:** To streamline the process, include the device_linking_token in your project's release configuration (`config/sys.config`):
```erlang
[
{grisp_io, [
{grisp_connect, [
{device_linking_token, <<"your_token_here">>}
]}
].
Expand All @@ -41,27 +41,27 @@ If you prefer a hands-on approach or need custom setup:

2. **Deploy Your Application:** After setting up, make sure your board is connected to the internet. You can verify that your board connected to GRiSP.io executing:
```erlang
> grisp_io:is_connected().
> grisp_connect:is_connected().
true
```

3. **Manually Link Your Board:** Access the Erlang shell on your board. To link, execute:
```erlang
> grisp_io:link_device(<<"your_unique_token">>).
> grisp_connect:link_device(<<"your_unique_token">>).
```
Or, if your token is pre-configured in the environment, run:
```erlang
> grisp_io:link_device().
> grisp_connect:link_device().
```

4. **Check Registration Status:** A successfull request returns `{ok, <<"ok">>}`. Confirm your device's registration on the [GRiSP Manager](https://grisp.io/grisp-manager/) page. Your device should appear under the **Devices** section. :tada:
### Troubleshooting:
`grisp_io:link_device/*` may fail with the following errors.
`grisp_connect:link_device/*` may fail with the following errors.
#### **Common Errors:**
- `token_expired`: regenerate one from the web page
- `invalid_token`: please double check you typed it correctly
- `token_undefined`: you called `grisp_io:link_device/0` without setting `device_linking_token`
- `token_undefined`: you called `grisp_connect:link_device/0` without setting `device_linking_token`
- `disconnected`: check that your board can connect
- `device_already_linked`: please do not steal GRiSP boards :smirk:
if you need to unlink a GRiSP board see below...
Expand Down
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# grisp_io
# grisp_connect

GRiSP.io Client Library for GRiSP

Add this application as a dependency in your GRiSP2 project.
Your board will connect securely through Mutual TLS to the [GRiSP.io](https://grisp.io) services.
See the [Board Registration](https://github.com/grisp/grisp_io/blob/main/Board_Registration.md) guide on how to start using your GRiSP2 board with GRiSP.io
See the [Board Registration](https://github.com/grisp/grisp_connect/blob/main/Board_Registration.md) guide on how to start using your GRiSP2 board with GRiSP.io

## Application env options

### connect

This option is set to `true` as default. Set it to `false` to prevent automatic connection to GRiSP.io on boot.
In such case the state machine that maintains the connection can be started manually using `grisp_io_connection:connect()`.
In such case the state machine that maintains the connection can be started manually using `grisp_connect_connection:connect()`.

### ntp

Expand All @@ -34,32 +34,32 @@ Accepts an integer that represents the maximum number of logs that can be batche

## API Usage example

ok = grisp_io:connect().
true = grisp_io:is_connected().
{ok, <<pong>>} = grisp_io:ping().
ok = grisp_connect:connect().
true = grisp_connect:is_connected().
{ok, <<pong>>} = grisp_connect:ping().

## See all logs from boot on GRiSP.io

Once this app is started, it forwards all logs to GRiSP.io without the need of setting up anything. The only logs that we do not catch are the ones generated before `grisp_io` boots.
If you want to see ALL logs, even from applications that boot before `grisp_io`, you need to disable the default logger handler and set the grisp_io handler as the default one. This involves changing the `kernel` and `grisp_io` app configuration settings in your sys.config file.
Once this app is started, it forwards all logs to GRiSP.io without the need of setting up anything. The only logs that we do not catch are the ones generated before `grisp_connect` boots.
If you want to see ALL logs, even from applications that boot before `grisp_connect`, you need to disable the default logger handler and set the grisp_connect handler as the default one. This involves changing the `kernel` and `grisp_connect` app configuration settings in your sys.config file.

You can copy paste these settings. Here we both swap the default logger handler with the grisp_io logger handler and also request it to print logs to stdout.
You can copy paste these settings. Here we both swap the default logger handler with the grisp_connect logger handler and also request it to print logs to stdout.
```erlang
% sys.config
[
{kernel, [
% Disable 'default' handler (which buffers all log events in logger).
{logger, [{handler, default, undefined}]}
]},
{grisp_io,[
{grisp_connect,[
{logger, [
% Enable the grisp_io handler as default,
% Enable the grisp_connect handler as default,
% so that it will receive all events from boot
{handler,
default, % name
grisp_io_logger_bin, % module
grisp_connect_logger_bin, % module
#{
formatter => {grisp_io_logger_bin, #{
formatter => {grisp_connect_logger_bin, #{
% To see logs printed on the USB serial appoint a logger
% formatter module of your choice and set the stdout
% configuration stdout => {Formatter, FormatterConfig}
Expand Down
2 changes: 1 addition & 1 deletion config/sys.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[
{grisp_io, [
{grisp_connect, [
{device_linking_token, <<"...">>}
]}
].
2 changes: 1 addition & 1 deletion config/test.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{grisp, [
{emulation, {grisp2, grisp_emulation}}
]},
{grisp_io, [
{grisp_connect, [
{domain, localhost},
{port, 3030}
]}
Expand Down
10 changes: 5 additions & 5 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

{shell, [
{config, "config/sys.config"},
{apps, [grisp_io]}
{apps, [grisp_connect]}
]}.

{plugins, [rebar3_grisp, rebar3_ex_doc]}.
Expand All @@ -26,7 +26,7 @@

{relx, [
{sys_config, "config/sys.config"},
{release, {grisp_io, "0.1.0"}, [grisp_io]}
{release, {grisp_connect, "0.1.0"}, [grisp_connect]}
]}.

{hex, [{doc, #{provider => ex_doc}}]}.
Expand All @@ -39,8 +39,8 @@
{"LICENSE", #{title => "License"}}
]},
{main, "README.md"},
{homepage_url, "https://github.com/grisp/grisp_io"},
{source_url, "https://github.com/grisp/grisp_io"},
{homepage_url, "https://github.com/grisp/grisp_connect"},
{source_url, "https://github.com/grisp/grisp_connect"},
{api_reference, true}
]}.

Expand All @@ -62,7 +62,7 @@
]},
{ct_opts, [{sys_config, "./config/test.config"}]},
{shell, [
{apps, [grisp_emulation, grisp_io]},
{apps, [grisp_emulation, grisp_connect]},
{config, "config/test.config"}
]}
]}
Expand Down
10 changes: 5 additions & 5 deletions src/grisp_io.app.src → src/grisp_connect.app.src
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{application, grisp_io, [
{application, grisp_connect, [
{description, "GRiSP.io Client Library for GRiSP"},
{vsn, "0.1.0"},
{registered, []},
{mod, {grisp_io_app, []}},
{mod, {grisp_connect_app, []}},
{applications, [
kernel,
inets,
Expand All @@ -28,9 +28,9 @@
% Enable our own default handler,
% which will receive all events from boot
{handler,
grisp_io_log_handler,
grisp_io_logger_bin,
#{formatter => {grisp_io_logger_bin, #{}}}}
grisp_connect_log_handler,
grisp_connect_logger_bin,
#{formatter => {grisp_connect_logger_bin, #{}}}}
]}
]},
{modules, []},
Expand Down
12 changes: 6 additions & 6 deletions src/grisp_io.erl → src/grisp_connect.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
%% @end
%%%-----------------------------------------------------------------------------

-module(grisp_io).
-module(grisp_connect).

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

Expand All @@ -19,24 +19,24 @@
% @doc Connect to GRiSP.io.
-spec connect() -> ok.
connect() ->
grisp_io_client:connect().
grisp_connect_client:connect().

% @doc Check if board is connected to GRiSP.io.
-spec is_connected() -> true | false.
is_connected() ->
grisp_io_client:is_connected().
grisp_connect_client:is_connected().

% @doc Ping GRiSP.io.
% Returns 'pong' if the board is linked to an account, 'pang' otherwise.
-spec ping() -> {ok, binary()} | {error, atom()}.
ping() ->
grisp_io_client:request(post, ping, #{}).
grisp_connect_client:request(post, ping, #{}).

% @doc Links the board to a GRiSP.io account.
% The token is took from the device_linking_token app env.
-spec link_device() -> {ok, binary()} | {error, token_undefined | invalid_token}.
link_device() ->
case application:get_env(grisp_io, device_linking_token) of
case application:get_env(grisp_connect, device_linking_token) of
undefined -> {error, token_undefined};
{ok, Token} -> link_device(Token)
end.
Expand All @@ -46,4 +46,4 @@ link_device() ->
-spec link_device(Token :: binary()) ->
{ok, binary()} | {error, invalid_token}.
link_device(Token) ->
grisp_io_client:request(post, device_linking_token, #{token => Token}).
grisp_connect_client:request(post, device_linking_token, #{token => Token}).
10 changes: 5 additions & 5 deletions src/grisp_io_api.erl → src/grisp_connect_api.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%% @doc Librabry module containing the jsonrpc API logic
-module(grisp_io_api).
-module(grisp_connect_api).

-export([request/3]).
-export([handle_msg/1]).
Expand All @@ -11,11 +11,11 @@
request(Method, Type, Params) ->
ID = id(),
Rpc = {request, Method, maps:put(type, Type, Params), ID},
Encoded = grisp_io_jsonrpc:encode(Rpc),
Encoded = grisp_connect_jsonrpc:encode(Rpc),
{ID, Encoded}.

handle_msg(JSON) ->
JSON_RPC = grisp_io_jsonrpc:decode(JSON),
JSON_RPC = grisp_connect_jsonrpc:decode(JSON),
handle_jsonrpc(JSON_RPC).

%--- Internal Funcitons --------------------------------------------------------
Expand All @@ -37,14 +37,14 @@ handle_rpc_messages([{error, _Code, _Msg, _Data, _ID} = E | Batch], Replies) ->
handle_rpc_messages([{internal_error, _, _} = E | Batch], Replies) ->
?LOG_ERROR("JsonRPC: ~p",[E]),
handle_rpc_messages(Batch,
[grisp_io_jsonrpc:format_error(E)| Replies]).
[grisp_connect_jsonrpc:format_error(E)| Replies]).

handle_request(<<"post">>, #{type := <<"flash">>} = Params, ID) ->
Led = maps:get(led, Params, 1),
Color = maps:get(color, Params, red),
{result, flash(Led, Color), ID};
handle_request(_, _, ID) ->
grisp_io_jsonrpc:format_error({internal_error, method_not_found, ID}).
grisp_connect_jsonrpc:format_error({internal_error, method_not_found, ID}).

handle_response(Response) ->
{ID, Reply} = case Response of
Expand Down
8 changes: 4 additions & 4 deletions src/grisp_io_app.erl → src/grisp_connect_app.erl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
%%%-------------------------------------------------------------------
%% @doc grisp_io public API
%% @doc grisp_connect public API
%% @end
%%%-------------------------------------------------------------------

-module(grisp_io_app).
-module(grisp_connect_app).

-behaviour(application).

Expand All @@ -17,8 +17,8 @@
%--- Behaviour application Callback Functions ----------------------------------

start(_StartType, _StartArgs) ->
logger:add_handlers(grisp_io),
grisp_io_sup:start_link().
logger:add_handlers(grisp_connect),
grisp_connect_sup:start_link().

stop(_State) ->
ok.
2 changes: 1 addition & 1 deletion src/grisp_io_binlog.erl → src/grisp_connect_binlog.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
%% It can be truncated once a batch of logs has been sent out
%% and is not needed anymore.
%% @end
-module(grisp_io_binlog).
-module(grisp_connect_binlog).

% API
-export([defaults/1]).
Expand Down
26 changes: 13 additions & 13 deletions src/grisp_io_client.erl → src/grisp_connect_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
%% This module contains a state machine to ensure connectivity with grisp.io.
%% JsonRPC traffic is managed here.
%% @end
-module(grisp_io_client).
-module(grisp_connect_client).

% External API
-export([start_link/0]).
Expand Down Expand Up @@ -49,7 +49,7 @@ handle_message(Payload) ->
% gen_statem CALLBACKS ---------------------------------------------------------

init([]) ->
{ok, Connect} = application:get_env(grisp_io, connect),
{ok, Connect} = application:get_env(grisp_connect, connect),
NextState = case Connect of
true -> waiting_ip;
false -> idle
Expand Down Expand Up @@ -78,22 +78,22 @@ when State =/= connected ->
{keep_state_and_data, [{reply, From, {error, disconnected}}]};
handle_event({call, From}, {request, Method, Type, Params}, connected,
#data{requests = Requests} = Data) ->
{ID, Payload} = grisp_io_api:request(Method, Type, Params),
grisp_io_ws:send(Payload),
{ID, Payload} = grisp_connect_api:request(Method, Type, Params),
grisp_connect_ws:send(Payload),
NewRequests = Requests#{ID => From},
{keep_state,
Data#data{requests = NewRequests},
[{{timeout, ID}, request_timeout(), request}]};

handle_event(cast, {handle_message, Payload}, connected,
#data{requests = Requests} = Data) ->
Replies = grisp_io_api:handle_msg(Payload),
Replies = grisp_connect_api:handle_msg(Payload),
% A reduce operation is needed to support jsonrpc batch comunications
case Replies of
[] ->
keep_state_and_data;
[{request, Response}] -> % Response for a GRiSP.io request
grisp_io_ws:send(Response),
grisp_connect_ws:send(Response),
keep_state_and_data;
[{response, ID, Response}] -> % GRiSP.io response
{OtherRequests, Actions} = dispatch_response(ID, Response, Requests),
Expand Down Expand Up @@ -133,13 +133,13 @@ handle_event(state_timeout, retry, waiting_ip, Data) ->

% CONNECTING
handle_event(enter, _OldState, connecting, _Data) ->
{ok, Domain} = application:get_env(grisp_io, domain),
{ok, Port} = application:get_env(grisp_io, port),
{ok, Domain} = application:get_env(grisp_connect, domain),
{ok, Port} = application:get_env(grisp_connect, port),
?LOG_NOTICE(#{event => connecting, domain => Domain, port => Port}),
grisp_io_ws:connect(Domain, Port),
grisp_connect_ws:connect(Domain, Port),
{keep_state_and_data, [{state_timeout, 0, retry}]};
handle_event(state_timeout, retry, connecting, Data) ->
case grisp_io_ws:is_connected() of
case grisp_connect_ws:is_connected() of
true ->
?LOG_NOTICE(#{event => connected}),
{next_state, connected, Data};
Expand All @@ -150,11 +150,11 @@ handle_event(state_timeout, retry, connecting, Data) ->

% CONNECTED
handle_event(enter, _OldState, connected, _Data) ->
grisp_io_log_server:start(),
grisp_connect_log_server:start(),
keep_state_and_data;
handle_event(cast, disconnected, connected, Data) ->
?LOG_WARNING(#{event => disconnected}),
grisp_io_log_server:stop(),
grisp_connect_log_server:stop(),
{next_state, waiting_ip, Data};

handle_event(E, OldS, NewS, Data) ->
Expand All @@ -178,7 +178,7 @@ dispatch_response(ID, Response, Requests) ->
end.

request_timeout() ->
{ok, V} = application:get_env(grisp_io, ws_requests_timeout),
{ok, V} = application:get_env(grisp_connect, ws_requests_timeout),
V.

% IP check functions
Expand Down
Loading

0 comments on commit 15ac7b0

Please sign in to comment.