use chrono::Utc;
pub use raystack::{
is_tag_name, skyspark_tz_string_to_tz, BasicNumber, Coord, Date, DateTime, Error,
FromHaysonError, Grid, Hayson, HisReadRange, Marker, Na, NewSkySparkClientError, Number,
ParseJsonGridError, ParseRefError, ParseTagNameError, Ref, RemoveMarker, ScientificNumber,
Symbol, TagName, Time, Uri, ValueExt, Xstr,
};
use std::sync::Arc;
use tokio::runtime::Runtime;
use url::Url;
pub mod auth;
mod utils;
pub use utils::new_client;
type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub struct SkySparkClient {
client: raystack::SkySparkClient,
rt: Arc<Runtime>,
}
impl SkySparkClient {
pub fn new(
project_api_url: Url,
username: &str,
password: &str,
) -> std::result::Result<Self, NewSkySparkClientError> {
let rt = Runtime::new().expect("could not create a new Tokio runtime");
Self::new_with_runtime(project_api_url, username, password, Arc::new(rt))
}
pub fn new_with_runtime(
project_api_url: Url,
username: &str,
password: &str,
rt: Arc<Runtime>,
) -> std::result::Result<Self, NewSkySparkClientError> {
let rclient = reqwest::Client::new();
let client = rt.block_on(raystack::SkySparkClient::new_with_client(
project_api_url,
username,
password,
rclient,
))?;
Ok(Self { client, rt })
}
pub fn project_name(&self) -> &str {
self.client.project_name()
}
pub fn project_api_url(&self) -> &Url {
self.client.project_api_url()
}
}
impl SkySparkClient {
pub fn about(&mut self) -> Result<Grid> {
self.rt.block_on(self.client.about())
}
pub fn filetypes(&mut self) -> Result<Grid> {
self.rt.block_on(self.client.filetypes())
}
pub fn his_read(&mut self, id: &Ref, range: &HisReadRange) -> Result<Grid> {
self.rt.block_on(self.client.his_read(id, range))
}
pub fn his_write_bool(&mut self, id: &Ref, his_data: &[(DateTime, bool)]) -> Result<Grid> {
self.rt.block_on(self.client.his_write_bool(id, his_data))
}
pub fn his_write_num(&mut self, id: &Ref, his_data: &[(DateTime, Number)]) -> Result<Grid> {
self.rt.block_on(self.client.his_write_num(id, his_data))
}
pub fn his_write_str(&mut self, id: &Ref, his_data: &[(DateTime, String)]) -> Result<Grid> {
self.rt.block_on(self.client.his_write_str(id, his_data))
}
pub fn utc_his_write_bool(
&mut self,
id: &Ref,
time_zone_name: &str,
his_data: &[(chrono::DateTime<Utc>, bool)],
) -> Result<Grid> {
self.rt
.block_on(self.client.utc_his_write_bool(id, time_zone_name, his_data))
}
pub fn utc_his_write_num(
&mut self,
id: &Ref,
time_zone_name: &str,
his_data: &[(chrono::DateTime<Utc>, Number)],
) -> Result<Grid> {
self.rt
.block_on(self.client.utc_his_write_num(id, time_zone_name, his_data))
}
pub fn utc_his_write_str(
&mut self,
id: &Ref,
time_zone_name: &str,
his_data: &[(chrono::DateTime<Utc>, String)],
) -> Result<Grid> {
self.rt
.block_on(self.client.utc_his_write_str(id, time_zone_name, his_data))
}
pub fn nav(&mut self, nav_id: Option<&Ref>) -> Result<Grid> {
self.rt.block_on(self.client.nav(nav_id))
}
pub fn ops(&mut self) -> Result<Grid> {
self.rt.block_on(self.client.ops())
}
pub fn read(&mut self, filter: &str, limit: Option<u64>) -> Result<Grid> {
self.rt.block_on(self.client.read(filter, limit))
}
pub fn read_by_ids(&mut self, ids: &[Ref]) -> Result<Grid> {
self.rt.block_on(self.client.read_by_ids(ids))
}
}
impl SkySparkClient {
pub fn eval(&mut self, axon_expr: &str) -> Result<Grid> {
self.rt.block_on(self.client.eval(axon_expr))
}
}