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

Commit

Permalink
feat: rename A-GPS to A-GNSS
Browse files Browse the repository at this point in the history
The endpoints and the implementation in the SDK
were renamed.

See nrfconnect/sdk-nrf#12568
See NordicSemiconductor/asset-tracker-cloud-docs#840

Closes #594
Closes #590

BREAKING CHANGE: the firmware needs to send requests to property bag agnss
  • Loading branch information
coderbyheart committed Oct 19, 2023
1 parent 7624903 commit be58990
Show file tree
Hide file tree
Showing 21 changed files with 164 additions and 164 deletions.
File renamed without changes.
4 changes: 2 additions & 2 deletions agps/cacheKey.ts → agnss/cacheKey.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Static } from '@sinclair/typebox'
import { agpsRequestSchema } from './types.js'
import { agnssRequestSchema } from './types.js'

export const cacheKey = ({
request,
binHours,
}: {
request: Static<typeof agpsRequestSchema>
request: Static<typeof agnssRequestSchema>
binHours: number
}): string => {
const binMs = binHours * 60 * 60 * 1000
Expand Down
6 changes: 3 additions & 3 deletions agps/types.ts → agnss/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Type } from '@sinclair/typebox'

export enum AGPSType {
export enum AGNSSType {
'UTC parameters' = 1,
'Ephemerides' = 2,
'Almanac' = 3,
Expand All @@ -13,11 +13,11 @@ export enum AGPSType {

const PositiveInteger = Type.Integer({ minimum: 1, title: 'positive integer' })

export const agpsRequestSchema = Type.Object({
export const agnssRequestSchema = Type.Object({
mcc: Type.Integer({ minimum: 100, maximum: 999 }),
mnc: Type.Integer({ minimum: 0, maximum: 99 }),
cell: PositiveInteger,
area: PositiveInteger,
phycell: Type.Optional(PositiveInteger),
types: Type.Array(Type.Enum(AGPSType), { minItems: 1 }),
types: Type.Array(Type.Enum(AGNSSType), { minItems: 1 }),
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import {
StorageSharedKeyCredential,
} from '@azure/storage-queue'
import { Static } from '@sinclair/typebox'
import { agpsRequestSchema } from '../agps/types.js'
import { agnssRequestSchema } from '../agnss/types.js'
import { fromEnv } from '../lib/fromEnv.js'
import { log, logError } from '../lib/log.js'
import { validateWithJSONSchema } from '../lib/validateWithJSONSchema.js'

const validateAgpsRequest = validateWithJSONSchema(agpsRequestSchema)
const validateAgnssRequest = validateWithJSONSchema(agnssRequestSchema)

const config = () =>
fromEnv({
storageAccountName: 'STORAGE_ACCOUNT_NAME',
storageAccessKey: 'STORAGE_ACCESS_KEY',
agpsRequestsQueueName: 'AGPS_REQUESTS_QUEUE_NAME',
agnssRequestsQueueName: 'AGNSS_REQUESTS_QUEUE_NAME',
})({
...process.env,
})
Expand All @@ -29,7 +29,7 @@ const config = () =>
*
* The requests are put in a queue for resolving.
*/
const agpsDeviceRequestsHandler: AzureFunction = async (
const agnssDeviceRequestsHandler: AzureFunction = async (
context: Context,
requests: (
| {
Expand All @@ -48,21 +48,21 @@ const agpsDeviceRequestsHandler: AzureFunction = async (
let queueClient: QueueClient

try {
const { storageAccountName, storageAccessKey, agpsRequestsQueueName } =
const { storageAccountName, storageAccessKey, agnssRequestsQueueName } =
config()

queueClient = new QueueServiceClient(
`https://${storageAccountName}.queue.core.windows.net`,
new StorageSharedKeyCredential(storageAccountName, storageAccessKey),
).getQueueClient(agpsRequestsQueueName)
).getQueueClient(agnssRequestsQueueName)
await queueClient.create()
} catch (error) {
logError(context)({ error: (error as Error).message })
return
}

// Find A-GNSS requests
const agpsRequests = requests
const agnssRequests = requests
.map((request, i) => ({
request,
deviceId:
Expand All @@ -74,22 +74,22 @@ const agpsDeviceRequestsHandler: AzureFunction = async (
string
>,
}))
.filter(({ properties }) => properties.agps === 'get')
.filter(({ properties }) => properties.agnss === 'get')

if (agpsRequests.length === 0) {
if (agnssRequests.length === 0) {
log(context)(`No A-GNSS requests found.`)
return
}

log(context)({ agpsRequests })
log(context)({ agnssRequests })

// Build list of valid requests
const deviceRequests: {
request: Static<typeof agpsRequestSchema>
request: Static<typeof agnssRequestSchema>
deviceId: string
}[] = []
agpsRequests.forEach(({ request, deviceId }) => {
const valid = validateAgpsRequest(request)
agnssRequests.forEach(({ request, deviceId }) => {
const valid = validateAgnssRequest(request)
if ('error' in valid) {
logError(context)(JSON.stringify(valid.error))
return
Expand Down Expand Up @@ -120,4 +120,4 @@ const agpsDeviceRequestsHandler: AzureFunction = async (
)
}

export default agpsDeviceRequestsHandler
export default agnssDeviceRequestsHandler
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"eventHubName": "%IOTHUB_EVENTS_EVENT_HUB_NAME%",
"connection": "IOTHUB_EVENTS_CONNECTION_STRING",
"cardinality": "many",
"consumerGroup": "%AGPS_REQUESTS_IOT_EVENTS_CONSUMER_GROUP_NAME%"
"consumerGroup": "%AGNSS_REQUESTS_IOT_EVENTS_CONSUMER_GROUP_NAME%"
}
],
"scriptFile": "./handler.mjs"
Expand Down
2 changes: 2 additions & 0 deletions agnssDeviceRequestsHandler/handler.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import handler from "../dist/agnssDeviceRequestsHandler/agnssDeviceRequestsHandler.js";
export default handler;
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,46 @@ import {
import { Static } from '@sinclair/typebox'
import iothubCommon from 'azure-iot-common'
import iothub from 'azure-iothub'
import { cacheKey } from '../agps/cacheKey.js'
import { agpsRequestSchema } from '../agps/types.js'
import { cacheKey } from '../agnss/cacheKey.js'
import { agnssRequestSchema } from '../agnss/types.js'
import { fromEnv } from '../lib/fromEnv.js'
import { log, logError } from '../lib/log.js'
import { parseConnectionString } from '../lib/parseConnectionString.js'

const config = () =>
fromEnv({
binHoursString: 'AGPS_BIN_HOURS',
binHoursString: 'AGNSS_BIN_HOURS',
iotHubConnectionString: 'IOTHUB_CONNECTION_STRING',
cosmosDbConnectionString: 'COSMOSDB_CONNECTION_STRING',
storageAccountName: 'STORAGE_ACCOUNT_NAME',
storageAccessKey: 'STORAGE_ACCESS_KEY',
maxResolutionTimeInMinutes: 'AGPS_MAX_RESOLUTION_TIME_IN_MINUTES',
maxResolutionTimeInMinutes: 'AGNSS_MAX_RESOLUTION_TIME_IN_MINUTES',
initialDelayString: 'INITIAL_DELAY',
delayFactorString: 'DELAY_FACTOR',
agpsRequestsDatabaseName: 'AGPS_REQUESTS_DATABASE_NAME',
agpsRequestsContainerName: 'AGPS_REQUESTS_CONTAINER_NAME',
agpsRequestsQueueName: 'AGPS_REQUESTS_QUEUE_NAME',
agpsRequestsNrfCloudQueueName: 'AGPS_REQUESTS_NRFCLOUD_QUEUE_NAME',
agnssRequestsDatabaseName: 'AGNSS_REQUESTS_DATABASE_NAME',
agnssRequestsContainerName: 'AGNSS_REQUESTS_CONTAINER_NAME',
agnssRequestsQueueName: 'AGNSS_REQUESTS_QUEUE_NAME',
agnssRequestsNrfCloudQueueName: 'AGNSS_REQUESTS_NRFCLOUD_QUEUE_NAME',
})({
AGPS_BIN_HOURS: '1',
AGPS_MAX_RESOLUTION_TIME_IN_MINUTES: '3',
AGNSS_BIN_HOURS: '1',
AGNSS_MAX_RESOLUTION_TIME_IN_MINUTES: '3',
INITIAL_DELAY: '5',
DELAY_FACTOR: '1.5',
...process.env,
})

// Keep a local cache in case many devices requests the same location
export type AGPSDataCache = Static<typeof agpsRequestSchema> & {
export type AGNSSDataCache = Static<typeof agnssRequestSchema> & {
source: string
dataHex?: string[]
unresolved?: boolean
updatedAt: Date
}
const resolvedRequests: Record<string, AGPSDataCache> = {}
const resolvedRequests: Record<string, AGNSSDataCache> = {}

type QueuedAGPSRequest = {
type QueuedAGNSSRequest = {
deviceId: string
request: Static<typeof agpsRequestSchema>
request: Static<typeof agnssRequestSchema>
timestamp: string
delayInSeconds?: number
}
Expand All @@ -57,16 +57,16 @@ type QueuedAGPSRequest = {
* a DB or kicking off the resoluting via a third-party API (currently only
* nRF Cloud Assisted GPS Location Service is implemented.)
*/
const agpsQueuedDeviceRequestsHandler: AzureFunction = async (
const agnssQueuedDeviceRequestsHandler: AzureFunction = async (
context: Context,
{ deviceId, request, delayInSeconds, timestamp }: QueuedAGPSRequest,
{ deviceId, request, delayInSeconds, timestamp }: QueuedAGNSSRequest,
): Promise<void> => {
log(context)({ request, deviceId, delayInSeconds, timestamp, context })

let binHours: number
let iotHubClient: iothub.Client
let cosmosDbContainer: Container
let agpsRequestsQueueClient: QueueClient
let agnssRequestsQueueClient: QueueClient
const resolverQueues: QueueClient[] = []
let maxResolutionTimeInSeconds: number
let delayFactor: number
Expand All @@ -82,10 +82,10 @@ const agpsQueuedDeviceRequestsHandler: AzureFunction = async (
delayFactorString,
initialDelayString,
cosmosDbConnectionString,
agpsRequestsDatabaseName,
agpsRequestsQueueName,
agpsRequestsContainerName,
agpsRequestsNrfCloudQueueName,
agnssRequestsDatabaseName,
agnssRequestsQueueName,
agnssRequestsContainerName,
agnssRequestsNrfCloudQueueName,
} = config()

binHours = parseInt(binHoursString, 10)
Expand All @@ -100,21 +100,21 @@ const agpsQueuedDeviceRequestsHandler: AzureFunction = async (
})

cosmosDbContainer = cosmosClient
.database(agpsRequestsDatabaseName)
.container(agpsRequestsContainerName)
.database(agnssRequestsDatabaseName)
.container(agnssRequestsContainerName)

agpsRequestsQueueClient = new QueueServiceClient(
agnssRequestsQueueClient = new QueueServiceClient(
`https://${storageAccountName}.queue.core.windows.net`,
new StorageSharedKeyCredential(storageAccountName, storageAccessKey),
).getQueueClient(agpsRequestsQueueName)
await agpsRequestsQueueClient.create()
).getQueueClient(agnssRequestsQueueName)
await agnssRequestsQueueClient.create()

const nrfCloudAgpsRequestsQueueClient = new QueueServiceClient(
const nrfCloudAgnssRequestsQueueClient = new QueueServiceClient(
`https://${storageAccountName}.queue.core.windows.net`,
new StorageSharedKeyCredential(storageAccountName, storageAccessKey),
).getQueueClient(agpsRequestsNrfCloudQueueName)
await nrfCloudAgpsRequestsQueueClient.create()
resolverQueues.push(nrfCloudAgpsRequestsQueueClient)
).getQueueClient(agnssRequestsNrfCloudQueueName)
await nrfCloudAgnssRequestsQueueClient.create()
resolverQueues.push(nrfCloudAgnssRequestsQueueClient)

maxResolutionTimeInSeconds = parseInt(maxResolutionTimeInMinutes, 10) * 60
delayFactor = parseFloat(delayFactorString)
Expand Down Expand Up @@ -179,11 +179,11 @@ const agpsQueuedDeviceRequestsHandler: AzureFunction = async (
)
await Promise.all(
(resolvedRequests[requestCacheKey]?.dataHex ?? []).map(
async (agpsdata) => {
const payload = Buffer.from(agpsdata, 'hex')
async (agnssdata) => {
const payload = Buffer.from(agnssdata, 'hex')
log(context)(`Sending ${payload.length} bytes to ${deviceId}`)
const m = new iothubCommon.Message(payload)
m.properties.add('agps', 'result')
m.properties.add('agnss', 'result')
return iotHubClient.send(deviceId, m)
},
),
Expand Down Expand Up @@ -222,7 +222,7 @@ const agpsQueuedDeviceRequestsHandler: AzureFunction = async (
const visibilityTimeout = Math.floor(
Math.min(900, (delayInSeconds ?? initialDelay) * delayFactor),
)
await agpsRequestsQueueClient.sendMessage(
await agnssRequestsQueueClient.sendMessage(
Buffer.from(
JSON.stringify({
deviceId,
Expand All @@ -240,4 +240,4 @@ const agpsQueuedDeviceRequestsHandler: AzureFunction = async (
log(context)(requestCacheKey, `re-scheduled request for`, deviceId)
}

export default agpsQueuedDeviceRequestsHandler
export default agnssQueuedDeviceRequestsHandler
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "queueTrigger",
"direction": "in",
"name": "request",
"queueName": "%AGPS_REQUESTS_QUEUE_NAME%"
"queueName": "%AGNSS_REQUESTS_QUEUE_NAME%"
}
],
"scriptFile": "./handler.mjs"
Expand Down
2 changes: 2 additions & 0 deletions agnssQueuedDeviceRequestsHandler/handler.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import handler from "../dist/agnssQueuedDeviceRequestsHandler/agnssQueuedDeviceRequestsHandler.js";
export default handler;
Loading

0 comments on commit be58990

Please sign in to comment.