-
Notifications
You must be signed in to change notification settings - Fork 4
How to create a thing
We will create a thing which sends mails if it gets a message from a specific driver from a specific node with a specific body. All other messages, which will come this node will be logged with a warning.
Before we can send the email, we have to initialize the driver, because we have to start an external application. This will be done in the init function.
Note: For sending the emails we will use gen_smtpc which is a simple mail client.
Ok, let's start
- Create a new module in the app/horst/src directory.
-module(mail_client_driver).
-export([init/1,handle_msg/3]).
init(Config) ->
lager:info("mail_client_driver:init('~p')", [Config]),
application:start(gen_smtpc).
handle_msg([Node ,Sensor, Id, Time, Body], Config, Module_config) ->
Config.- Edit the things.config and add the new driver
The things.config is the configuration file where you can enable/disable and configure your things.
{thing, "Mail_Client",
[
{type, actor},
{driver, {mail_client_driver, handle_msg}, [{init, true, [{options, [{use_ssl, true}, {host, "smtp.gmail.com"}, {port, 465}]}]}]},
{activ, true},
{timer, 0},
{database, []},
{description, "Mail Client for sending notification."}
]}.The new thing is named "Mail_Client". The name must be unique in your enviroment. The thing is from type actor. The driver tag describes the your thing. You can read like this: The driver is implemented in the module mail_client_driver and entry function is handle_msg. Note: it is implicit that you have 3 arguments. The arguments are: Message, Config, Module-Config.
Here, the module config contains the following elements :
- init true means that we need a init function inside our driver and that it will be called during init phase.
- options [list of parameters] These option are passed to gen_smtpc application when we send an email.
The timer is not used The database is not used The description contains only text which describes the thing.