Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
feat: use proper type
Browse files Browse the repository at this point in the history
  • Loading branch information
pudkrong committed Oct 31, 2023
1 parent 2e6c6bd commit bb096de
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 134 deletions.
54 changes: 32 additions & 22 deletions agnssDeviceRequestsHandler/agnssDeviceRequestsHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EventHubHandler } from '@azure/functions'
import type { InvocationContext } from '@azure/functions'
import {
QueueClient,
QueueServiceClient,
Expand All @@ -10,6 +10,25 @@ import { fromEnv } from '../lib/fromEnv.js'
import { log, logError } from '../lib/log.js'
import { validateWithJSONSchema } from '../lib/validateWithJSONSchema.js'

type AGNSS = (
| {
mcc: number
mnc: number
cell: number
area: number
types: number[]
}
| Record<string, any>
)[]
type AGNSSContext = Omit<InvocationContext, 'triggerMetadata'> & {
triggerMetadata: {
systemPropertiesArray: {
'iothub-connection-device-id': string
}[]
propertiesArray: Record<string, string>[]
}
}

const validateAgnssRequest = validateWithJSONSchema(agnssRequestSchema)

const config = () =>
Expand All @@ -29,14 +48,12 @@ const config = () =>
*
* The requests are put in a queue for resolving.
*/
const agnssDeviceRequestsHandler: EventHubHandler = async (
requests,
context,
) => {
const agnssDeviceRequestsHandler = async (
requests: AGNSS,
context: AGNSSContext,
): Promise<void> => {
log(context)({ context, requests })

if (!Array.isArray(requests)) return

const timestamp = new Date()
let queueClient: QueueClient

Expand All @@ -56,21 +73,14 @@ const agnssDeviceRequestsHandler: EventHubHandler = async (

// Find A-GNSS requests
const agnssRequests = requests
.map((request, i) => {
const systemPropertiesArray = Array.isArray(
context.triggerMetadata?.systemPropertiesArray,
)
? context.triggerMetadata?.systemPropertiesArray
: []
const properties = Array.isArray(context.triggerMetadata?.propertiesArray)
? context.triggerMetadata?.propertiesArray
: []
return {
request,
deviceId: systemPropertiesArray?.[i]['iothub-connection-device-id'],
properties: properties?.[i] as Record<string, string>,
}
})
.map((request, i) => ({
request,
deviceId:
context.triggerMetadata.systemPropertiesArray?.[i][
'iothub-connection-device-id'
],
properties: context.triggerMetadata.propertiesArray?.[i],
}))
.filter(({ properties }) => properties.agnss === 'get')

if (agnssRequests.length === 0) {
Expand Down
4 changes: 2 additions & 2 deletions agnssDeviceRequestsHandler/handler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { app } from '@azure/functions'
import { EventHubHandler, app } from '@azure/functions'
import handler from './agnssDeviceRequestsHandler.js'

app.eventHub('agnssDeviceRequestsHandler', {
eventHubName: '%IOTHUB_EVENTS_EVENT_HUB_NAME%',
connection: 'IOTHUB_EVENTS_CONNECTION_STRING',
cardinality: 'many',
consumerGroup: '%AGNSS_REQUESTS_IOT_EVENTS_CONSUMER_GROUP_NAME%',
handler,
handler: handler as EventHubHandler,
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Container, CosmosClient } from '@azure/cosmos'
import type { StorageQueueHandler } from '@azure/functions'
import type { FunctionHandler } from '@azure/functions'
import {
QueueClient,
QueueServiceClient,
Expand Down Expand Up @@ -57,12 +57,10 @@ type QueuedAGNSSRequest = {
* a DB or kicking off the resoluting via a third-party API (currently only
* nRF Cloud Assisted GPS Location Service is implemented.)
*/
const agnssQueuedDeviceRequestsHandler: StorageQueueHandler = async (
queueEntry,
const agnssQueuedDeviceRequestsHandler: FunctionHandler = async (
{ deviceId, request, delayInSeconds, timestamp }: QueuedAGNSSRequest,
context,
) => {
const { deviceId, request, delayInSeconds, timestamp } =
queueEntry as QueuedAGNSSRequest
): Promise<void> => {
log(context)({ request, deviceId, delayInSeconds, timestamp, context })

let binHours: number
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Container, CosmosClient } from '@azure/cosmos'
import type { StorageQueueHandler } from '@azure/functions'
import type { FunctionHandler } from '@azure/functions'
import { Static } from '@sinclair/typebox'
import { URL } from 'url'
import { cacheKey } from '../agnss/cacheKey.js'
Expand Down Expand Up @@ -39,11 +39,10 @@ let nrfCloudServiceKeyPromise: Promise<string>
/**
* Resolve A-GNSS requests from nRF Cloud
*/
const agnssResolveRequestFromNrfCloud: StorageQueueHandler = async (
queueEntry,
const agnssResolveRequestFromNrfCloud: FunctionHandler = async (
request: Static<typeof agnssRequestSchema>,
context,
) => {
const request = queueEntry as Static<typeof agnssRequestSchema>
): Promise<void> => {
log(context)({ context, request })

let resolver: ReturnType<typeof resolveAgnssRequest>
Expand Down
4 changes: 2 additions & 2 deletions pgpsDeviceRequestsHandler/handler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { app } from '@azure/functions'
import { EventHubHandler, app } from '@azure/functions'
import handler from './pgpsDeviceRequestsHandler.js'

app.eventHub('pgpsDeviceRequestsHandler', {
eventHubName: '%IOTHUB_EVENTS_EVENT_HUB_NAME%',
connection: 'IOTHUB_EVENTS_CONNECTION_STRING',
cardinality: 'many',
consumerGroup: '%PGPS_REQUESTS_IOT_EVENTS_CONSUMER_GROUP_NAME%',
handler,
handler: handler as EventHubHandler,
})
55 changes: 31 additions & 24 deletions pgpsDeviceRequestsHandler/pgpsDeviceRequestsHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EventHubHandler } from '@azure/functions'
import type { InvocationContext } from '@azure/functions'
import {
QueueClient,
QueueServiceClient,
Expand All @@ -10,6 +10,24 @@ import { log, logError } from '../lib/log.js'
import { validateWithJSONSchema } from '../lib/validateWithJSONSchema.js'
import { pgpsRequestSchema } from '../pgps/types.js'

type PGPS = (
| {
n: number
int: number
day: number
time: number
}
| Record<string, any>
)[]
type PGPSContext = Omit<InvocationContext, 'triggerMetadata'> & {
triggerMetadata: {
systemPropertiesArray: {
'iothub-connection-device-id': string
}[]
propertiesArray: Record<string, string>[]
}
}

const validatePgpsRequest = validateWithJSONSchema(pgpsRequestSchema)

const config = () =>
Expand All @@ -29,10 +47,10 @@ const config = () =>
*
* The requests are put in a queue for resolving.
*/
const pgpsDeviceRequestsHandler: EventHubHandler = async (
requests: unknown,
context,
) => {
const pgpsDeviceRequestsHandler = async (
requests: PGPS,
context: PGPSContext,
): Promise<void> => {
log(context)({ context, requests })

const timestamp = new Date()
Expand All @@ -53,26 +71,15 @@ const pgpsDeviceRequestsHandler: EventHubHandler = async (
}

// Find P-GPS requests
if (!Array.isArray(requests)) return

const pgpsRequests = requests
.map((request, i) => {
const systemPropertiesArray = Array.isArray(
context.triggerMetadata?.systemPropertiesArray,
)
? context.triggerMetadata?.systemPropertiesArray
: []
const propertiesArray = Array.isArray(
context.triggerMetadata?.propertiesArray,
)
? context.triggerMetadata?.propertiesArray
: []
return {
request,
deviceId: systemPropertiesArray?.[i]['iothub-connection-device-id'],
properties: propertiesArray?.[i] as Record<string, string>,
}
})
.map((request, i) => ({
request,
deviceId:
context.triggerMetadata.systemPropertiesArray?.[i][
'iothub-connection-device-id'
],
properties: context.triggerMetadata.propertiesArray?.[i],
}))
.filter(({ properties }) => properties.pgps === 'get')

if (pgpsRequests.length === 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Container, CosmosClient } from '@azure/cosmos'
import type { StorageQueueHandler } from '@azure/functions'
import type { FunctionHandler } from '@azure/functions'
import {
QueueClient,
QueueServiceClient,
Expand Down Expand Up @@ -59,12 +59,10 @@ type QueuedPGPSRequest = {
* a DB or kicking off the resoluting via a third-party API (currently only
* nRF Cloud Predicted GPS Location Service is implemented.)
*/
const pgpsQueuedDeviceRequestsHandler: StorageQueueHandler = async (
queueEntry,
const pgpsQueuedDeviceRequestsHandler: FunctionHandler = async (
{ deviceId, request, delayInSeconds, timestamp }: QueuedPGPSRequest,
context,
) => {
const { deviceId, request, delayInSeconds, timestamp } =
queueEntry as QueuedPGPSRequest
): Promise<void> => {
log(context)({ request, deviceId, delayInSeconds, timestamp, context })

let binHours: number
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Container, CosmosClient } from '@azure/cosmos'
import type { StorageQueueHandler } from '@azure/functions'
import type { FunctionHandler } from '@azure/functions'
import { Static } from '@sinclair/typebox'
import { URL } from 'url'
import { fromEnv } from '../lib/fromEnv.js'
Expand Down Expand Up @@ -40,11 +40,10 @@ let nrfCloudServiceKeyPromise: Promise<string>
/**
* Resolve P-GPS requests from nRF Cloud
*/
const pgpsResolveRequestFromNrfCloud: StorageQueueHandler = async (
queueEntry,
const pgpsResolveRequestFromNrfCloud: FunctionHandler = async (
request: Static<typeof pgpsRequestSchema>,
context,
) => {
const request = queueEntry as Static<typeof pgpsRequestSchema>
): Promise<void> => {
log(context)({ context, request })

let resolver: ReturnType<typeof resolvePgpsRequest>
Expand Down
4 changes: 2 additions & 2 deletions publishDeviceUpdatesToSignalR/handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, output } from '@azure/functions'
import { EventHubHandler, app, output } from '@azure/functions'
import handler from './publishDeviceUpdatesToSignalR.js'

const signalROutput = output.generic({
Expand All @@ -13,5 +13,5 @@ app.eventHub('publishDeviceUpdatesToSignalR', {
connection: 'IOTHUB_EVENTS_CONNECTION_STRING',
consumerGroup: 'publishdeviceupdates',
extraOutputs: [signalROutput],
handler: handler(signalROutput),
handler: handler(signalROutput) as EventHubHandler,
})
37 changes: 17 additions & 20 deletions publishDeviceUpdatesToSignalR/publishDeviceUpdatesToSignalR.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
import type { EventHubHandler, FunctionOutput } from '@azure/functions'
import type { FunctionOutput, InvocationContext } from '@azure/functions'
import { DeviceUpdate, TwinChangeEvent } from '../lib/iotMessages.js'
import { log } from '../lib/log.js'

type Context = Omit<InvocationContext, 'triggerMetadata'> & {
triggerMetadata: {
systemPropertiesArray: {
'iothub-message-source': string
'iothub-connection-device-id': string
}[]
propertiesArray: unknown[]
}
}

/**
* Publishes Device Twin Update to SignalR so the web application can receive real-time notifications
*/
const publishDeviceUpdatesToSignalR =
(signalROutput: FunctionOutput): EventHubHandler =>
async (eventMessages, context) => {
const updates = eventMessages as DeviceUpdate[]

const systemPropertiesArray = Array.isArray(
context.triggerMetadata?.systemPropertiesArray,
)
? context.triggerMetadata?.systemPropertiesArray
: []
const propertiesArray = Array.isArray(
context.triggerMetadata?.propertiesArray,
)
? context.triggerMetadata?.propertiesArray
: []

(signalROutput: FunctionOutput) =>
async (updates: DeviceUpdate[], context: Context): Promise<void> => {
log(context)({
messages: updates,
systemPropertiesArray,
propertiesArray,
systemPropertiesArray: context.triggerMetadata.systemPropertiesArray,
propertiesArray: context.triggerMetadata.propertiesArray,
})

const signalRMessages: Record<string, unknown>[] = []

const addProperties = (message: DeviceUpdate, k: number) => ({
message,
systemProperties: systemPropertiesArray?.[k],
propertiesArray: propertiesArray?.[k],
systemProperties: context.triggerMetadata.systemPropertiesArray?.[k],
propertiesArray: context.triggerMetadata.propertiesArray?.[k],
})

const reportedUpdates = updates
Expand Down
4 changes: 2 additions & 2 deletions storeDeviceUpdateInCosmosDB/handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, output } from '@azure/functions'
import { EventHubHandler, app, output } from '@azure/functions'
import handler from './storeDeviceUpdateInCosmosDB.js'

const deviceUpdate = output.cosmosDB({
Expand All @@ -15,5 +15,5 @@ app.eventHub('storeDeviceUpdateInCosmosDB', {
connection: 'IOTHUB_EVENTS_CONNECTION_STRING',
consumerGroup: 'storedeviceupdate',
extraOutputs: [deviceUpdate],
handler: handler(deviceUpdate),
handler: handler(deviceUpdate) as EventHubHandler,
})
Loading

0 comments on commit bb096de

Please sign in to comment.