Skip to content

Commit

Permalink
new enum type for OutputFormat, add level/pollprio/condition to HTTP …
Browse files Browse the repository at this point in the history
…GET and extend message key by circuit/direction/condition for OF_ALL_ATTRS, start message dump in JSON format
  • Loading branch information
john30 committed Apr 2, 2021
1 parent dc1a1ec commit 38bfc7a
Show file tree
Hide file tree
Showing 13 changed files with 264 additions and 200 deletions.
14 changes: 7 additions & 7 deletions src/ebusd/bushandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ bool ScanRequest::notify(result_t result, const SlaveSymbolString& slave) {
}
if (result == RESULT_OK) {
ostringstream output;
result = m_message->decodeLastData(true, nullptr, -1, 0, &output); // decode data
result = m_message->decodeLastData(true, nullptr, -1, OF_NONE, &output); // decode data
string str = output.str();
m_busHandler->setScanResult(dstAddress, m_notifyIndex+m_index, str);
}
Expand Down Expand Up @@ -216,7 +216,7 @@ bool decodeType(const DataType* type, const SymbolString& input, size_t length,
string in = input.getStr(input.getDataOffset());
for (size_t offset = 0; offset <= offsets; offset++) {
ostringstream out;
result_t result = type->readSymbols(offset, length, input, 0, &out);
result_t result = type->readSymbols(offset, length, input, OF_NONE, &out);
if (result != RESULT_OK) {
continue;
}
Expand All @@ -231,7 +231,7 @@ bool decodeType(const DataType* type, const SymbolString& input, size_t length,
first = false;
*output << endl << " ";
ostringstream::pos_type cnt = output->tellp();
type->dump(false, length, false, output);
type->dump(OF_NONE, length, false, output);
cnt = output->tellp() - cnt;
while (cnt < 5) {
*output << " ";
Expand Down Expand Up @@ -1156,7 +1156,7 @@ void BusHandler::messageCompleted() {
result = message->storeLastData(0, idData);
if (result == RESULT_OK) {
ostringstream output;
result = message->decodeLastData(true, nullptr, -1, 0, &output);
result = message->decodeLastData(true, nullptr, -1, OF_NONE, &output);
if (result == RESULT_OK) {
string str = output.str();
setScanResult(slaveAddress, 0, str);
Expand All @@ -1176,7 +1176,7 @@ void BusHandler::messageCompleted() {
result_t result = message->storeLastData(m_command, m_response);
if (result == RESULT_OK) {
ostringstream output;
result = message->decodeLastData(true, nullptr, -1, 0, &output);
result = message->decodeLastData(true, nullptr, -1, OF_NONE, &output);
if (result == RESULT_OK) {
string str = output.str();
setScanResult(dstAddress, 0, str);
Expand Down Expand Up @@ -1216,7 +1216,7 @@ void BusHandler::messageCompleted() {
result_t result = message->storeLastData(m_command, m_response);
ostringstream output;
if (result == RESULT_OK) {
result = message->decodeLastData(false, nullptr, -1, 0, &output);
result = message->decodeLastData(false, nullptr, -1, OF_NONE, &output);
}
if (result < RESULT_OK) {
logError(lf_update, "unable to parse %s %s %s from %s / %s: %s", mode, circuit.c_str(), name.c_str(),
Expand Down Expand Up @@ -1380,7 +1380,7 @@ void BusHandler::formatScanResult(ostringstream* output) const {
*output << endl;
}
*output << hex << setw(2) << setfill('0') << static_cast<unsigned>(slave);
message->decodeLastData(true, nullptr, -1, 0, output);
message->decodeLastData(true, nullptr, -1, OF_NONE, output);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/ebusd/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ result_t loadScanConfigFile(MessageMap* messages, symbol_t address, bool verbose
size_t offset = 0;
size_t field = 0;
bool fromLocal = s_configUriPrefix.empty();
result_t result = (*identFields)[field]->read(data, offset, false, nullptr, -1, 0, -1, &out); // manufacturer name
result_t result = (*identFields)[field]->read(data, offset, false, nullptr, -1, OF_NONE, -1, &out); // manufacturer name
if (result == RESULT_ERR_NOTFOUND && fromLocal) {
result = (*identFields)[field]->read(data, offset, false, nullptr, -1, OF_NUMERIC, -1, &out); // manufacturer name
}
Expand All @@ -1042,7 +1042,7 @@ result_t loadScanConfigFile(MessageMap* messages, symbol_t address, bool verbose
out.str("");
out.clear();
offset += (*identFields)[field++]->getLength(pt_slaveData, MAX_LEN);
result = (*identFields)[field]->read(data, offset, false, nullptr, -1, 0, -1, &out); // identification string
result = (*identFields)[field]->read(data, offset, false, nullptr, -1, OF_NONE, -1, &out); // identification string
}
if (result == RESULT_OK) {
ident = out.str();
Expand Down Expand Up @@ -1307,7 +1307,7 @@ int main(int argc, char* argv[]) {
}
if (result == RESULT_OK && opt.dumpConfig) {
logNotice(lf_main, "configuration dump:");
s_messageMap->dump(true, &cout);
s_messageMap->dump(true, OF_NONE, &cout);
}

shutdown(overallResult != RESULT_OK);
Expand Down
119 changes: 56 additions & 63 deletions src/ebusd/mainloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ result_t UserList::addFromFile(const string& filename, unsigned int lineNo, map<
}


#define VERBOSITY_0 OF_NONE
#define VERBOSITY_1 OF_NAMES
#define VERBOSITY_2 (VERBOSITY_1 | OF_UNITS)
#define VERBOSITY_3 (VERBOSITY_2 | OF_COMMENTS)
#define VERBOSITY_4 (VERBOSITY_3 | OF_ALL_ATTRS)


MainLoop::MainLoop(const struct options& opt, Device *device, MessageMap* messages)
: Thread(), m_device(device), m_reconnectCount(0), m_userList(opt.accessLevel), m_messages(messages),
m_address(opt.address), m_scanConfig(opt.scanConfig), m_initialScan(opt.readOnly ? ESC : opt.initialScan),
Expand Down Expand Up @@ -702,7 +709,7 @@ result_t MainLoop::executeAuth(const vector<string>& args, string* user, ostring
result_t MainLoop::executeRead(const vector<string>& args, const string& levels, ostringstream* ostream) {
size_t argPos = 1;
bool hex = false, newDefinition = false;
OutputFormat verbosity = 0;
OutputFormat verbosity = OF_NONE;
time_t maxAge = 5*60;
string circuit, params;
symbol_t srcAddress = SYN, dstAddress = SYN;
Expand Down Expand Up @@ -732,21 +739,17 @@ result_t MainLoop::executeRead(const vector<string>& args, const string& levels,
break;
}
} else if (args[argPos] == "-v") {
switch (verbosity) {
case 0:
verbosity = OF_NAMES;
break;
case OF_NAMES:
verbosity |= OF_UNITS;
break;
case OF_NAMES|OF_UNITS:
verbosity |= OF_COMMENTS;
break;
if ((verbosity & VERBOSITY_3) == VERBOSITY_0) {
verbosity |= VERBOSITY_1;
} else if ((verbosity & VERBOSITY_3) == VERBOSITY_1) {
verbosity |= VERBOSITY_2;
} else {
verbosity |= VERBOSITY_3;
}
} else if (args[argPos] == "-vv") {
verbosity |= OF_NAMES|OF_UNITS;
verbosity |= VERBOSITY_2;
} else if (args[argPos] == "-vvv" || args[argPos] == "-V") {
verbosity |= OF_NAMES|OF_UNITS|OF_COMMENTS;
verbosity |= VERBOSITY_3;
} else if (args[argPos] == "-n") {
verbosity = (verbosity & ~OF_VALUENAME) | OF_NUMERIC;
} else if (args[argPos] == "-N") {
Expand Down Expand Up @@ -799,7 +802,7 @@ result_t MainLoop::executeRead(const vector<string>& args, const string& levels,
}
argPos++;
}
if ((hex && (newDefinition || verbosity != 0 || !circuit.empty() || !params.empty() || dstAddress != SYN
if ((hex && (newDefinition || verbosity != OF_NONE || !circuit.empty() || !params.empty() || dstAddress != SYN
|| pollPriority > 0 || args.size() < argPos + 1))
|| (newDefinition && (hex || !circuit.empty() || pollPriority > 0 || args.size() != argPos + 1))) {
argPos = 0; // print usage
Expand Down Expand Up @@ -882,7 +885,7 @@ result_t MainLoop::executeRead(const vector<string>& args, const string& levels,
ret = message->storeLastData(master, slave);
ostringstream result;
if (ret == RESULT_OK) {
ret = message->decodeLastData(false, nullptr, -1, 0, &result);
ret = message->decodeLastData(false, nullptr, -1, OF_NONE, &result);
}
if (ret >= RESULT_OK) {
logInfo(lf_main, "read hex %s %s cache update: %s", message->getCircuit().c_str(), message->getName().c_str(),
Expand Down Expand Up @@ -1107,7 +1110,7 @@ result_t MainLoop::executeWrite(const vector<string>& args, const string levels,
ret = message->storeLastData(master, slave);
ostringstream result;
if (ret == RESULT_OK) {
ret = message->decodeLastData(false, nullptr, -1, 0, &result);
ret = message->decodeLastData(false, nullptr, -1, OF_NONE, &result);
}
if (ret >= RESULT_OK) {
logInfo(lf_main, "write hex %s %s cache update: %s", message->getCircuit().c_str(),
Expand Down Expand Up @@ -1178,7 +1181,7 @@ result_t MainLoop::executeWrite(const vector<string>& args, const string levels,
return RESULT_OK;
}

ret = message->decodeLastData(false, false, nullptr, -1, 0, ostream); // decode data
ret = message->decodeLastData(false, false, nullptr, -1, OF_NONE, ostream); // decode data
if (ret >= RESULT_OK && ostream->str().empty()) {
logNotice(lf_main, "write %s %s: decode %s", message->getCircuit().c_str(), message->getName().c_str(),
getResultCode(ret));
Expand Down Expand Up @@ -1325,29 +1328,27 @@ result_t MainLoop::executeFind(const vector<string>& args, const string& levels,
bool configFormat = false, exact = false, withRead = true, withWrite = false, withPassive = true, first = true,
onlyWithData = false, hexFormat = false, userLevel = true, withConditions = false;
string useLevels = levels;
OutputFormat verbosity = 0;
OutputFormat verbosity = OF_NONE;
vector<string> fieldNames;
string circuit;
vector<symbol_t> id;
while (args.size() > argPos && args[argPos][0] == '-') {
if (args[argPos] == "-v") {
switch (verbosity) {
case 0:
verbosity |= OF_NAMES;
break;
case OF_NAMES:
verbosity |= OF_UNITS;
break;
case OF_NAMES|OF_UNITS:
verbosity |= OF_COMMENTS;
break;
if ((verbosity & VERBOSITY_4) == VERBOSITY_0) {
verbosity |= VERBOSITY_1;
} else if ((verbosity & VERBOSITY_4) == VERBOSITY_1) {
verbosity |= VERBOSITY_2;
} else if ((verbosity & VERBOSITY_4) == VERBOSITY_2) {
verbosity |= VERBOSITY_3;
} else {
verbosity |= VERBOSITY_4;
}
} else if (args[argPos] == "-vv") {
verbosity |= OF_NAMES|OF_UNITS;
verbosity |= VERBOSITY_2;
} else if (args[argPos] == "-vvv") {
verbosity |= OF_NAMES|OF_UNITS|OF_COMMENTS;
} else if (args[argPos] == "-V") {
verbosity |= OF_NAMES|OF_UNITS|OF_COMMENTS|OF_ALL_ATTRS;
verbosity |= VERBOSITY_3;
} else if (args[argPos] == "-vvvv" || args[argPos] == "-V") {
verbosity |= VERBOSITY_4;
} else if (args[argPos] == "-f") {
configFormat = true;
if (hexFormat) {
Expand Down Expand Up @@ -1470,12 +1471,12 @@ result_t MainLoop::executeFind(const vector<string>& args, const string& levels,
if (found) {
*ostream << endl;
}
message->dump(nullptr, withConditions, ostream);
message->dump(nullptr, withConditions, verbosity, ostream);
} else if (!fieldNames.empty()) {
if (found) {
*ostream << endl;
}
message->dump(&fieldNames, withConditions, ostream);
message->dump(&fieldNames, withConditions, verbosity, ostream);
} else {
if (found) {
*ostream << endl;
Expand All @@ -1496,7 +1497,7 @@ result_t MainLoop::executeFind(const vector<string>& args, const string& levels,
<< " / " << message->getLastSlaveData().getStr() << ")";
}
}
if ((verbosity & (OF_NAMES|OF_UNITS|OF_COMMENTS)) == (OF_NAMES|OF_UNITS|OF_COMMENTS)) {
if ((verbosity & VERBOSITY_3) == VERBOSITY_3) {
symbol_t dstAddress = message->getDstAddress();
if (dstAddress != SYN) {
snprintf(str, sizeof(str), "%02x", dstAddress);
Expand Down Expand Up @@ -1536,26 +1537,22 @@ result_t MainLoop::executeFind(const vector<string>& args, const string& levels,

result_t MainLoop::executeListen(const vector<string>& args, ClientSettings* settings, ostringstream* ostream) {
size_t argPos = 1;
OutputFormat verbosity = 0;
OutputFormat verbosity = OF_NONE;
bool listenWithUnknown = false;
bool listenOnlyUnknown = false;
while (args.size() > argPos && args[argPos][0] == '-') {
if (args[argPos] == "-v") {
switch (verbosity) {
case 0:
verbosity = OF_NAMES;
break;
case OF_NAMES:
verbosity |= OF_UNITS;
break;
case OF_NAMES|OF_UNITS:
verbosity |= OF_COMMENTS;
break;
if ((verbosity & VERBOSITY_3) == VERBOSITY_0) {
verbosity |= VERBOSITY_1;
} else if ((verbosity & VERBOSITY_3) == VERBOSITY_1) {
verbosity |= VERBOSITY_2;
} else {
verbosity |= VERBOSITY_3;
}
} else if (args[argPos] == "-vv") {
verbosity |= OF_NAMES|OF_UNITS;
verbosity |= VERBOSITY_2;
} else if (args[argPos] == "-vvv" || args[argPos] == "-V") {
verbosity |= OF_NAMES|OF_UNITS|OF_COMMENTS;
verbosity |= VERBOSITY_3;
} else if (args[argPos] == "-n") {
verbosity = (verbosity & ~OF_VALUENAME) | OF_NUMERIC;
} else if (args[argPos] == "-N") {
Expand Down Expand Up @@ -1686,24 +1683,20 @@ result_t MainLoop::executeDefine(const vector<string>& args, ostringstream* ostr

result_t MainLoop::executeDecode(const vector<string>& args, ostringstream* ostream) {
size_t argPos = 1;
OutputFormat verbosity = 0;
OutputFormat verbosity = OF_NONE;
while (args.size() > argPos && args[argPos][0] == '-') {
if (args[argPos] == "-v") {
switch (verbosity) {
case 0:
verbosity = OF_NAMES;
break;
case OF_NAMES:
verbosity |= OF_UNITS;
break;
case OF_NAMES|OF_UNITS:
verbosity |= OF_COMMENTS;
break;
if ((verbosity & VERBOSITY_3) == VERBOSITY_0) {
verbosity |= VERBOSITY_1;
} else if ((verbosity & VERBOSITY_3) == VERBOSITY_1) {
verbosity |= VERBOSITY_2;
} else {
verbosity |= VERBOSITY_3;
}
} else if (args[argPos] == "-vv") {
verbosity |= OF_NAMES|OF_UNITS;
verbosity |= VERBOSITY_2;
} else if (args[argPos] == "-vvv" || args[argPos] == "-V") {
verbosity |= OF_NAMES|OF_UNITS|OF_COMMENTS;
verbosity |= VERBOSITY_3;
} else if (args[argPos] == "-n") {
verbosity = (verbosity & ~OF_VALUENAME) | OF_NUMERIC;
} else if (args[argPos] == "-N") {
Expand Down Expand Up @@ -2096,7 +2089,7 @@ result_t MainLoop::executeGet(const vector<string>& args, bool* connected, ostri
time_t maxLastUp = 0;
if (ret == RESULT_OK) {
bool first = true;
verbosity |= OF_JSON | (full ? OF_ALL_ATTRS : 0) | (withDefinition ? OF_DEFINTION : 0);
verbosity |= OF_JSON | (full ? OF_ALL_ATTRS : OF_NONE) | (withDefinition ? OF_DEFINITION : OF_NONE);
deque<Message*> messages;
m_messages->findAll(circuit, name, getUserLevels(user), exact, true, withWrite, true, true, true, 0, 0, false,
&messages);
Expand Down Expand Up @@ -2147,7 +2140,7 @@ result_t MainLoop::executeGet(const vector<string>& args, bool* connected, ostri
Message* next = *(it+1);
same = next->getCircuit() == lastCircuit && next->getName() == name;
}
message->decodeJson(!first, same, raw, verbosity, ostream);
message->decodeJson(!first, same, true, raw, verbosity, ostream);
lastName = name;
first = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ebusd/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class NetMessage {
explicit NetMessage(bool isHttp)
: m_isHttp(isHttp), m_resultSet(false), m_disconnect(false), m_listenSince(0) {
m_settings.mode = cm_normal;
m_settings.format = 0;
m_settings.format = OF_NONE;
m_settings.listenWithUnknown = false;
m_settings.listenOnlyUnknown = false;
pthread_mutex_init(&m_mutex, nullptr);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/ebus/contrib/test/test_tem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ int main() {
continue;
}
cout << "\"" << check[0] << "\"=\"";
fields->dump(false, false, &cout);
fields->dump(false, OF_NONE, &cout);
cout << "\": create OK" << endl;

ostringstream output;
Expand All @@ -194,9 +194,9 @@ int main() {
cout << " parse \"" << sstr.getStr().substr(0, 2) << "\" error: " << getResultCode(result) << endl;
error = true;
}
result = fields->read(mstr, 0, false, nullptr, -1, 0, -1, &output);
result = fields->read(mstr, 0, false, nullptr, -1, OF_NONE, -1, &output);
if (result >= RESULT_OK) {
result = fields->read(sstr, 0, !output.str().empty(), nullptr, -1, 0, -1, &output);
result = fields->read(sstr, 0, !output.str().empty(), nullptr, -1, OF_NONE, -1, &output);
}
if (failedRead) {
if (result >= RESULT_OK) {
Expand Down
Loading

0 comments on commit 38bfc7a

Please sign in to comment.