Skip to content

Commit

Permalink
feat(homebridge): more detailed logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dgreif committed Aug 1, 2019
1 parent bd64daa commit 6c2021e
Show file tree
Hide file tree
Showing 7 changed files with 1,893 additions and 100 deletions.
1 change: 0 additions & 1 deletion api/rest-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ async function requestWithRetry<T>(
options: AxiosRequestConfig
): Promise<T & ExtendedResponse> {
try {
logInfo(`Making request: ${JSON.stringify(options, null, 2)}`)
const { data, headers } = await axios(options)

if (typeof data === 'object' && headers.date) {
Expand Down
8 changes: 5 additions & 3 deletions api/sip-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,11 @@ export class SipSession {
}

if (response.status >= 300) {
logError(
`sip ${method} request failed with status ` + response.status
)
if (response.status !== 408 || method !== 'BYE') {
logError(
`sip ${method} request failed with status ` + response.status
)
}
reject(
new Error(
`sip ${method} request failed with status ` + response.status
Expand Down
25 changes: 21 additions & 4 deletions api/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,38 @@ import { red } from 'colors'
import { randomBytes } from 'crypto'
import { createInterface } from 'readline'

const logger = debug('ring')
const debugLogger = debug('ring')

interface Logger {
logInfo: (message: string) => void
logError: (message: string) => void
}

let logger: Logger = {
logInfo(message) {
debugLogger(message)
},
logError(message) {
debugLogger(red(message))
}
}

export function delay(milliseconds: number) {
return new Promise(resolve => {
setTimeout(resolve, milliseconds)
})
}

// TODO: use homebridge logger if available
export function logInfo(message: any) {
logger(message)
logger.logInfo(message)
}

export function logError(message: any) {
logger(red(message))
logger.logError(message)
}

export function useLogger(newLogger: Logger) {
logger = newLogger
}

export function generateRandomId() {
Expand Down
32 changes: 28 additions & 4 deletions homebridge/camera-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ interface PrepareStreamRequest {
audio: HapRtpConfig
}

function getDurationSeconds(start: number) {
return (Date.now() - start) / 1000
}

export class CameraSource {
services: Service[] = []
streamControllers: any[] = []
Expand Down Expand Up @@ -70,8 +74,15 @@ export class CameraSource {
callback: (err?: Error, snapshot?: Buffer) => void
) {
try {
const start = Date.now()
this.logger.info(`Snapshot Requested for ${this.ringCamera.name}`)
const snapshot = await this.ringCamera.getSnapshot(true)
const snapshot = await this.ringCamera.getSnapshot(true),
duration = (Date.now() - start) / 1000
this.logger.info(
`Snapshot Received for ${this.ringCamera.name} (${getDurationSeconds(
start
)}s)`
)
// Not currently resizing the image.
// HomeKit does a good job of resizing and doesn't seem to care if it's not right
callback(undefined, snapshot)
Expand All @@ -90,6 +101,7 @@ export class CameraSource {
request: PrepareStreamRequest,
callback: (response: any) => void
) {
const start = Date.now()
this.logger.info(`Preparing Live Stream for ${this.ringCamera.name}`)

try {
Expand Down Expand Up @@ -125,12 +137,20 @@ export class CameraSource {

this.sessions[hap.UUIDGen.unparse(sessionID)] = sipSession

this.logger.info(`Waiting for stream data from ${this.ringCamera.name}`)
this.logger.info(
`Waiting for stream data from ${
this.ringCamera.name
} (${getDurationSeconds(start)}s)`
)
const [audioSsrc, videoSsrc] = await Promise.all([
audioProxy.ssrcPromise,
videoProxy.ssrcPromise
])
this.logger.info(`Received stream data from ${this.ringCamera.name}`)
this.logger.info(
`Received stream data from ${
this.ringCamera.name
} (${getDurationSeconds(start)}s)`
)

const currentAddress = ip.address()
callback({
Expand All @@ -152,7 +172,11 @@ export class CameraSource {
}
})
} catch (e) {
this.logger.error(`Failed to prepare stream for ${this.ringCamera.name}`)
this.logger.error(
`Failed to prepare stream for ${
this.ringCamera.name
} (${getDurationSeconds(start)}s)`
)
this.logger.error(e)
}
}
Expand Down
10 changes: 10 additions & 0 deletions homebridge/ring-platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Switch } from './switch'
import { Camera } from './camera'
import { RingAuth } from '../api/rest-client'
import { platformName, pluginName } from './plugin-info'
import { useLogger } from '../api/util'

const debug = __filename.includes('release-homebridge')

Expand Down Expand Up @@ -68,6 +69,15 @@ export class RingPlatform {
public config: RingPlatformConfig & RingAuth,
public api: HAP.Platform
) {
useLogger({
logInfo(message) {
log.info(message)
},
logError(message) {
log.error(message)
}
})

if (!config) {
this.log.info('No configuration found for platform Ring')
return
Expand Down
Loading

0 comments on commit 6c2021e

Please sign in to comment.