-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToggleMicState.js
162 lines (132 loc) · 5.15 KB
/
ToggleMicState.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/********************************************************
Copyright (c) 2024 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.1 (the "License"). You may obtain a copy of the
License at
https://developer.cisco.com/docs/licenses
All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately in
writing, software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
*********************************************************
* Author(s): Gerardo Chaves
* Solutions Engineer
* Cisco Systems
*
*
* Released: December 19, 2024
*
* Version 1.0.0
*
* Description:
* - Implements a custom panel button to turn on/off a specific microphone
*
* Tested Devices
* - Codec Pro
*/
import xapi from 'xapi';
// set the ID of the microphone to toggle below
const MicId = '1'
const button_color_red = '#D43B52'
const button_color_green = '#1D805F'
async function init() {
console.log({ Message: `Intializing Macro [${_main_macro_name()}...]` })
await buildUI()
await StartSubscriptions()
console.log({ Message: `Macro [${_main_macro_name()}] initialization Complete!` })
}
//Iterates over the Subscribe Object
async function StartSubscriptions() {
const subs = Object.getOwnPropertyNames(Subscribe);
subs.sort();
let mySubscriptions = [];
subs.forEach(element => {
Subscribe[element]();
mySubscriptions.push(element);
Subscribe[element] = function () {
console.warn({ Warn: `The [${element}] subscription is already active, unable to fire it again` });
};
});
console.log({ Message: 'Subscriptions Set', Details: { Total_Subs: subs.length, Active_Subs: mySubscriptions.join(', ') } });
};
//Define all Event/Status subscriptions needed for the macro
const Subscribe = {
PanelClicked: function () { xapi.Event.UserInterface.Extensions.Panel.Clicked.on(handle.Event.PanelClicked) }
}
const handle = {
Event: {
PanelClicked: function (event) {
console.log(event.PanelId)
if (event.PanelId == 'toggle_mic_state') {
ToggleMic();
}
}
}
}
function delay(ms) { return new Promise(resolve => { setTimeout(resolve, ms) }) }
async function ToggleMic() {
console.log({ Message: `Toggle Mic ${MicId}` })
const current_value = await xapi.Config.Audio.Input.Microphone[1].Mode.get()
console.log({ Message: `Current Mic ${MicId} current value: ${current_value}` })
let toggle_value = ''
let tog_color = ''
if (current_value == 'On') { toggle_value = 'Off' } else { toggle_value = 'On' };
if (current_value == 'On') {
toggle_value = 'Off';
tog_color = button_color_red;
}
else {
toggle_value = 'On';
tog_color = button_color_green;
};
console.log({ Message: `Setting Mic ${MicId} to : ${toggle_value}` })
await xapi.Config.Audio.Input.Microphone[1].Mode.set(toggle_value);
await xapi.Command.UserInterface.Extensions.Panel.Update({ PanelId: 'toggle_mic_state', Name: `Mic${MicId} turn ${current_value}`, Color: tog_color });
}
async function buildUI() {
console.log({ Message: `Building UserInterface...` })
// delete any previous action buttons
let customPanelsList = (await xapi.Command.UserInterface.Extensions.List({ ActivityType: 'Custom' }))?.Extensions.Panel;
if (customPanelsList != undefined) {
customPanelsList.forEach(async panel => {
if (panel.PanelId.substring(0, 16) == "toggle_mic_state") {
await xapi.Command.UserInterface.Extensions.Panel.Remove({ PanelId: panel.PanelId });
console.log('removed previous panel: ', panel.PanelId)
}
})
}
// get current state of mic to toggle
const current_value = await xapi.Config.Audio.Input.Microphone[1].Mode.get()
console.log({ Message: `Current Mic ${MicId} current value: ${current_value}` })
let tog_state = ''
let tog_color = ''
if (current_value == 'On') {
tog_state = 'Off';
tog_color = button_color_green;
}
else {
tog_state = 'On';
tog_color = button_color_red;
};
//build action buttons
let actionButton_xml = ``
actionButton_xml = `<Extensions>
<Version>1.10</Version>
<Panel>
<Order>1</Order>
<PanelId>$toggle_mic_state</PanelId>
<Origin>local</Origin>
<Location>HomeScreenAndCallControls</Location>
<Icon>Microphone</Icon>
<Color>${tog_color}</Color>
<Name>Mic${MicId} turn ${tog_state}</Name>
<ActivityType>Custom</ActivityType>
</Panel>
</Extensions>`
await xapi.Command.UserInterface.Extensions.Panel.Save({ PanelId: 'toggle_mic_state' }, actionButton_xml)
console.log({ Message: `UserInterface Built!` })
}
init()