...
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
|
||||
namespace mcc
|
||||
{
|
||||
enum class MccMountTelemetryErrorCode : int { ERROR_OK, ERROR_HARDWARE };
|
||||
enum class MccMountTelemetryErrorCode : int { ERROR_OK, ERROR_HARDWARE, ERROR_DATA_TIMEOUT };
|
||||
|
||||
/* error category definition */
|
||||
|
||||
@@ -31,6 +31,8 @@ struct MccMountTelemetryCategory : public std::error_category {
|
||||
return "OK";
|
||||
case MccMountTelemetryErrorCode::ERROR_HARDWARE:
|
||||
return "hardware request failed";
|
||||
case MccMountTelemetryErrorCode::ERROR_DATA_TIMEOUT:
|
||||
return "an timeout occured while waiting for new data";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
@@ -138,11 +140,6 @@ public:
|
||||
|
||||
typedef std::error_code error_t;
|
||||
|
||||
typedef std::function<void(mount_telemetry_data_t)> update_callback_func_t;
|
||||
typedef std::list<update_callback_func_t> update_callback_container_t;
|
||||
|
||||
typedef std::list<std::shared_ptr<std::packaged_task<update_callback_func_t>>> cc_t;
|
||||
|
||||
MccMountTelemetry(astrom_engine_t& astrom_engine, pec_t& pec, hardware_t& hardware)
|
||||
: base_t(astrom_engine, pec), _hardware(hardware)
|
||||
{
|
||||
@@ -265,49 +262,37 @@ public:
|
||||
|
||||
_data = std::move(current_data);
|
||||
|
||||
std::lock_guard cl_lock{_callbackMutex};
|
||||
|
||||
// callbacks will be invoked with its own copy of telemetry data!
|
||||
for (auto& func : _callbackFuncs) {
|
||||
std::thread t([this, &func] {
|
||||
auto d = _data;
|
||||
func(std::move(d));
|
||||
});
|
||||
t.detach();
|
||||
}
|
||||
// notify all threads for new telemetry data
|
||||
_updateCondVar.notify_all();
|
||||
|
||||
return MccMountTelemetryErrorCode::ERROR_OK;
|
||||
}
|
||||
|
||||
|
||||
update_callback_container_t::iterator addCallbackFunc(update_callback_func_t func)
|
||||
{
|
||||
std::lock_guard lock{_callbackMutex};
|
||||
|
||||
return _callbackFuncs.emplace_back(std::move(func));
|
||||
}
|
||||
|
||||
void delCallbackFunc(update_callback_container_t::iterator iter)
|
||||
{
|
||||
std::lock_guard lock{_callbackMutex};
|
||||
|
||||
if (_callbackFuncs.size()) {
|
||||
_callbackFuncs.erase(iter);
|
||||
}
|
||||
}
|
||||
|
||||
void clearCallbackFuncs()
|
||||
{
|
||||
std::lock_guard lock{_callbackMutex};
|
||||
|
||||
_callbackFuncs.clear();
|
||||
}
|
||||
|
||||
error_t data(mount_telemetry_data_t& data)
|
||||
{
|
||||
std::lock_guard lock{_updateMutex};
|
||||
|
||||
data = std::move(_data);
|
||||
data = _data;
|
||||
|
||||
return MccMountTelemetryErrorCode::ERROR_OK;
|
||||
}
|
||||
|
||||
// wait until data is updated or given timeout occurs
|
||||
template <traits::mcc_time_duration_c DT>
|
||||
error_t waitForUpdatedData(mount_telemetry_data_t& data, const DT& timeout)
|
||||
{
|
||||
auto timeout_tp = std::chrono::steady_clock::now() + timeout;
|
||||
|
||||
std::unique_lock lk(_updateMutex);
|
||||
|
||||
auto res = _updateCondVar.wait_until(
|
||||
lk, timeout_tp, [last_time_point = _data.time_point, this]() { return last_time_point < _data.timepoint; });
|
||||
if (res == std::cv_status::timeout) {
|
||||
return MccMountTelemetryErrorCode::ERROR_DATA_TIMEOUT;
|
||||
}
|
||||
|
||||
data = _data;
|
||||
|
||||
return MccMountTelemetryErrorCode::ERROR_OK;
|
||||
}
|
||||
@@ -315,10 +300,9 @@ public:
|
||||
protected:
|
||||
mount_telemetry_data_t _data{};
|
||||
hardware_t& _hardware;
|
||||
update_callback_container_t _callbackFuncs{};
|
||||
|
||||
std::mutex _updateMutex;
|
||||
std::mutex _callbackMutex;
|
||||
std::condition_variable _updateCondVar;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user