Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash

docker build -t dnguyenclincase/huex:latest .
docker build --no-cache -t dnguyenclincase/huex:latest .
docker tag dnguyenclincase/huex:latest dnguyenclincase/huex:0.1.0
47 changes: 26 additions & 21 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "huex",
"version": "0.1.0",
"version": "0.2.0",
"private": true,
"scripts": {
"prebuild": "tslint -c tslint.json -p tsconfig.json --fix",
Expand All @@ -13,7 +13,7 @@
"dependencies": {
"@akanass/rx-http-request": "^3.3.0",
"@databases/pg": "^1.1.1",
"@network-utils/arp-lookup": "^1.2.1",
"@network-utils/arp-lookup": "^1.3.4",
"@types/cookie-parser": "^1.4.2",
"@types/http-errors": "^1.6.2",
"@types/lodash": "^4.14.144",
Expand All @@ -26,7 +26,7 @@
"lodash": "^4.17.15",
"log-timestamp": "^0.3.0",
"morgan": "~1.9.1",
"net-ping": "^1.2.3",
"ping": "^0.2.2",
"rxjs": "^6.5.3"
},
"devDependencies": {
Expand Down
28 changes: 14 additions & 14 deletions src/services/on.off.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { ConnectionPool, sql } from "@databases/pg";
import { getTable, IArpTable, IArpTableRow } from "@network-utils/arp-lookup";
import { find } from "lodash";
import { BehaviorSubject, from, interval, Observable, of } from "rxjs";
import { catchError, combineLatest, debounceTime, first, mapTo, switchMap, withLatestFrom } from "rxjs/operators";
import { catchError, debounceTime, first, switchMap, withLatestFrom } from "rxjs/operators";
import { IDevice, IUser } from "../models/user.info.model";
import { HueApiService } from "./hue.api.service";

// tslint:disable-next-line
const ping = require("net-ping");
const ping = require("ping");

// tslint:disable-next-line
require('log-timestamp');
Expand All @@ -36,12 +36,10 @@ interface ILightStateUpdate {
export class OnOffService {
private hueApiService: HueApiService;
private localIps: string[];
private pingSession: any;
private MONITORING_INTERVAL = 2000;

constructor(private db: ConnectionPool) {
this.hueApiService = new HueApiService();
this.pingSession = ping.createSession();
this.localIps = this.getLocalIps();

// TODO: figureout how to allow user to manage profile and devices
Expand All @@ -53,7 +51,7 @@ export class OnOffService {
*/
public start() {
// Start to ping to monitor device presence in network
this.reachableIps(this.pingSession, this.MONITORING_INTERVAL).pipe(
this.reachableIps(this.MONITORING_INTERVAL).pipe(
debounceTime(200),
withLatestFrom(
this.getArpTableInterval(this.MONITORING_INTERVAL),
Expand Down Expand Up @@ -83,6 +81,8 @@ export class OnOffService {
profile.user.devices.value, (device: IDevice) => reachableIpMacSet.has(device.MAC_ID)
);

console.log(reachableIpChangeE, arpTable, profiles);

if (onlineDevice &&
profile.hueStatus.state.on === false // Light status is off
) {
Expand Down Expand Up @@ -130,7 +130,7 @@ export class OnOffService {
}

// Get a stream of all online ips
private reachableIps(session: any, refreshInterval: number): Observable<IReachableIpChangeEvent> {
private reachableIps(refreshInterval: number): Observable<IReachableIpChangeEvent> {
const currentReachableIps: Set<string> = new Set([]);
const subject = new BehaviorSubject({
data: currentReachableIps,
Expand All @@ -148,18 +148,18 @@ export class OnOffService {
};

// Ping Ip address if there are changes
session.pingHost(ip, (error: any, target: any) => {
ping.sys.probe(ip, (isAlive: boolean) => {
let isUpdated = false;
if (error) { // remove id from reachableIp list
if (currentReachableIps.has(target)) {
newResponse.deletedIp = target;
currentReachableIps.delete(target);
if (!isAlive) { // remove id from reachableIp list
if (currentReachableIps.has(ip)) {
newResponse.deletedIp = ip;
currentReachableIps.delete(ip);
isUpdated = true;
}
} else {
if (!currentReachableIps.has(target)) {
currentReachableIps.add(target);
newResponse.addedIp = target;
if (!currentReachableIps.has(ip)) {
currentReachableIps.add(ip);
newResponse.addedIp = ip;
isUpdated = true;
}
}
Expand Down