msr_plugin_csv_register_recorder/api/
controller.rs

1use msr_plugin::{reply_channel, send_message_receive_result};
2
3use crate::{MessageSender, PluginResult};
4
5use super::{
6    query, Command, Config, ObservedRegisterValues, Query, RegisterGroupConfig, RegisterGroupId,
7    State, Status, StoredRegisterRecord,
8};
9
10/// Remote controller for the plugin
11///
12/// Wraps the message-based communication with the plugin
13/// into asynchronous functions.
14#[derive(Debug, Clone)]
15pub struct Controller {
16    message_tx: MessageSender,
17}
18
19impl Controller {
20    #[must_use]
21    pub const fn new(message_tx: MessageSender) -> Self {
22        Self { message_tx }
23    }
24
25    pub async fn command_replace_config(&self, new_config: Config) -> PluginResult<Config> {
26        let (reply_tx, reply_rx) = reply_channel();
27        let command = Command::ReplaceConfig(reply_tx, new_config);
28        send_message_receive_result(command, &self.message_tx, reply_rx).await
29    }
30
31    pub async fn command_replace_register_group_config(
32        &self,
33        register_group_id: RegisterGroupId,
34        new_config: RegisterGroupConfig,
35    ) -> PluginResult<Option<RegisterGroupConfig>> {
36        let (reply_tx, reply_rx) = reply_channel();
37        let command = Command::ReplaceRegisterGroupConfig(reply_tx, register_group_id, new_config);
38        send_message_receive_result(command, &self.message_tx, reply_rx).await
39    }
40
41    pub async fn command_switch_state(&self, new_state: State) -> PluginResult<()> {
42        let (reply_tx, reply_rx) = reply_channel();
43        let command = Command::SwitchState(reply_tx, new_state);
44        send_message_receive_result(command, &self.message_tx, reply_rx).await
45    }
46
47    pub async fn command_record_observed_register_group_values(
48        &self,
49        register_group_id: RegisterGroupId,
50        observed_register_values: ObservedRegisterValues,
51    ) -> PluginResult<()> {
52        let (reply_tx, reply_rx) = reply_channel();
53        let command = Command::RecordObservedRegisterGroupValues(
54            reply_tx,
55            register_group_id,
56            observed_register_values,
57        );
58        send_message_receive_result(command, &self.message_tx, reply_rx).await
59    }
60
61    pub async fn command_shutdown(&self) -> PluginResult<()> {
62        let (reply_tx, reply_rx) = reply_channel();
63        let command = Command::Shutdown(reply_tx);
64        send_message_receive_result(command, &self.message_tx, reply_rx).await
65    }
66
67    pub async fn command_smoke_test(&self) -> PluginResult<()> {
68        let (reply_tx, reply_rx) = reply_channel();
69        let command = Command::SmokeTest(reply_tx);
70        send_message_receive_result(command, &self.message_tx, reply_rx).await
71    }
72
73    pub async fn query_config(&self) -> PluginResult<Config> {
74        let (reply_tx, reply_rx) = reply_channel();
75        let query = Query::Config(reply_tx);
76        send_message_receive_result(query, &self.message_tx, reply_rx).await
77    }
78
79    pub async fn query_register_group_config(
80        &self,
81        register_group_id: RegisterGroupId,
82    ) -> PluginResult<Option<RegisterGroupConfig>> {
83        let (reply_tx, reply_rx) = reply_channel();
84        let query = Query::RegisterGroupConfig(reply_tx, register_group_id);
85        send_message_receive_result(query, &self.message_tx, reply_rx).await
86    }
87
88    pub async fn query_status(&self, request: query::StatusRequest) -> PluginResult<Status> {
89        let (reply_tx, reply_rx) = reply_channel();
90        let query = Query::Status(reply_tx, request);
91        send_message_receive_result(query, &self.message_tx, reply_rx).await
92    }
93
94    pub async fn query_recent_records(
95        &self,
96        register_group_id: RegisterGroupId,
97        req: query::RecentRecordsRequest,
98    ) -> PluginResult<Vec<StoredRegisterRecord>> {
99        let (reply_tx, reply_rx) = reply_channel();
100        let query = Query::RecentRecords(reply_tx, register_group_id, req);
101        send_message_receive_result(query, &self.message_tx, reply_rx).await
102    }
103
104    pub async fn query_filter_records(
105        &self,
106        register_group_id: RegisterGroupId,
107        req: query::FilterRecordsRequest,
108    ) -> PluginResult<Vec<StoredRegisterRecord>> {
109        let (reply_tx, reply_rx) = reply_channel();
110        let query = Query::FilterRecords(reply_tx, register_group_id, req);
111        send_message_receive_result(query, &self.message_tx, reply_rx).await
112    }
113}