From f0ae9fb4bfc72105e9f6108d90be3d53505135eb Mon Sep 17 00:00:00 2001 From: Ulf Date: Mon, 6 Oct 2014 06:38:30 +0200 Subject: [PATCH] refactoring --- apps/leni/src/ds18b20_resource.erl | 14 ++++--- docs/rules.txt | 61 ++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 docs/rules.txt diff --git a/apps/leni/src/ds18b20_resource.erl b/apps/leni/src/ds18b20_resource.erl index 6a848b0..1ef412a 100644 --- a/apps/leni/src/ds18b20_resource.erl +++ b/apps/leni/src/ds18b20_resource.erl @@ -232,11 +232,15 @@ to_html(ReqData, Context) -> {Content, ReqData, Context}. get_data(Node, Name) when is_list(Node)-> - case rpc:call(list_to_atom(Node), thing, get_module_config, [Name]) of - {badrpc, Reason} -> lager:error("got error during call ~p thing:get_driver(~p) with reason ~p", [Node, Name, Reason]), - []; - [{data, Data}] -> convert_timestamp_to_date(Data) - end. + D = mnesia_driver:select_entries('erlything@ronja:ds18b20_driver:default'), + D1 = [{date:seconds_to_date(Date), Value}|| {_N, Date, _O, {temp, Value}} <- D]. + +%%get_data(Node, Name) when is_list(Node)-> +%% case rpc:call(list_to_atom(Node), thing, get_module_config, [Name]) of +%% {badrpc, Reason} -> lager:error("got error during call ~p thing:get_driver(~p) with reason ~p", [Node, Name, Reason]), +%% []; +%% [{data, Data}] -> convert_timestamp_to_date(Data) +%% end. convert_timestamp_to_date(List_of_temps) -> lists:foldr(fun({Timestamp, Temp_hum}, Acc) -> [{date:timestamp_to_date(Timestamp), Temp_hum}|Acc] end, [], List_of_temps). diff --git a/docs/rules.txt b/docs/rules.txt new file mode 100644 index 0000000..a9fcf39 --- /dev/null +++ b/docs/rules.txt @@ -0,0 +1,61 @@ +Fail Over: + +When the node 'erlything1@macbook-pro' dies, than the thing 'Database' will be started on the local node. + +fun(Name, Pid, [Payload]) -> + case Payload of + {error,{dead,'erlything1@macbook-pro'}} -> node_config:set_active("Database", true), ok; + {info,{alive,'erlything1@macbook-pro'}} -> node_config:set_active("Database", false), ok; + _Any -> nothing_to_do + end +end. + +fun(Name, Pid, [Payload]) -> + case Payload of + {error,{dead,'erlything1@macbook-pro'}} -> node_config:set_active("Bell", true), ok; + {info,{alive,'erlything1@macbook-pro'}} -> node_config:set_active("Bell", false), ok; + _Any -> nothing_to_do + end +end. + +Messages.config + +{<<"erlything@macbook-pro">>, <<"node_driver">> ,all} + + +Switch on the light when the actual time is between 8:00 and 19:00 o'clock. + +fun(Pid, Name, Body) -> + case Body of + "off" -> ok; + "on" -> case date:is_time_in_range({08,00,00}, {19,00,00}, time()) of + true -> ok; + false -> sensor:send([], Name, {"Licht","11111 2","1"}), + sensor:send_after(Pid, Name, 20000, [], {"Licht","11111 2","0"}) + end; + _ -> ok + end +end. + +The following rule will switch on a light and after 20 seconds, the light will switched off. + +fun(Pid, Name, Body) -> + case Body of + "off" -> ok; + "on" -> sensor:send([], Name, {"Licht","11111 2","1"}), + sensor:send_after(Pid, Name, 20000, [], {"Licht","11111 2","0"}); + _ -> ok + end +end. + +Sends only an on singnal. If a driver registrier to this event, you can play, for example, a sound. + +alarm_fun + +erlything@horst hc_sr501_driver default + +fun(Pid, Name, Body) -> + sensor:send([], Name, "on") +end. + +If a motion is detect from the hc_sr501_driver on horst, an alarm message will be send. \ No newline at end of file