modified config struct
This commit is contained in:
@@ -78,11 +78,32 @@ class Config(object):
|
|||||||
|
|
||||||
def get(self, name, default = None):
|
def get(self, name, default = None):
|
||||||
return self.values.get(name, default)
|
return self.values.get(name, default)
|
||||||
|
|
||||||
|
def set(self, name, default = None):
|
||||||
|
config = self.ctrl.config
|
||||||
|
path = self.ctrl.get_path('config.json')
|
||||||
|
|
||||||
# def set(self, name, default = None):
|
try:
|
||||||
# self.log.info(f'name:{name} default:{default}')
|
if os.path.exists(path):
|
||||||
# self.log.info(f'config: {repr(self)}')
|
with open(path, 'r') as f: config_data = json.load(f)
|
||||||
|
else: config_data = {'version': self.version}
|
||||||
|
|
||||||
|
if name in config_data:
|
||||||
|
existing_value = config_data[name]
|
||||||
|
if isinstance(existing_value, dict) and isinstance(default, dict):
|
||||||
|
config_data[name] = {**existing_value, **default}
|
||||||
|
elif isinstance(existing_value, list) and isinstance(default, list):
|
||||||
|
config_data[name].extend(default)
|
||||||
|
elif isinstance(existing_value, list):
|
||||||
|
config_data[name].append(default)
|
||||||
|
else:
|
||||||
|
config_data[name] = default
|
||||||
|
else:
|
||||||
|
config_data[name] = default
|
||||||
|
|
||||||
|
config.save(config_data)
|
||||||
|
self.log.info('name:{} default:{}'.format(name, default))
|
||||||
|
except Exception: self.log.exception('Internal error: Failed to upgrade config')
|
||||||
|
|
||||||
def save(self, config):
|
def save(self, config):
|
||||||
self._upgrade(config)
|
self._upgrade(config)
|
||||||
|
|||||||
@@ -355,26 +355,10 @@ class Mach(Comm):
|
|||||||
def optional_pause(self, enable = True):
|
def optional_pause(self, enable = True):
|
||||||
self.ctrl.state.set('optional_pause', enable)
|
self.ctrl.state.set('optional_pause', enable)
|
||||||
|
|
||||||
def save_config(self, axis, target):
|
|
||||||
config = self.ctrl.config
|
|
||||||
path = self.ctrl.get_path('config.json')
|
|
||||||
|
|
||||||
try:
|
|
||||||
if os.path.exists(path):
|
|
||||||
with open(path, 'r') as f: config_data = json.load(f)
|
|
||||||
else: config_data = {'version': self.version}
|
|
||||||
|
|
||||||
axes = config_data.setdefault('axes',{})
|
|
||||||
axes[axis]['abs'] = target
|
|
||||||
config.save(config_data)
|
|
||||||
self.mlog.info(f'done with config: {config_data}')
|
|
||||||
except Exception: self.log.exception('Internal error: Failed to upgrade config')
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def set_position(self, axis, position):
|
def set_position(self, axis, position):
|
||||||
axis = axis.lower()
|
axis = axis.lower()
|
||||||
state = self.ctrl.state
|
state = self.ctrl.state
|
||||||
|
config = self.ctrl.config
|
||||||
|
|
||||||
if state.is_axis_homed(axis):
|
if state.is_axis_homed(axis):
|
||||||
# If homed, change the offset rather than the absolute position
|
# If homed, change the offset rather than the absolute position
|
||||||
@@ -390,7 +374,9 @@ class Mach(Comm):
|
|||||||
self.mlog.info('target ' + str(target))
|
self.mlog.info('target ' + str(target))
|
||||||
self.mlog.info('state.get ' + str(state.get('offset_' + axis)))
|
self.mlog.info('state.get ' + str(state.get('offset_' + axis)))
|
||||||
state.set(axis + 'p', target)
|
state.set(axis + 'p', target)
|
||||||
self.save_config(axis, target)
|
axes = config.values.setdefault('axes',{})
|
||||||
|
axes[axis + 'p'] = target
|
||||||
|
config.set('axes', axes)
|
||||||
super().queue_command(Cmd.set_axis(axis, target))
|
super().queue_command(Cmd.set_axis(axis, target))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -654,35 +654,29 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
"axes": {
|
"axes": {
|
||||||
"x": {
|
"xp": {
|
||||||
"abs": {
|
"type": "int",
|
||||||
"type": "string",
|
"default": 0
|
||||||
"default": "0"
|
|
||||||
},
|
|
||||||
"off": {
|
|
||||||
"type": "string",
|
|
||||||
"default": "0"
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"y": {
|
"yp": {
|
||||||
"abs": {
|
"type": "int",
|
||||||
"type": "string",
|
"default": 0
|
||||||
"default": "0"
|
|
||||||
},
|
|
||||||
"off": {
|
|
||||||
"type": "string",
|
|
||||||
"default": "0"
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"z": {
|
"zp": {
|
||||||
"abs": {
|
"type": "int",
|
||||||
"type": "string",
|
"default": 0
|
||||||
"default": "0"
|
},
|
||||||
},
|
"offset_x": {
|
||||||
"off": {
|
"type": "int",
|
||||||
"type": "string",
|
"default": 0
|
||||||
"default": "0"
|
},
|
||||||
}
|
"offset_y": {
|
||||||
|
"type": "int",
|
||||||
|
"default": 0
|
||||||
|
},
|
||||||
|
"offset_z": {
|
||||||
|
"type": "int",
|
||||||
|
"default": 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user