javasrcipt array


1:array group
const groups = [1,2,3,2,4,3,1].reduce((prev, curr) => {
if (!prev.includes(curr)) {
prev.push(curr);
}
return prev;
}, []);

import { Component, OnInit, OnDestroy } from '@angular/core';
import { logger } from '../../../../../../share/utils/logs';
import { TerminalHardware } from '../../../../services/terminal/hardware.service';
import { terminalConfig } from '../../../../../../share/main-proc/terminal-config/terminal-config';
import { HardwareAPI } from '../../../../../../share/protocols/kiosk-api-event';
import { HardwareError } from '../../../../services/terminal/terminal-config.service';
import { ListenEvent } from '../../../../services/terminal/terminal-config.service';
import { ActivatedRoute, Router } from '@angular/router';
import { PublicMethod } from '../../../../services/terminal/public-method.service';
import { kioskPlog } from '../../../../../../share/main-proc/kiosk-plog/kiosk-plog';
import { ErrorResolveService } from '../../../../services/error-resolve.service';
import { HardwareService } from '../../../../services/hardware/hardware.service';
import { CommonService } from '../../../../services/common.service';
import { DialogService } from '../../../../services/dialog.service';
import { ErrorSubModule } from '../../../../models/errorcontext';
import {
    ResponseObject,
    ResponseObjectCode,
} from '../../../../../../share/models/response-common';
import {
    PrinterStatus,
    DoorOpen,
    SystemMonitorErrorArg,
    HelplightErrorArg,
} from '../../../../../../share/models/hw-serv/event-args';
@Component({
    selector: 'app-system-watch',
    templateUrl: './system-watch.component.html',
    styleUrls: [
        './system-watch.component.scss',
        '../../common.scss',
        '../nfc-reader/nfc-reader.component.scss',
    ],
})
export class SystemWatchComponent implements OnInit, OnDestroy {
    constructor(
        private activatedRoute: ActivatedRoute,
        private router: Router,
        private dataService: TerminalHardware,
        private publicMethod: PublicMethod,
        private listenEvent: ListenEvent,
        private errorSvc: ErrorResolveService,
        private commonSvc: CommonService,
        private hwSvc: HardwareService,
        private dialogSvc: DialogService
    ) {
        this.commonSvc.enterPageLog(SystemWatchComponent.name);
    }
    helpLightVersion = {
        hardwareVersion: '',
        driverVersion: '',
    };
    sysMonVersion = {
        hardwareVersion: '',
        driverVersion: '',
    };
    doorStatus = [
        {
            normal: true,
            imgSrc: 'assets/imgs/terminal/gray01.png',
            content: 'PAPER SENSOR',
        },
        {
            normal: true,
            imgSrc: 'assets/imgs/terminal/gray01.png',
            content: 'MAIN DOOR',
        },
        {
            normal: true,
            imgSrc: 'assets/imgs/terminal/gray01.png',
            content: 'TICKET DOOR',
        },
        {
            normal: true,
            imgSrc: 'assets/imgs/terminal/gray01.png',
            content: 'PRINTER DOOR',
        },
    ];
    initHelpLight = false;
    isOn = false;
    isOff = false;
    hardwareId = terminalConfig.hardwareId;
    listen: any = {
        sysMonitorError: null,
        helpLightError: null,
        paperSensorStatus: null,
        doorStatus: null,
    };
    tipCn = '';
    tipEn = '';
    ngOnInit() {
        // this.removeWholeDoorListen();
        // this.removeWholePrinterListen();
        this.init();
    }
    removeWholeDoorListen() {
        if (this.listenEvent.doorListen) {
            this.listenEvent.doorListen.unsubscribe();
        }
    }
    startWholeDoorListen() {
        this.publicMethod.setListenDoor();
    }
    removeWholePrinterListen() {
        this.listenEvent.printerErrorListen.unsubscribe();
    }

    clearError() {
        kioskPlog.helpLightError = 0;
        kioskPlog.doorSensorError = 0;
    }
    async back() {
        // Door must be closed before continue
        const [id, isOpen]: any = await this.hwSvc.isAnyDoorOpened();
        if (isOpen) {
            logger.info('Door must be closed before continue');
            this.errorSvc.finishHandling();
            this.errorSvc.triggerDoorOpenError(id);
            return;
        }
        // this.startWholePrinterListen();
        // this.startWholeDoorListen();
        this.activatedRoute.queryParams.subscribe((queryParam) => {
            if (this.errorSvc.inErrorMessageFlow) {
                this.errorSvc.toNextPage();
            } else if (queryParam.hasOwnProperty('prevPath')) {
                this.router.navigateByUrl('/terminal');
            } else {
                history.back();
            }
        });
    }
    getVersion(path, type, hardwareName) {
        return new Promise((res, rej) => {
            this.dataService.getInfo(path).then(
                (data: ResponseObject) => {
                    // x=JSON.parse(x)
                    if (data.code === ResponseObjectCode.SUCCESS_CODE) {
                        if (hardwareName === HardwareAPI.SYSTEM_MONITER) {
                            this.sysMonVersion[type] = data.body;
                        }
                        if (hardwareName === HardwareAPI.HELP_LIGHT) {
                            this.helpLightVersion[type] = data.body;
                        }
                        res(true);
                    } else {
                        logger.info(path, type, data);
                        res(false);
                    }
                },
                (err) => {
                    logger.error(err);
                    res(false);
                }
            );
        });
    }
    ngOnDestroy() {
        this.errorSvc.errorContext.ErrorSubModule = ErrorSubModule.None;
        this.listen.sysMonitorError.unsubscribe();
        this.listen.helpLightError.unsubscribe();
        this.listen.paperSensorStatus.unsubscribe();
        this.listen.doorStatus.unsubscribe();
        this.commonSvc.exitPageLog(SystemWatchComponent.name);
    }
    getPaperSensorStatus() {
        return new Promise((res, rej) => {
            this.dataService.getPaperSensorStatus().then((data) => {
                if (data.code === ResponseObjectCode.SUCCESS_CODE) {
                    this.paperSensorLight(data.body);
                    res(true);
                } else {
                    logger.info('getPaperSensorStatus fail', data);
                    res(false);
                }
            });
        });
    }
    paperSensorLight(data) {
        switch (data) {
            case PrinterStatus.normal:
                this.doorStatus[0].normal = true;
                this.doorStatus[0].imgSrc = 'assets/imgs/terminal/gray01.png';
                break;
            case PrinterStatus.malfunction:
            case PrinterStatus.paperOut:
            case PrinterStatus.paperLow:
            case PrinterStatus.paperJam:
                this.doorStatus[0].imgSrc = 'assets/imgs/terminal/red01.png';
                this.doorStatus[0].normal = false;
                break;
            default:
                break;
        }
    }
    listenPaperSensor() {
        this.listen.paperSensorStatus = this.dataService
            .listenPrintError()
            .subscribe((data) => {
                console.log(data, 'aboutPaperSensor');
                this.paperSensorLight(data);
            });
    }
    getDoorStatus() {
        return new Promise((res, rej) => {
            this.dataService.getDoorStatus().then((data) => {
                if (data.code === ResponseObjectCode.SUCCESS_CODE) {
                    const doors = data.body.doors;
                    for (const door of doors) {
                        this.handleDoorValue(door);
                    }
                    res(true);
                } else {
                    logger.info('getDoorStatus fail', data);
                    res(false);
                }
            });
        });
    }

    handleDoorValue(door) {
        console.log('door', door);
        switch (door.id) {
            case DoorOpen.MainDoor:
            case DoorOpen.ScannerDoor:
            case DoorOpen.PrinterDoor:
                this.doorStatus[door.id].normal = !door.isOpen;
                this.doorStatus[door.id].imgSrc =
                    'assets/imgs/terminal/' +
                    (door.isOpen ? 'red01.png' : 'gray01.png');
                break;
        }
    }
    listenDoorStatus() {
        this.listen.doorStatus = this.dataService
            .listenDoor()
            .subscribe((data) => {
                this.handleDoorValue(data);
            });
    }
    async init() {
        this.commonSvc.loading();
        await this.getVersion(
            `${HardwareAPI.SYSTEM_MONITER}/${HardwareAPI.VERSION}`,
            'hardwareVersion',
            HardwareAPI.SYSTEM_MONITER
        );
        await this.getVersion(
            `${HardwareAPI.SYSTEM_MONITER}/${HardwareAPI.LIB_VERSION}`,
            'driverVersion',
            HardwareAPI.SYSTEM_MONITER
        );
        // get system monitor all type version
        await this.getVersion(
            `${HardwareAPI.HELP_LIGHT}/${HardwareAPI.VERSION}`,
            'hardwareVersion',
            HardwareAPI.HELP_LIGHT
        );
        await this.getVersion(
            `${HardwareAPI.HELP_LIGHT}/${HardwareAPI.LIB_VERSION}`,
            'driverVersion',
            HardwareAPI.HELP_LIGHT
        );
        // get help ligth all type version
        await this.getDoorStatus();
        await this.getPaperSensorStatus();
        // initHelpLight
        this.listenPaperSensor();
        this.listenDoorStatus();
        this.listen.sysMonitorError = this.dataService
            .systemMonitorError()
            .subscribe((error) => {
                switch (error) {
                    case SystemMonitorErrorArg.malfunction:
                        this.tipCn = '硬件故障';
                        this.tipEn = 'SYSMON ERROR - HARDWARE MALFUNCTION';
                        break;

                    default:
                        break;
                }
            });
        this.listen.helpLightError = this.dataService
            .helpLightError()
            .subscribe((error) => {
                switch (error) {
                    case HelplightErrorArg.malfunction:
                        this.helpLightErrTip();
                        break;

                    default:
                        break;
                }
            });
        this.commonSvc.cancelLoading();
    }
    switchChange($event) {
        this.commonSvc.loading();
        setTimeout(() => {
            const value = $event
                ? `${HardwareAPI.HELP_LIGHT}/${HardwareAPI.LIGHT_ON}`
                : `${HardwareAPI.HELP_LIGHT}/${HardwareAPI.LIGHT_OFF}`;
            this.dataService.getInfo(value).then(
                (res: ResponseObject) => {
                    this.commonSvc.cancelLoading();
                    // x=JSON.parse(x)
                    if (res.code === ResponseObjectCode.SUCCESS_CODE) {
                        this.tipCn = '';
                        this.tipEn = '';
                        if (
                            value ===
                            `${HardwareAPI.HELP_LIGHT}/${HardwareAPI.LIGHT_ON}`
                        ) {
                            console.log(111);
                            this.isOn = true;
                            this.isOff = false;
                        } else {
                            this.isOn = false;
                            this.isOff = true;
                        }
                    } else {
                        logger.info(value, res);
                        this.helpLightErrTip();
                    }
                },
                (err) => {
                    this.commonSvc.cancelLoading();
                    this.helpLightErrTip();
                    logger.error(err);
                    console.log(err);
                }
            );
        }, 1000);
    }
    helpLightErrTip() {
        this.tipCn = '硬件故障';
        this.tipEn = 'HELP LIGHT ERROR - Help Light Malfunction';
    }
}