1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
| const cmdJson = '';
const cmdObj = JSON.parse(cmdJson);
var CmdGenerator = (function() {
function getCmd(request) {
var requestJson = JSON.parse(request);
var cmdType = requestJson.cmdType;
var cmdName = requestJson.cmdName;
var typeObj = cmdObj[cmdType];
if(typeObj == undefined) {
return "no such cmdType: " + cmdType;
}
var rawCmd = typeObj[cmdName];
if(rawCmd == undefined) {
return "no such cmdName: " + cmdName + " with cmdType: " + cmdType;
}
var parameters = requestJson.parameters;
if(parameters != undefined) {
return getByCmdType(cmdType,cmdName,parameters);
} else {
return bytesToHex(hexToBytes(rawCmd));
}
}
function getByCmdType(cmdType,cmdName,parameters) {
var typeObj = cmdObj[cmdType];
switch(typeObj) {
case cmdObj.WoBlindTilt:
return getByBlindTiltCmdName(cmdName,parameters);
default :
return "no such cmdType: " + cmdType + " with parameters";
}
}
function getByBlindTiltCmdName(cmdName,parameters) {
var rawCmd = cmdObj.WoBlindTilt[cmdName];
switch(rawCmd) {
case cmdObj.WoBlindTilt.move:
return getBlindTiltMoveCmd(rawCmd,parameters);
case cmdObj.WoBlindTilt.setDirectionCmd:
return getBlindTiltDirectionCmd(rawCmd,parameters);
default:
return "no such cmdName: " + cmdName + " with cmdType: WoBlindTilt" + " with parameters";
}
}
function getBlindTiltMoveCmd(rawCmd,parameters) {
return rawCmd + hex8(parameters.percent);
}
function getBlindTiltDirectionCmd(rawCmd,parameters) {
var data = parameters.direction | 0x02;
return rawCmd + hex8(data);
}
function hexToBytes(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
}
function bytesToHex(bytes) {
for (var hex = [], i = 0; i < bytes.length; i++) {
var current = bytes[i] < 0 ? bytes[i] + 256 : bytes[i];
hex.push((current >>> 4).toString(16));
hex.push((current & 0xF).toString(16));
}
return hex.join("").toUpperCase();
}
function hex8(val) {
val &= 0xFF;
var hex = val.toString(16).toUpperCase();
return ("00" + hex).slice(-2);
}
return {
getCmd: getCmd
};
})();
|