Skip to content

Commit

Permalink
Merge 4465bb9 into cbc29b1
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton committed Mar 31, 2019
2 parents cbc29b1 + 4465bb9 commit 6888814
Show file tree
Hide file tree
Showing 90 changed files with 2,706 additions and 770 deletions.
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -143,6 +143,7 @@
"@trendmicro/react-checkbox": "~3.4.1",
"@trendmicro/react-datepicker": "~1.0.0-alpha.6",
"@trendmicro/react-dropdown": "~1.3.0",
"@trendmicro/react-form-control": "~0.1.0",
"@trendmicro/react-grid-system": "~0.2.0",
"@trendmicro/react-iframe": "~0.3.1",
"@trendmicro/react-interpolate": "~0.5.5",
Expand Down Expand Up @@ -188,6 +189,7 @@
"express": "~4.16.3",
"express-jwt": "~5.3.1",
"express-session": "~1.15.6",
"final-form": "~4.12.0",
"font-awesome": "~4.7.0",
"frac": "~1.1.2",
"gcode-interpreter": "~2.1.0",
Expand Down Expand Up @@ -231,6 +233,7 @@
"react-dom": "~15.6.2",
"react-dropzone": "~4.2.13",
"react-facebook-loading": "~0.6.2",
"react-final-form": "~3.7.0",
"react-foreach": "~0.1.1",
"react-ga": "~2.5.3",
"react-icon-base": "~2.1.2",
Expand Down
194 changes: 194 additions & 0 deletions src/app/api/api.machines.js
@@ -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)
});
}
};
3 changes: 2 additions & 1 deletion src/app/api/index.js
Expand Up @@ -2,9 +2,10 @@ export * as version from './api.version';
export * as state from './api.state';
export * as gcode from './api.gcode';
export * as controllers from './api.controllers';
export * as watch from './api.watch';
export * as commands from './api.commands';
export * as events from './api.events';
export * as machines from './api.machines';
export * as macros from './api.macros';
export * as mdi from './api.mdi';
export * as users from './api.users';
export * as watch from './api.watch';
19 changes: 13 additions & 6 deletions src/app/app.js
Expand Up @@ -271,6 +271,12 @@ const appMain = () => {
// Controllers
app.get(urljoin(settings.route, 'api/controllers'), api.controllers.get);

// Watch Directory
app.get(urljoin(settings.route, 'api/watch/files'), api.watch.getFiles);
app.post(urljoin(settings.route, 'api/watch/files'), api.watch.getFiles);
app.get(urljoin(settings.route, 'api/watch/file'), api.watch.readFile);
app.post(urljoin(settings.route, 'api/watch/file'), api.watch.readFile);

// Commands
app.get(urljoin(settings.route, 'api/commands'), api.commands.fetch);
app.post(urljoin(settings.route, 'api/commands'), api.commands.create);
Expand All @@ -286,6 +292,13 @@ const appMain = () => {
app.put(urljoin(settings.route, 'api/events/:id'), api.events.update);
app.delete(urljoin(settings.route, 'api/events/:id'), api.events.__delete);

// Machines
app.get(urljoin(settings.route, 'api/machines'), api.machines.fetch);
app.post(urljoin(settings.route, 'api/machines'), api.machines.create);
app.get(urljoin(settings.route, 'api/machines/:id'), api.machines.read);
app.put(urljoin(settings.route, 'api/machines/:id'), api.machines.update);
app.delete(urljoin(settings.route, 'api/machines/:id'), api.machines.__delete);

// Macros
app.get(urljoin(settings.route, 'api/macros'), api.macros.fetch);
app.post(urljoin(settings.route, 'api/macros'), api.macros.create);
Expand All @@ -307,12 +320,6 @@ const appMain = () => {
app.get(urljoin(settings.route, 'api/users/:id'), api.users.read);
app.put(urljoin(settings.route, 'api/users/:id'), api.users.update);
app.delete(urljoin(settings.route, 'api/users/:id'), api.users.__delete);

// Watch
app.get(urljoin(settings.route, 'api/watch/files'), api.watch.getFiles);
app.post(urljoin(settings.route, 'api/watch/files'), api.watch.getFiles);
app.get(urljoin(settings.route, 'api/watch/file'), api.watch.readFile);
app.post(urljoin(settings.route, 'api/watch/file'), api.watch.readFile);
}

// page
Expand Down
23 changes: 23 additions & 0 deletions src/app/lib/ensure-type.js
@@ -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);
};

0 comments on commit 6888814

Please sign in to comment.