Skip to content

Commit

Permalink
allow individual devices to be retrievable
Browse files Browse the repository at this point in the history
  • Loading branch information
csuermann committed Sep 17, 2022
1 parent ce11934 commit 9b772a9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions Device.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export type Device = {
deviceId: string
template: string
retrievable: boolean
friendlyName: string
thingId: string
}
16 changes: 15 additions & 1 deletion db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export function upsertDevice({
deviceId,
friendlyName,
template,
retrievable,
thingId,
}): Promise<any> {
let params = {
Expand All @@ -186,10 +187,11 @@ export function upsertDevice({
SK: `THING#${thingId}#DEVICE#${deviceId}`,
},
UpdateExpression:
'set friendlyName = :fn, template = :te, thingId = :th, deviceId = :de, updatedAt = :ua',
'set friendlyName = :fn, template = :te, retrievable = :rt, thingId = :th, deviceId = :de, updatedAt = :ua',
ExpressionAttributeValues: {
':fn': friendlyName,
':te': template,
':rt': retrievable ? true : false,
':th': thingId,
':de': deviceId,
':ua': dayjs().toISOString(),
Expand Down Expand Up @@ -252,6 +254,12 @@ export async function getDevicesOfUser(userId: string): Promise<Device[]> {
})
})

// ensure 'retrievable' attribute is present (old records in DB might not have it yet):
devices = devices.map((device) => {
device.retrievable = device.retrievable ?? false
return device
})

return devices as Device[]
}

Expand Down Expand Up @@ -290,6 +298,12 @@ export async function getDevicesOfThing(
})
})

// ensure 'retrievable' attribute is present (old records in DB might not have it yet):
devices = devices.map((device) => {
device.retrievable = device.retrievable ?? false
return device
})

return devices as Device[]
}

Expand Down
2 changes: 2 additions & 0 deletions handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ async function handleBackchannelBulkDiscover(event) {
thingId: event.thingId,
friendlyName: deviceStub['friendlyName'],
template: deviceStub['template'],
retrievable: deviceStub['retrievable'] ?? false,
}

devicesToDiscover.push(device)
Expand Down Expand Up @@ -223,6 +224,7 @@ export async function handleBackchannelBulkUndiscover({ thingId, devices }) {
thingId: thingId,
friendlyName: deviceStub['friendlyName'],
template: deviceStub['template'],
retrievable: deviceStub['retrievable'] ?? false,
}

devicesToUndiscover.push(device)
Expand Down
3 changes: 2 additions & 1 deletion handlers/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ function getEndpointForDevice(device: Device, asRetrievable: boolean) {

export function getEndpointsForDevices(
devices: Device[],
asRetrievable: boolean
asRetrievableAllowed: boolean
) {
return devices.reduce((acc, curr) => {
const asRetrievable = asRetrievableAllowed && curr.retrievable
const endpoint = getEndpointForDevice(curr, asRetrievable)

if (endpoint) {
Expand Down

0 comments on commit 9b772a9

Please sign in to comment.