-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a REST API to be able to configure devices (#8)
* Add API skeleton * Add another route * Remove useless callback * Rename handler * Add configuration parser * Add configuration parser * Fix configuration parser * Fix another bug in configuration parser * Add body decoding * Fix possible errors * Add configuration parser tests * Add a property based test * Add property based tests * Add missing tests * Use internal events instead of message * Feature/update config (#4) * Add update configuration * Add fetch configs * Change the README to reStructuredText * Fix command * Fix formatting * Add configurable synchronization interval * Modify response and validation * Fix tests * Add OTP 22 to CI * Change minimum coverage * Add an encoder * Fix type spec * Add function to iterate over config fields * Add more tests * Fix devices handler * Fix a bug in encoder * Add property based tests for the encoder * Stop breaking config API * Remove trx plugin * Add tests for the encoder * Use a macro for the UUID * Remove unused variable * Raise code coverage * Add appveyor configuration * Add appveyor badge * Fix appveyor * Fix erlang version in appveyor
- Loading branch information
1 parent
9ba211f
commit 53dd07d
Showing
24 changed files
with
1,073 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
language: erlang | ||
otp_release: | ||
- 21.3 | ||
- 22.1 | ||
|
||
script: | ||
- rebar3 check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
============== | ||
mqtt-simulator | ||
============== | ||
|
||
.. image:: https://travis-ci.org/AntoineGagne/mqtt-simulator.svg?branch=master | ||
:target: https://travis-ci.org/AntoineGagne/mqtt-simulator | ||
|
||
.. image:: https://ci.appveyor.com/api/projects/status/glyeekdu4vum33ht/branch/master?svg=true | ||
:target: https://ci.appveyor.com/api/projects/status/glyeekdu4vum33ht/branch/master | ||
|
||
:Author: `Antoine Gagné <[email protected]>`_ | ||
|
||
.. contents:: | ||
:backlinks: none | ||
|
||
.. sectnum:: | ||
|
||
Installation | ||
============ | ||
|
||
Local Build | ||
----------- | ||
|
||
To build the runnable release, you need to have Erlang with OTP 21 and above. | ||
You also need ``rebar3``. Then, you can run the following command: | ||
|
||
.. code-block:: sh | ||
rebar3 as prod release | ||
Docker Image | ||
------------ | ||
|
||
To build this image, you can use the following command: | ||
|
||
.. code-block:: sh | ||
docker build -f Dockerfile -t "${name_of_the_image}" . | ||
Usage | ||
===== | ||
|
||
From Local Build | ||
---------------- | ||
|
||
If you built the release, you can run it with: | ||
|
||
.. code-block:: sh | ||
./_build/prod/rel/mqtt_simulator/bin/mqtt_simulator foreground | ||
Docker | ||
------ | ||
|
||
After building the image, you can run the image by using the following command: | ||
|
||
.. code-block:: sh | ||
docker run \ | ||
--detach \ | ||
--name "${name_of_the_running_container}" \ | ||
--publish "${port_on_host}:${port_of_simulator:-8000}" \ | ||
"${name_of_the_image}" | ||
Development | ||
=========== | ||
|
||
Running all the tests and linters | ||
--------------------------------- | ||
|
||
You can run all the tests and linters with the ``rebar3`` alias: | ||
|
||
.. code-block:: sh | ||
rebar3 check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,10 @@ | |
[kernel, | ||
stdlib, | ||
gproc, | ||
emqttc | ||
emqttc, | ||
cowboy, | ||
jsone, | ||
uuid | ||
]}, | ||
{env,[]}, | ||
{modules, []}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
-module(mqtt_simulator_api). | ||
|
||
-behaviour(gen_server). | ||
|
||
%% API | ||
-export([start_link/0]). | ||
|
||
%% gen_server callbacks | ||
-export([init/1, | ||
handle_call/3, | ||
handle_cast/2, | ||
handle_info/2, | ||
terminate/2]). | ||
|
||
-define(SERVER, ?MODULE). | ||
-define(DEFAULT_PORT, 8000). | ||
|
||
-record(state, {}). | ||
|
||
%%%=================================================================== | ||
%%% API | ||
%%%=================================================================== | ||
|
||
-spec start_link() -> {ok, pid()} | ignore | {error, term()}. | ||
start_link() -> | ||
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). | ||
|
||
%%%=================================================================== | ||
%%% gen_server callbacks | ||
%%%=================================================================== | ||
|
||
init([]) -> | ||
process_flag(trap_exit, true), | ||
Port = application:get_env(mqtt_simulator, api_port, ?DEFAULT_PORT), | ||
Routes = [{"/devices/", mqtt_simulator_devices_handler, []}, | ||
{"/devices/:id", [{id, nonempty}], mqtt_simulator_devices_handler, []}], | ||
Dispatch = cowboy_router:compile([{'_', Routes}]), | ||
{ok, _} = cowboy:start_clear(?SERVER, | ||
[{port, Port}], | ||
#{env => #{dispatch => Dispatch}} | ||
), | ||
{ok, #state{}}. | ||
|
||
handle_call(_Request, _From, State) -> | ||
{reply, ok, State}. | ||
|
||
handle_cast(_Msg, State) -> | ||
{noreply, State}. | ||
|
||
handle_info(_Info, State) -> | ||
{noreply, State}. | ||
|
||
terminate(_Reason, _State) -> | ||
cowboy:stop_listener(?SERVER). | ||
|
||
%%%=================================================================== | ||
%%% Internal functions | ||
%%%=================================================================== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
-module(mqtt_simulator_api_sup). | ||
|
||
-behaviour(supervisor). | ||
|
||
%% API | ||
-export([start_link/0]). | ||
|
||
%% Supervisor callbacks | ||
-export([init/1]). | ||
|
||
-define(SERVER, ?MODULE). | ||
|
||
%%==================================================================== | ||
%% API functions | ||
%%==================================================================== | ||
|
||
-spec start_link() -> | ||
{ok, pid()} | ignore | {error, term()}. | ||
start_link() -> | ||
supervisor:start_link({local, ?SERVER}, ?MODULE, []). | ||
|
||
%%==================================================================== | ||
%% Supervisor callbacks | ||
%%==================================================================== | ||
|
||
init([]) -> | ||
{ok, {#{strategy => one_for_one, | ||
intensity => 5, | ||
period => 10}, | ||
[#{id => mqtt_simulator_api, | ||
start => {mqtt_simulator_api, start_link, []}, | ||
restart => permanent, | ||
shutdown => 5000, | ||
type => worker, | ||
modules => [mqtt_simulator_api]} | ||
]}}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.