11import {
2- Gateway as ChirpstackGateway ,
32 CreateGatewayRequest ,
43 DeleteGatewayRequest ,
4+ Gateway as ChirpstackGateway ,
55 GetGatewayMetricsRequest ,
66 GetGatewayMetricsResponse ,
77 GetGatewayRequest ,
@@ -53,6 +53,12 @@ import { Repository } from "typeorm";
5353
5454@Injectable ( )
5555export class ChirpstackGatewayService extends GenericChirpstackConfigurationService {
56+ GATEWAY_STATS_INTERVAL_IN_DAYS = 29 ;
57+ GATEWAY_LAST_ACTIVE_SINCE_IN_MINUTES = 3 ;
58+ private readonly logger = new Logger ( ChirpstackGatewayService . name , {
59+ timestamp : true ,
60+ } ) ;
61+
5662 constructor (
5763 @InjectRepository ( DbGateway )
5864 private gatewayRepository : Repository < DbGateway > ,
@@ -62,11 +68,6 @@ export class ChirpstackGatewayService extends GenericChirpstackConfigurationServ
6268 ) {
6369 super ( ) ;
6470 }
65- GATEWAY_STATS_INTERVAL_IN_DAYS = 29 ;
66- GATEWAY_LAST_ACTIVE_SINCE_IN_MINUTES = 3 ;
67- private readonly logger = new Logger ( ChirpstackGatewayService . name , {
68- timestamp : true ,
69- } ) ;
7071
7172 async createNewGateway ( dto : CreateGatewayDto , userId : number ) : Promise < ChirpstackResponseStatus > {
7273 dto . gateway = await this . updateDtoContents ( dto . gateway ) ;
@@ -87,8 +88,9 @@ export class ChirpstackGatewayService extends GenericChirpstackConfigurationServ
8788
8889 const gatewayChirpstack = await this . mapToChirpstackGateway ( dto , chirpstackLocation ) ;
8990 Object . entries ( dto . gateway . tags ) . forEach ( ( [ key , value ] ) => {
90- gatewayChirpstack . getTagsMap ( ) . set ( key , value ) ;
91+ gatewayChirpstack . getTagsMap ( ) . set ( key , value . toString ( ) ) ;
9192 } ) ;
93+ const test = gatewayChirpstack . getTagsMap ( ) ;
9294
9395 req . setGateway ( gatewayChirpstack ) ;
9496
@@ -302,54 +304,6 @@ export class ChirpstackGatewayService extends GenericChirpstackConfigurationServ
302304 }
303305 }
304306
305- //TODO: This could be moved to a helper function in the future, since it has a lot of similarities with metrics from chirpstack devices.
306- private mapPackets ( metrics : GetGatewayMetricsResponse ) {
307- const gatewayResponseDto : GatewayStatsElementDto [ ] = [ ] ;
308- const packetCounts : { [ timestamp : string ] : { rx : number ; tx : number } } = { } ;
309-
310- const rxTimestamps = metrics . getRxPackets ( ) . getTimestampsList ( ) ;
311- const rxPackets = metrics
312- . getRxPackets ( )
313- . getDatasetsList ( )
314- . find ( e => e . getLabel ( ) === "rx_count" )
315- . getDataList ( ) ;
316-
317- this . processPackets ( rxTimestamps , rxPackets , "rx" , packetCounts ) ;
318-
319- const txTimestamps = metrics . getTxPackets ( ) . getTimestampsList ( ) ;
320- const txPackets = metrics
321- . getTxPackets ( )
322- . getDatasetsList ( )
323- . find ( e => e . getLabel ( ) === "tx_count" )
324- . getDataList ( ) ;
325-
326- this . processPackets ( txTimestamps , txPackets , "tx" , packetCounts ) ;
327-
328- Object . keys ( packetCounts ) . forEach ( timestamp => {
329- const packetCount = packetCounts [ timestamp ] ;
330- const dto : GatewayStatsElementDto = {
331- timestamp,
332- rxPacketsReceived : packetCount . rx ,
333- txPacketsEmitted : packetCount . tx ,
334- } ;
335- gatewayResponseDto . push ( dto ) ;
336- } ) ;
337- return gatewayResponseDto ;
338- }
339-
340- private processPackets (
341- timestamps : Array < Timestamp > ,
342- packets : number [ ] ,
343- key : string ,
344- packetCounts : { [ timestamp : string ] : { rx : number ; tx : number } }
345- ) {
346- timestamps . forEach ( ( timestamp , index ) => {
347- const isoTimestamp = timestamp . toDate ( ) . toISOString ( ) ;
348- packetCounts [ isoTimestamp ] = packetCounts [ isoTimestamp ] || { rx : 0 , tx : 0 } ;
349- ( packetCounts [ isoTimestamp ] as any ) [ key ] = packets [ index ] ;
350- } ) ;
351- }
352-
353307 async modifyGateway (
354308 gatewayId : string ,
355309 dto : UpdateGatewayDto ,
@@ -447,20 +401,6 @@ export class ChirpstackGatewayService extends GenericChirpstackConfigurationServ
447401 }
448402 }
449403
450- private async updateDtoContents (
451- contentsDto : GatewayContentsDto | UpdateGatewayContentsDto
452- ) : Promise < GatewayContentsDto | UpdateGatewayContentsDto > {
453- if ( contentsDto ?. tagsString ) {
454- contentsDto . tags = JSON . parse ( contentsDto . tagsString ) ;
455- } else {
456- contentsDto . tags = { } ;
457- }
458-
459- contentsDto . id = contentsDto . gatewayId ;
460-
461- return contentsDto ;
462- }
463-
464404 public mapContentsDtoToGateway ( dto : GatewayContentsDto ) {
465405 const gateway = new DbGateway ( ) ;
466406 gateway . name = dto . name ;
@@ -536,24 +476,6 @@ export class ChirpstackGatewayService extends GenericChirpstackConfigurationServ
536476
537477 return gateway ;
538478 }
539- private mapGatewayToResponseDto ( gateway : DbGateway , forMap = false ) : GatewayResponseDto {
540- const responseDto = gateway as unknown as GatewayResponseDto ;
541- responseDto . organizationId = gateway . organization . id ;
542- responseDto . organizationName = gateway . organization . name ;
543-
544- const commonLocation = new CommonLocationDto ( ) ;
545- commonLocation . latitude = gateway . location . coordinates [ 1 ] ;
546- commonLocation . longitude = gateway . location . coordinates [ 0 ] ;
547-
548- if ( ! forMap ) {
549- commonLocation . altitude = gateway . altitude ;
550- responseDto . tags = JSON . parse ( gateway . tags ) ;
551- }
552-
553- responseDto . location = commonLocation ;
554-
555- return responseDto ;
556- }
557479
558480 async getAllGatewaysFromChirpstack ( ) : Promise < ListAllChirpstackGatewaysResponseDto > {
559481 const limit = 1000 ;
@@ -587,24 +509,6 @@ export class ChirpstackGatewayService extends GenericChirpstackConfigurationServ
587509 return responseList ;
588510 }
589511
590- private getSortingForGateways ( query : ListAllEntitiesDto ) {
591- let orderBy = "gateway.id" ;
592-
593- if ( ! query . orderOn ) {
594- return orderBy ;
595- }
596-
597- if ( query . orderOn === "organizationName" ) {
598- orderBy = "organization.name" ;
599- } else if ( query . orderOn === "status" ) {
600- orderBy = "gateway.lastSeenAt" ;
601- } else {
602- orderBy = `gateway.${ query . orderOn } ` ;
603- }
604-
605- return orderBy ;
606- }
607-
608512 validatePackageAlarmInput ( dto : UpdateGatewayDto ) {
609513 if ( dto . gateway . minimumPackages > dto . gateway . maximumPackages ) {
610514 throw new BadRequestException ( {
@@ -628,6 +532,105 @@ export class ChirpstackGatewayService extends GenericChirpstackConfigurationServ
628532 }
629533 }
630534
535+ //TODO: This could be moved to a helper function in the future, since it has a lot of similarities with metrics from chirpstack devices.
536+ private mapPackets ( metrics : GetGatewayMetricsResponse ) {
537+ const gatewayResponseDto : GatewayStatsElementDto [ ] = [ ] ;
538+ const packetCounts : { [ timestamp : string ] : { rx : number ; tx : number } } = { } ;
539+
540+ const rxTimestamps = metrics . getRxPackets ( ) . getTimestampsList ( ) ;
541+ const rxPackets = metrics
542+ . getRxPackets ( )
543+ . getDatasetsList ( )
544+ . find ( e => e . getLabel ( ) === "rx_count" )
545+ . getDataList ( ) ;
546+
547+ this . processPackets ( rxTimestamps , rxPackets , "rx" , packetCounts ) ;
548+
549+ const txTimestamps = metrics . getTxPackets ( ) . getTimestampsList ( ) ;
550+ const txPackets = metrics
551+ . getTxPackets ( )
552+ . getDatasetsList ( )
553+ . find ( e => e . getLabel ( ) === "tx_count" )
554+ . getDataList ( ) ;
555+
556+ this . processPackets ( txTimestamps , txPackets , "tx" , packetCounts ) ;
557+
558+ Object . keys ( packetCounts ) . forEach ( timestamp => {
559+ const packetCount = packetCounts [ timestamp ] ;
560+ const dto : GatewayStatsElementDto = {
561+ timestamp,
562+ rxPacketsReceived : packetCount . rx ,
563+ txPacketsEmitted : packetCount . tx ,
564+ } ;
565+ gatewayResponseDto . push ( dto ) ;
566+ } ) ;
567+ return gatewayResponseDto ;
568+ }
569+
570+ private processPackets (
571+ timestamps : Array < Timestamp > ,
572+ packets : number [ ] ,
573+ key : string ,
574+ packetCounts : { [ timestamp : string ] : { rx : number ; tx : number } }
575+ ) {
576+ timestamps . forEach ( ( timestamp , index ) => {
577+ const isoTimestamp = timestamp . toDate ( ) . toISOString ( ) ;
578+ packetCounts [ isoTimestamp ] = packetCounts [ isoTimestamp ] || { rx : 0 , tx : 0 } ;
579+ ( packetCounts [ isoTimestamp ] as any ) [ key ] = packets [ index ] ;
580+ } ) ;
581+ }
582+
583+ private async updateDtoContents (
584+ contentsDto : GatewayContentsDto | UpdateGatewayContentsDto
585+ ) : Promise < GatewayContentsDto | UpdateGatewayContentsDto > {
586+ if ( contentsDto ?. tagsString ) {
587+ contentsDto . tags = JSON . parse ( contentsDto . tagsString ) ;
588+ } else {
589+ contentsDto . tags = { } ;
590+ }
591+
592+ contentsDto . id = contentsDto . gatewayId ;
593+
594+ return contentsDto ;
595+ }
596+
597+ private mapGatewayToResponseDto ( gateway : DbGateway , forMap = false ) : GatewayResponseDto {
598+ const responseDto = gateway as unknown as GatewayResponseDto ;
599+ responseDto . organizationId = gateway . organization . id ;
600+ responseDto . organizationName = gateway . organization . name ;
601+
602+ const commonLocation = new CommonLocationDto ( ) ;
603+ commonLocation . latitude = gateway . location . coordinates [ 1 ] ;
604+ commonLocation . longitude = gateway . location . coordinates [ 0 ] ;
605+
606+ if ( ! forMap ) {
607+ commonLocation . altitude = gateway . altitude ;
608+ responseDto . tags = JSON . parse ( gateway . tags ) ;
609+ }
610+
611+ responseDto . location = commonLocation ;
612+
613+ return responseDto ;
614+ }
615+
616+ private getSortingForGateways ( query : ListAllEntitiesDto ) {
617+ let orderBy = "gateway.id" ;
618+
619+ if ( ! query . orderOn ) {
620+ return orderBy ;
621+ }
622+
623+ if ( query . orderOn === "organizationName" ) {
624+ orderBy = "organization.name" ;
625+ } else if ( query . orderOn === "status" ) {
626+ orderBy = "gateway.lastSeenAt" ;
627+ } else {
628+ orderBy = `gateway.${ query . orderOn } ` ;
629+ }
630+
631+ return orderBy ;
632+ }
633+
631634 private async checkForNotificationUnusualPackagesAlarms ( gateway : GatewayResponseDto ) {
632635 if ( ! gateway . lastSeenAt ) {
633636 return ;
0 commit comments