Демонстрирует списка разрешённых ключей контроллера.
#include <exception>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include "../ilg_cpp_helpers.h"
#define ILG_LOG_FILE
using namespace ilg;
uint8_t g_nBankCount = 0;
uint32_t g_nCtrFlags = 0;
size_t g_nMaxKeys = 0;
const char* kKeyTypeNames[] = {"Простой", "Блокирующий", "Мастер"};
#ifdef ILG_LOG
const char kLogLevelChars[] = {'-', 'E', 'W', 'I', 'D'};
const char kLogFileName[] = "ilguard.log";
void LogCallback(
ilg_log_level level,
const char* pContext,
const char* pMessage,
void*) {
#if 1
std::fstream file(kLogFileName, std::ios_base::out | std::ios_base::app);
auto& out = file;
#else
auto& out = std::cout;
#endif
auto t = std::time(nullptr);
auto tmb = std::localtime(&t);
out << std::put_time(tmb, "%d-%m-%Y %H:%M:%S") << " [" << kLogLevelChars[level] << ' '
<< pContext << "] " << pMessage << std::endl;
}
#endif
void ShowKeys(CController& oController) {
std::cout << "Ключи:" << std::endl;
auto tStartTime = now();
std::vector<ilg_key> oKeys;
for (uint8_t i = 0; i < g_nBankCount; i++) {
std::cout << "Банк " << (uint)i << ':' << std::endl;
auto nTopIdx = oController.ReadKeyTopIdx(i);
if (nTopIdx < 0) {
std::cout << "Некорректный указатель верхней границы ключей банка " << i << std::endl;
nTopIdx = static_cast<ssize_t>(g_nMaxKeys);
}
if (0 == nTopIdx)
std::cout << "Список пустой" << std::endl;
else {
oKeys.resize(static_cast<size_t>(nTopIdx));
std::cout << "Чтение..." << std::endl;
oController.ReadKeys(i, 0, oKeys.data(), oKeys.size());
for (size_t j = 0; j < oKeys.size(); j++) {
pKey = &oKeys[j];
std::cout << j << " пустой" << std::endl;
else
std::cout << j <<
" " << KeyNumberToStr(pKey->
rNumber, pKey->
nFlags, fWiegand)
<<
", " << kKeyTypeNames[pKey->
nType] <<
", доступ: " << std::hex
<< std::showbase << (uint)pKey->
nAccess << std::dec << std::endl;
}
}
std::cout << "------------" << std::endl;
}
std::cout << "Выполнено за " << since(tStartTime).count() << " мс" << std::endl;
}
void SetKey(CController& oController) {
std::cout << "Введите номер банка, позицию ключа (-1 верхняя граница), тип (1 Простой, 2 "
"Блокирующий, 3 "
"Мастер), доступ (16-ричное), номер (-1 последний ключ):"
<< std::endl;
int nIdx;
uint nBankN, nType, nAccess;
std::string sKeyNumber;
std::cin >> nBankN >> nIdx >> nType >> std::hex >> nAccess >> std::quoted(sKeyNumber);
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (std::cin.fail()) {
std::cout << "Неправильный ввод" << std::endl;
return;
}
if (-1 == nIdx) {
auto nTopIdx = oController.ReadKeyTopIdx(static_cast<uint8_t>(nBankN));
if (nTopIdx < 0) {
std::cout << "Некорректный указатель верхней границы ключей банка " << nBankN
<< std::endl;
nTopIdx = static_cast<ssize_t>(g_nMaxKeys);
}
if (nTopIdx == static_cast<ssize_t>(g_nMaxKeys)) {
std::cout << "Нет свободного места в конце списка" << std::endl;
return;
}
nIdx = static_cast<int>(nTopIdx);
}
if (sKeyNumber == "-1") {
oController.ReadRtcParams(rRtc);
}
else if (!TryStrToKeyNum(sKeyNumber.c_str(), &rKey.
rNumber, &rKey.
nFlags)) {
std::cout << "Неправильный ввод" << std::endl;
return;
}
rKey.
nAccess =
static_cast<uint8_t
>(nAccess);
std::cout << "Запись..." << std::endl;
auto tStartTime = now();
oController.WriteKeys(static_cast<uint8_t>(nBankN), static_cast<size_t>(nIdx), &rKey, 1);
std::cout << "Выполнено за " << since(tStartTime).count() << " мс" << std::endl;
}
void ClearKeys(CController& oController) {
std::cout << "Очистка..." << std::endl;
auto tStartTime = now();
for (uint8_t i = 0; i < g_nBankCount; ++i) {
auto nTopIdx = oController.ReadKeyTopIdx(i);
if (0 == nTopIdx)
continue;
size_t nCount;
if (nTopIdx < 0) {
std::cout << "Некорректный указатель верхней границы ключей банка " << i << std::endl;
nCount = g_nMaxKeys;
}
else
nCount = static_cast<size_t>(nTopIdx);
oController.EraseKeys(i, 0, nCount);
}
std::cout << "Выполнено за " << since(tStartTime).count() << " мс" << std::endl;
}
bool DoConnectToConverter(CILG& oILG, CConverter& oConverter) {
CConverterSearch oSearch(oILG.GetSearch());
std::cout << "Поиск конвертеров..." << std::endl;
oSearch.Scan();
auto nCount = oSearch.GetConverterCount();
if (0 == nCount) {
std::cout << "Конвертер не найден" << std::endl;
return false;
}
std::cout << "Найдено конвертеров: " << nCount << std::endl;
std::cout << std::endl << "Выберите конвертер:" << std::endl;
for (size_t i = 0; i < nCount; i++) {
oSearch.GetConverterInfo(i, rInfo);
std::stringstream ss;
ss << kConverterModelNames[rInfo.
nModel];
ss <<
" с/н:" << rInfo.
nSn;
ss <<
" прошивка:" << VersionToStr(rInfo.
nFwVersion);
ss <<
" режим: " << kConverterModeNames[rInfo.
nMode];
<< "]: " << ss.str() << std::endl;
}
std::cout << "0 - Выйти из программы" << std::endl;
int nCommand;
std::cin >> nCommand;
if (std::cin.fail()) {
std::cin.clear();
nCommand = -1;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if ((nCommand <= 0) || (static_cast<size_t>(nCommand) > nCount))
return false;
oSearch.GetConverterInfo(static_cast<size_t>(nCommand - 1), rInfo);
std::cout <<
"Подключение к конвертеру [" << kPortTypeNames[rInfo.
nPortType] <<
": "
oConverter.GetOptions(rOptions);
oConverter.SetOptions(rOptions);
oConverter.Connect();
oConverter.GetConverterInfo(rInfo);
std::stringstream ss;
ss << kConverterModelNames[rInfo.
nModel];
ss <<
" с/н:" << rInfo.
nSn;
ss <<
" прошивка:" << VersionToStr(rInfo.
nFwVersion);
ss <<
" режим: " << kConverterModeNames[rInfo.
nMode];
std::cout << "Конвертер успешно подключён [" << ss.str() << ']' << std::endl;
return true;
}
bool DoConnectToController(CConverter& oConverter, CController& oController) {
int nCommand;
while (true) {
std::cout << "Выберите контроллер:" << std::endl;
oConverter.Scan();
auto nCount = oConverter.GetControllerCount();
for (size_t i = 0; i < nCount; i++) {
oConverter.GetControllerInfo(i, rInfo);
std::cout << i + 1 <<
". #" << (uint)rInfo.
nAddress <<
": "
<< kControllerModelNames[rInfo.
nModel] <<
" с/н:" << rInfo.
nSn
<<
" прошивка:" << VersionToStr(rInfo.
nFwVersion) << std::endl;
}
if (0 == nCount)
std::cout << "1 - Искать снова" << std::endl;
std::cout << "0 - Выйти из программы" << std::endl;
std::cin >> nCommand;
if (std::cin.fail()) {
std::cin.clear();
nCommand = -1;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (0 == nCommand)
return false;
if ((nCommand >= 1) && (static_cast<size_t>(nCommand) <= nCount))
break;
}
oConverter.GetControllerInfo(static_cast<size_t>(nCommand - 1), rInfo);
oController = oConverter.GetController(rInfo.
nModel, rInfo.
nSn);
std::cout <<
"Подключение к контроллеру [" << kControllerModelNames[rInfo.
nModel] <<
": "
<< rInfo.
nSn <<
"]..." << std::endl;
oController.Connect();
oController.GetControllerInfo(rInfo);
std::cout <<
"Контроллер успешно подключён [#" << (uint)rInfo.
nAddress <<
' '
<< kControllerModelNames[rInfo.
nModel] <<
" с/н:" << rInfo.
nSn
<<
" прошивка:" << VersionToStr(rInfo.
nFwVersion) <<
']' << std::endl;
oConverter.SetAutoScan(false);
return true;
}
int main() {
try {
#ifdef ILG_LOG
#ifdef ILG_LOG_FILE
std::ofstream file(kLogFileName, std::ios_base::out | std::ios_base::trunc);
file.close();
#endif
CILG::SetLogCallback(LogCallback);
#endif
CILG oILG;
CConverter oConverter;
if (!DoConnectToConverter(oILG, oConverter))
return 0;
CController oController;
if (!DoConnectToController(oConverter, oController))
return 0;
oController.GetControllerInfo(rInfo);
g_nMaxKeys *= 2;
while (true) {
std::cout << "-----" << std::endl;
std::cout << "Введите номер команды:" << std::endl;
std::cout << "1 - Показать ключи" << std::endl;
std::cout << "2 - Установить ключ..." << std::endl;
std::cout << "3 - Очистить список ключей" << std::endl;
std::cout << "0 - Выйти из программы" << std::endl;
int nCommand;
std::cin >> nCommand;
if (std::cin.fail()) {
std::cin.clear();
nCommand = -1;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
switch (nCommand) {
case 0:
return 0;
case 1:
ShowKeys(oController);
break;
case 2:
SetKey(oController);
break;
case 3:
ClearKeys(oController);
break;
default:
std::cout << "Неправильный ввод" << std::endl;
break;
}
}
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
@ ILG_CTR_F_WIEGAND
Definition: ilguard.h:836
@ ILG_CTR_F_X2
Definition: ilguard.h:840
@ ILG_CTR_F_TWOBANKS
Definition: ilguard.h:834
Заголовочный файл API SDK Guard.
@ ILG_CONVERTER_MODE_UNKNOWN
Definition: ilguard.h:314
ilg_key_type
Definition: ilguard.h:1091
ilg_log_level
Уровень лога.
Definition: ilguard.h:421
@ ILG_LOG_LEVEL_DEBUG
Отладочные сообщения.
Definition: ilguard.h:451
@ ILG_CONVERTER_MODEL_UNKNOWN
Definition: ilguard.h:274
Информация о контроллере.
Definition: ilguard.h:924
uint32_t nCtrFlags
Definition: ilguard.h:940
uint32_t nBankSize
Definition: ilguard.h:936
int nSn
Definition: ilguard.h:934
uint8_t nAddress
Definition: ilguard.h:930
uint32_t nFwVersion
Definition: ilguard.h:932
ilg_controller_model nModel
Definition: ilguard.h:926
Информация о конвертере.
Definition: ilguard.h:514
ilg_port_type nPortType
Definition: ilguard.h:516
const char * pszPortName
Definition: ilguard.h:518
int64_t nFwBuildDate
Definition: ilguard.h:532
ilg_converter_model nModel
Definition: ilguard.h:523
const char * pszConnect
Definition: ilguard.h:521
ilg_converter_mode nMode
Definition: ilguard.h:534
uint32_t nFwVersion
Definition: ilguard.h:529
int nSn
Definition: ilguard.h:525
Настройки конвертера.
Definition: ilguard.h:788
ilg_converter_model nConnectModel
Definition: ilguard.h:790
Definition: ilguard.h:1143
uint8_t fErased
Definition: ilguard.h:1145
uint8_t nFlags
Definition: ilguard.h:1147
ilg_key_type nType
Definition: ilguard.h:1160
ilg_key_number rNumber
Definition: ilguard.h:1162
uint8_t nAccess
Definition: ilguard.h:1156
Definition: ilguard.h:1293
ilg_key_number rLastKey
Definition: ilguard.h:1301