This repository has been archived by the owner on Feb 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add route to fetch device
- Loading branch information
1 parent
3977345
commit 9e33f27
Showing
6 changed files
with
86 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"bindings": [ | ||
{ | ||
"authLevel": "anonymous", | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"name": "req", | ||
"methods": ["get"], | ||
"route": "device/{id}" | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "res" | ||
} | ||
], | ||
"scriptFile": "../dist/getDevice/index.js" | ||
} |
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,35 @@ | ||
import { AzureFunction, Context, HttpRequest } from '@azure/functions' | ||
import { Registry } from 'azure-iothub' | ||
import { r } from '../lib/http' | ||
import { ErrorInfo, ErrorType, toStatusCode } from '../lib/ErrorInfo' | ||
|
||
const connectionString = process.env.IOT_HUB_CONNECTION_STRING || '' | ||
|
||
const getDevice: AzureFunction = async ( | ||
context: Context, | ||
req: HttpRequest, | ||
): Promise<void> => { | ||
context.log({ req: JSON.stringify(req) }) | ||
try { | ||
const registry = Registry.fromConnectionString(connectionString) | ||
const devices = registry.createQuery( | ||
`SELECT * FROM devices WHERE deviceId='${req.params.id}'`, | ||
) | ||
const res = await devices.nextAsTwin() | ||
if (res.result.length === 0) { | ||
context.res = r( | ||
{ | ||
type: ErrorType.EntityNotFound, | ||
message: `Device ${req.params.id} not found!`, | ||
} as ErrorInfo, | ||
toStatusCode[ErrorType.EntityNotFound], | ||
) | ||
} else { | ||
context.res = r(res.result[0]) | ||
} | ||
} catch (error) { | ||
context.res = r(error, 500) | ||
} | ||
} | ||
|
||
export default getDevice |
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,21 @@ | ||
export enum ErrorType { | ||
EntityNotFound = 'EntityNotFound', | ||
BadRequest = 'BadRequest', | ||
AccessDenied = 'AccessDenied', | ||
InternalError = 'InternalError', | ||
Conflict = 'Conflict', | ||
} | ||
|
||
export type ErrorInfo = { | ||
type: ErrorType | ||
message: string | ||
detail?: any | ||
} | ||
|
||
export const toStatusCode = { | ||
[ErrorType.BadRequest]: 400, | ||
[ErrorType.AccessDenied]: 403, | ||
[ErrorType.EntityNotFound]: 404, | ||
[ErrorType.InternalError]: 500, | ||
[ErrorType.Conflict]: 409, | ||
} |
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,8 @@ | ||
export const r = (result: any, status = 200) => ({ | ||
headers: { | ||
'Content-Type': 'application/json; charset=uft-8', | ||
}, | ||
status, | ||
isRaw: true, | ||
body: JSON.stringify(result), | ||
}) |
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