ctb_io_webui/controllers/rpc/
center_of_gravity.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! JSON-RPC dispatcher for aircraft Center of Gravity and moment calculations.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use serde_json::Value;
30
31/// Dispatches a Center of Gravity RPC method call.
32pub async fn handle_center_of_gravity_call(
33    func: &str,
34    args: &[Value],
35) -> anyhow::Result<Value> {
36    use ctb_formats_math::center_of_gravity::{
37        CenterOfGravityInput, calculate_center_of_gravity, calculate_loaded_cg,
38        calculate_moment,
39    };
40
41    match func {
42        "calculateCenterOfGravity" => {
43            let input_val = args
44                .first()
45                .ok_or_else(|| anyhow::anyhow!("Missing arg 0: input"))?;
46
47            // Reason for fallback: optional weight, arm, and CG inputs default to 0.0 when omitted from payload.
48            let empty_weight = input_val
49                .get("empty_weight")
50                .and_then(Value::as_f64)
51                .unwrap_or(0.0);
52            // Reason for fallback: optional weight, arm, and CG inputs default to 0.0 when omitted from payload.
53            let empty_cg = input_val
54                .get("empty_cg")
55                .and_then(Value::as_f64)
56                .unwrap_or(0.0);
57            let datum = input_val.get("datum").and_then(Value::as_f64);
58            // Reason for fallback: optional weight, arm, and CG inputs default to 0.0 when omitted from payload.
59            let front_pilot_weight = input_val
60                .get("front_pilot_weight")
61                .and_then(Value::as_f64)
62                .unwrap_or(0.0);
63            let front_pilot_arm =
64                input_val.get("front_pilot_arm").and_then(Value::as_f64);
65            let front_pilot_distance_in_front_of_datum = input_val
66                .get("front_pilot_distance_in_front_of_datum")
67                .and_then(Value::as_f64);
68            // Reason for fallback: optional weight, arm, and CG inputs default to 0.0 when omitted from payload.
69            let rear_pilot_weight = input_val
70                .get("rear_pilot_weight")
71                .and_then(Value::as_f64)
72                .unwrap_or(0.0);
73            let rear_pilot_arm =
74                input_val.get("rear_pilot_arm").and_then(Value::as_f64);
75            let rear_pilot_distance_behind_datum = input_val
76                .get("rear_pilot_distance_behind_datum")
77                .and_then(Value::as_f64);
78            // Reason for fallback: optional weight, arm, and CG inputs default to 0.0 when omitted from payload.
79            let ballast_weight = input_val
80                .get("ballast_weight")
81                .and_then(Value::as_f64)
82                .unwrap_or(0.0);
83            // Reason for fallback: optional weight, arm, and CG inputs default to 0.0 when omitted from payload.
84            let ballast_arm = input_val
85                .get("ballast_arm")
86                .and_then(Value::as_f64)
87                .unwrap_or(0.0);
88
89            let input = CenterOfGravityInput {
90                empty_weight,
91                empty_cg,
92                datum,
93                front_pilot_weight,
94                front_pilot_arm,
95                front_pilot_distance_in_front_of_datum,
96                rear_pilot_weight,
97                rear_pilot_arm,
98                rear_pilot_distance_behind_datum,
99                ballast_weight,
100                ballast_arm,
101            };
102
103            let res = calculate_center_of_gravity(&input)?;
104            Ok(serde_json::json!({
105                "empty_moment": res.empty_moment,
106                "front_pilot_arm": res.front_pilot_arm,
107                "front_pilot_moment": res.front_pilot_moment,
108                "rear_pilot_arm": res.rear_pilot_arm,
109                "rear_pilot_moment": res.rear_pilot_moment,
110                "ballast_moment": res.ballast_moment,
111                "total_moment": res.total_moment,
112                "total_weight": res.total_weight,
113                "loaded_cg": res.loaded_cg
114            }))
115        }
116        "calculateMoment" => {
117            let weight = args
118                .first()
119                .and_then(Value::as_f64)
120                .ok_or_else(|| anyhow::anyhow!("Missing arg 0: weight"))?;
121            let arm = args
122                .get(1)
123                .and_then(Value::as_f64)
124                .ok_or_else(|| anyhow::anyhow!("Missing arg 1: arm"))?;
125            Ok(serde_json::to_value(calculate_moment(weight, arm))?)
126        }
127        "calculateLoadedCg" => {
128            let total_moment =
129                args.first().and_then(Value::as_f64).ok_or_else(|| {
130                    anyhow::anyhow!("Missing arg 0: total_moment")
131                })?;
132            let total_weight =
133                args.get(1).and_then(Value::as_f64).ok_or_else(|| {
134                    anyhow::anyhow!("Missing arg 1: total_weight")
135                })?;
136            Ok(serde_json::to_value(calculate_loaded_cg(
137                total_moment,
138                total_weight,
139            )?)?)
140        }
141        _ => anyhow::bail!("Unknown center of gravity function: {func}"),
142    }
143}