<commit_before>%%%-------------------------------------------------------------------
%%% @author <NAME> <<EMAIL>>
%%% @copyright (C) 2013, <NAME>
%%% @doc
%%%
%%% @end
%%% Created : 17 Nov 2013 by <NAME> <<EMAIL>>
%%%-------------------------------------------------------------------
-module(marionet_device_sup2).

-behaviour(supervisor).

%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

-define(SERVER, ?MODULE).

%%%===================================================================
%%% API functions
%%%===================================================================

%%--------------------------------------------------------------------
%% @doc Starts the supervisor
%% @end
%%--------------------------------------------------------------------
-spec start_link() -> {ok, Pid} | ignore | {error, Error} when
      Pid :: pid(),
      Error :: term().
start_link() ->
    supervisor:start_link({local, ?SERVER}, ?MODULE, []).

%%%===================================================================
%%% Supervisor callbacks
%%%===================================================================

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Whenever a supervisor is started using supervisor:start_link/[2,3],
%% this function is called by the new process to find out about
%% restart strategy, maximum restart frequency and child
%% specifications.
%%
%% @spec init(Args) -> {ok, {SupFlags, [ChildSpec]}} |
%%                     ignore |
%%                     {error, Reason}
%% @end
%%--------------------------------------------------------------------
init([]) ->
    RestartStrategy = one_for_one,
    MaxRestarts = 1000,
    MaxSecondsBetweenRestarts = 3600,

    SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts},

    ArduinoEnable = marionet_config:get(arduino_enable),
    OmronFinsEnable = marionet_config:get(omron_fins_enable),
    IOEventHandler = marionet_config:get(io_event_handler),
    SubEventHandler = marionet_config:get(subscribe_event_handler),
    Subscribes = marionet_config:get(subscribes),
    GroupId = marionet_config:get(group_id),
    DeviceId = marionet_config:get(device_id),
    AnalogPubInterval = marionet_config:get(analog_publish_interval),

    Specs = [gpio_sup_spec(),
	     mqtt_spec(),
	     status_spec(),
	     event_sup_spec(emqttc_event, SubEventHandler, [Subscribes]),
	     event_sup_spec(gpio_pin_event, marionet_device_event, []),
	     event_sup_spec(gpio_pin_event, IOEventHandler,
			    [GroupId, DeviceId, AnalogPubInterval])
	    ],

    Specs1 = 
	case ArduinoEnable of
	    true  -> 
		[arduino_sup_spec(),
		 event_sup_spec(arduino_event, marionet_device_event, []),
		 event_sup_spec(arduino_event, IOEventHandler, 
				[GroupId, DeviceId, AnalogPubInterval])
		];
		 false -> 
		     []
	     end,

    Specs2 = 
	case OmronFinsEnable of
	    true  -> 
		[fins_port_spec(),
		 fins_event_spec(),
		 event_sup_spec(omron_fins_event, IOEventHandler,
				[GroupId, DeviceId, AnalogPubInterval]),
		 fins_watcher_spec()
		];
	    false ->
		[]
	end,

    {ok, {SupFlags, Specs ++ Specs1 ++ Specs2}}.

%%%===================================================================
%%% Internal functions
%%%===================================================================

event_sup_spec(EventManager, EventHandler, Args) ->
    Restart = permanent,
    Shutdown = 2000,
    Type = worker,

    Handler = {EventManager, EventHandler, Args},

    {{marionet_event_sup, Handler},
     {marionet_event_sup, start_link, [Handler]},
     Restart, Shutdown, Type, [marionet_event_sup]}.

fins_port_spec() -><commit_msg>add zmq server for receive request from other process.<commit_after>%%%-------------------------------------------------------------------
%%% @author <NAME> <<EMAIL>>
%%% @copyright (C) 2013, <NAME>
%%% @doc
%%%
%%% @end
%%% Created : 17 Nov 2013 by HIROE Shin <<EMAIL>>
%%%-------------------------------------------------------------------
-module(marionet_device_sup2).

-behaviour(supervisor).

%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

-define(SERVER, ?MODULE).

%%%===================================================================
%%% API functions
%%%===================================================================

%%--------------------------------------------------------------------
%% @doc Starts the supervisor
%% @end
%%--------------------------------------------------------------------
-spec start_link() -> {ok, Pid} | ignore | {error, Error} when
      Pid :: pid(),
      Error :: term().
start_link() ->
    supervisor:start_link({local, ?SERVER}, ?MODULE, []).

%%%===================================================================
%%% Supervisor callbacks
%%%===================================================================

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Whenever a supervisor is started using supervisor:start_link/[2,3],
%% this function is called by the new process to find out about
%% restart strategy, maximum restart frequency and child
%% specifications.
%%
%% @spec init(Args) -> {ok, {SupFlags, [ChildSpec]}} |
%%                     ignore |
%%                     {error, Reason}
%% @end
%%--------------------------------------------------------------------
init([]) ->
    RestartStrategy = one_for_one,
    MaxRestarts = 1000,
    MaxSecondsBetweenRestarts = 3600,

    SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts},

    ArduinoEnable = marionet_config:get(arduino_enable),
    OmronFinsEnable = marionet_config:get(omron_fins_enable),
    IOEventHandler = marionet_config:get(io_event_handler),
    SubEventHandler = marionet_config:get(subscribe_event_handler),
    Subscribes = marionet_config:get(subscribes),
    GroupId = marionet_config:get(group_id),
    DeviceId = marionet_config:get(device_id),
    AnalogPubInterval = marionet_config:get(analog_publish_interval),

    Specs = [gpio_sup_spec(),
	     mqtt_spec(),
	     status_spec(),
	     zmq_server_spec(),
	     event_sup_spec(emqttc_event, SubEventHandler, [Subscribes]),
	     event_sup_spec(emqttc_event, marionet_sub_event_handler,
			    [Subscribes]),
	     event_sup_spec(gpio_pin_event, marionet_device_event, []),
	     event_sup_spec(gpio_pin_event, IOEventHandler,
			    [GroupId, DeviceId, AnalogPubInterval])
	    ],

    Specs1 = 
	case ArduinoEnable of
	    true  -> 
		[arduino_sup_spec(),
		 event_sup_spec(arduino_event, marionet_device_event, []),
		 event_sup_spec(arduino_event, IOEventHandler, 
				[GroupId, DeviceId, AnalogPubInterval])
		];
		 false -> 
		     []
	     end,

    Specs2 = 
	case OmronFinsEnable of
	    true  -> 
		[fins_port_spec(),
		 fins_event_spec(),
		 event_sup_spec(omron_fins_event, IOEventHandler,
				[GroupId, DeviceId, AnalogPubInterval]),
		 fins_watcher_spec()
		];
	    false ->
		[]
	end,

    {ok, {SupFlags, Specs ++ Specs1 ++ Specs2}}.

%%%===================================================================
%%% Internal functions
%%%===================================================================

zmq_server_spec() ->
    Restart = permanent,
    Shutdown = 2000,
    Type = worker,

    Config = marionet_config:get(omron_fins),
    IPAddress = proplists:get_value(ip_address, Config),
    Port = proplists:get_value(port, Config, 9600),

    {marionet_zmq_server, {marionet_zmq_server, start_link, [IPAddress, Port]},
     Restart, Shutdown, Type, [marionet_zmq_server]}.

event_sup_spec(EventManager, EventHandler, Args) ->
    Restart = permanent,
    Shutdown = 2000,
    Type = worker,

    Handler = {EventManager, EventHandler, Args},

    {{marionet_event_sup, Handler},
     {marionet_event_sup, start_link, [Handler]},
     Restart, Shutdown, Type, [marionet_event_sup]}.

fins_port_spec() ->
{'fraction_non_alphanumeric': 0.31697730442500316, 'fraction_numerical': 0.008875364523900088, 'mean_word_length': 2.7813998082454456, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6860388', 'n_tokens_mistral': 2420, 'n_tokens_neox': 2197, 'n_words': 418}