~umgeher/utos-usr-local-hcs-exam-ecg

ref: 67b7a6f4e83990d7d5591d5cd8c3f01486a4efa4 utos-usr-local-hcs-exam-ecg/src/utos_usr_local_hcs_exam_ecg_corelead_etu_process.erl -rw-r--r-- 2.5 KiB
67b7a6f4Umgeher Torgersen qt-ai upgraded to 0.2.0 1 year, 27 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
-module(utos_usr_local_hcs_exam_ecg_corelead_etu_process).
-behaviour(gen_server).

-include_lib("utos_usr_local_tsk_include/src/utos_usr_local_tsk_include.hrl").

-export([start_link/3]).

-export(
   [
    code_change/3,
    handle_call/3,
    handle_cast/2,
    handle_info/2,
    init/1,
    terminate/2
   ]
  ).

-record(state, {
                buffer = [],
                from = null,
                now = 0,
                time = null,
                to = null
               }).

-define(OP, <<"utos">>).
-define(TSTEP, 3600000000000).

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

handle_call(_, _, State) ->
    {reply, {error, request}, State}.

handle_cast({{csv, stream}, [<<"result">> | _]}, S) ->
    {noreply, S};
handle_cast({{csv, stream}, [_, <<DT/binary>>, _, <<Label/binary>>]}, S) ->
    {ok, TS} = utos_datetime:rfc3339_to_ns(DT),
    io:format("TS -> ~p~n", [TS]),
    utos_actor:cast(self(), {ts, now, TS}),
    {noreply, S#state{buffer = S#state.buffer ++ [TS, Label]}};
handle_cast({{csv, stream}, stop}, S) ->
    utos_actor:cast(self(), push),
    {noreply, S};
handle_cast({{csv, stream}, M}, S) ->
    io:format("M -> ~p~n", [M]),
    {noreply, S};
handle_cast(init, #state{time = {T0, T1}} = S) when is_integer(T0), is_integer(T1) ->
    utos_actor:cast(self(), scan),
    {noreply, S#state{now = T0}};
handle_cast(push, S) ->
    {noreply, S};
handle_cast(scan, S) ->
    {ok, TS} = utos_datetime:ns_to_rfc3339(S#state.now),
    C = #{
          <<"bucket">> => <<"ecg">>,
          <<"corelead">> => S#state.from,
          <<"t0">> => TS
         },
    case catch utos_usr_local_hcs_exam_ecg_influx:flux_exec(?OP, <<"ecg-corelead-get">>, C) of
        {ok, {200, <<"\r\n", _/binary>>}} ->
            {noreply, S};
        {ok, {200, <<R/binary>>}} ->
            utos_usr_share_csv:stream(self(), R),
            {noreply, S};
        E ->
            io:format("Error -> ~p~n", [E]),
            {noreply, S}
    end;
handle_cast({ts, now, TS}, #state{now = Now} = S) when TS > Now ->
    {noreply, S#state{now = TS}};
handle_cast({ts, now, _}, S) ->
    {noreply, S};
handle_cast(M, S) ->
    io:format("~p~p missing msg: ~p~n", [?MODULE, self(), M]),
    {noreply, S}.

handle_info(_Info, State) ->
    {noreply, State}.

init({From, To, {T0, T1}}) ->
    process_flag(trap_exit, true),
    utos_actor:cast(self(), init),
    {ok, #state{from = From, to = To, time = {T0, T1}}}.

start_link(From, To, {T0, T1}) ->
    gen_server:start(?MODULE, {From, To, {T0, T1}}, []).
    
terminate(_Reason, _State) ->
    ok.