Skip to content

Commit 66aabdb

Browse files
committed
feat: add support for setting float parameters
1 parent 8f44813 commit 66aabdb

File tree

1 file changed

+87
-19
lines changed

1 file changed

+87
-19
lines changed

lib/driveweb.js

+87-19
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ function DriveInterface(ip, config) {
3737
this.ip = ip;
3838
this.port = 48556;
3939
this.PROTOCOL_DEVICE = 0x81;
40-
this.LEN_WRAPPER = 4;
40+
this.LEN_WRAPPER = 0x04;
4141
this.REQ_GET = 0x84;
42-
this.LEN_GET = 4;
42+
this.LEN_GET = 0x04;
4343
this.REQ_SET = 0x86;
44-
this.LEN_SET = 6;
44+
this.LEN_FLOAT_SET = 0x08;
45+
this.LEN_SET = 0x06;
4546
this.EX_TYPE = 0xff;
4647
this.EX_LOCK_FAIL = 0xff;
4748
this.EX_LOCK_TIMEOUT = 0xfe;
@@ -62,14 +63,7 @@ function DriveInterface(ip, config) {
6263
return this;
6364
}
6465

65-
DriveInterface.prototype.getParameter = async function getParameter(
66-
paramId,
67-
pure
68-
) {
69-
if (Array.isArray(paramId)) {
70-
return this.getParameters(paramId);
71-
}
72-
66+
async function _getParameter(paramId, pure) {
7367
const data = Buffer.alloc(this.LEN_WRAPPER + this.LEN_GET);
7468
data[3] = this.PROTOCOL_DEVICE;
7569
data[4] = this.LEN_GET;
@@ -84,6 +78,31 @@ DriveInterface.prototype.getParameter = async function getParameter(
8478

8579
const n = Buffer.from([rslt[6], rslt[7]]);
8680
return n.readInt16BE();
81+
}
82+
83+
DriveInterface.prototype.getParameter = async function getParameter(
84+
paramId,
85+
pure
86+
) {
87+
if (Array.isArray(paramId)) {
88+
return this.getParameters(paramId);
89+
}
90+
91+
let retry = 3;
92+
let v;
93+
while (retry !== 0) {
94+
try {
95+
return await _getParameter.bind(this)(paramId, pure);
96+
} catch (e) {
97+
retry = retry - 1;
98+
}
99+
}
100+
101+
if (v) {
102+
return v;
103+
}
104+
105+
throw new SocketTimeout();
87106
};
88107

89108
DriveInterface.prototype.getParameters = function getParameters(paramIds) {
@@ -98,10 +117,7 @@ DriveInterface.prototype.getParameters = function getParameters(paramIds) {
98117
return new Promise((resolve) => resolve(null));
99118
};
100119

101-
DriveInterface.prototype.setParameter = async function setParameter(
102-
paramId,
103-
value
104-
) {
120+
async function _setParameter(paramId, value) {
105121
if (paramId !== null && typeof paramId === `object`) {
106122
return this.setParameters(paramId);
107123
}
@@ -117,12 +133,32 @@ DriveInterface.prototype.setParameter = async function setParameter(
117133
v.writeInt16BE(value);
118134
[data[8], data[9]] = v;
119135
await this.deviceTransaction(data);
120-
const t = await this.getParameter(paramId);
121-
if (t !== value) {
122-
throw new Error(`Value not set.`);
136+
return undefined;
137+
}
138+
139+
DriveInterface.prototype.setParameter = async function setParameter(
140+
paramId,
141+
value
142+
) {
143+
if (paramId !== null && typeof paramId === `object`) {
144+
return this.setParameters(paramId);
123145
}
124146

125-
return t;
147+
let retry = 3;
148+
let v;
149+
while (retry !== 0) {
150+
try {
151+
return await _setParameter.bind(this)(paramId, value);
152+
} catch (e) {
153+
retry = retry - 1;
154+
}
155+
}
156+
157+
if (v) {
158+
return v;
159+
}
160+
161+
throw new SocketTimeout();
126162
};
127163

128164
DriveInterface.prototype.setParameters = function setParameters(obj) {
@@ -145,6 +181,38 @@ DriveInterface.prototype.getFloatParameter = async function getFloatParameter(
145181
return n.readFloatBE();
146182
};
147183

184+
async function _setFloatParameter(paramId, value) {
185+
const data = Buffer.alloc(this.LEN_WRAPPER + this.LEN_FLOAT_SET);
186+
data[3] = this.PROTOCOL_DEVICE;
187+
data[4] = this.LEN_FLOAT_SET;
188+
data[5] = this.REQ_SET;
189+
data.writeUInt16BE(paramId, 6);
190+
data.writeFloatBE(value, 8);
191+
await this.deviceTransaction(data);
192+
return undefined;
193+
}
194+
195+
DriveInterface.prototype.setFloatParameter = async function setFloatParameter(
196+
paramId,
197+
value
198+
) {
199+
let retry = 3;
200+
let v;
201+
while (retry !== 0) {
202+
try {
203+
return await _setFloatParameter.bind(this)(paramId, value);
204+
} catch (e) {
205+
retry = retry - 1;
206+
}
207+
}
208+
209+
if (v) {
210+
return v;
211+
}
212+
213+
throw new SocketTimeout();
214+
};
215+
148216
DriveInterface.prototype.deviceTransaction = function deviceTransaction(pkt) {
149217
return new Promise(async (resolve, reject) => {
150218
const self = this;

0 commit comments

Comments
 (0)