forked from cncjs/cncjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Machine Profile (cncjs#426)
* Add support for Machine Profile * Replace deprecated lifecycle method "componentWillReceiveProps" with "componentDidUpdate" * [Axes widget] Passing only necessary props to child components * dep: socket.io@~2.2.0 * Bump to 1.9.17 (cncjs#427) * Change "Home" to "View In Browser" * Update dependencies - [email protected] - [email protected] * Electron 4: Replace deprecated `app.makeSingleInstance` with `app.requestSingleInstanceLock` * Bump to 1.9.17 * Change AppImage filename * Render box and edges in transparent * Add WCS support for machine limit * Change "Machines" to "Machine Profiles" * Change "Machines" to "Machine Profiles" * Fixed a bug that would set wrong machine limits when WCS is not zero * Rename TargetPoint to Cutter * Add SectionGroup and SectionTitle components * Add fluid prop to FlexContainer * Disable overlay click to prevent accidentally closing modal window * Update menu item position * Make FormGroup/SectionGroup/SectionTitle independent * react-final-form@~3.7.0 * eslint fixes * Use React Final Form to create form * Add FormControl component * Refactor the code configuring machine travel limits * No enabled state * Fix incorrect import path * Change section names and routing paths * Update FormGroup component * Update FormControl component * Machine limits improvement * Render limits with a dashed cuboid * Remove visualizer instance when a G-code program is going to unload * Explicitly specifies the boolean value of the disableOverlay prop on Modal components * fix incorrect comment * Update resource.json * Update resource.json
- Loading branch information
Showing
90 changed files
with
2,706 additions
and
770 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
import _get from 'lodash/get'; | ||
import _set from 'lodash/set'; | ||
import _find from 'lodash/find'; | ||
import _castArray from 'lodash/castArray'; | ||
import _isPlainObject from 'lodash/isPlainObject'; | ||
import uuid from 'uuid'; | ||
import settings from '../config/settings'; | ||
import { ensureNumber, ensureString } from '../lib/ensure-type'; | ||
import logger from '../lib/logger'; | ||
import config from '../services/configstore'; | ||
import { getPagingRange } from './paging'; | ||
import { | ||
ERR_BAD_REQUEST, | ||
ERR_NOT_FOUND, | ||
ERR_INTERNAL_SERVER_ERROR | ||
} from '../constants'; | ||
|
||
const log = logger('api:machines'); | ||
const CONFIG_KEY = 'machines'; | ||
|
||
const getSanitizedRecords = () => { | ||
const records = _castArray(config.get(CONFIG_KEY, [])); | ||
|
||
let shouldUpdate = false; | ||
for (let i = 0; i < records.length; ++i) { | ||
if (!_isPlainObject(records[i])) { | ||
records[i] = {}; | ||
} | ||
|
||
const record = records[i]; | ||
|
||
if (!record.id) { | ||
record.id = uuid.v4(); | ||
shouldUpdate = true; | ||
} | ||
} | ||
|
||
if (shouldUpdate) { | ||
log.debug(`update sanitized records: ${JSON.stringify(records)}`); | ||
|
||
// Pass `{ silent changes }` will suppress the change event | ||
config.set(CONFIG_KEY, records, { silent: true }); | ||
} | ||
|
||
return records; | ||
}; | ||
|
||
const ensureMachineProfile = (payload) => { | ||
const { id, name, limits } = { ...payload }; | ||
const { xmin = 0, xmax = 0, ymin = 0, ymax = 0, zmin = 0, zmax = 0 } = { ...limits }; | ||
|
||
return { | ||
id, | ||
name: ensureString(name), | ||
limits: { | ||
xmin: ensureNumber(xmin) || 0, | ||
xmax: ensureNumber(xmax) || 0, | ||
ymin: ensureNumber(ymin) || 0, | ||
ymax: ensureNumber(ymax) || 0, | ||
zmin: ensureNumber(zmin) || 0, | ||
zmax: ensureNumber(zmax) || 0, | ||
} | ||
}; | ||
}; | ||
|
||
export const fetch = (req, res) => { | ||
const records = getSanitizedRecords(); | ||
const paging = !!req.query.paging; | ||
|
||
if (paging) { | ||
const { page = 1, pageLength = 10 } = req.query; | ||
const totalRecords = records.length; | ||
const [begin, end] = getPagingRange({ page, pageLength, totalRecords }); | ||
const pagedRecords = records.slice(begin, end); | ||
|
||
res.send({ | ||
pagination: { | ||
page: Number(page), | ||
pageLength: Number(pageLength), | ||
totalRecords: Number(totalRecords) | ||
}, | ||
records: pagedRecords.map(record => ensureMachineProfile(record)) | ||
}); | ||
} else { | ||
res.send({ | ||
records: records.map(record => ensureMachineProfile(record)) | ||
}); | ||
} | ||
}; | ||
|
||
export const create = (req, res) => { | ||
const record = { ...req.body }; | ||
|
||
if (!record.name) { | ||
res.status(ERR_BAD_REQUEST).send({ | ||
msg: 'The "name" parameter must not be empty' | ||
}); | ||
return; | ||
} | ||
|
||
try { | ||
const records = getSanitizedRecords(); | ||
records.push(ensureMachineProfile(record)); | ||
config.set(CONFIG_KEY, records); | ||
|
||
res.send({ id: record.id }); | ||
} catch (err) { | ||
res.status(ERR_INTERNAL_SERVER_ERROR).send({ | ||
msg: 'Failed to save ' + JSON.stringify(settings.rcfile) | ||
}); | ||
} | ||
}; | ||
|
||
export const read = (req, res) => { | ||
const id = req.params.id; | ||
const records = getSanitizedRecords(); | ||
const record = _find(records, { id: id }); | ||
|
||
if (!record) { | ||
res.status(ERR_NOT_FOUND).send({ | ||
msg: 'Not found' | ||
}); | ||
return; | ||
} | ||
|
||
res.send(ensureMachineProfile(record)); | ||
}; | ||
|
||
export const update = (req, res) => { | ||
const id = req.params.id; | ||
const records = getSanitizedRecords(); | ||
const record = _find(records, { id: id }); | ||
|
||
if (!record) { | ||
res.status(ERR_NOT_FOUND).send({ | ||
msg: 'Not found' | ||
}); | ||
return; | ||
} | ||
|
||
try { | ||
const nextRecord = req.body; | ||
|
||
[ // [key, ensureType] | ||
['name', ensureString], | ||
['limits.xmin', ensureNumber], | ||
['limits.xmax', ensureNumber], | ||
['limits.ymin', ensureNumber], | ||
['limits.ymax', ensureNumber], | ||
['limits.zmin', ensureNumber], | ||
['limits.zmax', ensureNumber], | ||
].forEach(it => { | ||
const [key, ensureType] = it; | ||
const defaultValue = _get(record, key); | ||
const value = _get(nextRecord, key, defaultValue); | ||
|
||
_set(record, key, (typeof ensureType === 'function') ? ensureType(value) : value); | ||
}); | ||
|
||
config.set(CONFIG_KEY, records); | ||
|
||
res.send({ id: record.id }); | ||
} catch (err) { | ||
res.status(ERR_INTERNAL_SERVER_ERROR).send({ | ||
msg: 'Failed to save ' + JSON.stringify(settings.rcfile) | ||
}); | ||
} | ||
}; | ||
|
||
export const __delete = (req, res) => { | ||
const id = req.params.id; | ||
const records = getSanitizedRecords(); | ||
const record = _find(records, { id: id }); | ||
|
||
if (!record) { | ||
res.status(ERR_NOT_FOUND).send({ | ||
msg: 'Not found' | ||
}); | ||
return; | ||
} | ||
|
||
try { | ||
const filteredRecords = records.filter(record => { | ||
return record.id !== id; | ||
}); | ||
config.set(CONFIG_KEY, filteredRecords); | ||
|
||
res.send({ id: record.id }); | ||
} catch (err) { | ||
res.status(ERR_INTERNAL_SERVER_ERROR).send({ | ||
msg: 'Failed to save ' + JSON.stringify(settings.rcfile) | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export const ensureBoolean = (value, defaultValue = false) => { | ||
if (value === undefined || value === null) { | ||
return Boolean(defaultValue); | ||
} | ||
|
||
return (typeof value === 'boolean') ? value : Boolean(value); | ||
}; | ||
|
||
export const ensureString = (value, defaultValue = '') => { | ||
if (value === undefined || value === null) { | ||
return String(defaultValue); | ||
} | ||
|
||
return (typeof value === 'string') ? value : String(value); | ||
}; | ||
|
||
export const ensureNumber = (value, defaultValue = 0) => { | ||
if (value === undefined || value === null) { | ||
return Number(defaultValue); | ||
} | ||
|
||
return (typeof value === 'number') ? value : Number(value); | ||
}; |
Oops, something went wrong.