Skip to content

Commit

Permalink
wip: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Oct 17, 2023
1 parent 80da79e commit 448a869
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 73 deletions.
2 changes: 1 addition & 1 deletion lib/Cloud/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class CloudController extends CoreBase {
this.clouddb = clouddb
this.cache = cache

this.companionId = registry.machineId
this.companionId = registry.appInfo.machineId
const uuid = this.clouddb.getKey('uuid', undefined)
this.setState({ uuid })

Expand Down
2 changes: 1 addition & 1 deletion lib/Cloud/Region.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class CloudRegion {
token: this.cloud.data.token,
uuid: this.cloud.state.uuid,
companionId: this.cloud.companionId,
version: this.cloud.registry.appBuild,
version: this.cloud.registry.appInfo.appBuild,
})
this.logger.debug('Login ok')

Expand Down
2 changes: 1 addition & 1 deletion lib/Data/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class DataController {
* @param {import('../Registry.js').default} registry
*/
constructor(registry) {
this.cache = new DataCache(registry.configDir)
this.cache = new DataCache(registry.appInfo.configDir)
this.userconfig = new DataUserConfig(registry)
this.importExport = new DataImportExport(registry)
this.metrics = new DataMetrics(registry)
Expand Down
4 changes: 2 additions & 2 deletions lib/Data/ImportExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ class DataImportExport extends CoreBase {
archive.glob(
'*',
{
cwd: this.registry.configDir,
cwd: this.registry.appInfo.configDir,
nodir: true,
ignore: [
'cloud', // Ignore companion-cloud credentials
Expand All @@ -427,7 +427,7 @@ class DataImportExport extends CoreBase {
)

// Add the logs if found
const logsDir = path.join(this.registry.configDir, '../logs')
const logsDir = path.join(this.registry.appInfo.configDir, '../logs')
if (fs.existsSync(logsDir)) {
archive.glob(
'*',
Expand Down
2 changes: 1 addition & 1 deletion lib/Data/Metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class DataMetrics extends CoreBase {

try {
const payload = {
i: this.registry.machineId,
i: this.registry.appInfo.machineId,
r: process.uptime(),
m: instanceCount,
d: relevantDevices,
Expand Down
54 changes: 38 additions & 16 deletions lib/Graphics/Preview.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import CoreBase from '../Core/Base.js'
import Registry from '../Registry.js'
import { ControlConfigRoom } from '../Controls/ControlBase.js'
import { ParseInternalControlReference } from '../Internal/Util.js'
// import LogController from '../Log/Controller.js'

/**
* Get Socket.io room for preview updates
Expand All @@ -28,7 +27,6 @@ function ensureLocationIsNumber(location) {
/**
* The class that manages bank preview generation/relay for interfaces
*
* @extends CoreBase
* @author Håkon Nessjøen <[email protected]>
* @author Keith Rocheck <[email protected]>
* @author William Viker <[email protected]>
Expand All @@ -46,20 +44,44 @@ function ensureLocationIsNumber(location) {
* develop commercial activities involving the Companion software without
* disclosing the source code of your own applications.
*/
class GraphicsPreview extends CoreBase {
class GraphicsPreview {
// #logger = LogController.createLogger('Graphics/Preview')

/**
* @readonly
* @type {import('./Controller.js').default}
*/
#graphicsController

/**
* @readonly
* @type {import('../UI/Handler.js').default}
*/
#ioController

/**
* @readonly
* @type {import('../Page/Controller.js').default}
*/
#pageController

/**
* Current bank reference previews
* @type {Map<string, PreviewSession>}
*/
#bankReferencePreviews = new Map()

/**
* @param {Registry} registry - the application core
* @param {import('./Controller.js').default} graphicsController
* @param {import("../UI/Handler.js").default } ioController
* @param {import("../Page/Controller.js").default} pageController
*/
constructor(registry) {
super(registry, 'preview', 'Graphics/Preview')
constructor(graphicsController, ioController, pageController) {
this.#graphicsController = graphicsController
this.#ioController = ioController
this.#pageController = pageController

this.graphics.on('button_drawn', this.#updateButton.bind(this))
this.#graphicsController.on('button_drawn', this.#updateButton.bind(this))
}

/**
Expand Down Expand Up @@ -94,7 +116,7 @@ class GraphicsPreview extends CoreBase {

client.join(PreviewLocationRoom(location))

const render = this.graphics.getBank(location)
const render = this.#graphicsController.getBank(location)
return { image: render.asDataUrl, isUsed: !!render.style }
}
)
Expand Down Expand Up @@ -149,7 +171,7 @@ class GraphicsPreview extends CoreBase {
client,
})

return result.location ? this.graphics.getBank(result.location).asDataUrl : null
return result.location ? this.#graphicsController.getBank(result.location).asDataUrl : null
}
)
client.onPromise(
Expand All @@ -174,17 +196,17 @@ class GraphicsPreview extends CoreBase {
*/
#updateButton(location, render) {
// Push the updated render to any clients viewing a preview of a control
const controlId = this.page.getControlIdAt(location)
const controlId = this.#pageController.getControlIdAt(location)
if (controlId) {
const controlRoom = ControlConfigRoom(controlId)
if (this.io.countRoomMembers(controlRoom) > 0) {
this.io.emitToRoom(controlRoom, `controls:preview-${controlId}`, render.asDataUrl)
if (this.#ioController.countRoomMembers(controlRoom) > 0) {
this.#ioController.emitToRoom(controlRoom, `controls:preview-${controlId}`, render.asDataUrl)
}
}

const locationRoom = PreviewLocationRoom(location)
if (this.io.countRoomMembers(locationRoom) > 0) {
this.io.emitToRoom(locationRoom, `preview:location:render`, location, render.asDataUrl, !!render.style)
if (this.#ioController.countRoomMembers(locationRoom) > 0) {
this.#ioController.emitToRoom(locationRoom, `preview:location:render`, location, render.asDataUrl, !!render.style)
}

// Lookup any sessions
Expand Down Expand Up @@ -238,7 +260,7 @@ class GraphicsPreview extends CoreBase {

previewSession.client.emit(
`preview:button-reference:update:${previewSession.id}`,
this.graphics.getBank(result.location).asDataUrl
this.#graphicsController.getBank(result.location).asDataUrl
)
}
}
Expand Down
26 changes: 13 additions & 13 deletions lib/Log/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,9 @@ class LogController {
#history = []

/**
* The socket.io server
* @type {import('../UI/Handler.js').default | null}
*/
#io = null
#ioController = null

/**
* Create a new logger
Expand Down Expand Up @@ -184,10 +183,10 @@ class LogController {
client.onPromise('logs:clear', () => {
this.#history = []

if (this.#io && this.#io.countRoomMembers(LogRoom) > 0) {
this.#io.emitToRoom(LogRoom, 'logs:clear')
if (this.#ioController && this.#ioController.countRoomMembers(LogRoom) > 0) {
this.#ioController.emitToRoom(LogRoom, 'logs:clear')

this.#io.emitToRoom(LogRoom, 'logs:lines', [
this.#ioController.emitToRoom(LogRoom, 'logs:lines', [
{
time: Date.now(),
source: 'log',
Expand All @@ -203,8 +202,8 @@ class LogController {
#pendingLines = []
debounceSendLines = debounceFn(
() => {
if (this.#io && this.#io.countRoomMembers(LogRoom) > 0) {
this.#io.emitToRoom(LogRoom, 'logs:lines', this.#pendingLines)
if (this.#ioController && this.#ioController.countRoomMembers(LogRoom) > 0) {
this.#ioController.emitToRoom(LogRoom, 'logs:lines', this.#pendingLines)
}
this.#pendingLines = []
},
Expand Down Expand Up @@ -265,12 +264,13 @@ class LogController {

/**
* Initialize Sentry and UI logging
* @param {import('../Registry.js').default} registry
* @param {import('../Registry.js').AppInfo } appInfo
* @param {import('../UI/Handler.js').default} ioController
* @returns {void}
* @access public
*/
init(registry) {
this.#io = registry.io
init(appInfo, ioController) {
this.#ioController = ioController

// Allow the DSN to be provided as an env variable
let sentryDsn = process.env.SENTRY_DSN
Expand All @@ -289,7 +289,7 @@ class LogController {
try {
init({
dsn: sentryDsn,
release: `companion@${registry.appBuild || registry.appVersion}`,
release: `companion@${appInfo.appBuild || appInfo.appVersion}`,
beforeSend(event) {
if (event.exception) {
console.log('sentry', 'error', JSON.stringify(event.exception, undefined, 4))
Expand All @@ -300,8 +300,8 @@ class LogController {
})

configureScope((scope) => {
scope.setUser({ id: registry.machineId })
scope.setExtra('build', registry.appBuild)
scope.setUser({ id: appInfo.machineId })
scope.setExtra('build', appInfo.appBuild)
})
} catch (e) {
this.logger.info(`Failed to setup sentry reporting: ${e}`)
Expand Down
37 changes: 28 additions & 9 deletions lib/Registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ class Registry extends EventEmitter {
*/
api_router

/**
* @type {AppInfo}
* @access public
* @readonly
*/
appInfo

/**
* Create a new application <code>Registry</code>
* @param {string} configDir - the configuration path
Expand All @@ -197,11 +204,13 @@ class Registry extends EventEmitter {
this.logger.info(`Build ${buildNumber}`)
this.logger.info(`configuration directory: ${configDir}`)

this.configDir = configDir
this.machineId = machineId
this.appVersion = pkgInfo.version
this.appBuild = buildNumber
this.pkgInfo = pkgInfo
this.appInfo = {
configDir: configDir,
machineId: machineId,
appVersion: pkgInfo.version,
appBuild: buildNumber,
pkgInfo: pkgInfo,
}
}

/**
Expand All @@ -216,15 +225,15 @@ class Registry extends EventEmitter {
this.api_router = express.Router()
this.ui = new UIController(this)
this.io = this.ui.io
this.db = new DataDatabase(this.configDir)
this.clouddb = new CloudDatabase(this.configDir)
this.db = new DataDatabase(this.appInfo.configDir)
this.clouddb = new CloudDatabase(this.appInfo.configDir)
this.data = new DataController(this)
this.userconfig = this.data.userconfig
LogController.init(this)
LogController.init(this.appInfo, this.ui.io)
this.page = new PageController(this)
this.controls = new ControlsController(this)
this.graphics = new GraphicsController(this)
this.preview = new GraphicsPreview(this)
this.preview = new GraphicsPreview(this.graphics, this.io, this.page)
this.surfaces = new SurfaceController(this)
this.instance = new InstanceController(this)
this.services = new ServiceController(this)
Expand Down Expand Up @@ -348,3 +357,13 @@ class Registry extends EventEmitter {
}

export default Registry

/**
* @typedef {{
* configDir: string
* machineId: string
* appVersion: string
* appBuild: string
* pkgInfo: string
* }} AppInfo
*/
4 changes: 2 additions & 2 deletions lib/Service/EmberPlus.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class ServiceEmberPlus extends ServiceBase {
EmberModel.ParameterType.String,
'version',
undefined,
this.registry.appVersion
this.registry.appInfo.appVersion
)
),
3: new EmberModel.NumberedTreeNodeImpl(
Expand All @@ -229,7 +229,7 @@ class ServiceEmberPlus extends ServiceBase {
EmberModel.ParameterType.String,
'build',
undefined,
this.registry.appBuild
this.registry.appInfo.appBuild
)
),
}),
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/Satellite.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class ServiceSatellite extends ServiceBase {

socket.on('close', doCleanup)

socket.write(`BEGIN CompanionVersion=${this.registry.appBuild} ApiVersion=${API_VERSION}\n`)
socket.write(`BEGIN CompanionVersion=${this.registry.appInfo.appBuild} ApiVersion=${API_VERSION}\n`)
}

/**
Expand Down
6 changes: 4 additions & 2 deletions lib/UI/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ class UIController {
*/
constructor(registry) {
this.express = new UIExpress(registry)
this.server = new UIServer(registry, this.express.app)
this.server = new UIServer(this.express.app)
registry.on('http_rebind', this.server.rebindHttp.bind(this.server))

this.io = new UIHandler(registry, this.server)
this.update = new UIUpdate(registry)
this.update = new UIUpdate(registry.appInfo, this.io)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/UI/Handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ class UIHandler {

client.onPromise('app-version-info', () => {
return {
appVersion: this.registry.appVersion,
appBuild: this.registry.appBuild,
appVersion: this.registry.appInfo.appVersion,
appBuild: this.registry.appInfo.appBuild,
}
})

Expand Down
7 changes: 2 additions & 5 deletions lib/UI/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,18 @@ class UIServer extends _http {
#logger = LogController.createLogger('UI/Server')

/**
* @param {import('../Registry.js').default} registry
* @param {import('express').Express} express
*/
constructor(registry, express) {
constructor(express) {
super(express)

registry.on('http_rebind', this.#rebindHttp.bind(this))
}

/**
*
* @param {string} bind_ip
* @param {number} http_port
*/
#rebindHttp(bind_ip, http_port) {
rebindHttp(bind_ip, http_port) {
this.bind_ip = bind_ip
this.http_port = http_port

Expand Down
Loading

0 comments on commit 448a869

Please sign in to comment.