@robotical/webapp-types 3.14.48 → 3.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,14 +1,3 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
1
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
2
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14
3
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -48,50 +37,138 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
48
37
  import Logger from "../../services/logger/Logger";
49
38
  import { RICHWElemList_Str } from "@robotical/roboticaljs";
50
39
  import { RIC_WHOAMI_TYPE_CODE_ADDON_COLOUR } from "@robotical/ricjs-robotical-addons/dist/RICRoboticalAddOns";
40
+ import { COLOUR_SENSOR_CALIBRATION_ERROR, COLOUR_SENSOR_CALIBRATION_STAGE, } from "../../types/colour-sensor-calibration";
41
+ import { getIRFootAirReadingsFromStateInfo, } from "../../utils/helpers/ir-on-air";
51
42
  // colour sensor commands
52
- var GET_CALIBRATION_COMMAND = function (hwElemName) { return "elem/".concat(hwElemName, "/json?cmd=raw&hexWr=104020&numToRd=8&msgKey=1234"); };
53
- var GET_COLOUR_SENSOR_READING_COMMAND = function (hwElemName) { return "elem/".concat(hwElemName, "/json?cmd=raw&hexWr=&numToRd=5&msgKey=1"); };
54
- var CALIBRATE_COMMAND = function (hwElemName, baseAddr, highByte, lowByte, i) {
55
- return "elem/".concat(hwElemName, "/json?cmd=raw&hexWr=ff4040").concat(baseAddr.toString(16)).concat(highByte).concat(lowByte, "&numToRd=0&msgKey=11").concat(i);
43
+ var GET_CALIBRATION_COMMAND = function (hwElemName, msgKey) {
44
+ return "elem/".concat(hwElemName, "/json?cmd=raw&hexWr=104020&numToRd=8&msgKey=").concat(msgKey);
45
+ };
46
+ var GET_COLOUR_SENSOR_READING_COMMAND = function (hwElemName, msgKey) {
47
+ return "elem/".concat(hwElemName, "/json?cmd=raw&hexWr=&numToRd=13&msgKey=").concat(msgKey);
48
+ };
49
+ var GET_OFFSET_COMMAND = function (hwElemName, msgKey) {
50
+ return "elem/".concat(hwElemName, "/json?cmd=raw&hexWr=104018&numToRd=4&msgKey=").concat(msgKey);
56
51
  };
52
+ var SET_OFFSET_COMMAND = function (hwElemName, clearHex, redHex, greenHex, blueHex, i) {
53
+ return "elem/".concat(hwElemName, "/json?cmd=raw&hexWr=ff404018").concat(clearHex).concat(redHex).concat(greenHex).concat(blueHex, "&numToRd=0&msgKey=21").concat(i);
54
+ };
55
+ var SET_CALIBRATION_COMMAND = function (hwElemName, clearLow, redLow, greenLow, blueLow, i) {
56
+ return "elem/".concat(hwElemName, "/json?cmd=raw&hexWr=ff40402000").concat(clearLow, "00").concat(redLow, "00").concat(greenLow, "00").concat(blueLow, "&numToRd=0&msgKey=11").concat(i);
57
+ };
58
+ var DEFAULT_PREFERRED_COLOUR_SENSOR_NAMES = ["LeftColorSensor", "coloursensor"];
57
59
  var MAX_CALIBRATION_ATTEMPTS = 5;
58
60
  var TOLERANCE = 3; // tolerance for difference between calibration values and colour sensor readings
61
+ var PAUSE_AFTER_SET_MS = 3000;
62
+ var POWER_ON_DELAY_MS = 3000;
63
+ var REPORT_WAIT_TIMEOUT_MS = 1500;
59
64
  var SHOW_LOGS = true;
60
65
  var TAG = "ColourSensorManualCalibratorMarty";
61
66
  var ColourSensorManualCalibratorMarty = /** @class */ (function () {
62
67
  function ColourSensorManualCalibratorMarty() {
63
68
  }
64
- ColourSensorManualCalibratorMarty.calibrate = function (raftConnector, retryTimes) {
69
+ ColourSensorManualCalibratorMarty.calibrate = function (raftConnector, stage, retryTimes) {
65
70
  if (retryTimes === void 0) { retryTimes = 0; }
66
71
  return __awaiter(this, void 0, void 0, function () {
67
- var isSame, error_1;
72
+ var didCalibrate, error_1, errorMessage;
68
73
  return __generator(this, function (_a) {
69
74
  switch (_a.label) {
70
75
  case 0:
71
- Logger.info(SHOW_LOGS, TAG, "\n==== Calibration attempt ".concat(retryTimes + 1, " ===="));
76
+ Logger.vipLog(SHOW_LOGS, TAG, "\n==== Calibration ".concat(stage, " attempt ").concat(retryTimes + 1, " ===="));
77
+ if (!stage) {
78
+ Logger.vipLog(SHOW_LOGS, TAG, "Calibration stage missing");
79
+ return [2 /*return*/, false];
80
+ }
72
81
  if (!this.prepareForCalibration(raftConnector)) {
73
82
  return [2 /*return*/, false];
74
83
  }
75
84
  _a.label = 1;
76
85
  case 1:
77
- _a.trys.push([1, 6, 7, 8]);
78
- return [4 /*yield*/, this.performCalibrationSequence()];
86
+ _a.trys.push([1, 3, 4, 5]);
87
+ return [4 /*yield*/, this.performCalibrationSequence(stage)];
79
88
  case 2:
80
- if (!_a.sent()) return [3 /*break*/, 5];
81
- return [4 /*yield*/, this.compareCalibrationValues(this._calibrationValues, this._colourSensorReadings)];
89
+ didCalibrate = _a.sent();
90
+ if (didCalibrate) {
91
+ return [2 /*return*/, true];
92
+ }
93
+ return [2 /*return*/, this.handleMaxAttempts(raftConnector, stage, retryTimes)];
82
94
  case 3:
83
- isSame = _a.sent();
84
- return [4 /*yield*/, this.handleCalibrationOutcome(isSame, raftConnector, retryTimes)];
85
- case 4: return [2 /*return*/, _a.sent()];
86
- case 5: return [2 /*return*/, this.handleMaxAttempts(retryTimes, raftConnector)];
87
- case 6:
88
95
  error_1 = _a.sent();
89
- Logger.error(SHOW_LOGS, TAG, "Error calibrating colour sensor: " + JSON.stringify(error_1));
96
+ Logger.vipLog(SHOW_LOGS, TAG, "Error calibrating colour sensor: " + JSON.stringify(error_1));
97
+ errorMessage = error_1 instanceof Error ? error_1.message : String(error_1);
98
+ if (errorMessage === COLOUR_SENSOR_CALIBRATION_ERROR.NOT_IN_AIR) {
99
+ throw new Error(errorMessage);
100
+ }
90
101
  return [2 /*return*/, false];
102
+ case 4:
103
+ this._isCalibrating = false;
104
+ return [7 /*endfinally*/];
105
+ case 5: return [2 /*return*/];
106
+ }
107
+ });
108
+ });
109
+ };
110
+ ColourSensorManualCalibratorMarty.resetOffsets = function (raftConnector) {
111
+ return __awaiter(this, void 0, void 0, function () {
112
+ var names, zeroValues, _i, names_1, name_1, cleared, offsetValues, didMatch, error_2;
113
+ return __generator(this, function (_a) {
114
+ switch (_a.label) {
115
+ case 0:
116
+ Logger.vipLog(SHOW_LOGS, TAG, "\n==== Resetting colour sensor offsets ====");
117
+ if (!this.prepareForCalibration(raftConnector)) {
118
+ return [2 /*return*/, false];
119
+ }
120
+ _a.label = 1;
121
+ case 1:
122
+ _a.trys.push([1, 8, 9, 10]);
123
+ return [4 /*yield*/, this.getColourSensorNames()];
124
+ case 2:
125
+ names = _a.sent();
126
+ names = this.selectPreferredColourSensorNames(names);
127
+ if (names.length === 0) {
128
+ Logger.vipLog(SHOW_LOGS, TAG, "No colour sensors found to reset");
129
+ return [2 /*return*/, false];
130
+ }
131
+ return [4 /*yield*/, this.turnOnServosIfRequired(names)];
132
+ case 3:
133
+ _a.sent();
134
+ return [4 /*yield*/, this.ensureColourSensorPowerOn(false)];
135
+ case 4:
136
+ _a.sent();
137
+ zeroValues = {};
138
+ for (_i = 0, names_1 = names; _i < names_1.length; _i++) {
139
+ name_1 = names_1[_i];
140
+ zeroValues[name_1] = { clear: 0, red: 0, green: 0, blue: 0 };
141
+ }
142
+ return [4 /*yield*/, this.setOffsets(zeroValues)];
143
+ case 5:
144
+ cleared = _a.sent();
145
+ if (!cleared) {
146
+ Logger.vipLog(SHOW_LOGS, TAG, "Failed to clear offsets during reset");
147
+ return [2 /*return*/, false];
148
+ }
149
+ return [4 /*yield*/, this.wait(PAUSE_AFTER_SET_MS)];
150
+ case 6:
151
+ _a.sent();
152
+ return [4 /*yield*/, this.getOffsetValues(names)];
91
153
  case 7:
154
+ offsetValues = _a.sent();
155
+ if (!offsetValues) {
156
+ Logger.vipLog(SHOW_LOGS, TAG, "Failed to read offsets after reset");
157
+ return [2 /*return*/, false];
158
+ }
159
+ didMatch = this.compareValues(zeroValues, offsetValues, 0);
160
+ if (didMatch) {
161
+ this._recentOffsetResetAt = Date.now();
162
+ }
163
+ return [2 /*return*/, didMatch];
164
+ case 8:
165
+ error_2 = _a.sent();
166
+ Logger.vipLog(SHOW_LOGS, TAG, "Error resetting colour sensor offsets: " + JSON.stringify(error_2));
167
+ return [2 /*return*/, false];
168
+ case 9:
92
169
  this._isCalibrating = false;
93
170
  return [7 /*endfinally*/];
94
- case 8: return [2 /*return*/];
171
+ case 10: return [2 /*return*/];
95
172
  }
96
173
  });
97
174
  });
@@ -104,117 +181,110 @@ var ColourSensorManualCalibratorMarty = /** @class */ (function () {
104
181
  this._isCalibrating = true;
105
182
  return true;
106
183
  };
107
- ColourSensorManualCalibratorMarty.performCalibrationSequence = function () {
184
+ ColourSensorManualCalibratorMarty.performCalibrationSequence = function (stage) {
108
185
  return __awaiter(this, void 0, void 0, function () {
109
- var names, calibration1Success, csReadingSuccess, calibration2Success;
186
+ var names;
110
187
  return __generator(this, function (_a) {
111
188
  switch (_a.label) {
112
189
  case 0: return [4 /*yield*/, this.getColourSensorNames()];
113
190
  case 1:
114
191
  names = _a.sent();
192
+ names = this.selectPreferredColourSensorNames(names);
193
+ if (names.length === 0) {
194
+ Logger.vipLog(SHOW_LOGS, TAG, "No colour sensors found to calibrate");
195
+ return [2 /*return*/, false];
196
+ }
115
197
  return [4 /*yield*/, this.turnOnServosIfRequired(names)];
116
198
  case 2:
117
199
  _a.sent();
118
- return [4 /*yield*/, this.getCalibration(names)];
119
- case 3:
120
- calibration1Success = _a.sent();
121
- return [4 /*yield*/, this.getColourSensorReading(names)];
200
+ if (!(stage === COLOUR_SENSOR_CALIBRATION_STAGE.OFFSET)) return [3 /*break*/, 4];
201
+ return [4 /*yield*/, this.calibrateOffsets(names)];
202
+ case 3: return [2 /*return*/, _a.sent()];
122
203
  case 4:
123
- csReadingSuccess = _a.sent();
124
- return [4 /*yield*/, this.getCalibration(names)];
125
- case 5:
126
- calibration2Success = _a.sent();
127
- return [2 /*return*/, calibration1Success && csReadingSuccess && calibration2Success];
204
+ if (!(stage === COLOUR_SENSOR_CALIBRATION_STAGE.WHITE)) return [3 /*break*/, 6];
205
+ return [4 /*yield*/, this.calibrateWhite(names)];
206
+ case 5: return [2 /*return*/, _a.sent()];
207
+ case 6:
208
+ Logger.vipLog(SHOW_LOGS, TAG, "Unknown calibration stage: ".concat(stage));
209
+ return [2 /*return*/, false];
128
210
  }
129
211
  });
130
212
  });
131
213
  };
132
- ColourSensorManualCalibratorMarty.handleCalibrationOutcome = function (isSame, raftConnector, retryTimes) {
133
- return __awaiter(this, void 0, void 0, function () {
134
- return __generator(this, function (_a) {
135
- if (isSame) {
136
- return [2 /*return*/, true];
137
- }
138
- this._isCalibrating = false;
139
- return [2 /*return*/, this.handleMaxAttempts(retryTimes, raftConnector)];
140
- });
141
- });
214
+ ColourSensorManualCalibratorMarty.selectPreferredColourSensorNames = function (names) {
215
+ var preferred = DEFAULT_PREFERRED_COLOUR_SENSOR_NAMES.map(function (name) { return name.toLowerCase(); });
216
+ var matches = names.filter(function (name) { return preferred.includes(name.toLowerCase()); });
217
+ return matches.length > 0 ? matches : names;
142
218
  };
143
- ColourSensorManualCalibratorMarty.handleMaxAttempts = function (retryTimes, raftConnector) {
219
+ ColourSensorManualCalibratorMarty.handleMaxAttempts = function (raftConnector, stage, retryTimes) {
144
220
  return __awaiter(this, void 0, void 0, function () {
145
221
  return __generator(this, function (_a) {
146
222
  switch (_a.label) {
147
223
  case 0:
148
224
  if (retryTimes >= MAX_CALIBRATION_ATTEMPTS) {
149
- Logger.error(SHOW_LOGS, TAG, "Max calibration attempts reached");
225
+ Logger.vipLog(SHOW_LOGS, TAG, "Max calibration attempts reached");
150
226
  return [2 /*return*/, false];
151
227
  }
152
228
  this._isCalibrating = false;
153
- return [4 /*yield*/, this.calibrate(raftConnector, retryTimes + 1)];
229
+ return [4 /*yield*/, this.calibrate(raftConnector, stage, retryTimes + 1)];
154
230
  case 1: return [2 /*return*/, _a.sent()];
155
231
  }
156
232
  });
157
233
  });
158
234
  };
159
235
  ColourSensorManualCalibratorMarty.almostEqual = function (v1, v2, tolerance) {
160
- if (tolerance === void 0) { tolerance = TOLERANCE; }
161
236
  return Math.abs(v1 - v2) <= tolerance;
162
237
  };
163
- ColourSensorManualCalibratorMarty.compareCalibrationValues = function (expected, actual) {
164
- return __awaiter(this, void 0, void 0, function () {
165
- var expectedKeys, actualKeys, _i, expectedKeys_1, key, expectedValue, actualValue;
166
- return __generator(this, function (_a) {
167
- Logger.info(SHOW_LOGS, TAG, "\n==== Comparing calibration values ====");
168
- Logger.info(SHOW_LOGS, TAG, "CalibrationValues: " + JSON.stringify(this._calibrationValues));
169
- Logger.info(SHOW_LOGS, TAG, "ColourSensorReadings: " + JSON.stringify(this._colourSensorReadings));
170
- if (expected === null || actual === null) {
171
- return [2 /*return*/, false];
172
- }
173
- expectedKeys = Object.keys(expected);
174
- actualKeys = Object.keys(actual);
175
- if (expectedKeys.length !== actualKeys.length) {
176
- return [2 /*return*/, false];
177
- }
178
- for (_i = 0, expectedKeys_1 = expectedKeys; _i < expectedKeys_1.length; _i++) {
179
- key = expectedKeys_1[_i];
180
- if (!actualKeys.includes(key)) {
181
- return [2 /*return*/, false];
182
- }
183
- expectedValue = expected[key];
184
- actualValue = actual[key];
185
- if (!this.almostEqual(expectedValue.clear, actualValue.clear)) {
186
- return [2 /*return*/, false];
187
- }
188
- if (!this.almostEqual(expectedValue.red, actualValue.red)) {
189
- return [2 /*return*/, false];
190
- }
191
- if (!this.almostEqual(expectedValue.green, actualValue.green)) {
192
- return [2 /*return*/, false];
193
- }
194
- if (!this.almostEqual(expectedValue.blue, actualValue.blue)) {
195
- return [2 /*return*/, false];
196
- }
197
- }
198
- return [2 /*return*/, true];
199
- });
200
- });
238
+ ColourSensorManualCalibratorMarty.compareValues = function (expected, actual, tolerance) {
239
+ Logger.vipLog(SHOW_LOGS, TAG, "\n==== Comparing values ====");
240
+ Logger.vipLog(SHOW_LOGS, TAG, "Expected: " + JSON.stringify(expected));
241
+ Logger.vipLog(SHOW_LOGS, TAG, "Actual: " + JSON.stringify(actual));
242
+ if (expected === null || actual === null) {
243
+ return false;
244
+ }
245
+ var expectedKeys = Object.keys(expected);
246
+ var actualKeys = Object.keys(actual);
247
+ if (expectedKeys.length !== actualKeys.length) {
248
+ return false;
249
+ }
250
+ for (var _i = 0, expectedKeys_1 = expectedKeys; _i < expectedKeys_1.length; _i++) {
251
+ var key = expectedKeys_1[_i];
252
+ if (!actualKeys.includes(key)) {
253
+ return false;
254
+ }
255
+ var expectedValue = expected[key];
256
+ var actualValue = actual[key];
257
+ if (!this.almostEqual(expectedValue.clear, actualValue.clear, tolerance)) {
258
+ return false;
259
+ }
260
+ if (!this.almostEqual(expectedValue.red, actualValue.red, tolerance)) {
261
+ return false;
262
+ }
263
+ if (!this.almostEqual(expectedValue.green, actualValue.green, tolerance)) {
264
+ return false;
265
+ }
266
+ if (!this.almostEqual(expectedValue.blue, actualValue.blue, tolerance)) {
267
+ return false;
268
+ }
269
+ }
270
+ return true;
201
271
  };
202
272
  ColourSensorManualCalibratorMarty.turnOnServosIfRequired = function (names) {
203
273
  var _a;
204
274
  return __awaiter(this, void 0, void 0, function () {
205
- var _i, names_1, name_1, hwElemList, hwElem;
275
+ var _i, names_2, name_2, hwElemList, hwElem;
206
276
  return __generator(this, function (_b) {
207
277
  switch (_b.label) {
208
278
  case 0:
209
279
  if (names.length === 0)
210
280
  return [2 /*return*/];
211
- _i = 0, names_1 = names;
281
+ _i = 0, names_2 = names;
212
282
  _b.label = 1;
213
283
  case 1:
214
- if (!(_i < names_1.length)) return [3 /*break*/, 8];
215
- name_1 = names_1[_i];
284
+ if (!(_i < names_2.length)) return [3 /*break*/, 8];
285
+ name_2 = names_2[_i];
216
286
  return [4 /*yield*/, this._raftConnector.getRaftMsgHandler()
217
- .sendRICRESTURL("hwstatus/status/".concat(name_1))];
287
+ .sendRICRESTURL("hwstatus/status/".concat(name_2))];
218
288
  case 2:
219
289
  hwElemList = _b.sent();
220
290
  if (!((hwElemList === null || hwElemList === void 0 ? void 0 : hwElemList.rslt) === "ok" && ((_a = hwElemList.hw) === null || _a === void 0 ? void 0 : _a.length) > 0)) return [3 /*break*/, 5];
@@ -223,11 +293,11 @@ var ColourSensorManualCalibratorMarty = /** @class */ (function () {
223
293
  return [4 /*yield*/, this._raftConnector.getRaftMsgHandler().sendRICRESTURL("pwrctrl/5von")];
224
294
  case 3:
225
295
  _b.sent();
226
- return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 4000); })];
296
+ return [4 /*yield*/, this.wait(4000)];
227
297
  case 4:
228
298
  _b.sent();
229
299
  return [2 /*return*/];
230
- case 5: return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 200); })];
300
+ case 5: return [4 /*yield*/, this.wait(200)];
231
301
  case 6:
232
302
  _b.sent();
233
303
  _b.label = 7;
@@ -239,6 +309,114 @@ var ColourSensorManualCalibratorMarty = /** @class */ (function () {
239
309
  });
240
310
  });
241
311
  };
312
+ ColourSensorManualCalibratorMarty.getIRFootAirReadings = function () {
313
+ var _a;
314
+ var systemType = this._raftConnector.getSystemType();
315
+ var stateInfo = (_a = systemType === null || systemType === void 0 ? void 0 : systemType.getStateInfo) === null || _a === void 0 ? void 0 : _a.call(systemType);
316
+ return getIRFootAirReadingsFromStateInfo(stateInfo);
317
+ };
318
+ ColourSensorManualCalibratorMarty.toHexByte = function (value) {
319
+ return (value & 0xff).toString(16).padStart(2, "0");
320
+ };
321
+ ColourSensorManualCalibratorMarty.validateCalibrationByte = function (value, label, sensorName) {
322
+ if (!Number.isFinite(value) || value < 0 || value > 255) {
323
+ Logger.vipLog(SHOW_LOGS, TAG, "Invalid calibration value for ".concat(sensorName, " ").concat(label, ": ").concat(value, " (expected 0-255)"));
324
+ return false;
325
+ }
326
+ return true;
327
+ };
328
+ ColourSensorManualCalibratorMarty.collectReports = function (names, reportKey, commandFactory) {
329
+ return __awaiter(this, void 0, void 0, function () {
330
+ var reportsByElem, expectedMsgKeys, reportMsgKeyBase, expectedCount, isResolved, timeoutId, resolveReports, finish, maybeFinish, reportsPromise, index, hwElemName, msgKey, command, reports;
331
+ var _this = this;
332
+ return __generator(this, function (_a) {
333
+ switch (_a.label) {
334
+ case 0:
335
+ if (names.length === 0) {
336
+ return [2 /*return*/, []];
337
+ }
338
+ reportsByElem = new Map();
339
+ expectedMsgKeys = new Map();
340
+ reportMsgKeyBase = this.nextReportMsgKeyBase(names.length);
341
+ expectedCount = names.length;
342
+ isResolved = false;
343
+ timeoutId = null;
344
+ resolveReports = null;
345
+ finish = function (reports) {
346
+ if (isResolved) {
347
+ return;
348
+ }
349
+ isResolved = true;
350
+ if (timeoutId) {
351
+ clearTimeout(timeoutId);
352
+ }
353
+ resolveReports === null || resolveReports === void 0 ? void 0 : resolveReports(reports);
354
+ };
355
+ maybeFinish = function () {
356
+ if (reportsByElem.size >= expectedCount) {
357
+ finish(Array.from(reportsByElem.values()));
358
+ }
359
+ };
360
+ reportsPromise = new Promise(function (resolve) {
361
+ resolveReports = resolve;
362
+ timeoutId = setTimeout(function () {
363
+ finish(Array.from(reportsByElem.values()));
364
+ }, REPORT_WAIT_TIMEOUT_MS);
365
+ });
366
+ this._raftConnector.getRaftMsgHandler().reportMsgCallbacksSet(reportKey, function (report) { return __awaiter(_this, void 0, void 0, function () {
367
+ var elemName, msgKey, expectedMsgKey;
368
+ var _a;
369
+ return __generator(this, function (_b) {
370
+ elemName = report.elemName;
371
+ msgKey = (_a = report.msgKey) === null || _a === void 0 ? void 0 : _a.toString();
372
+ if (!elemName || !msgKey) {
373
+ return [2 /*return*/];
374
+ }
375
+ expectedMsgKey = expectedMsgKeys.get(elemName);
376
+ if (!expectedMsgKey || expectedMsgKey !== msgKey) {
377
+ return [2 /*return*/];
378
+ }
379
+ if (!reportsByElem.has(elemName)) {
380
+ reportsByElem.set(elemName, report);
381
+ maybeFinish();
382
+ }
383
+ Logger.vipLog(SHOW_LOGS, TAG, "Report callback ".concat(JSON.stringify(report)));
384
+ return [2 /*return*/];
385
+ });
386
+ }); });
387
+ index = 0;
388
+ _a.label = 1;
389
+ case 1:
390
+ if (!(index < names.length)) return [3 /*break*/, 4];
391
+ hwElemName = names[index];
392
+ msgKey = reportMsgKeyBase + index;
393
+ expectedMsgKeys.set(hwElemName, msgKey.toString());
394
+ command = commandFactory(hwElemName, msgKey);
395
+ return [4 /*yield*/, this._raftConnector.getRaftMsgHandler().sendRICRESTURL(command)];
396
+ case 2:
397
+ _a.sent();
398
+ _a.label = 3;
399
+ case 3:
400
+ index += 1;
401
+ return [3 /*break*/, 1];
402
+ case 4: return [4 /*yield*/, reportsPromise];
403
+ case 5:
404
+ reports = _a.sent();
405
+ this._raftConnector.getRaftMsgHandler().reportMsgCallbacksDelete(reportKey);
406
+ return [2 /*return*/, reports];
407
+ }
408
+ });
409
+ });
410
+ };
411
+ ColourSensorManualCalibratorMarty.nextReportMsgKeyBase = function (requestCount) {
412
+ var base = this._reportMsgKeyCounter;
413
+ var increment = Math.max(1, requestCount);
414
+ this._reportMsgKeyCounter += increment;
415
+ if (this._reportMsgKeyCounter >= 60000) {
416
+ this._reportMsgKeyCounter = 1000;
417
+ }
418
+ return base;
419
+ };
242
420
  ColourSensorManualCalibratorMarty.getColourSensorNames = function () {
243
421
  var _a;
244
422
  return __awaiter(this, void 0, void 0, function () {
@@ -246,7 +424,7 @@ var ColourSensorManualCalibratorMarty = /** @class */ (function () {
246
424
  return __generator(this, function (_c) {
247
425
  switch (_c.label) {
248
426
  case 0:
249
- Logger.info(SHOW_LOGS, TAG, "\n==== getColourSensorNames ====");
427
+ Logger.vipLog(SHOW_LOGS, TAG, "\n==== getColourSensorNames ====");
250
428
  names = [];
251
429
  _c.label = 1;
252
430
  case 1:
@@ -275,196 +453,353 @@ var ColourSensorManualCalibratorMarty = /** @class */ (function () {
275
453
  names.push(hwElem.name);
276
454
  }
277
455
  }
278
- Logger.info(SHOW_LOGS, TAG, "Colour sensor names: " + JSON.stringify(names));
456
+ Logger.vipLog(SHOW_LOGS, TAG, "Colour sensor names: " + JSON.stringify(names));
279
457
  return [2 /*return*/, names];
280
458
  case 6:
281
459
  e_1 = _c.sent();
282
- new Error("Error getting colour sensor names: " + JSON.stringify(e_1));
460
+ Logger.vipLog(SHOW_LOGS, TAG, "Error getting colour sensor names: " + JSON.stringify(e_1));
283
461
  return [2 /*return*/, []];
284
462
  case 7: return [2 /*return*/];
285
463
  }
286
464
  });
287
465
  });
288
466
  };
289
- ColourSensorManualCalibratorMarty.getCalibration = function (names) {
467
+ ColourSensorManualCalibratorMarty.getCalibrationValues = function (names) {
290
468
  return __awaiter(this, void 0, void 0, function () {
291
- var REPORT_MSG_KEY, reports, _i, names_2, hwElemName, command, _a, reports_1, report, dataRead, clear, red, green, blue;
292
- var _b;
293
- var _this = this;
294
- return __generator(this, function (_c) {
295
- switch (_c.label) {
469
+ var reports, values, _i, reports_1, report, dataRead, clear, red, green, blue;
470
+ return __generator(this, function (_a) {
471
+ switch (_a.label) {
296
472
  case 0:
297
- Logger.info(SHOW_LOGS, TAG, "\n==== getCalibration ====");
298
- REPORT_MSG_KEY = "GET_CALIBRATION";
299
- reports = [];
300
- this._raftConnector.getRaftMsgHandler().reportMsgCallbacksSet(REPORT_MSG_KEY, function (report) { return __awaiter(_this, void 0, void 0, function () {
301
- return __generator(this, function (_a) {
302
- reports.push(report);
303
- Logger.debug(SHOW_LOGS, TAG, "Report callback ".concat(JSON.stringify(report)));
304
- return [2 /*return*/];
305
- });
306
- }); });
307
- _i = 0, names_2 = names;
308
- _c.label = 1;
473
+ Logger.vipLog(SHOW_LOGS, TAG, "\n==== getCalibration ====");
474
+ return [4 /*yield*/, this.collectReports(names, "GET_CALIBRATION", GET_CALIBRATION_COMMAND)];
309
475
  case 1:
310
- if (!(_i < names_2.length)) return [3 /*break*/, 4];
311
- hwElemName = names_2[_i];
312
- command = GET_CALIBRATION_COMMAND(hwElemName);
313
- return [4 /*yield*/, this._raftConnector.getRaftMsgHandler().sendRICRESTURL(command)];
314
- case 2:
315
- _c.sent();
316
- _c.label = 3;
317
- case 3:
318
- _i++;
319
- return [3 /*break*/, 1];
320
- case 4:
321
- // Wait for report messages to be received
322
- return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2000); })];
323
- case 5:
324
- // Wait for report messages to be received
325
- _c.sent();
326
- this._raftConnector.getRaftMsgHandler().reportMsgCallbacksDelete(REPORT_MSG_KEY);
476
+ reports = _a.sent();
327
477
  if (reports.length === 0) {
328
- Logger.info(SHOW_LOGS, TAG, "No reports received");
329
- return [2 /*return*/, false];
478
+ Logger.vipLog(SHOW_LOGS, TAG, "No reports received");
479
+ return [2 /*return*/, null];
330
480
  }
331
- // Process reports
332
- for (_a = 0, reports_1 = reports; _a < reports_1.length; _a++) {
333
- report = reports_1[_a];
481
+ values = {};
482
+ for (_i = 0, reports_1 = reports; _i < reports_1.length; _i++) {
483
+ report = reports_1[_i];
334
484
  if (report.hexRd && report.elemName) {
335
485
  dataRead = report.hexRd;
336
486
  clear = parseInt(dataRead.slice(0, 4), 16);
337
487
  red = parseInt(dataRead.slice(4, 8), 16);
338
488
  green = parseInt(dataRead.slice(8, 12), 16);
339
489
  blue = parseInt(dataRead.slice(12, 16), 16);
340
- Logger.info(SHOW_LOGS, TAG, "Calibration Report -- " + "clear: " + clear + " red: " + red + " green: " + green + " blue: " + blue);
341
- this._calibrationValues = __assign(__assign({}, this._calibrationValues), (_b = {}, _b[report.elemName] = { clear: clear, red: red, green: green, blue: blue }, _b));
490
+ Logger.vipLog(SHOW_LOGS, TAG, "Calibration Report -- " + "clear: " + clear + " red: " + red + " green: " + green + " blue: " + blue);
491
+ values[report.elemName] = { clear: clear, red: red, green: green, blue: blue };
342
492
  }
343
493
  }
344
- return [2 /*return*/, true];
494
+ return [2 /*return*/, Object.keys(values).length ? values : null];
495
+ }
496
+ });
497
+ });
498
+ };
499
+ ColourSensorManualCalibratorMarty.getOffsetValues = function (names) {
500
+ return __awaiter(this, void 0, void 0, function () {
501
+ var reports, values, _i, reports_2, report, dataRead, clear, red, green, blue;
502
+ return __generator(this, function (_a) {
503
+ switch (_a.label) {
504
+ case 0:
505
+ Logger.vipLog(SHOW_LOGS, TAG, "\n==== getOffsetValues ====");
506
+ return [4 /*yield*/, this.collectReports(names, "GET_OFFSET", GET_OFFSET_COMMAND)];
507
+ case 1:
508
+ reports = _a.sent();
509
+ if (reports.length === 0) {
510
+ Logger.vipLog(SHOW_LOGS, TAG, "No reports received");
511
+ return [2 /*return*/, null];
512
+ }
513
+ values = {};
514
+ for (_i = 0, reports_2 = reports; _i < reports_2.length; _i++) {
515
+ report = reports_2[_i];
516
+ if (report.hexRd && report.elemName) {
517
+ dataRead = report.hexRd;
518
+ clear = parseInt(dataRead.slice(0, 2), 16);
519
+ red = parseInt(dataRead.slice(2, 4), 16);
520
+ green = parseInt(dataRead.slice(4, 6), 16);
521
+ blue = parseInt(dataRead.slice(6, 8), 16);
522
+ Logger.vipLog(SHOW_LOGS, TAG, "Offset Report -- " + "clear: " + clear + " red: " + red + " green: " + green + " blue: " + blue);
523
+ values[report.elemName] = { clear: clear, red: red, green: green, blue: blue };
524
+ }
525
+ }
526
+ return [2 /*return*/, Object.keys(values).length ? values : null];
527
+ }
528
+ });
529
+ });
530
+ };
531
+ ColourSensorManualCalibratorMarty.getColourSensorReadings = function (names) {
532
+ return __awaiter(this, void 0, void 0, function () {
533
+ var reports, readings, _i, reports_3, report, dataRead, clear, red, green, blue;
534
+ return __generator(this, function (_a) {
535
+ switch (_a.label) {
536
+ case 0:
537
+ Logger.vipLog(SHOW_LOGS, TAG, "\n==== getColourSensorReading ====");
538
+ return [4 /*yield*/, this.collectReports(names, "GET_COLOUR_SENSOR_READING", GET_COLOUR_SENSOR_READING_COMMAND)];
539
+ case 1:
540
+ reports = _a.sent();
541
+ if (reports.length === 0) {
542
+ Logger.vipLog(SHOW_LOGS, TAG, "No reports received");
543
+ return [2 /*return*/, null];
544
+ }
545
+ readings = {};
546
+ for (_i = 0, reports_3 = reports; _i < reports_3.length; _i++) {
547
+ report = reports_3[_i];
548
+ if (report.hexRd && report.elemName) {
549
+ dataRead = report.hexRd;
550
+ clear = parseInt(dataRead.slice(2, 4), 16);
551
+ red = parseInt(dataRead.slice(4, 6), 16);
552
+ green = parseInt(dataRead.slice(6, 8), 16);
553
+ blue = parseInt(dataRead.slice(8, 10), 16);
554
+ Logger.vipLog(SHOW_LOGS, TAG, "getColourSensorReading -- " + "clear: " + clear + " red: " + red + " green: " + green + " blue: " + blue);
555
+ readings[report.elemName] = { clear: clear, red: red, green: green, blue: blue };
556
+ }
557
+ }
558
+ return [2 /*return*/, Object.keys(readings).length ? readings : null];
345
559
  }
346
560
  });
347
561
  });
348
562
  };
349
- ColourSensorManualCalibratorMarty.getColourSensorReading = function (names) {
563
+ ColourSensorManualCalibratorMarty.setOffsets = function (values, delayMs) {
564
+ if (delayMs === void 0) { delayMs = 0; }
350
565
  return __awaiter(this, void 0, void 0, function () {
351
- var REPORT_MSG_KEY, reports, _i, names_3, hwElemName, command, _a, reports_2, report, dataRead, clear, red, green, blue;
352
- var _b;
566
+ var index, _i, _a, _b, name_3, channels, clearHex, redHex, greenHex, blueHex, command, rslt;
353
567
  return __generator(this, function (_c) {
354
568
  switch (_c.label) {
355
569
  case 0:
356
- REPORT_MSG_KEY = "GET_COLOUR_SENSOR_READING";
357
- Logger.info(SHOW_LOGS, TAG, "\n==== getColourSensorReading ====");
358
- reports = [];
359
- this._raftConnector.getRaftMsgHandler()
360
- .reportMsgCallbacksSet(REPORT_MSG_KEY, function (report) {
361
- return __awaiter(this, void 0, void 0, function () {
362
- return __generator(this, function (_a) {
363
- reports.push(report);
364
- Logger.debug(SHOW_LOGS, TAG, "getHWElemCB Report callback ".concat(JSON.stringify(report)));
365
- return [2 /*return*/];
366
- });
367
- });
368
- });
369
- _i = 0, names_3 = names;
570
+ Logger.vipLog(SHOW_LOGS, TAG, "\n==== setOffsets ====");
571
+ index = 0;
572
+ _i = 0, _a = Object.entries(values);
370
573
  _c.label = 1;
371
574
  case 1:
372
- if (!(_i < names_3.length)) return [3 /*break*/, 4];
373
- hwElemName = names_3[_i];
374
- command = GET_COLOUR_SENSOR_READING_COMMAND(hwElemName);
575
+ if (!(_i < _a.length)) return [3 /*break*/, 5];
576
+ _b = _a[_i], name_3 = _b[0], channels = _b[1];
577
+ Logger.vipLog(SHOW_LOGS, TAG, "Setting offset for ".concat(name_3, " to clear: ").concat(channels.clear, " red: ").concat(channels.red, " green: ").concat(channels.green, " blue: ").concat(channels.blue));
578
+ clearHex = this.toHexByte(channels.clear);
579
+ redHex = this.toHexByte(channels.red);
580
+ greenHex = this.toHexByte(channels.green);
581
+ blueHex = this.toHexByte(channels.blue);
582
+ command = SET_OFFSET_COMMAND(name_3, clearHex, redHex, greenHex, blueHex, index);
375
583
  return [4 /*yield*/, this._raftConnector.getRaftMsgHandler().sendRICRESTURL(command)];
376
584
  case 2:
585
+ rslt = _c.sent();
586
+ if (rslt.rslt !== "ok") {
587
+ Logger.vipLog(SHOW_LOGS, TAG, "Error setting offset");
588
+ return [2 /*return*/, false];
589
+ }
590
+ index += 1;
591
+ if (!(delayMs > 0)) return [3 /*break*/, 4];
592
+ return [4 /*yield*/, this.wait(delayMs)];
593
+ case 3:
377
594
  _c.sent();
595
+ _c.label = 4;
596
+ case 4:
597
+ _i++;
598
+ return [3 /*break*/, 1];
599
+ case 5: return [2 /*return*/, true];
600
+ }
601
+ });
602
+ });
603
+ };
604
+ ColourSensorManualCalibratorMarty.setCalibrationValues = function (values) {
605
+ return __awaiter(this, void 0, void 0, function () {
606
+ var index, _i, _a, _b, name_4, reading, didSet;
607
+ return __generator(this, function (_c) {
608
+ switch (_c.label) {
609
+ case 0:
610
+ Logger.vipLog(SHOW_LOGS, TAG, "\n==== setCalibrationValues ====");
611
+ index = 0;
612
+ _i = 0, _a = Object.entries(values);
613
+ _c.label = 1;
614
+ case 1:
615
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
616
+ _b = _a[_i], name_4 = _b[0], reading = _b[1];
617
+ return [4 /*yield*/, this.setCalibrationForSensor(name_4, reading, index)];
618
+ case 2:
619
+ didSet = _c.sent();
620
+ if (!didSet) {
621
+ return [2 /*return*/, false];
622
+ }
623
+ index += 1;
378
624
  _c.label = 3;
379
625
  case 3:
380
626
  _i++;
381
627
  return [3 /*break*/, 1];
382
- case 4:
383
- // wait a couple of seconds for any report messages to be received
384
- return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2000); })];
628
+ case 4: return [2 /*return*/, true];
629
+ }
630
+ });
631
+ });
632
+ };
633
+ ColourSensorManualCalibratorMarty.setCalibrationForSensor = function (name, reading, index) {
634
+ return __awaiter(this, void 0, void 0, function () {
635
+ var clearLow, redLow, greenLow, blueLow, command, rslt;
636
+ return __generator(this, function (_a) {
637
+ switch (_a.label) {
638
+ case 0:
639
+ Logger.vipLog(SHOW_LOGS, TAG, "\n==== setCalibrationForSensor ====");
640
+ Logger.vipLog(SHOW_LOGS, TAG, "Setting calibration to clear: ".concat(reading.clear, " red: ").concat(reading.red, " green: ").concat(reading.green, " blue: ").concat(reading.blue));
641
+ if (!this.validateCalibrationByte(reading.clear, "clear", name) ||
642
+ !this.validateCalibrationByte(reading.red, "red", name) ||
643
+ !this.validateCalibrationByte(reading.green, "green", name) ||
644
+ !this.validateCalibrationByte(reading.blue, "blue", name)) {
645
+ return [2 /*return*/, false];
646
+ }
647
+ clearLow = this.toHexByte(reading.clear);
648
+ redLow = this.toHexByte(reading.red);
649
+ greenLow = this.toHexByte(reading.green);
650
+ blueLow = this.toHexByte(reading.blue);
651
+ command = SET_CALIBRATION_COMMAND(name, clearLow, redLow, greenLow, blueLow, index);
652
+ return [4 /*yield*/, this._raftConnector.getRaftMsgHandler().sendRICRESTURL(command)];
653
+ case 1:
654
+ rslt = _a.sent();
655
+ if (rslt.rslt !== "ok") {
656
+ Logger.vipLog(SHOW_LOGS, TAG, "Error setting calibration");
657
+ return [2 /*return*/, false];
658
+ }
659
+ Logger.vipLog(SHOW_LOGS, TAG, "Calibration completed successfully.");
660
+ return [2 /*return*/, true];
661
+ }
662
+ });
663
+ });
664
+ };
665
+ ColourSensorManualCalibratorMarty.calibrateOffsets = function (names) {
666
+ return __awaiter(this, void 0, void 0, function () {
667
+ var now, skipReset, zeroValues, _i, names_3, name_5, cleared, readings, setOffsets, offsetValues;
668
+ return __generator(this, function (_a) {
669
+ switch (_a.label) {
670
+ case 0:
671
+ Logger.vipLog(SHOW_LOGS, TAG, "\n==== calibrateOffsets ====");
672
+ return [4 /*yield*/, this.ensureColourSensorPowerOn(false)];
673
+ case 1:
674
+ _a.sent();
675
+ now = Date.now();
676
+ skipReset = this._recentOffsetResetAt !== null
677
+ && now - this._recentOffsetResetAt <= this.OFFSET_RESET_WINDOW_MS;
678
+ this._recentOffsetResetAt = null;
679
+ if (!!skipReset) return [3 /*break*/, 3];
680
+ zeroValues = {};
681
+ for (_i = 0, names_3 = names; _i < names_3.length; _i++) {
682
+ name_5 = names_3[_i];
683
+ zeroValues[name_5] = { clear: 0, red: 0, green: 0, blue: 0 };
684
+ }
685
+ return [4 /*yield*/, this.setOffsets(zeroValues, 0)];
686
+ case 2:
687
+ cleared = _a.sent();
688
+ if (!cleared) {
689
+ Logger.vipLog(SHOW_LOGS, TAG, "Failed to clear offsets");
690
+ return [2 /*return*/, false];
691
+ }
692
+ _a.label = 3;
693
+ case 3: return [4 /*yield*/, this.getColourSensorReadings(names)];
694
+ case 4:
695
+ readings = _a.sent();
696
+ if (!readings) {
697
+ Logger.vipLog(SHOW_LOGS, TAG, "Failed to read colour sensor for offset calibration");
698
+ return [2 /*return*/, false];
699
+ }
700
+ return [4 /*yield*/, this.setOffsets(readings, 0)];
385
701
  case 5:
386
- // wait a couple of seconds for any report messages to be received
387
- _c.sent();
388
- // remove report callback
389
- this._raftConnector
390
- .getRaftMsgHandler()
391
- .reportMsgCallbacksDelete(REPORT_MSG_KEY);
392
- if (reports.length === 0) {
393
- Logger.info(SHOW_LOGS, TAG, "No reports received");
702
+ setOffsets = _a.sent();
703
+ if (!setOffsets) {
704
+ Logger.vipLog(SHOW_LOGS, TAG, "Failed to set offsets");
394
705
  return [2 /*return*/, false];
395
706
  }
396
- _a = 0, reports_2 = reports;
397
- _c.label = 6;
707
+ return [4 /*yield*/, this.wait(PAUSE_AFTER_SET_MS)];
398
708
  case 6:
399
- if (!(_a < reports_2.length)) return [3 /*break*/, 9];
400
- report = reports_2[_a];
401
- if (!(report.hexRd && report.elemName)) return [3 /*break*/, 8];
402
- dataRead = report.hexRd;
403
- clear = parseInt(dataRead.slice(2, 4), 16);
404
- red = parseInt(dataRead.slice(4, 6), 16);
405
- green = parseInt(dataRead.slice(6, 8), 16);
406
- blue = parseInt(dataRead.slice(8, 10), 16);
407
- // Logger.info(SHOW_LOGS, TAG, "getColourSensorReading", "clear", clear, "red", red, "green", green, "blue", blue);
408
- Logger.info(SHOW_LOGS, TAG, "getColourSensorReading -- " + "clear: " + clear + " red: " + red + " green: " + green + " blue: " + blue);
409
- this._colourSensorReadings = __assign(__assign({}, this._colourSensorReadings), (_b = {}, _b[report.elemName] = { clear: clear, red: red, green: green, blue: blue }, _b));
410
- return [4 /*yield*/, this.calibrateCS(report.elemName, { clear: clear, red: red, green: green, blue: blue })];
709
+ _a.sent();
710
+ return [4 /*yield*/, this.getOffsetValues(names)];
411
711
  case 7:
412
- if (!(_c.sent())) {
712
+ offsetValues = _a.sent();
713
+ if (!offsetValues) {
714
+ Logger.vipLog(SHOW_LOGS, TAG, "Failed to read offsets");
413
715
  return [2 /*return*/, false];
414
716
  }
415
- _c.label = 8;
416
- case 8:
417
- _a++;
418
- return [3 /*break*/, 6];
419
- case 9: return [2 /*return*/, true];
717
+ return [2 /*return*/, this.compareValues(readings, offsetValues, 0)];
420
718
  }
421
719
  });
422
720
  });
423
721
  };
424
- ColourSensorManualCalibratorMarty.calibrateCS = function (name, reading) {
722
+ ColourSensorManualCalibratorMarty.calibrateWhite = function (names) {
425
723
  return __awaiter(this, void 0, void 0, function () {
426
- var baseAddr, channels, i, channel, highByte, lowByte, command, rslt;
724
+ var readings, didCalibrate, calibrationValues;
427
725
  return __generator(this, function (_a) {
428
726
  switch (_a.label) {
429
727
  case 0:
430
- Logger.info(SHOW_LOGS, TAG, "\n==== calibrateCS ====");
431
- baseAddr = 0x20;
432
- channels = [reading.clear, reading.red, reading.green, reading.blue];
433
- i = 0;
434
- _a.label = 1;
728
+ Logger.vipLog(SHOW_LOGS, TAG, "\n==== calibrateWhite ====");
729
+ return [4 /*yield*/, this.ensureColourSensorPowerOn(false)];
435
730
  case 1:
436
- if (!(i < channels.length)) return [3 /*break*/, 5];
437
- channel = channels[i];
438
- Logger.info(SHOW_LOGS, TAG, "Setting 0x".concat(baseAddr.toString(16), " to ").concat(channel));
439
- highByte = (channel >> 8).toString(16).padStart(2, "0");
440
- lowByte = (channel & 0xff).toString(16).padStart(2, "0");
441
- command = CALIBRATE_COMMAND(name, baseAddr, highByte, lowByte, i);
442
- return [4 /*yield*/, this._raftConnector.getRaftMsgHandler().sendRICRESTURL(command)];
731
+ _a.sent();
732
+ return [4 /*yield*/, this.getColourSensorReadings(names)];
443
733
  case 2:
444
- rslt = _a.sent();
445
- // Logger.info(SHOW_LOGS, TAG, "rslt:" + JSON.stringify(rslt));
446
- if (rslt.rslt !== "ok") {
447
- Logger.info(SHOW_LOGS, TAG, "Error setting calibration");
734
+ readings = _a.sent();
735
+ if (!readings) {
736
+ Logger.vipLog(SHOW_LOGS, TAG, "Failed to read colour sensor for white calibration");
448
737
  return [2 /*return*/, false];
449
738
  }
450
- baseAddr += 2;
451
- return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 1000); })];
739
+ return [4 /*yield*/, this.setCalibrationValues(readings)];
452
740
  case 3:
453
- _a.sent();
454
- _a.label = 4;
741
+ didCalibrate = _a.sent();
742
+ if (!didCalibrate) {
743
+ Logger.vipLog(SHOW_LOGS, TAG, "Failed to set calibration values");
744
+ return [2 /*return*/, false];
745
+ }
746
+ return [4 /*yield*/, this.wait(PAUSE_AFTER_SET_MS)];
455
747
  case 4:
456
- i++;
457
- return [3 /*break*/, 1];
748
+ _a.sent();
749
+ return [4 /*yield*/, this.getCalibrationValues(names)];
458
750
  case 5:
459
- Logger.info(SHOW_LOGS, TAG, "Calibration completed successfully.");
460
- return [2 /*return*/, true];
751
+ calibrationValues = _a.sent();
752
+ if (!calibrationValues) {
753
+ Logger.vipLog(SHOW_LOGS, TAG, "Failed to read calibration values");
754
+ return [2 /*return*/, false];
755
+ }
756
+ return [2 /*return*/, this.compareValues(readings, calibrationValues, TOLERANCE)];
757
+ }
758
+ });
759
+ });
760
+ };
761
+ ColourSensorManualCalibratorMarty.wait = function (ms) {
762
+ return __awaiter(this, void 0, void 0, function () {
763
+ return __generator(this, function (_a) {
764
+ switch (_a.label) {
765
+ case 0: return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, ms); })];
766
+ case 1:
767
+ _a.sent();
768
+ return [2 /*return*/];
769
+ }
770
+ });
771
+ });
772
+ };
773
+ ColourSensorManualCalibratorMarty.ensureColourSensorPowerOn = function (waitAfter) {
774
+ if (waitAfter === void 0) { waitAfter = true; }
775
+ return __awaiter(this, void 0, void 0, function () {
776
+ var error_3;
777
+ return __generator(this, function (_a) {
778
+ switch (_a.label) {
779
+ case 0:
780
+ _a.trys.push([0, 4, , 5]);
781
+ return [4 /*yield*/, this._raftConnector.getRaftMsgHandler().sendRICRESTURL("pwrctrl/5von")];
782
+ case 1:
783
+ _a.sent();
784
+ if (!waitAfter) return [3 /*break*/, 3];
785
+ return [4 /*yield*/, this.wait(POWER_ON_DELAY_MS)];
786
+ case 2:
787
+ _a.sent();
788
+ _a.label = 3;
789
+ case 3: return [3 /*break*/, 5];
790
+ case 4:
791
+ error_3 = _a.sent();
792
+ Logger.vipLog(SHOW_LOGS, TAG, "Failed to enable 5V power for colour sensor: " + JSON.stringify(error_3));
793
+ return [3 /*break*/, 5];
794
+ case 5: return [2 /*return*/];
461
795
  }
462
796
  });
463
797
  });
464
798
  };
465
799
  ColourSensorManualCalibratorMarty._isCalibrating = false;
466
- ColourSensorManualCalibratorMarty._calibrationValues = null;
467
- ColourSensorManualCalibratorMarty._colourSensorReadings = null;
800
+ ColourSensorManualCalibratorMarty._reportMsgKeyCounter = 1000;
801
+ ColourSensorManualCalibratorMarty._recentOffsetResetAt = null;
802
+ ColourSensorManualCalibratorMarty.OFFSET_RESET_WINDOW_MS = 20000;
468
803
  return ColourSensorManualCalibratorMarty;
469
804
  }());
470
805
  export default ColourSensorManualCalibratorMarty;