forked from dgreif/ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ts
73 lines (61 loc) · 1.87 KB
/
config.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { AlarmMode, RingApiOptions } from '../api'
import { readFileSync, writeFileSync } from 'fs'
import { API } from 'homebridge'
import { createHash, randomBytes } from 'crypto'
import { join } from 'path'
const systemIdFileName = '.ring.json'
export interface RingPlatformConfig extends RingApiOptions {
alarmOnEntryDelay?: boolean
beamDurationSeconds?: number
hideLightGroups?: boolean
hideDoorbellSwitch?: boolean
hideCameraMotionSensor?: boolean
sendCameraMotionNotificationsToTv?: boolean
sendDoorbellMotionNotificationsToTv?: boolean
hideCameraSirenSwitch?: boolean
hideInHomeDoorbellSwitch?: boolean
hideAlarmSirenSwitch?: boolean
hideUnsupportedServices?: boolean
hideDeviceIds?: string[]
onlyDeviceTypes?: string[]
showPanicButtons?: boolean
nightModeBypassFor: AlarmMode
ffmpegPath?: string
}
export function updateHomebridgeConfig(
homebridge: API,
update: (config: string) => string
) {
const configPath = homebridge.user.configPath(),
config = readFileSync(configPath).toString(),
updatedConfig = update(config)
if (config !== updatedConfig) {
writeFileSync(configPath, updatedConfig)
return true
}
return false
}
function createSystemId() {
return createHash('sha256').update(randomBytes(32)).digest('hex')
}
interface RingContext {
systemId: string
}
export function getSystemId(homebridge: API) {
const storagePath = homebridge.user.storagePath(),
filePath = join(storagePath, systemIdFileName)
try {
const ringContext: RingContext = JSON.parse(
readFileSync(filePath).toString()
)
if (ringContext.systemId) {
return ringContext.systemId
}
} catch (_) {
// expect errors if file doesn't exist or is in a bad format
}
const systemId = createSystemId(),
ringContext: RingContext = { systemId }
writeFileSync(filePath, JSON.stringify(ringContext))
return systemId
}