-
Notifications
You must be signed in to change notification settings - Fork 18
/
shadow.ts
55 lines (46 loc) · 1.21 KB
/
shadow.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import Axios, { AxiosResponse, AxiosError } from 'axios'
import * as aws4 from 'aws4'
export interface Shadow {
state: {
desired: {
[index: string]: any
}
reported: {
[index: string]: any
}
}
}
async function fetchShadow(path: string): Promise<Shadow | boolean> {
let opts: any = {
host: process.env.VSH_IOT_ENDPOINT, //'a1pv0eq8s016ut-ats.iot.eu-west-1.amazonaws.com'
path: path,
service: 'iotdata',
region: process.env.VSH_IOT_REGION, //'eu-west-1'
}
aws4.sign(opts)
// console.log(
// 'FETCHING SHADOW:::',
// `https://${opts.host}${opts.path}`,
// opts.headers
// )
const response: AxiosResponse = await Axios.get(
`https://${opts.host}${opts.path}`,
{
headers: opts.headers,
validateStatus: (status) => status == 200 || status == 404,
}
)
if (response.status == 200) {
return response.data
}
return false
}
export function fetchThingShadow(thingId: string): Promise<Shadow | boolean> {
return fetchShadow(`/things/${thingId}/shadow`)
}
export function fetchDeviceShadow(
thingId: string,
endpointId: string
): Promise<Shadow | boolean> {
return fetchShadow(`/things/${thingId}/shadow?name=${endpointId}`)
}