import {Buffer} from "buffer"; import {Unsigned8} from "./Unsigned8"; import {AxdrType} from "../asn1.axdr/AxdrType"; import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream"; import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream"; export class Time implements AxdrType{ dataCode: Buffer | null = null; hour: Unsigned8 | null = null; minute: Unsigned8 | null = null; second: Unsigned8 | null = null; constructor() { } set_dataCode(dataCode: Buffer) { this.dataCode = dataCode; } set_time(hour: Unsigned8, minute: Unsigned8, second: Unsigned8) { this.hour = hour; this.minute = minute; this.second = second; } setHour(hour: Unsigned8){ this.hour = hour; } setMinute(minute: Unsigned8){ this.minute = minute; } setSecond(second: Unsigned8){ this.second = second; } decode(input: ReverseByteArrayInputStream): number { let codeLength = 0; this.hour = new Unsigned8(); this.hour.set_const() codeLength += this.hour.decode(input); this.minute = new Unsigned8(); this.minute.set_const() codeLength += this.minute.decode(input); this.second = new Unsigned8(); this.second.set_const() codeLength += this.second.decode(input); return codeLength; } encode(output: ReverseByteArrayOutputStream): number { let codeLength; if (this.dataCode != null) { codeLength = this.dataCode.length; for (let i = this.dataCode.length - 1; i >= 0; i--) { output.write(this.dataCode[i]); } } else { if(this.hour != null && this.minute != null && this.second != null){ codeLength = 0; codeLength += this.second.encode(output); codeLength += this.minute.encode(output); codeLength += this.hour.encode(output); }else{ throw new Error("time is null"); } } return codeLength; } encodeAndSave(encodingSizeGuess: number) { let revOStream = new ReverseByteArrayOutputStream(); revOStream.setBufSize(encodingSizeGuess); this.encode(revOStream); this.dataCode = revOStream.getArray(); } toString() : string{ if(this.hour != null && this.minute != null && this.second != null){ return "sequence: {" + "hour: " + this.hour + ", minute: " + this.minute + ", second: " + this.second + "}"; }else{ return "time is null"; } } }