homebridge-myplace
Version:
Exec Plugin bringing Advanatge Air MyPlace system to Homekit
144 lines (120 loc) • 4.87 kB
JavaScript
;
//
// Homebridge
// Flow / \
// / \
// api.registerPlatform api.registerAccessory
// forEach Accessories{ } Any { } before/after Accessories{ }
// Cmd5Platform Cmd5Accessory
// Cmd5Accessory
//
//
//
//
//
//
//
//
//
//
// The Cmd5 Classes
const Cmd5Accessory = require( "./Cmd5Accessory" ).Cmd5Accessory;
const Cmd5Platform = require( "./Cmd5Platform" ).Cmd5Platform;
const settings = require( "./cmd5Settings" );
// Pretty colors
const chalk = require( "chalk" );
// The Library files that know all.
var CHAR_DATA = require( "./lib/CMD5_CHAR_TYPE_ENUMS" );
var ACC_DATA = require( "./lib/CMD5_ACC_TYPE_ENUM" );
var DEVICE_DATA = require( "./lib/CMD5_DEVICE_TYPE_ENUM" );
module.exports =
{
default: function ( api )
{
// Init the libraries for all to use
let CMD5_CHAR_TYPE_ENUMS = CHAR_DATA.init( api.hap.Formats, api.hap.Units, api.hap.Perms );
let CMD5_ACC_TYPE_ENUM = ACC_DATA.init( api.hap.Characteristic, api.hap.Formats, api.hap.Units, api.hap.Perms );
let CMD5_DEVICE_TYPE_ENUM = DEVICE_DATA.init(
CMD5_ACC_TYPE_ENUM, api.hap.Service, api.hap.Characteristic, api.hap.Categories );
api.registerAccessory( settings.PLATFORM_NAME, Cmd5Accessory );
api.registerPlatform( settings.PLATFORM_NAME, Cmd5Platform );
setTimeout( removeTempWorkDir, 1800 );
setTimeout( checkForUpdates, 1800 );
// This is not required by homebridge and does not affect it. I use it for
// unit testing.
return { CMD5_CHAR_TYPE_ENUMS,
CMD5_ACC_TYPE_ENUM,
CMD5_DEVICE_TYPE_ENUM,
api
};
},
// These would be the uninitialized values,
// used for unit testing
CHAR_DATA: CHAR_DATA, // properties would be { } empty.
ACC_DATA: ACC_DATA, // properties would be { } empty.
DEVICE_DATA: DEVICE_DATA // properties would be { } empty.
}
function removeTempWorkDir( )
{
const fs = require('fs');
( async( ) =>
{
// Remove MyPlace shell script temporary working directories on Hombridge RESTART
// Get current date and time
// Extract date components
const now = new Date();
const day = now.getDate().toString().padStart(2, '0');
const month = (now.getMonth() + 1).toString().padStart(2, '0'); // Months are zero-based
const year = now.getFullYear();
// Extract time components
const hours = now.getHours().toString().padStart(2, '0');
const minutes = now.getMinutes().toString().padStart(2, '0');
const seconds = now.getSeconds().toString().padStart(2, '0');
try {
const directoryPath = process.env.TMPDIR || "/tmp";
// Read directory contents
const files = fs.readdirSync(directoryPath);
// Filter files that match "AA-xxx" or "BB-xxx" patterns
const filteredFiles = files.filter(file => file.match(/^(AA|BB)-\d{3}$/));
// Process each filtered file
filteredFiles.forEach(file => {
const sdir = `${directoryPath}/${file}`;
try {
// Remove sub-directory recursively
fs.rmSync(sdir, { recursive: true, force: true });
console.log(`[${day}/${month}/${year}, ${hours}:${minutes}:${seconds}] [MyPlace] Temporary working directory ${sdir} removed`);
} catch (err) {
console.error(`[${day}/${month}/${year}, ${hours}:${minutes}:${seconds}] [MyPlace] Unable to remove temporary working directory ${sdir}: [${err}]`);
}
});
} catch (err) {
console.error(`[${day}/${month}/${year}, ${hours}:${minutes}:${seconds}] [MyPlace] Unable to scan directory: [${err}]`);
}
})( );
}
function checkForUpdates( )
{
// Don't show the updates message in mocha test mode
if ( process.argv.includes( "test/mocha-setup" ) )
return;
const { getLatestVersion, isVersionNewerThanPackagedVersion } = require( "./utils/versionChecker" );
const myPkg = require( "./package.json" );
( async( ) =>
{
// Fix for #127, constant crash loops when no internet connection
// trying to get latest Cmd5 version.
// thx nano9g
try
{
let lv = await getLatestVersion( );
if ( isVersionNewerThanPackagedVersion( lv ) )
{
console.log( chalk.green( `[UPDATE AVAILABLE] ` ) + `Version ${lv} of ${myPkg.name} is available. Any release notes can be found here: ` + chalk.underline( `${myPkg.changelog}` ) );
}
}
catch( error )
{
console.log( chalk.yellow( `[UPDATE CHECK FAILED] ` ) + `Could not check for newer versions of ${myPkg.name} due to error ${error.name}: ${error.message}`)
}
})( );
}