Compare commits

..

No commits in common. "18723bdb0d01cca09b1010c94d78226419d0c60f" and "678b8cf9280761af922624adacf273bade1e1298" have entirely different histories.

2 changed files with 13 additions and 21 deletions

View File

@ -48,13 +48,9 @@ start_link(FileName) when is_list(FileName) ->
{stop, Reason :: term()} | ignore).
init([FileName]) ->
ensure_dir(filename:dirname(FileName)),
case file:open(FileName, [append, binary]) of
{ok, IoDevice} ->
{ok, #state{file = IoDevice, file_name = FileName}};
{error, Reason} ->
lager:warning("[modbus_logger] create log file: ~p, error: ~p", [FileName, Reason]),
ignore
end.
{ok, IoDevice} = file:open(FileName, [append, binary]),
{ok, #state{file = IoDevice, file_name = FileName}}.
%% @private
%% @doc Handling call messages

View File

@ -77,8 +77,12 @@ start_link(AST = #ast{}) ->
%% @doc Whenever a gen_statem is started using gen_statem:start/[3,4] or
%% gen_statem:start_link/[3,4], this function is called by the new
%% process to initialize.
init([AST = #ast{modbus = Modbus = #modbus{error_log = ErrorLog, access_log = AccessLog}, devices = Devices}]) ->
init([AST = #ast{modbus = Modbus = #modbus{transport = Transport}, devices = Devices}]) ->
lager:debug("modbus is: ~p", [Modbus]),
Device = hd(Devices),
lager:debug("devices is: ~p", [Device#modbus_device.metrics]),
% Res = connect(Transport),
% lager:debug("connect res: ~p", [Res]),
%%
erlang:start_timer(0, self(), modbus_connect),
@ -92,8 +96,10 @@ init([AST = #ast{modbus = Modbus = #modbus{error_log = ErrorLog, access_log = Ac
lager:debug("device pid: ~p", [DevicesMap]),
{ok, ?DISCONNECTED, #state{ast = AST, access_log_pid = create_log_file(AccessLog), error_log_pid = create_log_file(ErrorLog),
queue = queue:new(), packet_id = 1, devices_map = DevicesMap}}.
%{ok, AccessLogPid} = modbus_logger:start_link(AccessLog),
%{ok, ErrorLogPid} = modbus_logger:start_link(ErrorLog),
{ok, ?DISCONNECTED, #state{ast = AST, queue = queue:new(), packet_id = 1, devices_map = DevicesMap}}.
%% @private
%% @doc This function is called by a gen_statem when it needs to find out
@ -236,14 +242,4 @@ frame_delay(BaudRate, DataBits, Parity, StopBits) when is_integer(BaudRate), is_
erlang:ceil((3500 * CharBits) / BaudRate * 1.2).
retry_connect() ->
erlang:start_timer(5000, self(), modbus_connect).
create_log_file(undefined) ->
undefined;
create_log_file(FileName) ->
case modbus_logger:start_link(FileName) of
{ok, FilePid} ->
FilePid;
_ ->
undefined
end.
erlang:start_timer(5000, self(), modbus_connect).