Compare commits
2 Commits
678b8cf928
...
18723bdb0d
| Author | SHA1 | Date | |
|---|---|---|---|
| 18723bdb0d | |||
| d9d8e6b53c |
@ -48,9 +48,13 @@ start_link(FileName) when is_list(FileName) ->
|
|||||||
{stop, Reason :: term()} | ignore).
|
{stop, Reason :: term()} | ignore).
|
||||||
init([FileName]) ->
|
init([FileName]) ->
|
||||||
ensure_dir(filename:dirname(FileName)),
|
ensure_dir(filename:dirname(FileName)),
|
||||||
{ok, IoDevice} = file:open(FileName, [append, binary]),
|
case file:open(FileName, [append, binary]) of
|
||||||
|
{ok, IoDevice} ->
|
||||||
{ok, #state{file = IoDevice, file_name = FileName}}.
|
{ok, #state{file = IoDevice, file_name = FileName}};
|
||||||
|
{error, Reason} ->
|
||||||
|
lager:warning("[modbus_logger] create log file: ~p, error: ~p", [FileName, Reason]),
|
||||||
|
ignore
|
||||||
|
end.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
%% @doc Handling call messages
|
%% @doc Handling call messages
|
||||||
|
|||||||
@ -77,12 +77,8 @@ start_link(AST = #ast{}) ->
|
|||||||
%% @doc Whenever a gen_statem is started using gen_statem:start/[3,4] or
|
%% @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
|
%% gen_statem:start_link/[3,4], this function is called by the new
|
||||||
%% process to initialize.
|
%% process to initialize.
|
||||||
init([AST = #ast{modbus = Modbus = #modbus{transport = Transport}, devices = Devices}]) ->
|
init([AST = #ast{modbus = Modbus = #modbus{error_log = ErrorLog, access_log = AccessLog}, devices = Devices}]) ->
|
||||||
lager:debug("modbus is: ~p", [Modbus]),
|
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),
|
erlang:start_timer(0, self(), modbus_connect),
|
||||||
@ -96,10 +92,8 @@ init([AST = #ast{modbus = Modbus = #modbus{transport = Transport}, devices = Dev
|
|||||||
|
|
||||||
lager:debug("device pid: ~p", [DevicesMap]),
|
lager:debug("device pid: ~p", [DevicesMap]),
|
||||||
|
|
||||||
%{ok, AccessLogPid} = modbus_logger:start_link(AccessLog),
|
{ok, ?DISCONNECTED, #state{ast = AST, access_log_pid = create_log_file(AccessLog), error_log_pid = create_log_file(ErrorLog),
|
||||||
%{ok, ErrorLogPid} = modbus_logger:start_link(ErrorLog),
|
queue = queue:new(), packet_id = 1, devices_map = DevicesMap}}.
|
||||||
|
|
||||||
{ok, ?DISCONNECTED, #state{ast = AST, queue = queue:new(), packet_id = 1, devices_map = DevicesMap}}.
|
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
%% @doc This function is called by a gen_statem when it needs to find out
|
%% @doc This function is called by a gen_statem when it needs to find out
|
||||||
@ -243,3 +237,13 @@ frame_delay(BaudRate, DataBits, Parity, StopBits) when is_integer(BaudRate), is_
|
|||||||
|
|
||||||
retry_connect() ->
|
retry_connect() ->
|
||||||
erlang:start_timer(5000, self(), modbus_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.
|
||||||
Loading…
x
Reference in New Issue
Block a user