Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

Commit

Permalink
feat(api): add route to fetch device
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Apr 2, 2020
1 parent 3977345 commit 9e33f27
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 11 deletions.
18 changes: 18 additions & 0 deletions getDevice/function.json
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"
}
35 changes: 35 additions & 0 deletions getDevice/index.ts
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
21 changes: 21 additions & 0 deletions lib/ErrorInfo.ts
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,
}
8 changes: 8 additions & 0 deletions lib/http.ts
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),
})
3 changes: 2 additions & 1 deletion listDevices/function.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": ["get", "post"]
"methods": ["get"],
"route": "devices"
},
{
"type": "http",
Expand Down
12 changes: 2 additions & 10 deletions listDevices/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import { AzureFunction, Context, HttpRequest } from '@azure/functions'
import { Registry } from 'azure-iothub'
import { r } from '../lib/http'

const connectionString = process.env.IOT_HUB_CONNECTION_STRING || ''

const r = (result: any, status = 200) => ({
headers: {
'Content-Type': 'application/json; charset=uft-8',
},
status,
isRaw: true,
body: JSON.stringify(result),
})

const listDevices: AzureFunction = async (
context: Context,
req: HttpRequest,
): Promise<void> => {
context.log({ req })
try {
const registry = Registry.fromConnectionString(connectionString)
const devices = registry.createQuery('SELECT * FROM devices')
const devices = registry.createQuery('SELECT deviceId FROM devices')
const res = await devices.nextAsTwin()
context.res = r(res.result)
} catch (error) {
Expand Down

0 comments on commit 9e33f27

Please sign in to comment.