diff --git a/getDevice/function.json b/getDevice/function.json new file mode 100644 index 0000000..a08f70d --- /dev/null +++ b/getDevice/function.json @@ -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" +} diff --git a/getDevice/index.ts b/getDevice/index.ts new file mode 100644 index 0000000..477b52f --- /dev/null +++ b/getDevice/index.ts @@ -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 => { + 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 diff --git a/lib/ErrorInfo.ts b/lib/ErrorInfo.ts new file mode 100644 index 0000000..8c168c0 --- /dev/null +++ b/lib/ErrorInfo.ts @@ -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, +} diff --git a/lib/http.ts b/lib/http.ts new file mode 100644 index 0000000..1c07021 --- /dev/null +++ b/lib/http.ts @@ -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), +}) diff --git a/listDevices/function.json b/listDevices/function.json index 3a85e69..481c81c 100644 --- a/listDevices/function.json +++ b/listDevices/function.json @@ -5,7 +5,8 @@ "type": "httpTrigger", "direction": "in", "name": "req", - "methods": ["get", "post"] + "methods": ["get"], + "route": "devices" }, { "type": "http", diff --git a/listDevices/index.ts b/listDevices/index.ts index 905492d..e71a692 100644 --- a/listDevices/index.ts +++ b/listDevices/index.ts @@ -1,17 +1,9 @@ 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, @@ -19,7 +11,7 @@ const listDevices: AzureFunction = async ( 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) {