updated sync function

This commit is contained in:
sanjayk03-dev
2025-09-07 00:40:02 +05:30
parent e18b0089fa
commit b20ab8bdd6

View File

@@ -215,66 +215,40 @@ module.exports = {
syncStateToConfig: function() { syncStateToConfig: function() {
// Force sync all state values to motor config // Force sync all state values to motor config
// This ensures the UI reflects the current state even if changes happened while component was unmounted // This ensures the UI reflects the current state even if changes happened while component was unmounted
if(this.state == undefined) {
console.log("State is undefined");
return;
}
const motor_axes = ["X", "Y", "Z", "A", "B", "C"]; if (this.state[this.index + 'an'] != this.motor['axis']) {
const motor_axes = ["X", "Y", "Z", "A", "B", "C"];
// Define mapping between state properties and motor config properties this.motor['axis'] = motor_axes[this.state[this.index + 'an']];
const stateToMotorMapping = [ }
{ if (this.state[this.index + 'vm'] != this.motor['max-velocity']) {
stateKey: 'current_axis', this.motor['max-velocity'] = this.state[this.index + 'vm'];
motorKey: 'axis', }
transform: (value) => motor_axes[value] if (this.state[this.index + 'tm'] != this.motor['max-soft-limit']) {
}, this.motor['max-soft-limit'] = this.state[this.index + 'tm'];
{ }
stateKey: 'current_max_velocity', if (this.state[this.index + 'tn'] != this.motor['min-soft-limit']) {
motorKey: 'max-velocity' this.motor['min-soft-limit'] = this.state[this.index + 'tn'];
}, }
{ if (this.state[this.index + 'am'] != this.motor['max-accel']) {
stateKey: 'current_max_soft_limit', this.motor['max-accel'] = this.state[this.index + 'am'];
motorKey: 'max-soft-limit' }
}, if (this.state[this.index + 'jm'] != this.motor['max-jerk']) {
{ this.motor['max-jerk'] = this.state[this.index + 'jm'];
stateKey: 'current_min_soft_limit', }
motorKey: 'min-soft-limit' if (this.state[this.index + 'sa'] != this.motor['step-angle']) {
}, this.motor['step-angle'] = this.state[this.index + 'sa'];
{ }
stateKey: 'current_max_accel', if (this.state[this.index + 'tr'] != this.motor['travel-per-rev']) {
motorKey: 'max-accel' this.motor['travel-per-rev'] = this.state[this.index + 'tr'];
}, }
{ if (this.state[this.index + 'mi'] != this.motor['microsteps']) {
stateKey: 'current_max_jerk', this.motor['microsteps'] = this.state[this.index + 'mi'];
motorKey: 'max-jerk' }
},
{
stateKey: 'current_step_angle',
motorKey: 'step-angle'
},
{
stateKey: 'current_travel_per_rev',
motorKey: 'travel-per-rev'
},
{
stateKey: 'current_microsteps',
motorKey: 'microsteps'
}
];
// Sync all properties using the mapping
stateToMotorMapping.forEach(({ stateKey, motorKey, transform }) => {
const stateValue = this[stateKey];
if (stateValue === undefined) {
console.log(`State value for ${stateKey} is undefined`);
return; // Skip if state value is not defined
}
const transformedValue = transform ? transform(stateValue) : stateValue;
const currentMotorValue = this.motor[motorKey];
if (transformedValue !== currentMotorValue) {
this.motor[motorKey] = transformedValue;
}
});
} }
} }
}; };