/**The MIT License Copyright (c) 2017-2020 Exalif Inc Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import { FSUIPC, Simulator, Type, FSUIPCError } from "fsuipc"; import { timer, from, Observable, throwError } from "rxjs"; import { catchError, map, mergeMap } from "rxjs/operators"; //interfaces import { IOptions } from "./lib/interfaces/options"; import { IllegalOperationError, InstanceError, InvalidArgumentType, RuntimeExceptionError, } from "./lib/utils/erros"; import { IConvertedOffsetValues } from "./lib/interfaces/convertOffset"; import { IOffsetValues } from "./lib/interfaces/rawOffset"; import { applyConversion, applyReverseConversion } from "./lib/applyConversion"; import { OFFSETS } from "./lib/offsetList"; import { Offset } from "./lib/offset"; export class FsuipcClient { public isObjectOpen: boolean = false; private fsuipc: FSUIPC; private watchedOffsetCache: Array = []; private offsetList: Array = []; constructor( public options: IOptions = { simulator: Simulator.ANY, interval: 1000, includeRaw: false, closeOnError: true, }, ) { if (!this.options.simulator) this.options.simulator = Simulator.ANY; if (!this.options.closeOnError) this.options.closeOnError = true; if (!this.options.includeRaw) this.options.includeRaw = false; this.fsuipc = new FSUIPC(); } public async connect(): Promise { try { if (this.options.simulator) this.fsuipc = await this.fsuipc.open(this.options.simulator); else this.fsuipc = await this.fsuipc.open(); return (this.isObjectOpen = true); } catch (error: any) { throw new FSUIPCError(error.message, error.code); } } public listen(offsetList: string[]): Observable { if (!this.fsuipc || !this.isObjectOpen) throw new InstanceError("fsuipc instance not found"); this.offsetList = offsetList; this.watchOffsets(this.offsetList); return timer(this.options.interval, this.options.interval).pipe( mergeMap(() => from(this.fsuipc.process()).pipe( map((result: object) => { const rawOffsetValues: IOffsetValues = { ...result }; let offsetValues: IConvertedOffsetValues = {}; for (let offsetName of Object.keys(rawOffsetValues)) { if (!this.options.includeRaw) offsetValues = { ...offsetValues, [offsetName]: applyConversion( OFFSETS[offsetName], rawOffsetValues[offsetName], ), }; else offsetValues = { ...offsetValues, [offsetName]: applyConversion( OFFSETS[offsetName], rawOffsetValues[offsetName], ), [`${offsetName}Raw`]: rawOffsetValues[offsetName], }; } return offsetValues; }), catchError((error: FSUIPCError | InvalidArgumentType) => { if (this.options.closeOnError) { this.fsuipc.close(); this.isObjectOpen = false; } return throwError( () => new RuntimeExceptionError(error.message, error.code), ); }), ), ), ); } //Dynamically add offsetwhile is listening public addOffset(offset: string | Array) { if (!this.isObjectOpen) throw new IllegalOperationError("Cannot add offset to a close instance"); if (typeof offset === "string" && this.offsetList.indexOf(offset) === -1) { this.offsetList.push(offset); this.watchOffsets(this.offsetList); } else if (Array.isArray(offset)) { offset = offset.filter((item) => this.offsetList.indexOf(item) === -1); this.offsetList = [...this.offsetList, ...offset]; this.watchOffsets(this.offsetList); } } public async writeOffset( offsetTowrite: string, value: any, ): Promise { if (!this.fsuipc || !this.isObjectOpen) throw new InstanceError("fsuipc instance not found"); const offset: Offset = OFFSETS[offsetTowrite]; if (!offset) throw new RuntimeExceptionError("Offset does not exist", 404); //http error like to inidicate non existance of the offset else if (offset.permission === "r") throw new IllegalOperationError( "Operation not permitted offset is in read only mode", ); try { if ( offset.type !== Type.BitArray && offset.type !== Type.String && offset.type !== Type.ByteArray && offset.type !== Type.UInt64 && offset.type !== Type.Int64 ) { const offsetType = offset.type as Type.Byte; const valueToWrite = parseFloat( applyReverseConversion(offset, value) as string, ); this.fsuipc.write(offset.value, offsetType, valueToWrite); return true; } else if (offset.type === Type.UInt64 || offset.type === Type.Int64) { const offsetType = offset.type as Type.String; const valueToWrite = parseInt( applyReverseConversion(offset, value) as string, ) as any; this.fsuipc.write(offset.value, offsetType, valueToWrite, valueToWrite); return true; } else throw new IllegalOperationError( "Offset of type bitArray, ByteArray, string are not supported", ); } catch (error) { throw error; } } public async disconnect(): Promise { try { await this.fsuipc.close(); } catch (error) { throw new InstanceError("Error closing instance with reason: " + error); } //return anyway close object so that the client know that he as to stop for asking. return (this.isObjectOpen = false); } private watchOffsets(offsetList: string[]): void { if (this.shouldUpdateCache(offsetList)) { this.watchedOffsetCache = offsetList; } for (const offsetName of this.watchedOffsetCache) { const offset: Offset = OFFSETS[offsetName]; if ( offset.type === Type.ByteArray || offset.type === Type.String || offset.type === Type.BitArray ) { const offsetType = offset.type as | Type.ByteArray | Type.String | Type.BitArray; this.fsuipc.add(offset.name, offset.value, offsetType, offset.length); } else { const offsetType = offset.type as | Type.Byte | Type.SByte | Type.Int16 | Type.Int32 | Type.Int64 | Type.UInt16 | Type.UInt32 | Type.UInt64 | Type.Double | Type.Single; this.fsuipc.add(offset.name, offset.value, offsetType); } } } private shouldUpdateCache(offsetList: string[] = []): boolean { return ( offsetList.length > 0 && (!this.watchedOffsetCache.length || !this.watchedOffsetCache.every((item) => offsetList.includes(item))) ); } } export { Simulator } from "fsuipc";