Skip to content

Commit 4f1e821

Browse files
committedJul 7, 2022
leitor-cli - add timeout, add log policy stdout default
1 parent 4193124 commit 4f1e821

File tree

3 files changed

+48
-20
lines changed

3 files changed

+48
-20
lines changed
 

‎app/leitor/leitor-cli.cpp

+13-10
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66

77
using namespace NBR14522;
88

9+
#define TIMEOUT_SEM_RESPOSTA_MS 5000
10+
911
template <typename T, size_t N>
1012
void print_arr_hex(const std::array<T, N>& arr) {
1113
for (const auto i : arr)
1214
printf("%02X", (int)i);
1315
}
1416

15-
// TODO
1617
void print_usage() {
1718
printf(
18-
"Este programa recebe dois argumentos:\n\n"
19-
"1 - porta serial que o medidor está conectado. Em sistemas unix será\n"
19+
"Este programa recebe dois argumentos obrigatórios:\n\n"
20+
"1 - porta serial que o medidor está conectado. Em sistemas unix será\n"
2021
"um pseudo-arquivo localizado em /dev/, por exemplo: /dev/ttyUSB0,\n"
2122
"/dev/ttyUSB1, etc. No Windows: COM1, COM2, etc\n\n"
2223

@@ -31,8 +32,7 @@ void print_usage() {
3132
"./leitor-cli /dev/ttyUSB0 14\n"
3233
"./leitor-cli /dev/ttyUSB0 14123456\n"
3334
"./leitor-cli /dev/ttyUSB0 20\n"
34-
"./leitor-cli /dev/ttyUSB0 204455660101\n\n"
35-
);
35+
"./leitor-cli /dev/ttyUSB0 204455660101\n\n");
3636
}
3737

3838
std::vector<byte_t> get_hex_bytes(const std::string& hexbytes) {
@@ -78,15 +78,18 @@ int main(int argc, char* argv[]) {
7878
}
7979

8080
for (auto& comando : comandos) {
81-
setCRC(comando, CRC16(comando.data(), comando.size()-2));
81+
setCRC(comando, CRC16(comando.data(), comando.size() - 2));
8282
printf("\nComando:\n");
8383
print_arr_hex(comando);
8484

8585
printf("\nResposta(s):\n");
86-
leitor.leitura(comando, [&](const resposta_t& rsp) {
87-
print_arr_hex(rsp);
88-
printf("\n");
89-
});
86+
leitor.leitura(
87+
comando,
88+
[&](const resposta_t& rsp) {
89+
print_arr_hex(rsp);
90+
printf("\n");
91+
},
92+
TIMEOUT_SEM_RESPOSTA_MS);
9093
}
9194

9295
printf("\n");

‎include/leitor.h

+31-6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
#include <functional>
44
#include <leitor_fsm.h>
5+
#include <log_policy.h>
56
#include <serial/serial_policy_win_unix.h>
67
#include <timer/timer_policy_win_unix.h>
78

8-
template <class TimerPolicy, class SerialPolicy> class Leitor {
9+
template <class TimerPolicy, class SerialPolicy,
10+
class LogPolicy = LogPolicyStdout>
11+
class Leitor {
912

1013
using FSM = LeitorFSM<TimerPolicy, SerialPolicy>;
1114

@@ -33,7 +36,9 @@ template <class TimerPolicy, class SerialPolicy> class Leitor {
3336
});
3437

3538
while (true) {
36-
switch (_leitor.processaEstado()) {
39+
typename FSM::estado_t estado = _leitor.processaEstado();
40+
41+
switch (estado) {
3742
case FSM::estado_t::Dessincronizado:
3843
case FSM::estado_t::Sincronizado:
3944
case FSM::estado_t::ComandoTransmitido:
@@ -44,22 +49,42 @@ template <class TimerPolicy, class SerialPolicy> class Leitor {
4449
if (_leitor.status() == FSM::status_t::Sucesso) {
4550
return true;
4651
} else {
47-
printf("%s\n", _status2verbose(_leitor.status()));
52+
LogPolicy::log("Processo falhou. Status: %s\n",
53+
_status2verbose(_leitor.status()));
4854
return false;
4955
}
5056
break;
5157
}
5258

5359
if (timeout_resposta_ms && leituraDeadline.timedOut()) {
54-
printf("O processo de leitura excedeu o tempo informado pelo "
55-
"usuário (%i ms) sem receber nenhuma resposta\n",
56-
timeout_resposta_ms);
60+
LogPolicy::log(
61+
"O processo de leitura excedeu o tempo informado pelo "
62+
"usuário (%i ms) sem receber nenhuma resposta. Estado: "
63+
"%s\n",
64+
timeout_resposta_ms, _estado2string(estado));
5765
return false;
5866
}
5967
}
6068
}
6169

6270
private:
71+
const char* _estado2string(const typename FSM::estado_t estado) {
72+
switch (estado) {
73+
case FSM::estado_t::Dessincronizado:
74+
return "Dessincronizado";
75+
case FSM::estado_t::Sincronizado:
76+
return "Sincronizado";
77+
case FSM::estado_t::ComandoTransmitido:
78+
return "Comando Transmitido";
79+
case FSM::estado_t::AtrasoDeSequenciaRecebido:
80+
return "Atraso de Sequência Recebido";
81+
case FSM::estado_t::CodigoRecebido:
82+
return "Código Recebido";
83+
case FSM::estado_t::AguardaNovoComando:
84+
return "Aguarda Novo Comando";
85+
}
86+
return "";
87+
}
6388
const char* _status2verbose(const typename FSM::status_t status) {
6489
switch (status) {
6590
case FSM::status_t::Sucesso:

‎include/log_policy.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class LogPolicyNull {
88

99
class LogPolicyStdout {
1010
public:
11-
static void log(const std::string& input) { std::cout << input; }
12-
// static void log(...) {
13-
// fprintf(stdout, __VA_ARGS__);
14-
// }
11+
template <typename... Args>
12+
static void log(char const* const format, Args const&... args) noexcept {
13+
printf(format, args...);
14+
}
1515
};

0 commit comments

Comments
 (0)
Please sign in to comment.