Skip to content

Commit

Permalink
Add parseCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
lgaticaq committed Mar 15, 2016
1 parent a909420 commit 4724ef9
Show file tree
Hide file tree
Showing 2 changed files with 249 additions and 2 deletions.
84 changes: 83 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,87 @@ const isTz = (raw) => {
return result;
};

const parseCommand = (data) => {
let command = '';
const password = data.password || '000000';
let state, digit, port, number, speed, interval, times, trigger, a, b, c, d, e, f, g, h;
if (/^set_password$/.test(data.instruction)) {
command = `*${password},001,${data.newPassword}#`;
} else if (/^[1-4]{1}_(on|off)$/.test(data.instruction)) {
[port, state] = data.instruction.split('_');
const ports = {'1': 'A', '2': 'B', '3': 'C', '4': 'D'};
digit = state === 'on' ? 1 : 0;
command = `*${password},025,${ports[port]},${digit}#`;
} else if (/^gprs_(on|off)$/.test(data.instruction)) {
state = data.instruction.split('_')[1];
digit = state === 'on' ? 1 : 0;
command = `*${password},016,${digit}#`;
} else if (data.instruction === 'clear_mem') {
command = `*${password},500#`;
} else if (data.instruction === 'set_interval_gprs') {
times = data.times || 999;
command = `*${password},018,${data.interval},${times}#`;
} else if (/^(set|setoff)_sos_number(E)?$/.test(data.instruction)) {
number = data.number;
state = data.instruction.split('_')[0];
digit = state === 'set' ? 1 : 0;
command = `*${password},003,0,${digit},00569${number},00569${number}#`;
} else if (/^set_speed_(on|off)(E)?$/.test(data.instruction)) {
speed = data.speed || 100;
state = data.instruction.split('_')[2];
times = data.times || 10;
interval = data.interval || 60;
digit = state === 'on' ? 1 : 0;
command = `*${password},005,${digit},${speed},${times},${interval}#`;
} else if (data.instruction === 'picture') {
interval = data.interval || 5;
times = data.times || 1;
command = `*${password},200,${interval},${times}#`;
} else if (data.instruction === 'take_picture') {
command = `*${password},210#`;
} else if (data.instruction === 'configure_io_picture') {
const triggers = {on: 1, off: 2, both: 3};
trigger = triggers[data.trigger || 'both'];
number = data.number || 1;
command = `*${password},201,${data.port},${trigger},${number}#`;
} else if(data.instruction === 'Custom'){
command = data.command;
} else if (/^set_memory_(on|off)$/.test(data.instruction)) {
state = data.instruction.split('_')[2];
digit = state === 'on' ? 1 : 0;
command = `*${password},601,${digit}#`;
} else if (/^set_tremble$/.test(data.instruction)) {
command = `*${password},021,${data.sleep ? 1 : 0},${data.tremble ? 1 : 0}#`;
} else if (/^set_into_sleep$/.test(data.instruction)) {
command = `*${password},044,${data.time}#`;
} else if (/^set_wake_up$/.test(data.instruction)) {
command = `*${password},043,${data.time}#`;
} else if (/^reboot$/.test(data.instruction)) {
command = `*${password},991#`;
} else if (/^set_extend$/.test(data.instruction)) {
a = data.a ? 1 : 0;
b = data.b ? 1 : 0;
c = data.c ? 1 : 0;
d = data.d ? 1 : 0;
e = data.e ? 1 : 0;
f = data.f ? 1 : 0;
g = data.g ? 1 : 0;
h = data.h ? 1 : 0;
command = `*${password},118,${a}${b}${c}${d}${e}${f}${g}${h}#`;
} else if (/^set_heartbeat_(on|off)$/.test(data.instruction)) {
state = data.instruction.split('_')[2];
digit = state === 'on' ? 1 : 0;
command = `*${password},040,${digit}#`;
} else if (/^set_interval_heartbeat$/.test(data.instruction)) {
command = `*${password},041,${data.times}#`;
} else if (/^set_idling_(on|off)$/.test(data.instruction)) {
state = data.instruction.split('_')[2];
digit = state === 'on' ? 1 : 0;
command = `*${password},404,${digit},${data.times}#`;
}
return command;
};

module.exports = {
parse: parse,
patterns: patterns,
Expand All @@ -670,5 +751,6 @@ module.exports = {
getCommandMap: getCommandMap,
verifyLen: verifyLen,
verifyCrc: verifyCrc,
isTz: isTz
isTz: isTz,
parseCommand: parseCommand
};
167 changes: 166 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,9 +679,174 @@ describe('tz-parzer', () => {
}]);
});

it('should return true', () => {
it('should return true for valid tz data', () => {
const raw = new Buffer('$$B6869444005480041|AA$GPRMC,194329.000,A,3321.6735,S,07030.7640,W,0.00,0.00,090216,,,A*6C|02.1|01.3|01.7|000000000000|20160209194326|13981188|00000000|32D3A03F|0000|0.6376|0100|995F\r\n');
const data = tz.isTz(raw);
expect(data).to.be.a.true;
});

it('should return raw command set password', () => {
const data = {
instruction: 'set_password',
newPassword: 111111
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*000000,001,111111#');
});

it('should return raw command di 1 on', () => {
const data = {
instruction: '1_on'
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*000000,025,A,1#');
});

it('should return raw command gprs on', () => {
const data = {
instruction: 'gprs_on'
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*000000,016,1#');
});

it('should return raw command clear internal memory', () => {
const data = {
instruction: 'clear_mem'
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*000000,500#');
});

it('should return raw command set interval gprs', () => {
const data = {
instruction: 'set_interval_gprs',
interval: 10
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*000000,018,10,999#');
});

it('should return raw command set off sos number', () => {
const data = {
instruction: 'setoff_sos_numberE',
number: 92052518
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*000000,003,0,0,0056992052518,0056992052518#');
});

it('should return raw command set on speed', () => {
const data = {
instruction: 'set_speed_on',
speed: 120
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*000000,005,1,120,10,60#');
});

it('should return raw command set time taking pictures', () => {
const data = {
instruction: 'picture'
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*000000,200,5,1#');
});

it('should return raw command set take picture', () => {
const data = {
instruction: 'take_picture'
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*000000,210#');
});

it('should return raw command set take picture', () => {
const data = {
instruction: 'configure_io_picture',
password: 589756,
port: 3
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*589756,201,3,3,1#');
});

it('should return raw command set memory on', () => {
const data = {
instruction: 'set_memory_on'
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*000000,601,1#');
});

it('should return raw command set sleep and tremble', () => {
const data = {
instruction: 'set_tremble',
sleep: true,
tremble: true
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*000000,021,1,1#');
});

it('should return raw command set into sleep', () => {
const data = {
instruction: 'set_into_sleep',
time: 60
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*000000,044,60#');
});

it('should return raw command set wake up', () => {
const data = {
instruction: 'set_wake_up',
time: 60
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*000000,043,60#');
});

it('should return raw command reboot', () => {
const data = {
instruction: 'reboot'
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*000000,991#');
});

it('should return raw command extend', () => {
const data = {
instruction: 'set_extend',
a: true
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*000000,118,10000000#');
});

it('should return raw command set heartbeat on', () => {
const data = {
instruction: 'set_heartbeat_on'
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*000000,040,1#');
});

it('should return raw command set interval heartbeat', () => {
const data = {
instruction: 'set_interval_heartbeat',
times: 3
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*000000,041,3#');
});

it('should return raw command set idling alarm', () => {
const data = {
instruction: 'set_idling_on',
times: 300
};
const raw = tz.parseCommand(data);
expect(raw).to.eql('*000000,404,1,300#');
});
});

0 comments on commit 4724ef9

Please sign in to comment.