use cogcore::{HostRoutesHandler, Shell, View};
use crate::{Bridge, bridge::attach};
const DIST: &str = match option_env!("CONSORTIUM_HMI_DIST") {
Some(dist) => dist,
None => "dist",
};
const DEBUG_URL: Option<&str> = option_env!("CONSORTIUM_HMI_DEBUG_URL");
pub struct KioskApp {
shell: Shell,
view: View,
}
impl KioskApp {
pub fn new(
name: &str,
platform: Option<&str>,
module_path: Option<&str>,
) -> cogcore::Result<Self> {
cogcore::init(platform, module_path)?;
let shell = Shell::new(name, false)?;
let view = View::new()?;
Ok(Self { shell, view })
}
pub fn view(&self) -> &View {
&self.view
}
pub fn attach_bridge(&self, bridge: Bridge) -> cogcore::Result<()> {
self.attach_bridge_with_runtime(bridge, tokio::runtime::Handle::current())
}
pub fn attach_bridge_with_runtime(
&self,
bridge: Bridge,
runtime: tokio::runtime::Handle,
) -> cogcore::Result<()> {
attach(bridge, runtime, &self.view)
}
pub fn load(&self) -> cogcore::Result<()> {
#[cfg(debug_assertions)]
if let Some(url) = DEBUG_URL {
return self.view.load_uri(url);
}
let security = self.shell.security_manager()?;
security.register_secure("csti")?;
security.register_cors_enabled("csti")?;
let routes = HostRoutesHandler::new(None)?;
routes.add_path("app", DIST)?;
self.shell
.set_request_handler("csti", routes.as_request_handler())?;
self.view.load_uri("csti://app/index.html")
}
}