-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(memfault): fetch reboots for devices on button press
If a device publishes a button press for the button 42 the backend will poll every 15 seconds for up to two minutes for reboots on the Memfault API. If a reboot is found that is newer than the button press it is sent to the frontend.
- Loading branch information
1 parent
40178f6
commit 1847a11
Showing
9 changed files
with
321 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { GetParametersByPathCommand, SSMClient } from '@aws-sdk/client-ssm' | ||
|
||
export const createAPIClient = async ( | ||
ssm: SSMClient, | ||
stackName: string, | ||
): Promise<{ | ||
getLastReboots: (deviceId: string) => Promise<null | Array<Reboot>> | ||
}> => { | ||
const Prefix = `/${stackName}/memfault/` | ||
|
||
const { organizationAuthToken, organizationId, projectId } = ( | ||
( | ||
await ssm.send( | ||
new GetParametersByPathCommand({ | ||
Path: Prefix, | ||
}), | ||
) | ||
)?.Parameters ?? [] | ||
).reduce( | ||
(params, p) => ({ | ||
...params, | ||
[(p.Name ?? '').replace(Prefix, '')]: p.Value ?? '', | ||
}), | ||
{} as Record<string, string>, | ||
) | ||
|
||
if ( | ||
organizationAuthToken === undefined || | ||
organizationId === undefined || | ||
projectId === undefined | ||
) | ||
throw new Error(`Memfault settings not configured!`) | ||
|
||
return { | ||
getLastReboots: async (deviceId) => { | ||
const res = await fetch( | ||
`https://api.memfault.com/api/v0/organizations/${organizationId}/projects/${projectId}/devices/${deviceId}/reboots?${new URLSearchParams( | ||
{ | ||
since: new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(), | ||
}, | ||
).toString()}`, | ||
{ | ||
headers: new Headers({ | ||
Authorization: `Basic ${Buffer.from(`:${organizationAuthToken}`).toString('base64')}`, | ||
}), | ||
}, | ||
) | ||
if (!res.ok) return null | ||
return (await res.json()).data | ||
}, | ||
} | ||
} | ||
|
||
export type Reboot = { | ||
type: 'memfault' | ||
mcu_reason_register: null | ||
time: string // e.g. '2024-03-14T07:26:37.270000+00:00' | ||
reason: number // e.g. 7 | ||
software_version: { | ||
version: string // e.g. '1.11.1+thingy91.low-power.memfault' | ||
id: number // e.g.504765 | ||
software_type: { | ||
id: number //e.g. 32069; | ||
name: string // e.g. 'thingy_world' | ||
} | ||
archived: boolean | ||
} | ||
} |
Oops, something went wrong.