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

Commit

Permalink
feat: implement delete device
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Apr 6, 2020
1 parent 036e562 commit f7e7874
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
41 changes: 41 additions & 0 deletions deleteDevice/deleteDevice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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 deleteDevice: 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 {
await registry.delete(req.params.id)
context.res = r({ success: true })
}
} catch (error) {
context.log(
JSON.stringify({
error: error.message,
}),
)
context.res = r(error, 500)
}
}

export default deleteDevice
18 changes: 18 additions & 0 deletions deleteDevice/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": ["delete"],
"route": "device/{id}"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
],
"scriptFile": "../dist/deleteDevice/deleteDevice.js"
}
5 changes: 5 additions & 0 deletions getDevice/getDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ const getDevice: AzureFunction = async (
context.res = r(res.result[0])
}
} catch (error) {
context.log(
JSON.stringify({
error: error.message,
}),
)
context.res = r(error, 500)
}
}
Expand Down
5 changes: 5 additions & 0 deletions listDevices/listDevices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const listDevices: AzureFunction = async (
const res = await devices.nextAsTwin()
context.res = r(res.result)
} catch (error) {
context.log(
JSON.stringify({
error: error.message,
}),
)
context.res = r(error, 500)
}
}
Expand Down
5 changes: 5 additions & 0 deletions updateDevice/updateDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ const updateDevice: AzureFunction = async (
context.res = r(true)
}
} catch (error) {
context.log(
JSON.stringify({
error: error.message,
}),
)
context.res = r(error, 500)
}
}
Expand Down

0 comments on commit f7e7874

Please sign in to comment.