Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

Commit

Permalink
new ip function
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandawg93 committed Aug 10, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent c619433 commit 9be9668
Showing 6 changed files with 25 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"displayName": "Homebridge Nest Cam",
"name": "homebridge-nest-cam",
"version": "4.4.1",
"version": "4.4.2",
"description": "Nest cam plugin for homebridge: https://homebridge.io/",
"main": "dist/index.js",
"license": "GPL-3.0",
@@ -61,6 +61,7 @@
"cli.sh"
],
"dependencies": {
"systeminformation": "^4.26.10",
"axios": "^0.19.2",
"ffmpeg-for-homebridge": "^0.0.7",
"pbf": "^3.2.1",
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -73,6 +73,7 @@ class NestCamPlatform implements DynamicPlatformPlugin {
this.options[key] = true;
this.log.debug(`Defaulting ${key} to true`);
} else {
this.options[key] = configVal;
this.log.debug(`Using ${key} from config: ${configVal}`);
}
});
12 changes: 10 additions & 2 deletions src/nest-cam.ts
Original file line number Diff line number Diff line change
@@ -59,7 +59,13 @@ export class NestCam extends EventEmitter {
this.accessory = accessory;
this.info = info;
this.alertCooldown = (config.options?.alertCooldownRate || 180) * 1000;
this.alertInterval = (this.config.options?.alertCheckRate || 10) * 1000;
if (this.alertCooldown > 300000) {
this.alertCooldown = 300000;
}
this.alertInterval = (config.options?.alertCheckRate || 10) * 1000;
if (this.alertInterval > 60000) {
this.alertInterval = 60000;
}
this.endpoints = new NestEndpoints(config.fieldTest);

const alertTypes = config.options?.alertTypes;
@@ -207,7 +213,9 @@ export class NestCam extends EventEmitter {
}
} catch (error) {
handleError(this.log, error, 'Error checking alerts');
this.alertFailures++;
if (this.alertFailures < 10) {
this.alertFailures++;
}
this.alertsSend = false;
setTimeout(() => {
this.alertsSend = true;
8 changes: 4 additions & 4 deletions src/streaming-delegate.ts
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ import {
VideoInfo,
AudioInfo,
} from 'homebridge';
import { getIpAddresses } from './util/ip';
import { getDefaultIpAddress } from './util/ip';
import { NexusStreamer } from './streamer';
import { NestCam } from './nest-cam';
import { NestEndpoints, handleError } from './nest-endpoints';
@@ -151,10 +151,10 @@ export class StreamingDelegate implements CameraStreamingDelegate {
audioSSRC: audioSSRC,
};

const addresses = getIpAddresses(request.addressVersion);
if (addresses.length > 0) {
const address = await getDefaultIpAddress();
if (address) {
const response: PrepareStreamResponse = {
address: addresses[0],
address: address,
video: {
port: returnVideoPort,
ssrc: videoSSRC,
26 changes: 7 additions & 19 deletions src/util/ip.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
import os from 'os';
import { networkInterfaceDefault } from 'systeminformation';

export function getIpAddresses(family = 'ipv4'): Array<string> {
export async function getDefaultIpAddress(): Promise<string | undefined> {
const interfaces = os.networkInterfaces(),
familyLower = family.toLowerCase();
defaultInterfaceName = await networkInterfaceDefault(),
defaultInterface = interfaces[defaultInterfaceName],
externalInfo = defaultInterface?.filter((info) => !info.internal),
addressInfo = externalInfo?.find((info) => info.family === 'IPv4') || externalInfo?.[0];

return Object.entries(interfaces).reduce((addresses, [key, interfaceInfos]) => {
// Skip all virtual and bridge interfaces
if (key.startsWith('v') || key.startsWith('br')) {
return addresses;
}

const matchingAddresses = (interfaceInfos || []).reduce((matches, interfaceInfo) => {
// Remove addresses that have incorrect family or are internal
if (interfaceInfo.internal || interfaceInfo.family.toLowerCase() !== familyLower) {
return matches;
}

return matches.concat([interfaceInfo.address]);
}, [] as Array<string>);

return addresses.concat(matchingAddresses);
}, [] as Array<string>);
return addressInfo?.address;
}

0 comments on commit 9be9668

Please sign in to comment.