use super::GCBorrowedDatapointValue;
use crate::{GCDatapoint, GCDatapointID, GCDatapointValue};
use gc_abi::error::GCPluginABIError;
use gc_abi::{GCAbiConversion, GCLogLevel, raw};
use std::ffi::CStr;
#[repr(transparent)]
pub struct GCPluginInterface(gc_abi::GCPluginInterface);
impl GCPluginInterface {
#[inline(always)]
pub fn publish_datapoint(&self, datapoint_value: &GCDatapointValue) -> bool {
self.0.publish_datapoint(datapoint_value)
}
#[inline(always)]
pub fn store_datapoint(&self, datapoint_value: &GCDatapointValue) {
self.0.store_datapoint(datapoint_value)
}
#[inline(always)]
pub fn get_own_datapoints(&self) -> &[GCDatapoint] {
self.0.get_config().get_own_datapoints()
}
#[inline(always)]
pub fn get_last_datapoint_value(&self, datapoint_id: GCDatapointID) -> Option<GCBorrowedDatapointValue> {
unsafe {
let ptr = self.0.get_last_datapoint_value(datapoint_id);
if ptr.is_null() {
None
} else {
Some(GCBorrowedDatapointValue::from(ptr as gc_abi::raw::GCDatapointValue))
}
}
}
#[inline(always)]
pub fn get_subscribed_datapoints(&self) -> &[GCDatapoint] {
self.0.get_config().get_subscribed_datapoints()
}
#[inline(always)]
pub fn log(&self, level: raw::eGCLogLevel, message: &CStr) {
self.0.log(level, message)
}
#[inline(always)]
pub fn log_audit(&self, message: &CStr) {
self.0.log_audit(message)
}
#[inline]
pub fn get_log_level(&self) -> Result<GCLogLevel, GCPluginABIError> {
self.0.get_log_level()
}
#[inline]
pub fn get_raw_log_level(&self) -> raw::eGCLogLevel {
self.0.get_raw_log_level()
}
pub fn get_own_configuration<T>(&self) -> Result<T, GCPluginABIError>
where
T: serde::de::DeserializeOwned,
{
let config = unsafe { *self.0.ref_c().config.pluginJsonConfig };
let data = config.data;
let mut result: raw::eResultCallback = raw::eResultCallback_GC_JSON_ERROR;
let str_data_obj =
unsafe { config.getObjectSerialized.ok_or(GCPluginABIError::ErrorCallbackNotAvailable)?(data, &mut result as *mut raw::eResultCallback) };
if result == raw::eResultCallback_GC_JSON_ERROR {
return Err(GCPluginABIError::ErrorSerializingJsonConfig);
}
let str_data = unsafe { CStr::from_ptr(str_data_obj).to_str()? };
let json_result = serde_json::from_str(str_data)?;
unsafe { config.freeString.ok_or(GCPluginABIError::ErrorCallbackNotAvailable)?(str_data_obj) };
Ok(json_result)
}
}
impl From<raw::GCPluginInterface> for &GCPluginInterface {
fn from(interface: raw::GCPluginInterface) -> Self {
unsafe {
let a = interface as *const GCPluginInterface;
(a.as_ref().unwrap()) as _
}
}
}
unsafe impl Send for GCPluginInterface {}
unsafe impl Sync for GCPluginInterface {}