- read_csv.hpp: replace std::exit(1) with std::runtime_error throw - read_csv.hpp: remove duplicate #pragma once - RNG.cc: wrap JSON load in try-catch to prevent crash on missing config - Generator.cc: fix comments to reflect actual 3-pass structure
82 lines
2.6 KiB
C++
82 lines
2.6 KiB
C++
#include <TestProject/RNG/Generator.h>
|
|
#include <TestProject/RNG/model/ModelRegistry.h>
|
|
#include <glob/BinaryTele.h>
|
|
#include <zlib/MemFix.hpp>
|
|
#include <zlib/zoneDef.h>
|
|
#include <string>
|
|
#include <cmath>
|
|
#include "mix_cc/debug/pre_define.h"
|
|
|
|
using namespace std;
|
|
using namespace chrono;
|
|
|
|
Generator::Generator() : registry(ModelRegistry::instance()) {
|
|
stime = chrono::system_clock::now();
|
|
logger_ = make_unique<LOG>("Generator");
|
|
}
|
|
|
|
Generator::~Generator() {
|
|
for (auto& it : m_mapfix) {
|
|
if (it.second != nullptr) {
|
|
delete it.second;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool Generator::wtite_in_shm(int event_no) {
|
|
try {
|
|
if (m_mapfix.find(event_no) == m_mapfix.end()) {
|
|
m_mapfix.insert(
|
|
make_pair(event_no, new CMemFix<PLC_DATA>(to_string(event_no),
|
|
TEL_CACHE_SIZE)));
|
|
}
|
|
|
|
binary_tele.ReBuild(event_no);
|
|
int size = binary_tele.size();
|
|
|
|
size_t data_index =
|
|
((chrono::system_clock::now() - stime).count() / (size_t)pow(10, 6)) /
|
|
20;
|
|
|
|
// Pass 1: create model instances
|
|
for (int i = 0; i < size; i++) {
|
|
const char* spec = binary_tele[i].tables[1];
|
|
string specStr = (spec != nullptr && strlen(spec) > 0) ? string(spec) : "";
|
|
float defaultVal = atof(binary_tele[i].defaultValue);
|
|
string key = string(binary_tele[i].item);
|
|
registry.getOrCreate(specStr, defaultVal, key);
|
|
}
|
|
// Pass 2: link valve_pair models to action signals
|
|
for (int i = 0; i < size; i++) {
|
|
const char* spec = binary_tele[i].tables[1];
|
|
string specStr = (spec != nullptr && strlen(spec) > 0) ? string(spec) : "";
|
|
float defaultVal = atof(binary_tele[i].defaultValue);
|
|
string key = string(binary_tele[i].item);
|
|
IModel* model = registry.getOrCreate(specStr, defaultVal, key);
|
|
model->linkPeers(registry);
|
|
}
|
|
|
|
// Pass 3: evaluate all models
|
|
for (int i = 0; i < size; i++) {
|
|
const char* spec = binary_tele[i].tables[1];
|
|
string specStr = (spec != nullptr && strlen(spec) > 0) ? string(spec) : "";
|
|
float defaultVal = atof(binary_tele[i].defaultValue);
|
|
string key = string(binary_tele[i].item);
|
|
IModel* model = registry.getOrCreate(specStr, defaultVal, key);
|
|
|
|
if (binary_tele[i].type[0] == 'b') {
|
|
binary_tele[i] = model->evaluateBool(data_index) ? 1.0f : 0.0f;
|
|
} else {
|
|
binary_tele[i] = model->evaluate(data_index);
|
|
}
|
|
}
|
|
|
|
char* buff = binary_tele.GetTeleData();
|
|
m_mapfix[event_no]->push((PLC_DATA*)buff);
|
|
return true;
|
|
} catch (const std::exception& e) {
|
|
logger_->Error() << "wtite_in_shm Error!" << e.what() << std::endl;
|
|
return false;
|
|
}
|
|
}
|