Skip to content

Commit

Permalink
Minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Sep 23, 2020
1 parent 0843e2a commit e858e39
Show file tree
Hide file tree
Showing 42 changed files with 663 additions and 839 deletions.
6 changes: 3 additions & 3 deletions src/BridgedCore.ts
Expand Up @@ -22,8 +22,8 @@ bridge.on(AccessoryEventTypes.IDENTIFY, (paired: boolean, callback: VoidCallback
});

// Load up all accessories in the /accessories folder
var dir = path.join(__dirname, "accessories");
var accessories = AccessoryLoader.loadDirectory(dir);
const dir = path.join(__dirname, "accessories");
const accessories = AccessoryLoader.loadDirectory(dir);

// Add them all to the bridge
accessories.forEach((accessory: Accessory) => {
Expand All @@ -38,7 +38,7 @@ bridge.publish({
category: Categories.BRIDGE
});

var signals = { 'SIGINT': 2, 'SIGTERM': 15 } as Record<string, number>;
const signals = {'SIGINT': 2, 'SIGTERM': 15} as Record<string, number>;
Object.keys(signals).forEach((signal: any) => {
process.on(signal, function () {
bridge.unpublish();
Expand Down
10 changes: 5 additions & 5 deletions src/Core.ts
Expand Up @@ -13,11 +13,11 @@ console.warn("DEPRECATION NOTICE: The use of Core and BridgeCore are deprecated
storage.initSync();

// Our Accessories will each have their own HAP server; we will assign ports sequentially
var targetPort = 51826;
let targetPort = 51826;

// Load up all accessories in the /accessories folder
var dir = path.join(__dirname, "accessories");
var accessories = AccessoryLoader.loadDirectory(dir);
const dir = path.join(__dirname, "accessories");
const accessories = AccessoryLoader.loadDirectory(dir);

// Publish them all separately (as opposed to BridgedCore which publishes them behind a single Bridge accessory)
accessories.forEach((accessory) => {
Expand Down Expand Up @@ -45,10 +45,10 @@ accessories.forEach((accessory) => {
});
});

var signals = { 'SIGINT': 2, 'SIGTERM': 15 } as Record<string, number>;
const signals = {'SIGINT': 2, 'SIGTERM': 15} as Record<string, number>;
Object.keys(signals).forEach((signal: any) => {
process.on(signal, () => {
for (var i = 0; i < accessories.length; i++) {
for (let i = 0; i < accessories.length; i++) {
accessories[i].unpublish();
}

Expand Down
23 changes: 12 additions & 11 deletions src/accessories/AirConditioner_accessory.ts
@@ -1,4 +1,4 @@
//In This example we create an Airconditioner Accessory that Has a Thermostat linked to a Fan Service.
//In This example we create an air conditioner Accessory that Has a Thermostat linked to a Fan Service.
//For example, I've also put a Light Service that should be hidden to represent a light in the closet that is part of the AC. It is to show how to hide services.
//The linking and Hiding does NOT appear to be reflected in Home

Expand All @@ -8,14 +8,16 @@ import {
AccessoryEventTypes,
Categories,
Characteristic,
CharacteristicEventTypes, CharacteristicGetCallback, CharacteristicSetCallback,
CharacteristicEventTypes,
CharacteristicGetCallback,
CharacteristicSetCallback,
CharacteristicValue,
Service,
uuid,
} from '..';
import { NodeCallback, VoidCallback } from '../types';
import { VoidCallback } from '../types';

var ACTest_data: Record<string, CharacteristicValue> = {
const ACTest_data: Record<string, CharacteristicValue> = {
fanPowerOn: false,
rSpeed: 100,
CurrentHeatingCoolingState: 1,
Expand All @@ -24,10 +26,10 @@ var ACTest_data: Record<string, CharacteristicValue> = {
TargetTemperature: 32,
TemperatureDisplayUnits: 1,
LightOn: false
}
};

// This is the Accessory that we'll return to HAP-NodeJS that represents our fake fan.
var ACTest = exports.accessory = new Accessory('Air Conditioner', uuid.generate('hap-nodejs:accessories:airconditioner'));
const ACTest = exports.accessory = new Accessory('Air Conditioner', uuid.generate('hap-nodejs:accessories:airconditioner'));

// Add properties for publishing (in case we're using Core.js and not BridgedCore.js)
// @ts-ignore
Expand All @@ -51,7 +53,7 @@ ACTest.on(AccessoryEventTypes.IDENTIFY, (paired: boolean, callback: VoidCallback
// Add the actual Fan Service and listen for change events from iOS.
// We can see the complete list of Services and Characteristics in `lib/gen/HomeKit.ts`

var FanService = ACTest.addService(Service.Fan, "Blower") // services exposed to the user should have "names" like "Fake Light" for us
const FanService = ACTest.addService(Service.Fan, "Blower"); // services exposed to the user should have "names" like "Fake Light" for us
FanService.getCharacteristic(Characteristic.On)!
.on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
console.log("Fan Power Changed To "+value);
Expand All @@ -68,7 +70,7 @@ FanService.getCharacteristic(Characteristic.On)!
// the fan hardware itself to find this out, then call the callback. But if you take longer than a
// few seconds to respond, Siri will give up.

var err = null; // in case there were any problems
const err = null; // in case there were any problems

if (ACTest_data.fanPowerOn) {
callback(err, true);
Expand All @@ -90,7 +92,7 @@ FanService.addCharacteristic(Characteristic.RotationSpeed)
callback();
})

var ThermostatService = ACTest.addService(Service.Thermostat,"Thermostat");
const ThermostatService = ACTest.addService(Service.Thermostat, "Thermostat");
ThermostatService.addLinkedService(FanService);
ThermostatService.setPrimaryService();

Expand Down Expand Up @@ -146,8 +148,7 @@ ThermostatService.getCharacteristic(Characteristic.CurrentHeatingCoolingState)!
});



var LightService = ACTest.addService(Service.Lightbulb, 'AC Light');
const LightService = ACTest.addService(Service.Lightbulb, 'AC Light');
LightService.getCharacteristic(Characteristic.On)!
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
callback(null, ACTest_data.LightOn);
Expand Down
13 changes: 6 additions & 7 deletions src/accessories/Fan_accessory.ts
Expand Up @@ -12,15 +12,14 @@ import {
VoidCallback
} from '..';

var FAKE_FAN: Record<string, any> = {
const FAKE_FAN: Record<string, any> = {
powerOn: false,
rSpeed: 100,
setPowerOn: (on: CharacteristicValue) => {
if(on){
if (on) {
//put your code here to turn on the fan
FAKE_FAN.powerOn = on;
}
else{
} else {
//put your code here to turn off the fan
FAKE_FAN.powerOn = on;
}
Expand All @@ -34,10 +33,10 @@ var FAKE_FAN: Record<string, any> = {
//put your code here to identify the fan
console.log("Fan Identified!");
}
}
};

// This is the Accessory that we'll return to HAP-NodeJS that represents our fake fan.
var fan = exports.accessory = new Accessory('Fan', uuid.generate('hap-nodejs:accessories:Fan'));
const fan = exports.accessory = new Accessory('Fan', uuid.generate('hap-nodejs:accessories:Fan'));

// Add properties for publishing (in case we're using Core.js and not BridgedCore.js)
// @ts-ignore
Expand Down Expand Up @@ -79,7 +78,7 @@ fan
// the fan hardware itself to find this out, then call the callback. But if you take longer than a
// few seconds to respond, Siri will give up.

var err = null; // in case there were any problems
const err = null; // in case there were any problems

if (FAKE_FAN.powerOn) {
callback(err, true);
Expand Down
10 changes: 5 additions & 5 deletions src/accessories/GarageDoorOpener_accessory.ts
Expand Up @@ -10,7 +10,7 @@ import {
VoidCallback
} from '..';

var FAKE_GARAGE = {
const FAKE_GARAGE = {
opened: false,
open: () => {
console.log("Opening the Garage!");
Expand All @@ -26,15 +26,15 @@ var FAKE_GARAGE = {
//add your code here which allows the garage to be identified
console.log("Identify the Garage");
},
status: () =>{
status: () => {
//use this section to get sensor values. set the boolean FAKE_GARAGE.opened with a sensor value.
console.log("Sensor queried!");
//FAKE_GARAGE.opened = true/false;
}
};

var garageUUID = uuid.generate('hap-nodejs:accessories:'+'GarageDoor');
var garage = exports.accessory = new Accessory('Garage Door', garageUUID);
const garageUUID = uuid.generate('hap-nodejs:accessories:' + 'GarageDoor');
const garage = exports.accessory = new Accessory('Garage Door', garageUUID);

// Add properties for publishing (in case we're using Core.js and not BridgedCore.js)
// @ts-ignore
Expand Down Expand Up @@ -83,7 +83,7 @@ garage
.getCharacteristic(Characteristic.CurrentDoorState)!
.on(CharacteristicEventTypes.GET, (callback: NodeCallback<CharacteristicValue>) => {

var err = null;
const err = null;
FAKE_GARAGE.status();

if (FAKE_GARAGE.opened) {
Expand Down
4 changes: 2 additions & 2 deletions src/accessories/Light_accessory.ts
Expand Up @@ -77,10 +77,10 @@ const LightController = new LightControllerClass();
// Generate a consistent UUID for our light Accessory that will remain the same even when
// restarting our server. We use the `uuid.generate` helper function to create a deterministic
// UUID based on an arbitrary "namespace" and the word "light".
var lightUUID = uuid.generate('hap-nodejs:accessories:light' + LightController.name);
const lightUUID = uuid.generate('hap-nodejs:accessories:light' + LightController.name);

// This is the Accessory that we'll return to HAP-NodeJS that represents our light.
var lightAccessory = exports.accessory = new Accessory(LightController.name as string, lightUUID);
const lightAccessory = exports.accessory = new Accessory(LightController.name as string, lightUUID);

// Add properties for publishing (in case we're using Core.js and not BridgedCore.js)
// @ts-ignore
Expand Down
10 changes: 5 additions & 5 deletions src/accessories/Lock_accessory.ts
Expand Up @@ -11,7 +11,7 @@ import {
import { NodeCallback, VoidCallback } from '../types';

// here's a fake hardware device that we'll expose to HomeKit
var FAKE_LOCK = {
const FAKE_LOCK = {
locked: false,
lock: () => {
console.log("Locking the lock!");
Expand All @@ -24,15 +24,15 @@ var FAKE_LOCK = {
identify: () => {
console.log("Identify the lock!");
}
}
};

// Generate a consistent UUID for our Lock Accessory that will remain the same even when
// restarting our server. We use the `uuid.generate` helper function to create a deterministic
// UUID based on an arbitrary "namespace" and the word "lock".
var lockUUID = uuid.generate('hap-nodejs:accessories:lock');
const lockUUID = uuid.generate('hap-nodejs:accessories:lock');

// This is the Accessory that we'll return to HAP-NodeJS that represents our fake lock.
var lock = exports.accessory = new Accessory('Lock', lockUUID);
const lock = exports.accessory = new Accessory('Lock', lockUUID);

// Add properties for publishing (in case we're using Core.js and not BridgedCore.js)
// @ts-ignore
Expand Down Expand Up @@ -93,7 +93,7 @@ lock
// the lock hardware itself to find this out, then call the callback. But if you take longer than a
// few seconds to respond, Siri will give up.

var err = null; // in case there were any problems
const err = null; // in case there were any problems

if (FAKE_LOCK.locked) {
console.log("Are we locked? Yes.");
Expand Down
8 changes: 4 additions & 4 deletions src/accessories/MotionSensor_accessory.ts
Expand Up @@ -11,7 +11,7 @@ import {
uuid, VoidCallback
} from '..';

var MOTION_SENSOR = {
const MOTION_SENSOR = {
motionDetected: false,

getStatus: () => {
Expand All @@ -21,15 +21,15 @@ var MOTION_SENSOR = {
identify: () => {
console.log("Identify the motion sensor!");
}
}
};

// Generate a consistent UUID for our Motion Sensor Accessory that will remain the same even when
// restarting our server. We use the `uuid.generate` helper function to create a deterministic
// UUID based on an arbitrary "namespace" and the word "motionsensor".
var motionSensorUUID = uuid.generate('hap-nodejs:accessories:motionsensor');
const motionSensorUUID = uuid.generate('hap-nodejs:accessories:motionsensor');

// This is the Accessory that we'll return to HAP-NodeJS that represents our fake motionSensor.
var motionSensor = exports.accessory = new Accessory('Motion Sensor', motionSensorUUID);
const motionSensor = exports.accessory = new Accessory('Motion Sensor', motionSensorUUID);

// Add properties for publishing (in case we're using Core.js and not BridgedCore.js)
// @ts-ignore
Expand Down
32 changes: 18 additions & 14 deletions src/accessories/Outlet_accessory.ts
Expand Up @@ -14,32 +14,36 @@ import { Nullable } from '../types';
let err: Nullable<Error> = null; // in case there were any problems

// here's a fake hardware device that we'll expose to HomeKit
var FAKE_OUTLET = {
const FAKE_OUTLET = {
powerOn: false,
setPowerOn: (on: CharacteristicValue) => {
setPowerOn: (on: CharacteristicValue) => {
console.log("Turning the outlet %s!...", on ? "on" : "off");
if (on) {
FAKE_OUTLET.powerOn = true;
if(err) { return console.log(err); }
console.log("...outlet is now on.");
FAKE_OUTLET.powerOn = true;
if (err) {
return console.log(err);
}
console.log("...outlet is now on.");
} else {
FAKE_OUTLET.powerOn = false;
if(err) { return console.log(err); }
console.log("...outlet is now off.");
FAKE_OUTLET.powerOn = false;
if (err) {
return console.log(err);
}
console.log("...outlet is now off.");
}
},
identify: function() {
identify: function () {
console.log("Identify the outlet.");
}
}
}
};

// Generate a consistent UUID for our outlet Accessory that will remain the same even when
// restarting our server. We use the `uuid.generate` helper function to create a deterministic
// UUID based on an arbitrary "namespace" and the accessory name.
var outletUUID = uuid.generate('hap-nodejs:accessories:Outlet');
const outletUUID = uuid.generate('hap-nodejs:accessories:Outlet');

// This is the Accessory that we'll return to HAP-NodeJS that represents our fake light.
var outlet = exports.accessory = new Accessory('Outlet', outletUUID);
const outlet = exports.accessory = new Accessory('Outlet', outletUUID);

// Add properties for publishing (in case we're using Core.js and not BridgedCore.js)
// @ts-ignore
Expand Down Expand Up @@ -83,7 +87,7 @@ outlet
// the light hardware itself to find this out, then call the callback. But if you take longer than a
// few seconds to respond, Siri will give up.

var err = null; // in case there were any problems
const err = null; // in case there were any problems

if (FAKE_OUTLET.powerOn) {
console.log("Are we on? Yes.");
Expand Down

0 comments on commit e858e39

Please sign in to comment.