Skip to content

Commit

Permalink
feat: store single cell geolocation in shadow
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Nov 7, 2023
1 parent 03c7cce commit 5029f8d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cdk/resources/ResolveCellLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ export class ResolveCellLocation extends Construct {
actions: ['execute-api:ManageConnections'],
resources: [websocketAPI.websocketAPIArn],
}),
new IAM.PolicyStatement({
actions: ['iot:SearchIndex', 'iot:UpdateThingShadow'],
resources: ['*'],
}),
],
layers: [baseLayer],
logRetention: Logs.RetentionDays.ONE_WEEK,
Expand Down
77 changes: 77 additions & 0 deletions lambda/onCellGeoLocationResolved.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import { fromEnv } from '@nordicsemiconductor/from-env'
import { Type } from '@sinclair/typebox'
import { LocationSource, Network, notifyClients } from './notifyClients.js'
import { validateWithTypeBox } from './validateWithTypeBox.js'
import { IoTClient, SearchIndexCommand } from '@aws-sdk/client-iot'
import {
LwM2MObjectID,
type ConnectionInformation_14203,
} from '@hello.nrfcloud.com/proto-lwm2m'
import {
IoTDataPlaneClient,
UpdateThingShadowCommand,
} from '@aws-sdk/client-iot-data-plane'
import { objectsToShadow } from '../lwm2m/objectsToShadow.js'

const { connectionsTableName, websocketManagementAPIURL } = fromEnv({
connectionsTableName: 'CONNECTIONS_TABLE_NAME',
Expand All @@ -25,6 +35,9 @@ enum Nw {
nbIoT = 'nbiot',
}

const iot = new IoTClient({})
const iotData = new IoTDataPlaneClient({})

const validateCellGeoLocation = validateWithTypeBox(
Type.Object({
area: Type.Integer({ minimum: 1 }),
Expand Down Expand Up @@ -95,4 +108,68 @@ export const handler = async (event: {
geoLocation: { lat, lng, accuracy, source },
},
})

for (const { shadow, thingName } of (
await iot.send(
new SearchIndexCommand({
queryString: `connectivity.timestamp > ${
Date.now() - 24 * 60 * 60 * 1000
}`,
}),
)
).things ?? []) {
if (shadow === undefined) continue
if (shadow === null) continue
const connRes =
JSON.parse(shadow).name.lwm2m?.reported?.[
`${LwM2MObjectID.ConnectionInformation_14203}:1.0`
]?.['0']
if (connRes === undefined) continue
const connection: ConnectionInformation_14203 = {
ObjectID: LwM2MObjectID.ConnectionInformation_14203,
ObjectVersion: '1.0',
Resources: connRes,
}

const thingCell = {
nw: connection.Resources[0],
cell: connection.Resources['4'],
mccmnc: connection.Resources['5'],
area: connection.Resources['3'],
}

if (
thingCell.nw === nw &&
thingCell.cell === cell &&
thingCell.mccmnc === mccmnc &&
thingCell.area === area
) {
await iotData.send(
new UpdateThingShadowCommand({
thingName,
shadowName: 'lwm2m',
payload: JSON.stringify({
state: {
reported: objectsToShadow([
{
ObjectID: LwM2MObjectID.Geolocation_14201,
ObjectInstanceID: 2,
ObjectVersion: '1.0',
Resources: {
0: lat,
1: lng,
3: accuracy,
6: source,
99: Date.now(),
},
},
]),
},
}),
}),
)
} else {
console.debug(`No match`, thingCell)
}
}
}

0 comments on commit 5029f8d

Please sign in to comment.