//! Single-session commands: version, ping, session lifecycle, register/advise and single-item writes. use serde_json::json; use zb_mom_ww_mxgateway_client::generated::mxaccess_gateway::v1::{ CloseSessionRequest, MxCommand, MxCommandKind, MxCommandRequest, OpenSessionRequest, PingCommand, }; use zb_mom_ww_mxgateway_client::{next_correlation_id, Error, MxValue}; use crate::cli::*; use crate::connect::*; use crate::output::*; use crate::parse::*; /// Run the `session` command group. pub(crate) async fn run(command: Command) -> Result<(), Error> { match command { Command::Version { json } => print_version(json), Command::Ping { connection, message, json, } => { let client = connect(connection).await?; let reply = client .invoke(MxCommandRequest { client_correlation_id: next_correlation_id("cli-ping"), command: Some(MxCommand { kind: MxCommandKind::Ping as i32, payload: Some(zb_mom_ww_mxgateway_client::generated::mxaccess_gateway::v1::mx_command::Payload::Ping( PingCommand { message }, )), }), ..MxCommandRequest::default() }) .await?; print_command_reply("ping", &reply, json); } Command::OpenSession { connection, client_name, json, } => { let client = connect(connection).await?; let reply = client .open_session_raw(OpenSessionRequest { client_session_name: client_name, ..OpenSessionRequest::default() }) .await?; if json { println!( "{}", json!({ "sessionId": reply.session_id, "backendName": reply.backend_name, "gatewayProtocolVersion": reply.gateway_protocol_version, "workerProtocolVersion": reply.worker_protocol_version, }) ); } else { println!("{}", reply.session_id); } } Command::CloseSession { connection, session_id, json, } => { let client = connect(connection).await?; let reply = client .close_session_raw(CloseSessionRequest { session_id, client_correlation_id: next_correlation_id("cli-close-session"), }) .await?; if json { println!("{}", json!({ "sessionId": reply.session_id })); } else { println!("closed {}", reply.session_id); } } Command::Register { connection, session_id, client_name, json, } => { let session = session_for(connection, session_id).await?; let server_handle = session.register(&client_name).await?; print_handle("serverHandle", server_handle, json); } Command::AddItem { connection, session_id, server_handle, item, json, } => { let session = session_for(connection, session_id).await?; let item_handle = session.add_item(server_handle, &item).await?; print_handle("itemHandle", item_handle, json); } Command::Advise { connection, session_id, server_handle, item_handle, json, } => { let session = session_for(connection, session_id).await?; session.advise(server_handle, item_handle).await?; print_ok("advise", json); } Command::AdviseSupervisory { connection, session_id, server_handle, item_handle, json, } => { let session = session_for(connection, session_id).await?; session .advise_supervisory(server_handle, item_handle) .await?; print_ok("advise-supervisory", json); } Command::Unregister { connection, session_id, server_handle, json, } => { let session = session_for(connection, session_id).await?; session.unregister(server_handle).await?; print_ok("unregister", json); } Command::AuthenticateUser { connection, session_id, server_handle, verify_user, password, password_env, json, } => { let verify_user_password = resolve_verify_user_password(password, &password_env)?; let session = session_for(connection, session_id).await?; let user_id = session .authenticate_user(server_handle, &verify_user, &verify_user_password) .await?; print_handle("userId", user_id, json); } Command::WriteSecured { connection, session_id, server_handle, item_handle, value_type, value, current_user_id, verifier_user_id, json, } => { let session = session_for(connection, session_id).await?; session .write_secured( server_handle, item_handle, current_user_id, verifier_user_id, parse_value(value_type, &value)?, ) .await?; print_ok("write-secured", json); } Command::Write { connection, session_id, server_handle, item_handle, value_type, value, user_id, json, } => { let session = session_for(connection, session_id).await?; session .write( server_handle, item_handle, parse_value(value_type, &value)?, user_id, ) .await?; print_ok("write", json); } Command::Write2 { connection, session_id, server_handle, item_handle, value_type, value, timestamp, user_id, json, } => { let session = session_for(connection, session_id).await?; session .write2( server_handle, item_handle, parse_value(value_type, &value)?, MxValue::string(timestamp), user_id, ) .await?; print_ok("write2", json); } _ => unreachable!("command routed to the wrong group"), } Ok(()) }