diff --git a/control-core-derive/src/lib.rs b/control-core-derive/src/lib.rs index fd776e3d..ec2929e2 100644 --- a/control-core-derive/src/lib.rs +++ b/control-core-derive/src/lib.rs @@ -40,10 +40,15 @@ fn machine_derive2(item: TokenStream2) -> Result { let ast: DeriveInput = syn::parse2(item)?; let ident = &ast.ident; + let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl(); let expanded = quote! { - impl #impl_generics control_core::machines::Machine for #ident #ty_generics #where_clause {} + impl #impl_generics control_core::machines::Machine for #ident #ty_generics #where_clause { + fn get_machine_identification_unique(&self) -> control_core::machines::identification::MachineIdentificationUnique { + self.machine_identification_unique.clone() + } + } impl #impl_generics control_core::machines::AnyGetters for #ident #ty_generics #where_clause { fn as_any(&self) -> &dyn std::any::Any { diff --git a/control-core/src/machines/mod.rs b/control-core/src/machines/mod.rs index 87d53ad5..0e6c8759 100644 --- a/control-core/src/machines/mod.rs +++ b/control-core/src/machines/mod.rs @@ -16,3 +16,8 @@ pub mod registry; pub trait Machine: MachineAct + MachineNewTrait + MachineApi + Any + Debug + Send + Sync { fn get_machine_identification_unique(&self) -> MachineIdentificationUnique; } + +pub trait AnyGetters: Any { + fn as_any(&self) -> &dyn Any; + fn as_any_mut(&mut self) -> &mut dyn Any; +} diff --git a/server/src/machines/aquapath1/mod.rs b/server/src/machines/aquapath1/mod.rs index e98828fc..1195142f 100644 --- a/server/src/machines/aquapath1/mod.rs +++ b/server/src/machines/aquapath1/mod.rs @@ -1,11 +1,9 @@ use control_core::{ - machines::{ - Machine, - identification::{MachineIdentification, MachineIdentificationUnique}, - }, + machines::identification::{MachineIdentification, MachineIdentificationUnique}, socketio::namespace::NamespaceCacheingLogic, }; +use control_core_derive::Machine; use serde::{Deserialize, Serialize}; use std::time::Instant; use uom::si::{ @@ -80,7 +78,7 @@ impl Default for Flow { } } -#[derive(Debug)] +#[derive(Debug, Machine)] pub struct AquaPathV1 { machine_identification_unique: MachineIdentificationUnique, namespace: AquaPathV1Namespace, @@ -89,6 +87,7 @@ pub struct AquaPathV1 { front_controller: Controller, back_controller: Controller, } + impl AquaPathV1 { pub const MACHINE_IDENTIFICATION: MachineIdentification = MachineIdentification { vendor: VENDOR_QITECH, @@ -96,12 +95,6 @@ impl AquaPathV1 { }; } -impl Machine for AquaPathV1 { - fn get_machine_identification_unique(&self) -> MachineIdentificationUnique { - self.machine_identification_unique.clone() - } -} - impl std::fmt::Display for AquaPathV1 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Aquapath") diff --git a/server/src/machines/buffer1/mod.rs b/server/src/machines/buffer1/mod.rs index 966f335a..a96b5d88 100644 --- a/server/src/machines/buffer1/mod.rs +++ b/server/src/machines/buffer1/mod.rs @@ -8,19 +8,19 @@ use buffer_tower_controller::BufferTowerController; use control_core::machines::connection::{CrossConnectableMachine, MachineCrossConnection}; use control_core::{ machines::{ - Machine, identification::{MachineIdentification, MachineIdentificationUnique}, manager::MachineManager, }, socketio::namespace::NamespaceCacheingLogic, }; +use control_core_derive::Machine; use serde::{Deserialize, Serialize}; use smol::lock::RwLock; use std::{sync::Weak, time::Instant}; use crate::machines::{MACHINE_BUFFER_V1, VENDOR_QITECH, winder2::Winder2}; -#[derive(Debug)] +#[derive(Debug, Machine)] pub struct BufferV1 { // controllers pub buffer_tower_controller: BufferTowerController, @@ -40,12 +40,6 @@ pub struct BufferV1 { mode: BufferV1Mode, } -impl Machine for BufferV1 { - fn get_machine_identification_unique(&self) -> MachineIdentificationUnique { - self.machine_identification_unique.clone() - } -} - impl CrossConnectableMachine for BufferV1 { fn get_cross_connection(&mut self) -> &mut MachineCrossConnection { &mut self.connected_winder diff --git a/server/src/machines/extruder1/api.rs b/server/src/machines/extruder1/api.rs index 21cf094f..db05f7ec 100644 --- a/server/src/machines/extruder1/api.rs +++ b/server/src/machines/extruder1/api.rs @@ -3,6 +3,7 @@ use super::{ExtruderV2Mode, mitsubishi_cs80::MotorStatus}; #[cfg(not(feature = "mock-machine"))] use super::ExtruderV2; +#[cfg(not(feature = "mock-machine"))] use crate::machines::extruder1::HeatingType; #[cfg(not(feature = "mock-machine"))] diff --git a/server/src/machines/extruder1/emit.rs b/server/src/machines/extruder1/emit.rs index 37a5e794..76d791fe 100644 --- a/server/src/machines/extruder1/emit.rs +++ b/server/src/machines/extruder1/emit.rs @@ -8,13 +8,20 @@ use crate::machines::extruder1::{ RotationState, ScrewState, StateEvent, }, }; +#[cfg(not(feature = "mock-machine"))] use control_core::helpers::hasher_serializer::hash_with_serde_model; +#[cfg(not(feature = "mock-machine"))] use control_core::socketio::event::BuildEvent; +#[cfg(not(feature = "mock-machine"))] use control_core::socketio::namespace::NamespaceCacheingLogic; +#[cfg(not(feature = "mock-machine"))] use uom::si::{angular_velocity::revolution_per_minute, thermodynamic_temperature::degree_celsius}; +#[cfg(not(feature = "mock-machine"))] use uom::si::angular_velocity::AngularVelocity; +#[cfg(not(feature = "mock-machine"))] use uom::si::pressure::{Pressure, bar}; +#[cfg(not(feature = "mock-machine"))] use uom::si::thermodynamic_temperature::ThermodynamicTemperature; #[cfg(not(feature = "mock-machine"))] diff --git a/server/src/machines/extruder1/mock/mod.rs b/server/src/machines/extruder1/mock/mod.rs index 3d48c212..1f325877 100644 --- a/server/src/machines/extruder1/mock/mod.rs +++ b/server/src/machines/extruder1/mock/mod.rs @@ -1,10 +1,10 @@ #[cfg(feature = "mock-machine")] -use control_core::machines::Machine; -#[cfg(feature = "mock-machine")] use control_core::machines::identification::MachineIdentification; #[cfg(feature = "mock-machine")] use control_core::machines::identification::MachineIdentificationUnique; #[cfg(feature = "mock-machine")] +use control_core_derive::Machine; +#[cfg(feature = "mock-machine")] use std::time::Instant; #[cfg(feature = "mock-machine")] @@ -31,7 +31,7 @@ pub mod mock_emit; pub mod new; #[cfg(feature = "mock-machine")] -#[derive(Debug)] +#[derive(Debug, Machine)] pub struct ExtruderV2 { machine_identification_unique: MachineIdentificationUnique, namespace: ExtruderV2Namespace, @@ -104,10 +104,3 @@ impl ExtruderV2 { machine: MACHINE_EXTRUDER_V1, }; } - -#[cfg(feature = "mock-machine")] -impl Machine for ExtruderV2 { - fn get_machine_identification_unique(&self) -> MachineIdentificationUnique { - self.machine_identification_unique.clone() - } -} diff --git a/server/src/machines/extruder1/mod.rs b/server/src/machines/extruder1/mod.rs index 097c8c2a..1570d4af 100644 --- a/server/src/machines/extruder1/mod.rs +++ b/server/src/machines/extruder1/mod.rs @@ -2,11 +2,14 @@ use std::time::Instant; #[cfg(not(feature = "mock-machine"))] -use control_core::machines::{ - Machine, - identification::{MachineIdentification, MachineIdentificationUnique}, -}; +use control_core::machines::identification::{MachineIdentification, MachineIdentificationUnique}; +#[cfg(not(feature = "mock-machine"))] +use control_core_derive::Machine; use serde::{Deserialize, Serialize}; +#[cfg(not(feature = "mock-machine"))] +use uom::si::electric_current::ampere; +#[cfg(not(feature = "mock-machine"))] +use uom::si::electric_potential::volt; use uom::si::{f64::ThermodynamicTemperature, thermodynamic_temperature::degree_celsius}; #[cfg(not(feature = "mock-machine"))] @@ -17,6 +20,7 @@ use crate::machines::{ temperature_controller::TemperatureController, }, }; + pub mod act; pub mod api; pub mod emit; @@ -25,8 +29,6 @@ pub mod mock; pub mod new; pub mod screw_speed_controller; pub mod temperature_controller; -use uom::si::electric_current::ampere; -use uom::si::electric_potential::volt; #[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)] pub enum ExtruderV2Mode { @@ -62,9 +64,9 @@ pub enum HeatingType { } #[cfg(not(feature = "mock-machine"))] -#[derive(Debug)] +#[derive(Debug, Machine)] pub struct ExtruderV2 { - machine_identificttion_unique: MachineIdentificationUnique, + machine_identification_unique: MachineIdentificationUnique, namespace: ExtruderV2Namespace, last_measurement_emit: Instant, last_status_hash: Option, @@ -84,13 +86,6 @@ pub struct ExtruderV2 { emitted_default_state: bool, } -#[cfg(not(feature = "mock-machine"))] -impl Machine for ExtruderV2 { - fn get_machine_identification_unique(&self) -> MachineIdentificationUnique { - self.machine_identificttion_unique.clone() - } -} - #[cfg(not(feature = "mock-machine"))] impl std::fmt::Display for ExtruderV2 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { diff --git a/server/src/machines/extruder1/new.rs b/server/src/machines/extruder1/new.rs index 4b251422..10c011d4 100644 --- a/server/src/machines/extruder1/new.rs +++ b/server/src/machines/extruder1/new.rs @@ -1,30 +1,43 @@ +#[cfg(not(feature = "mock-machine"))] use crate::machines::get_ethercat_device; +#[cfg(not(feature = "mock-machine"))] use anyhow::Error; +#[cfg(not(feature = "mock-machine"))] use control_core::machines::new::MachineNewHardware; #[cfg(not(feature = "mock-machine"))] use control_core::machines::new::{MachineNewParams, MachineNewTrait}; +#[cfg(not(feature = "mock-machine"))] use control_core::machines::new::{ validate_no_role_dublicates, validate_same_machine_identification_unique, }; +#[cfg(not(feature = "mock-machine"))] use ethercat_hal::coe::ConfigurableDevice; -use ethercat_hal::devices::EthercatDeviceUsed; -use ethercat_hal::devices::ek1100::EK1100; -use ethercat_hal::devices::ek1100::EK1100_IDENTITY_A; -use ethercat_hal::devices::el1002::EL1002; -use ethercat_hal::devices::el1002::EL1002_IDENTITY_A; -use ethercat_hal::devices::el6021::EL6021; -use ethercat_hal::devices::el6021::EL6021Configuration; -use ethercat_hal::devices::el6021::EL6021Port; -use ethercat_hal::devices::el6021::{ - EL6021_IDENTITY_A, EL6021_IDENTITY_B, EL6021_IDENTITY_C, EL6021_IDENTITY_D, +#[cfg(not(feature = "mock-machine"))] +use ethercat_hal::devices::{ + EthercatDeviceUsed, + ek1100::EK1100, + ek1100::EK1100_IDENTITY_A, + el1002::EL1002, + el1002::EL1002_IDENTITY_A, + el6021::EL6021, + el6021::EL6021Configuration, + el6021::EL6021Port, + el6021::{EL6021_IDENTITY_A, EL6021_IDENTITY_B, EL6021_IDENTITY_C, EL6021_IDENTITY_D}, }; +#[cfg(not(feature = "mock-machine"))] use std::time::Duration; +#[cfg(not(feature = "mock-machine"))] use std::time::Instant; +#[cfg(not(feature = "mock-machine"))] use uom::si::angular_velocity::AngularVelocity; +#[cfg(not(feature = "mock-machine"))] use uom::si::angular_velocity::revolution_per_minute; +#[cfg(not(feature = "mock-machine"))] use uom::si::pressure::Pressure; +#[cfg(not(feature = "mock-machine"))] use uom::si::pressure::bar; +#[cfg(not(feature = "mock-machine"))] use ethercat_hal::{ devices::{ el2004::{EL2004, EL2004_IDENTITY_A, EL2004Port}, @@ -36,8 +49,10 @@ use ethercat_hal::{ serial_interface::SerialInterface, temperature_input::TemperatureInput, }, }; +#[cfg(not(feature = "mock-machine"))] use uom::si::thermodynamic_temperature::{ThermodynamicTemperature, degree_celsius}; +#[cfg(not(feature = "mock-machine"))] use crate::machines::extruder1::temperature_controller::TemperatureController; #[cfg(not(feature = "mock-machine"))] @@ -207,7 +222,7 @@ impl MachineNewTrait for ExtruderV2 { ScrewSpeedController::new(inverter, target_pressure, target_rpm, pressure_sensor); let mut extruder: ExtruderV2 = Self { - machine_identificttion_unique: params.get_machine_identification_unique(), + machine_identification_unique: params.get_machine_identification_unique(), namespace: ExtruderV2Namespace { namespace: params.namespace.clone(), }, diff --git a/server/src/machines/laser/mod.rs b/server/src/machines/laser/mod.rs index 258dbf59..be2419f1 100644 --- a/server/src/machines/laser/mod.rs +++ b/server/src/machines/laser/mod.rs @@ -4,12 +4,10 @@ use crate::{ }; use api::{LaserEvents, LaserMachineNamespace, LaserState, LiveValuesEvent, StateEvent}; use control_core::{ - machines::{ - Machine, - identification::{MachineIdentification, MachineIdentificationUnique}, - }, + machines::identification::{MachineIdentification, MachineIdentificationUnique}, socketio::namespace::NamespaceCacheingLogic, }; +use control_core_derive::Machine; use smol::lock::RwLock; use std::{sync::Arc, time::Instant}; use uom::si::{f64::Length, length::millimeter}; @@ -18,7 +16,7 @@ pub mod act; pub mod api; pub mod new; -#[derive(Debug)] +#[derive(Debug, Machine)] pub struct LaserMachine { machine_identification_unique: MachineIdentificationUnique, @@ -43,12 +41,6 @@ pub struct LaserMachine { emitted_default_state: bool, } -impl Machine for LaserMachine { - fn get_machine_identification_unique(&self) -> MachineIdentificationUnique { - self.machine_identification_unique.clone() - } -} - impl LaserMachine { pub const MACHINE_IDENTIFICATION: MachineIdentification = MachineIdentification { vendor: VENDOR_QITECH, diff --git a/server/src/machines/mock/mod.rs b/server/src/machines/mock/mod.rs index 18afe158..f9b14bd6 100644 --- a/server/src/machines/mock/mod.rs +++ b/server/src/machines/mock/mod.rs @@ -1,11 +1,11 @@ use crate::machines::{MACHINE_MOCK, VENDOR_QITECH}; use api::{LiveValuesEvent, MockEvents, MockMachineNamespace, Mode, ModeState, StateEvent}; -use control_core::machines::Machine; use control_core::machines::identification::MachineIdentificationUnique; use control_core::socketio::event::BuildEvent; use control_core::{ machines::identification::MachineIdentification, socketio::namespace::NamespaceCacheingLogic, }; +use control_core_derive::Machine; use std::time::Instant; use tracing::info; @@ -18,9 +18,9 @@ pub mod act; pub mod api; pub mod new; -#[derive(Debug)] +#[derive(Debug, Machine)] pub struct MockMachine { - machine_identifaction_unique: MachineIdentificationUnique, + machine_identification_unique: MachineIdentificationUnique, // socketio namespace: MockMachineNamespace, @@ -41,12 +41,6 @@ pub struct MockMachine { emitted_default_state: bool, } -impl Machine for MockMachine { - fn get_machine_identification_unique(&self) -> MachineIdentificationUnique { - self.machine_identifaction_unique.clone() - } -} - impl MockMachine { pub const MACHINE_IDENTIFICATION: MachineIdentification = MachineIdentification { machine: MACHINE_MOCK, diff --git a/server/src/machines/mock/new.rs b/server/src/machines/mock/new.rs index 4a4b0f76..46fc000d 100644 --- a/server/src/machines/mock/new.rs +++ b/server/src/machines/mock/new.rs @@ -5,10 +5,7 @@ use super::{ api::{MockMachineNamespace, Mode}, }; use anyhow::Error; -use control_core::machines::{ - identification::MachineIdentificationUnique, - new::{MachineNewHardware, MachineNewParams, MachineNewTrait}, -}; +use control_core::machines::new::{MachineNewHardware, MachineNewParams, MachineNewTrait}; use uom::si::{f64::Frequency, frequency::hertz}; impl MachineNewTrait for MockMachine { @@ -32,15 +29,10 @@ impl MachineNewTrait for MockMachine { } } - let machine_identifaction_unique: MachineIdentificationUnique = params.device_group[0] - .device_machine_identification - .machine_identification_unique - .clone(); - let now = Instant::now(); let mut mock_machine = Self { - machine_identifaction_unique: machine_identifaction_unique, + machine_identification_unique: params.get_machine_identification_unique(), namespace: MockMachineNamespace { namespace: params.namespace.clone(), }, diff --git a/server/src/machines/winder2/mod.rs b/server/src/machines/winder2/mod.rs index 0478742d..ac0789e1 100644 --- a/server/src/machines/winder2/mod.rs +++ b/server/src/machines/winder2/mod.rs @@ -21,7 +21,6 @@ use control_core::socketio::event::BuildEvent; use control_core::{ converters::angular_step_converter::AngularStepConverter, machines::{ - Machine, connection::{CrossConnectableMachine, MachineCrossConnection}, identification::{MachineIdentification, MachineIdentificationUnique}, manager::MachineManager, @@ -29,6 +28,7 @@ use control_core::{ socketio::namespace::NamespaceCacheingLogic, uom_extensions::velocity::meter_per_minute, }; +use control_core_derive::Machine; use ethercat_hal::io::{ digital_input::DigitalInput, digital_output::DigitalOutput, stepper_velocity_el70x1::StepperVelocityEL70x1, @@ -59,7 +59,7 @@ pub struct SpoolAutomaticAction { pub mode: SpoolAutomaticActionMode, } -#[derive(Debug)] +#[derive(Debug, Machine)] pub struct Winder2 { // drivers pub traverse: StepperVelocityEL70x1, @@ -104,12 +104,6 @@ pub struct Winder2 { emitted_default_state: bool, } -impl Machine for Winder2 { - fn get_machine_identification_unique(&self) -> MachineIdentificationUnique { - self.machine_identification_unique.clone() - } -} - impl CrossConnectableMachine for Winder2 { fn get_cross_connection(&mut self) -> &mut MachineCrossConnection { &mut self.connected_buffer diff --git a/server/src/mock/init.rs b/server/src/mock/init.rs index 5fc3626b..6ff6f5d8 100644 --- a/server/src/mock/init.rs +++ b/server/src/mock/init.rs @@ -25,7 +25,7 @@ pub fn init_mock(app_state: Arc) -> Result<(), anyhow::Error> { }; // Create the mock serial device - match MockSerialDevice::new_serial(&serial_params) { + let _ = match MockSerialDevice::new_serial(&serial_params) { Ok((device_identification, mock_serial_device)) => { // Add the mock device to the machine manager { @@ -47,7 +47,7 @@ pub fn init_mock(app_state: Arc) -> Result<(), anyhow::Error> { .write() .await .main_namespace; - let event = MachinesEventBuilder().build(app_state_event.clone()).await; + let event = MachinesEventBuilder().build(app_state_event.clone()); main_namespace.emit(MainNamespaceEvents::MachinesEvent(event)); Ok::<(), anyhow::Error>(()) } @@ -79,7 +79,7 @@ pub fn init_mock(app_state: Arc) -> Result<(), anyhow::Error> { .write() .await .main_namespace; - let event = MachinesEventBuilder().build(app_state_event.clone()).await; + let event = MachinesEventBuilder().build(app_state_event.clone()); main_namespace.emit(MainNamespaceEvents::MachinesEvent(event)); Ok(()) }