properties beginning with two underscores will be automatically hidden from json

This commit is contained in:
Wolfgang Hottgenroth 2017-08-10 16:30:06 +02:00
parent 3d551201e8
commit e857b7baa7
2 changed files with 16 additions and 20 deletions

View File

@ -4,25 +4,24 @@ import {MeterbusLibUtils} from './utils'
export namespace MeterbusLibFrames {
export abstract class Frame {
protected _telegram : number[]
protected _startCharacter : number
protected _frameLength : number
protected _firstPayload : number
protected _payloadLength : number
protected __startCharacter : number
protected __frameLength : number
protected __firstPayload : number
protected __payloadLength : number
constructor(startCharacter : number, frameLength : number,
firstPayload : number, telegram : number[]) {
this._startCharacter = startCharacter
this._frameLength = frameLength
this._firstPayload = firstPayload
this.__startCharacter = startCharacter
this.__frameLength = frameLength
this.__firstPayload = firstPayload
this._telegram = telegram
this._payloadLength = this._frameLength - 6
this.__payloadLength = this.__frameLength - 6
}
toJSON() : any {
return MeterbusLibUtils.jsonPrepaper(this, ["_startCharacter", "_frameLength",
"_firstPayload", "_payloadLength" ])
return MeterbusLibUtils.jsonPrepaper(this, [])
}
getJSON() : string {
@ -30,12 +29,12 @@ export namespace MeterbusLibFrames {
}
verifyChecksumAndEnd() {
if (this._firstPayload != 0) {
if (this.__firstPayload != 0) {
if (this._telegram[this._telegram.length - 1] != 0x16) {
throw new MeterbusLibExceptions.InvalidStopCharError()
}
let checksum : number = 0
this._telegram.slice(this._firstPayload, this._telegram.length -2).forEach((val) => {
this._telegram.slice(this.__firstPayload, this._telegram.length -2).forEach((val) => {
checksum += val
})
// console.log(`calc. checksum ${checksum}, ${checksum & 0xff}`)
@ -49,10 +48,10 @@ export namespace MeterbusLibFrames {
abstract parse2()
parse() {
if (this._telegram[0] != this._startCharacter) {
if (this._telegram[0] != this.__startCharacter) {
throw new MeterbusLibExceptions.InvalidStartCharError()
}
if (this._telegram.length != this._frameLength) {
if (this._telegram.length != this.__frameLength) {
throw new MeterbusLibExceptions.InvalidLengthError()
}
this.verifyChecksumAndEnd()
@ -83,10 +82,10 @@ export namespace MeterbusLibFrames {
}
parse2() {
if (this._telegram[2] != this._payloadLength) {
if (this._telegram[2] != this.__payloadLength) {
throw new MeterbusLibExceptions.InvalidSecondLengthError()
}
if (this._payloadLength < 3) {
if (this.__payloadLength < 3) {
throw new MeterbusLibExceptions.PayloadTooShortError()
}

View File

@ -17,10 +17,7 @@ export namespace MeterbusLibUtils {
export function jsonPrepaper(obj:any, hideKeys:string[]) : any {
let dup = {}
for (let key in obj) {
if (hideKeys.indexOf(key) == -1) {
//if (key[0] == "_") {
// key = key.slice(1)
//}
if ((hideKeys.indexOf(key) == -1) && ! ((key[0] == "_") && (key[1] == "_"))) {
let dkey = (key[0] == "_") ? key.slice(1) : key
dup[dkey] = obj[key]
}