Skip to content

Commit

Permalink
remove redundant async actions & feedbacks
Browse files Browse the repository at this point in the history
refactor init (calls configUpdated, since run the same code).
refactor record action
feedback crosspoint_connected, crosspoiint_connected_by_name add subscribe
subscribeActions and subscribeFeedbacks on connect
  • Loading branch information
phillipivan committed Jul 9, 2024
1 parent b2707c2 commit cd9f96d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 44 deletions.
41 changes: 26 additions & 15 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,36 @@ export async function UpdateActions(self) {
actionDefinitions['select_level'] = {
name: 'Select Levels',
options: [{ ...actionOptions.levels, choices: self.levels }],
callback: async ({ options }) => {
callback: ({ options }) => {
self.processLevelsSelection(options.level, true)
},
}
actionDefinitions['deselect_level'] = {
name: 'De-Select Levels',
options: [{ ...actionOptions.levels, choices: self.levels }],
callback: async ({ options }) => {
callback: ({ options }) => {
self.processLevelsSelection(options.level, false)
},
}
actionDefinitions['toggle_level'] = {
name: 'Toggle Levels',
options: [{ ...actionOptions.levels, choices: self.levels }],
callback: async ({ options }) => {
callback: ({ options }) => {
self.processLevelsSelection(options.level, 'toggle')
},
}
actionDefinitions['select_dest'] = {
name: 'Select Destination',
options: [ actionOptions.destination ],
callback: async ({ options }) => {
options: [actionOptions.destination],
callback: ({ options }) => {
self.selected_dest = parseInt(options.dest)
self.getCrosspoints(options.dest)
console.log('set destination ' + self.selected_dest)
self.setVariableValues({ Destination: self.selected_dest })
self.checkFeedbacks('selected_dest', 'selected_level_dest')
self.checkFeedbacks('selected_dest', 'selected_level_dest', 'source_dest_route')
},
subscribe: (action) => {
self.getCrosspoints(action.options.dest)
},
}
actionDefinitions['select_dest_name'] = {
Expand All @@ -48,13 +51,21 @@ export async function UpdateActions(self) {
self.getCrosspoints(dest)
console.log('set destination ' + self.selected_dest)
self.setVariableValues({ Destination: self.selected_dest })
self.checkFeedbacks('selected_dest', 'selected_level_dest')
self.checkFeedbacks('selected_dest', 'selected_level_dest', 'source_dest_route')
},
subscribe: async (action) => {
const dest = parseInt(await self.parseVariablesInString(action.options.dest))
if (isNaN(dest) || dest < 1 || dest > 65536) {
self.log('warn', `select_dest_name:Subscribe has been passed an out of range variable - dst ${dest}`)
return undefined
}
self.getCrosspoints(dest)
},
}
actionDefinitions['select_source'] = {
name: 'Select Source',
options: [ actionOptions.source ],
callback: async ({ options }) => {
callback: ({ options }) => {
self.selected_source = parseInt(options.source)
console.log('set source ' + self.selected_source)
self.setVariableValues({ Source: self.selected_source })
Expand All @@ -79,7 +90,7 @@ export async function UpdateActions(self) {
actionDefinitions['route_source'] = {
name: 'Route Source to selected Levels and Destination',
options: [ actionOptions.source ],
callback: async ({ options }) => {
callback: ({ options }) => {
console.log(self.selected_level)
const l = self.selected_level.length
for (let i = 0; i < l; i++) {
Expand Down Expand Up @@ -110,7 +121,7 @@ export async function UpdateActions(self) {
actionDefinitions['take'] = {
name: 'Take',
options: [],
callback: async () => {
callback: () => {
console.log(self.selected_level)
const l = self.selected_level.length
for (let i = 0; i < l; i++) {
Expand All @@ -123,21 +134,21 @@ export async function UpdateActions(self) {
actionDefinitions['clear'] = {
name: 'Clear',
options: [ actionOptions.clear, actionOptions.clearEnableLevels ],
callback: async ({ options }) => {
callback: ({ options }) => {
if (options.clear === 'all' || options.clear === 'level') {
self.selected_level = []
for (let i = 1; i <= self.config.max_levels; i++) {
self.selected_level.push({ id: i, enabled: options.clear_enable_levels })
}
self.checkFeedbacks('selected_level', 'selected_level_dest')
self.checkFeedbacks('selected_level', 'selected_level_dest', 'source_dest_route')
console.log('clear levels')
console.log(self.selected_level)
}

if (options.clear === 'all' || options.clear === 'dest') {
self.selected_dest = 0
self.setVariableValues({ Destination: self.selected_dest })
self.checkFeedbacks('selected_dest', 'selected_level_dest')
self.checkFeedbacks('selected_dest', 'selected_level_dest', 'source_dest_route')
console.log('clear dest')
}

Expand All @@ -155,7 +166,7 @@ export async function UpdateActions(self) {
actionOptions.source,
actionOptions.destination,
],
callback: async ({ options }) => {
callback: ({ options }) => {
for (let level_val of options.level) {
self.SetCrosspoint(options.source, options.dest, level_val)
}
Expand Down Expand Up @@ -183,7 +194,7 @@ export async function UpdateActions(self) {
actionDefinitions['get_names'] = {
name: 'Refresh Source and Destination names',
options: [],
callback: async () => {
callback: () => {
self.readNames()
},
}
Expand Down
25 changes: 15 additions & 10 deletions src/crosspoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ export function ext_crosspointConnected(data) {
this.update_crosspoints(source, dest, level)
}

export function record_crosspoint(source, dest, level) {
if (this.isRecordingActions) {
this.recordAction(
{
actionId: 'set_crosspoint',
options: { level: [level], source: source, dest: dest },
},
`connect dest ${dest} level ${level}`
)
}
}

export function update_crosspoints(source, dest, level) {
if (dest == this.selected_dest) {
// update variables for selected dest source
Expand All @@ -52,7 +64,8 @@ export function update_crosspoints(source, dest, level) {
// update existing
this.routeTable[i].source = source
console.log(this.routeTable)
//this.checkFeedbacks('source_dest_route', 'crosspoint_connected', 'crosspoint_connected_by_name')
this.checkFeedbacks('source_dest_route', 'crosspoint_connected', 'crosspoint_connected_by_name')
this.record_crosspoint(source, dest, level)
return
}
}
Expand All @@ -62,15 +75,7 @@ export function update_crosspoints(source, dest, level) {
this.routeTable.push(new_route)
console.log(this.routeTable)
this.checkFeedbacks('source_dest_route', 'crosspoint_connected', 'crosspoint_connected_by_name')
if (this.isRecordingActions) {
this.recordAction(
{
actionId: 'set_crosspoint',
options: { level: [level], source: source, dest: dest },
},
`connect dest ${dest} level ${level}`
)
}
this.record_crosspoint(source, dest, level)
}

export function SetCrosspoint(sourceN, destN, levelN) {
Expand Down
26 changes: 20 additions & 6 deletions src/feedbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function UpdateFeedbacks(self) {
bgcolor: colours.purple,
},
options: [{ ...feedbackOptions.levels, choices: self.levels }],
callback: async (feedback) => {
callback: (feedback) => {
let l = feedback.options.level.length
let k = self.selected_level.length

Expand Down Expand Up @@ -41,7 +41,7 @@ export async function UpdateFeedbacks(self) {
bgcolor: colours.purple,
},
options: [{ ...feedbackOptions.levels, choices: self.levels }, feedbackOptions.destination],
callback: async (feedback) => {
callback: (feedback) => {
if (self.selected_dest === feedback.options.dest) {
let l = feedback.options.level.length
let k = self.selected_level.length
Expand Down Expand Up @@ -74,7 +74,7 @@ export async function UpdateFeedbacks(self) {
bgcolor: colours.green,
},
options: [feedbackOptions.destination],
callback: async (feedback) => {
callback: (feedback) => {
if (self.selected_dest === feedback.options.dest) {
return true
} else {
Expand All @@ -92,7 +92,7 @@ export async function UpdateFeedbacks(self) {
bgcolor: colours.cyan,
},
options: [feedbackOptions.source],
callback: async (feedback) => {
callback: (feedback) => {
if (self.selected_source === feedback.options.source) {
return true
} else {
Expand All @@ -110,7 +110,7 @@ export async function UpdateFeedbacks(self) {
bgcolor: colours.orange,
},
options: [feedbackOptions.source],
callback: async (feedback) => {
callback: (feedback) => {
// look for this dest in route table
console.log('dest:source feedback ' + self.selected_dest + ':' + feedback.options.source)
for (let i = 0; i < self.routeTable.length; i++) {
Expand All @@ -133,7 +133,7 @@ export async function UpdateFeedbacks(self) {
bgcolor: colours.orange,
},
options: [feedbackOptions.source, feedbackOptions.destination],
callback: async (feedback) => {
callback: (feedback) => {
// look for this dest in route table
console.log('dest:source feedback ' + feedback.options.dest + ':' + feedback.options.source)
for (let i = 0; i < self.routeTable.length; i++) {
Expand All @@ -145,6 +145,9 @@ export async function UpdateFeedbacks(self) {
}
return false
},
subscribe: (feedback) => {
self.getCrosspoints(feedback.options.dest)
}
}
feedbackDefinitions['crosspoint_connected_by_name'] = {
type: 'boolean',
Expand Down Expand Up @@ -185,6 +188,17 @@ export async function UpdateFeedbacks(self) {
}
return false
},
subscribe: async (feedback, context) => {
const dest = parseInt(await context.parseVariablesInString(feedback.options.dest))
if (isNaN(dest) || dest < 1 || dest > 65536) {
self.log(
'warn',
`crosspoint_connected_by_name:Subscribe has been passed an out of range variable - dst ${dest}`
)
return undefined
}
self.getCrosspoints(dest)
},
}

self.setFeedbackDefinitions(feedbackDefinitions)
Expand Down
21 changes: 9 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,27 @@ class SW_P_08 extends InstanceBase {
}

async init(config) {
this.updateStatus(InstanceStatus.Connecting)
this.config = config
this.setupVariables()
this.updateFeedbacks()
this.updateActions()
this.updatePresets()
this.init_tcp()
this.checkFeedbacks('selected_level', 'selected_level_dest', 'selected_dest', 'selected_source')
this.configUpdated(config)
}

async configUpdated(config) {
this.log('debug', 'update config')
this.updateStatus(InstanceStatus.Connecting)
this.config = config
this.setupVariables()
this.updateFeedbacks()
this.updateActions()
this.updatePresets()
this.init_tcp()
this.checkFeedbacks('selected_level', 'selected_level_dest', 'selected_dest', 'selected_source')
this.checkFeedbacks(
'selected_level',
'selected_level_dest',
'selected_dest',
'selected_source',
'crosspoint_connected',
'crosspoint_connected_by_name'
)
}

// When module gets deleted
async destroy() {
this.log('debug', `destroy. ID: ${this.id}`)
this.stopKeepAliveTimer()
Expand All @@ -73,7 +71,6 @@ class SW_P_08 extends InstanceBase {
this.updateStatus(InstanceStatus.Disconnected)
}

// Track whether actions are being recorded
handleStartStopRecordActions(isRecording) {
this.isRecordingActions = isRecording
}
Expand Down
5 changes: 4 additions & 1 deletion src/tcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@ export function init_tcp() {

this.socket.on('connect', () => {
this.updateStatus(InstanceStatus.Ok, 'Connected')
this.startKeepAliveTimer()
if (this.config.supported_commands_on_connect === true) {
// request protocol implementation
this.sendMessage('61019E')
}
this.subscribeActions()
this.subscribeFeedbacks()
this.startKeepAliveTimer()
this.checkFeedbacks()
})

this.socket.on('data', (chunk) => {
Expand Down

0 comments on commit cd9f96d

Please sign in to comment.