All files / sportident/src/fakes FakeSiMainStation.ts

94.64% Statements 53/56
88.88% Branches 24/27
100% Functions 9/9
94.64% Lines 53/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 1331x 1x   1x 1x 1x     1x   1x 1x       1x       4x     4x       4x       15x             18x             4x       2x 2x 2x       12x     12x 1x 1x       11x 2x       9x 2x 2x       7x 1x       6x 1x 1x 1x       5x 1x 1x 1x               4x 1x 1x 1x 1x 4x   1x 1x       3x         1x     1x 1x 1x   2x 1x                 1x  
import {proto} from '../constants';
import * as utils from '../utils';
import * as storage from '../storage';
import * as siProtocol from '../siProtocol';
import {ISiStationStorageFields, siStationStorageDefinition} from '../SiStation/BaseSiStation';
import {FakeSiMainStationEvents, FakeSiMainStationMessageEvent} from './IFakeSiMainStation';
import {IFakeSiCard} from './FakeSiCard/IFakeSiCard';
 
export class FakeSiMainStation {
    public storage: storage.ISiStorage<ISiStationStorageFields>;
    public isMaster = true;
    public dateOffset = 0;
    public fakeSiCard?: IFakeSiCard;
 
    constructor(storageArg: (number|undefined)[]|undefined) {
        this.storage = siStationStorageDefinition(storageArg);
    }
 
    dispatchCardMessage(cardMessage: siProtocol.SiMessage): void {
        Iif (cardMessage.mode !== undefined) {
            return;
        }
        const message = {
            command: cardMessage.command,
            parameters: [...this.getCode(), ...cardMessage.parameters],
        };
        this.dispatchMessage(message);
    }
 
    dispatchMessage(message: siProtocol.SiMessage): void {
        this.dispatchEvent(
            'message',
            new FakeSiMainStationMessageEvent(this, message),
        );
    }
 
    getCode(): number[] {
        return [
            ((this.storage.data.get(0x73)! & 0xC0) << 2),
            this.storage.data.get(0x72)!,
        ];
    }
 
    getDateTime(): Date {
        return new Date(Date.now() + this.dateOffset);
    }
 
    insertCard(fakeSiCard: IFakeSiCard): void {
        this.fakeSiCard = fakeSiCard;
        const cardMessage = this.fakeSiCard.handleDetect();
        this.dispatchCardMessage(cardMessage);
    }
 
    sendMessage(message: siProtocol.SiMessage): void {
        Iif (message.mode !== undefined) {
            return;
        }
        if (message.command === proto.cmd.SIGNAL) {
            const numSignal = message.parameters[0];
            this.dispatchMessage({
                command: proto.cmd.SIGNAL,
                parameters: [...this.getCode(), numSignal],
            });
        } else if (message.command === proto.cmd.GET_MS) {
            this.dispatchMessage({
                command: proto.cmd.GET_MS,
                parameters: [...this.getCode(), this.isMaster ? proto.P_MS_DIRECT : proto.P_MS_REMOTE],
            });
        } else if (message.command === proto.cmd.SET_MS) {
            this.isMaster = message.parameters[0] === proto.P_MS_DIRECT;
            this.dispatchMessage({
                command: proto.cmd.SET_MS,
                parameters: [...this.getCode(), this.isMaster ? proto.P_MS_DIRECT : proto.P_MS_REMOTE],
            });
        } else if (message.command === proto.cmd.GET_TIME) {
            this.dispatchMessage({
                command: proto.cmd.GET_TIME,
                parameters: [...this.getCode(), ...siProtocol.date2arr(this.getDateTime())],
            });
        } else if (message.command === proto.cmd.SET_TIME) {
            const newTime = siProtocol.arr2date(message.parameters.slice(0, 7));
            console.log(newTime); // TODO: Use new time to set dateOffset
            this.dispatchMessage({
                command: proto.cmd.SET_TIME,
                parameters: [...this.getCode(), ...siProtocol.date2arr(this.getDateTime())],
            });
        } else if (message.command === proto.cmd.GET_SYS_VAL) {
            const offset = message.parameters[0];
            const length = message.parameters[1];
            this.dispatchMessage({
                command: proto.cmd.GET_SYS_VAL,
                parameters: [
                    ...this.getCode(),
                    offset,
                    ...this.storage.data.slice(offset, offset + length),
                ] as number[],
            });
        } else if (message.command === proto.cmd.SET_SYS_VAL) {
            const offset = message.parameters[0];
            const newContent = message.parameters.slice(1);
            let data = this.storage.data;
            newContent.forEach((newByte: number, index: number) => {
                data = data.set(offset + index, newByte);
            });
            this.storage = siStationStorageDefinition(data);
            this.dispatchMessage({
                command: proto.cmd.SET_SYS_VAL,
                parameters: [...this.getCode(), offset],
            });
        } else if (
            message.command === proto.cmd.GET_SI5
            || message.command === proto.cmd.GET_SI6
            || message.command === proto.cmd.GET_SI8
        ) {
            Iif (this.fakeSiCard === undefined) {
                return;
            }
            const cardMessages = this.fakeSiCard.handleRequest(message);
            cardMessages.forEach((cardMessage: siProtocol.SiMessage) => {
                this.dispatchCardMessage(cardMessage);
            });
        } else if (message.command === proto.cmd.ERASE_BDATA) {
            this.dispatchMessage({
                command: proto.cmd.ERASE_BDATA,
                parameters: [...this.getCode()],
            });
        }
    }
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface FakeSiMainStation extends utils.EventTarget<FakeSiMainStationEvents> {}
utils.applyMixins(FakeSiMainStation, [utils.EventTarget]);