@robotical/roboticaljs
Version:
Javascript/TS library for Robotical products
161 lines • 6.48 kB
JavaScript
"use strict";
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// RICJS
// Communications Library
//
// Rob Dobson & Chris Greening 2020-2022
// (C) 2020-2022
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Object.defineProperty(exports, "__esModule", { value: true });
const raftjs_1 = require("@robotical/raftjs");
class CogLEDPatternChecker {
constructor() {
// LED pattern refresh
this._ledPatternTimeoutMs = 10000;
this._ledPatternRefreshTimer = null;
// Verification of correct device
this._ledColours = new Array();
this._lcdColours = new Array();
this._bleVerifActive = false;
// Message handler, etc
this._msgHandler = null;
this._onConnEvent = null;
this._isConnectedFn = () => false;
}
/**
* Start checking correct RIC using LED pattern
*
* @param {RICLedLcdColours} ricLedLcdColours - available colours
* @param {RaftMsgHandler} msgHandler - message handler
* @param {RaftConnEventFn} onConnEvent - connection event handler
* @param {() => boolean} isConnectedFn - function to check if connected
* @return boolean - true if started ok
*
*/
async checkCorrectRICStart(ricLedLcdColours, msgHandler, onConnEvent, isConnectedFn) {
// Store the onConnEvent, etc
this._msgHandler = msgHandler;
this._onConnEvent = onConnEvent;
this._isConnectedFn = isConnectedFn;
// Set colour pattern checker colours
const randomColours = this.setupColours(ricLedLcdColours);
// Start timer to repeat checking LED pattern
raftjs_1.RaftLog.debug(`checkCorrectRICStart: starting LED pattern checker`);
if (!await this._checkCorrectRICRefreshLEDs()) {
return false;
}
// Event
onConnEvent(raftjs_1.RaftConnEvent.CONN_VERIFYING_CORRECT, randomColours);
// Start timer to repeat sending of LED pattern
// This is because RIC's LED pattern override times out after a while
// so has to be refreshed periodically
this._ledPatternRefreshTimer = setInterval(async () => {
raftjs_1.RaftLog.verbose(`checkCorrectRICStart: loop`);
if (!this._checkCorrectRICRefreshLEDs()) {
raftjs_1.RaftLog.debug('checkCorrectRICStart no longer active - clearing timer');
this._clearLedPatternRefreshTimer();
}
}, this._ledPatternTimeoutMs / 2.1);
return true;
}
/**
* Stop checking correct RIC
* @param {boolean} confirmCorrectRIC - true if user confirmed correct RIC
* @return void
*
*/
async checkCorrectRICStop(confirmCorrectRIC) {
// Stop refreshing LED pattern on RIC
this._clearLedPatternRefreshTimer();
// Stop the LED pattern checker if connected
if (this._isConnectedFn()) {
await this.clearRICColors();
}
// Check correct
if (!confirmCorrectRIC) {
// Event
if (this._onConnEvent)
this._onConnEvent(raftjs_1.RaftConnEvent.CONN_REJECTED);
// Indicate as rejected if we're not connected or if user didn't confirm
return false;
}
// Event
if (this._onConnEvent)
this._onConnEvent(raftjs_1.RaftConnEvent.CONN_VERIFIED_CORRECT);
return true;
}
/**
* Refresh LED pattern on RIC
*
* @return boolean - true if checking still active
*
*/
async _checkCorrectRICRefreshLEDs() {
// Check LED pattern is active
if (!this.isActive()) {
return false;
}
// Check connected
raftjs_1.RaftLog.debug(`_verificationRepeat getting isConnected`);
if (!this._isConnectedFn()) {
console.warn('_verificationRepeat not connected');
return false;
}
// Repeat the LED pattern (RIC times out the LED override after ~10 seconds)
raftjs_1.RaftLog.debug(`_verificationRepeat setting pattern`);
return await this.setRICColors();
}
_clearLedPatternRefreshTimer() {
if (this._ledPatternRefreshTimer) {
clearInterval(this._ledPatternRefreshTimer);
this._ledPatternRefreshTimer = null;
}
}
isActive() {
return this._bleVerifActive;
}
clear() {
this._bleVerifActive = false;
}
setupColours(availableColors) {
// Check length of available colours
if (availableColors.length == 0) {
raftjs_1.RaftLog.warn('start no available colours');
}
// Random colour selection
const LED_1 = availableColors[Math.floor(Math.random() * availableColors.length)];
const LED_2 = availableColors[Math.floor(Math.random() * availableColors.length)];
const LED_3 = availableColors[Math.floor(Math.random() * availableColors.length)];
// LED and LCD colours are different to attempt to be visually similar
// 12 in total. 4 each
this._ledColours = [LED_1.led, LED_1.led, LED_1.led, LED_1.led, LED_2.led, LED_2.led, LED_2.led, LED_2.led, LED_3.led, LED_3.led, LED_3.led, LED_3.led];
this._lcdColours = [LED_1.lcd, LED_1.lcd, LED_1.lcd, LED_1.lcd, LED_2.lcd, LED_2.lcd, LED_2.lcd, LED_2.lcd, LED_3.lcd, LED_3.lcd, LED_3.lcd, LED_3.lcd];
// Set the colours to display on LEDs
this._bleVerifActive = true;
// Return LCD colours to display
return this._lcdColours;
}
async setRICColors() {
var _a;
// Set bonding colours
raftjs_1.RaftLog.debug('setRICColors setting colours');
(_a = this._msgHandler) === null || _a === void 0 ? void 0 : _a.sendRICRESTURL(`led/ring/setleds/${this._ledColours.join('').replace(/#/g, '')}`);
return true;
}
async clearRICColors() {
// Clear the LED colours
raftjs_1.RaftLog.debug('clearRICColors');
try {
if (this._msgHandler) {
await this._msgHandler.sendRICRESTURL(`led/ring/color/#000000`);
}
}
catch (error) {
raftjs_1.RaftLog.debug(`clearRICColors failed to send ${error}`);
}
}
}
exports.default = CogLEDPatternChecker;
//# sourceMappingURL=CogLEDPatternChecker.js.map