kos_stub/
actuator.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use async_trait::async_trait;
use eyre::Result;
use kos_core::google_proto::longrunning::Operation;
use kos_core::services::OperationsServiceImpl;
use kos_core::{
    hal::{
        ActionResponse, Actuator, ActuatorCommand, CalibrateActuatorMetadata,
        CalibrateActuatorRequest, CalibrationStatus,
    },
    kos_proto::{actuator::*, common::ActionResult},
};
use std::sync::mpsc::{channel, Sender};
use std::sync::Arc;
use std::thread;
use tokio::runtime::Runtime;
use tokio::time::Duration;
use tracing::debug;

pub struct StubActuator {
    operations: Arc<OperationsServiceImpl>,
    calibration_tx: Sender<u32>,
}

impl StubActuator {
    pub fn new(operations: Arc<OperationsServiceImpl>) -> Self {
        let (tx, rx) = channel::<u32>();

        // Spawn the calibration thread
        let operations_clone = operations.clone();
        thread::spawn(move || {
            // Create a new runtime for this thread
            let rt = Runtime::new().expect("Failed to create runtime");

            loop {
                // Wait for actuator IDs to calibrate
                if let Ok(actuator_id) = rx.recv() {
                    let ops = operations_clone.clone();
                    debug!("Calibrating actuator ID: {}", actuator_id);

                    // Sleep for 15 seconds to simulate calibration
                    thread::sleep(Duration::from_secs(15));
                    debug!("Calibrated actuator ID: {}", actuator_id);

                    // Update the operation status
                    let operation_name = format!("operations/calibrate_actuator/{:?}", actuator_id);
                    debug!("Updating operation status for: {}", operation_name);

                    let metadata = CalibrateActuatorMetadata {
                        actuator_id,
                        status: CalibrationStatus::Calibrated.to_string(),
                    };

                    if let Err(e) =
                        rt.block_on(ops.update_metadata(&operation_name, metadata, true))
                    {
                        debug!("Failed to update calibration status: {}", e);
                    }

                    debug!("Updated operation status for: {}", operation_name);
                }
            }
        });

        StubActuator {
            operations,
            calibration_tx: tx,
        }
    }
}

#[async_trait]
impl Actuator for StubActuator {
    async fn command_actuators(
        &self,
        _commands: Vec<ActuatorCommand>,
    ) -> Result<Vec<ActionResult>> {
        Ok(vec![])
    }

    async fn configure_actuator(
        &self,
        _config: ConfigureActuatorRequest,
    ) -> Result<ActionResponse> {
        Ok(ActionResponse {
            success: true,
            error: None,
        })
    }

    async fn calibrate_actuator(&self, request: CalibrateActuatorRequest) -> Result<Operation> {
        let metadata = CalibrateActuatorMetadata {
            actuator_id: request.actuator_id,
            status: CalibrationStatus::Calibrating.to_string(),
        };

        let name = format!("operations/calibrate_actuator/{:?}", request.actuator_id);
        let operation = self
            .operations
            .create(
                name,
                metadata,
                "type.googleapis.com/kos.actuator.CalibrateActuatorMetadata",
            )
            .await
            .map_err(|e| eyre::eyre!("Failed to create operation: {}", e))?;

        // Send actuator ID to calibration thread
        self.calibration_tx
            .send(request.actuator_id)
            .map_err(|e| eyre::eyre!("Failed to start calibration: {}", e))?;

        Ok(operation)
    }

    async fn get_actuators_state(
        &self,
        _actuator_ids: Vec<u32>,
    ) -> Result<Vec<ActuatorStateResponse>> {
        Ok(vec![ActuatorStateResponse {
            actuator_id: 1,
            online: true,
            position: Some(0.0),
            velocity: Some(0.0),
            torque: Some(0.0),
            temperature: Some(0.0),
            voltage: Some(0.0),
            current: Some(0.0),
        }])
    }
}