weblab-instrument
Version:
communication with instrument through usb
210 lines (192 loc) • 8.2 kB
JavaScript
const chID = require('../sys/sysConstant.js').chID;
const debug = require('debug');
const log = debug('method:log');
function checkParameterString(arg, par) {
for (let i = 0; i < par.length; i += 1) {
if (arg.toUpperCase() === par[i].toUpperCase()) {
return true;
}
}
return false;
}
function Method(id) {
const method = {
get: function get(prop, res, callback) {
const self = this;
let cmd;
if (this.dev.gdsType === '') {
callback('300', `${this.dev.gdsType}: not supported`);
return;
}
log(`this.gdsType=${this.dev.gdsType}`);
log(`prop=+${prop}`);
log(`id=${id}`);
if (this.commandObj[this.dev.gdsType][prop].command[0].length > 1) {
log(`id = ${id}`);
if (this.commandObj[this.dev.gdsType][prop].parameter_type === 'bind_para') {
cmd = `${this.commandObj[this.dev.gdsType][prop].command[chID[id]]}${res.toString()}?\r\n`;
} else {
cmd = `${this.commandObj[this.dev.gdsType][prop].command[chID[id]]}?\r\n`;
}
} else if (this.commandObj[this.dev.gdsType][prop].parameter_type === 'bind_para') {
cmd = `${this.commandObj[this.dev.gdsType][prop].command}${res.toString()}?\r\n`;
} else {
cmd = `${this.commandObj[this.dev.gdsType][prop].command}?\r\n`;
}
if (this.dev.gdsType === 'LCR6000') {
cmd = `${cmd.slice(0, -2)}\n`;
}
log(`getProp cmd = ${cmd}`);
if (this.commandObj[this.dev.gdsType][prop].parameter_type === 'query_only_block') {
this.dev.queryBlock = true;
} else {
this.dev.queryBlock = false;
}
this.dev.state.conn = 'query';
this.dev.state.currentCmd = cmd;
this.dev.state.currentId = id;
this.dev.state.setTimeout = true;
this.dev.state.timeoutCb = function timeoutCb() {
log(`!!!!!!!query timeout: cmd =${self.dev.state.currentCmd}`);
self.dev.queryBlock = false;
self.dev.state.conn = 'timeout';
if (self.dev.errHandler) {
self.dev.errHandler(self);
}
callback(['408', `command : ${self.dev.state.currentCmd},timeout`]);
};
this.dev.state.timeoutObj = setTimeout(() => {
self.dev.state.timeoutCb();
}, 5000);
this.dev.cmdHandler = this[id].cmdHandler[prop].getHandler;
this.dev.handlerSelf = this[id];
this.dev.syncCallback = callback;
if (!this.dev.write(cmd)) {
clearTimeout(this.dev.state.timeoutObj);
callback(['405', 'device not avaiable']);
}
}.bind(this),
set: function set(prop, arg, callback) {
let cmd;
const rangeLimit = this.commandObj[this.dev.gdsType][prop];
const self = this;
if (this.dev.gdsType === '' || this.dev.gdsType === 'undefined') {
callback('300', `${this.dev.gdsType} : not supported`);
return;
}
log(`${this.gdsType} = ${this.dev.gdsType}`);
if (prop === 'delay_for_a_while') {
setTimeout(() => {
if (callback) {
callback();
}
}, arg);
return;
}
if (this.commandObj[this.dev.gdsType][prop].command[0].length > 1) {
log(`id = ${id}`);
cmd = this.commandObj[this.dev.gdsType][prop].command[chID[id]];
log(`${prop} command = ${cmd}`);
} else {
cmd = this.commandObj[this.dev.gdsType][prop].command;
}
if (rangeLimit.parameter_type === 'string') {
if (checkParameterString(arg, rangeLimit.parameter)) {
cmd += ` ${arg}\r\n`;
} else {
if (callback) {
callback(['100', `${arg}: argument not supported`, `cmd=${cmd}`]);
}
return;
}
} else if (rangeLimit.parameter_type === 'bind_para') {
if (checkParameterString(arg, rangeLimit.parameter)) {
cmd += `${arg}\r\n`;
} else {
if (callback) {
callback(['100', `${arg}: argument not supported`, `cmd=${cmd}`]);
}
return;
}
} else if (rangeLimit.parameter_type === 'user_string') {
cmd += ` "${arg}"\r\n`;
} else if (rangeLimit.parameter_type === 'float_string') {
let i = 0;
if (isNaN(arg)) {
cmd += ` ${rangeLimit.parameter[0]}\r\n`;
} else {
const fval = parseFloat(arg);
for (i = 0; i < rangeLimit.parameter.length; i += 1) {
if (fval === parseFloat(rangeLimit.parameter[i])) {
log(`parseFloat=${parseFloat(rangeLimit.parameter[i])}`);
log(`arg=${fval.toExponential(3)}`);
cmd += ` ${rangeLimit.parameter[i]}\r\n`;
break;
}
}
}
if (i >= rangeLimit.parameter.length) {
if (callback) {
callback(['100', `${arg}: argument not supported`, `cmd=${cmd}`]);
}
return;
}
} else if (rangeLimit.parameter_type === 'parameter_free') {
cmd += '\r\n';
} else if (rangeLimit.parameter_type === 'int_value') {
const val = parseInt(arg, 10);
if (isNaN(arg)) {
if (callback) {
callback(['100', `${arg}: argument not supported`, `cmd=${cmd}`]);
}
return;
}
cmd = `${cmd} ${val.toString()}\r\n`;
} else if (rangeLimit.parameter_type === 'pwr_specific') {
if (rangeLimit.format === 'no_separate') {
cmd += `${arg}\r\n`;
} else {
cmd += `:${arg}\r\n`;
}
} else if (isNaN(arg)) {
if ((arg === 'SETTO50') || (arg === 'TTL') || (arg === 'ECL')) {
cmd = `${cmd} ${arg}\r\n`;
} else {
if (callback) {
callback(['100', `${arg}: argument not supported`, `cmd=${cmd}`]);
}
return;
}
} else {
const fval = parseFloat(arg);
cmd = `${cmd} ${fval.toExponential(3)}\r\n`;
}
if (this.dev.gdsType === 'LCR6000') {
cmd = `${cmd.slice(0, -2)}\n`;
}
log(`cmd set = ${cmd}`);
this.dev.cmdHandler = null;
if (this.dev.write(cmd)) {
self[id].cmdHandler[prop].setHelper(self[id], arg);
if (callback) {
log('cmd set done');
callback(null);
}
} else if (this.dev.interf === 'net') {
this.dev.net.socket.once('drain', () => {
self.dev.write(cmd);
self[id].cmdHandler[prop].setHelper(self[id], arg);
if (callback) {
callback(null);
}
});
} else if (this.dev.interf === 'usb') {
if (callback) {
callback('usb write error');
}
}
}.bind(this),
};
return method;
}
exports.CreatMethod = Method;