@@ -2,10 +2,23 @@ import { CoreBase } from '../Core/Base.js'
2
2
import { parseColorToNumber } from '../Resources/Util.js'
3
3
import { formatLocation } from '@companion-app/shared/ControlId.js'
4
4
import { RegexRouter } from './RegexRouter.js'
5
+ import { Bank , Element , Location , Page , VariableName } from './RoutePatterns.js'
5
6
import type { ControlLocation } from '@companion-app/shared/Model/Common.js'
6
7
import type { Registry } from '../Registry.js'
7
8
import type { UserConfigModel } from '@companion-app/shared/Model/UserConfigModel.js'
8
9
10
+ const SurfaceId = `(?<surfaceId>${ Element } )`
11
+ const Step = `(?<step>${ Element } )`
12
+
13
+ const MatchAnyCharactersIfSpacePrecedes = '(?<= ).*'
14
+ const MatchZeroCharactersIfNoSpacePrecedes = '(?<! )'
15
+ const TextRequiringPrecedingSpace =
16
+ `(?<text>${ MatchAnyCharactersIfSpacePrecedes } |${ MatchZeroCharactersIfNoSpacePrecedes } )`
17
+
18
+ const VariableValue = `(?<value>.*)`
19
+
20
+ const Color = `(?<color>${ Element } )`
21
+
9
22
/**
10
23
* Common API command processing for {@link ServiceTcp} and {@link ServiceUdp}.
11
24
*
@@ -71,7 +84,7 @@ export class ServiceTcpUdpApi extends CoreBase {
71
84
}
72
85
73
86
#setupLegacyRoutes( ) {
74
- this . #router. addPath ( ' page-set :page :surfaceId' , ( match ) => {
87
+ this . #router. addRoute ( ` page-set ${ Page } ${ SurfaceId } ` , ( match ) => {
75
88
this . #checkLegacyRouteAllowed( )
76
89
77
90
const page = Number ( match . page )
@@ -85,7 +98,7 @@ export class ServiceTcpUdpApi extends CoreBase {
85
98
return `If ${ surfaceId } is connected`
86
99
} )
87
100
88
- this . #router. addPath ( ' page-up :surfaceId' , ( match ) => {
101
+ this . #router. addRoute ( ` page-up ${ SurfaceId } ` , ( match ) => {
89
102
this . #checkLegacyRouteAllowed( )
90
103
91
104
const surfaceId = match . surfaceId
@@ -95,7 +108,7 @@ export class ServiceTcpUdpApi extends CoreBase {
95
108
return `If ${ surfaceId } is connected`
96
109
} )
97
110
98
- this . #router. addPath ( ' page-down :surfaceId' , ( match ) => {
111
+ this . #router. addRoute ( ` page-down ${ SurfaceId } ` , ( match ) => {
99
112
this . #checkLegacyRouteAllowed( )
100
113
101
114
const surfaceId = match . surfaceId
@@ -105,7 +118,7 @@ export class ServiceTcpUdpApi extends CoreBase {
105
118
return `If ${ surfaceId } is connected`
106
119
} )
107
120
108
- this . #router. addPath ( ' bank-press :page :bank' , ( match ) => {
121
+ this . #router. addRoute ( ` bank-press ${ Page } ${ Bank } ` , ( match ) => {
109
122
this . #checkLegacyRouteAllowed( )
110
123
111
124
const controlId = this . page . getControlIdAtOldBankIndex ( Number ( match . page ) , Number ( match . bank ) )
@@ -123,7 +136,7 @@ export class ServiceTcpUdpApi extends CoreBase {
123
136
} , 20 )
124
137
} )
125
138
126
- this . #router. addPath ( ' bank-down :page :bank' , ( match ) => {
139
+ this . #router. addRoute ( ` bank-down ${ Page } ${ Bank } ` , ( match ) => {
127
140
this . #checkLegacyRouteAllowed( )
128
141
129
142
const controlId = this . page . getControlIdAtOldBankIndex ( Number ( match . page ) , Number ( match . bank ) )
@@ -136,7 +149,7 @@ export class ServiceTcpUdpApi extends CoreBase {
136
149
}
137
150
} )
138
151
139
- this . #router. addPath ( ' bank-up :page :bank' , ( match ) => {
152
+ this . #router. addRoute ( ` bank-up ${ Page } ${ Bank } ` , ( match ) => {
140
153
this . #checkLegacyRouteAllowed( )
141
154
142
155
const controlId = this . page . getControlIdAtOldBankIndex ( Number ( match . page ) , Number ( match . bank ) )
@@ -149,7 +162,7 @@ export class ServiceTcpUdpApi extends CoreBase {
149
162
}
150
163
} )
151
164
152
- this . #router. addPath ( ' bank-step :page :bank :step' , ( match ) => {
165
+ this . #router. addRoute ( ` bank-step ${ Page } ${ Bank } ${ Step } ` , ( match ) => {
153
166
this . #checkLegacyRouteAllowed( )
154
167
155
168
const controlId = this . page . getControlIdAtOldBankIndex ( Number ( match . page ) , Number ( match . bank ) )
@@ -167,7 +180,7 @@ export class ServiceTcpUdpApi extends CoreBase {
167
180
if ( ! control . stepMakeCurrent ( step ) ) throw new ApiMessageError ( 'Step out of range' )
168
181
} )
169
182
170
- this . #router. addPath ( ' style bank :page :bank text{ * text}' , ( match ) => {
183
+ this . #router. addRoute ( ` style bank ${ Page } ${ Bank } text ? ${ TextRequiringPrecedingSpace } ` , ( match ) => {
171
184
this . #checkLegacyRouteAllowed( )
172
185
173
186
const controlId = this . page . getControlIdAtOldBankIndex ( Number ( match . page ) , Number ( match . bank ) )
@@ -184,7 +197,7 @@ export class ServiceTcpUdpApi extends CoreBase {
184
197
}
185
198
} )
186
199
187
- this . #router. addPath ( ' style bank :page :bank bgcolor #:color' , ( match ) => {
200
+ this . #router. addRoute ( ` style bank ${ Page } ${ Bank } bgcolor #${ Color } ` , ( match ) => {
188
201
this . #checkLegacyRouteAllowed( )
189
202
190
203
const controlId = this . page . getControlIdAtOldBankIndex ( Number ( match . page ) , Number ( match . bank ) )
@@ -202,7 +215,7 @@ export class ServiceTcpUdpApi extends CoreBase {
202
215
}
203
216
} )
204
217
205
- this . #router. addPath ( ' style bank :page :bank color #:color' , ( match ) => {
218
+ this . #router. addRoute ( ` style bank ${ Page } ${ Bank } color #${ Color } ` , ( match ) => {
206
219
this . #checkLegacyRouteAllowed( )
207
220
208
221
const controlId = this . page . getControlIdAtOldBankIndex ( Number ( match . page ) , Number ( match . bank ) )
@@ -220,7 +233,7 @@ export class ServiceTcpUdpApi extends CoreBase {
220
233
}
221
234
} )
222
235
223
- this . #router. addPath ( 'rescan' , async ( ) => {
236
+ this . #router. addRoute ( 'rescan' , async ( ) => {
224
237
this . #checkLegacyRouteAllowed( )
225
238
226
239
this . logger . debug ( 'Rescanning USB' )
@@ -235,27 +248,27 @@ export class ServiceTcpUdpApi extends CoreBase {
235
248
236
249
#setupNewRoutes( ) {
237
250
// surface pages
238
- this . #router. addPath ( ' surface :surfaceId page-set :page' , this . #surfaceSetPage)
239
- this . #router. addPath ( ' surface :surfaceId page-up' , this . #surfacePageUp)
240
- this . #router. addPath ( ' surface :surfaceId page-down' , this . #surfacePageDown)
251
+ this . #router. addRoute ( ` surface ${ SurfaceId } page-set ${ Page } ` , this . #surfaceSetPage)
252
+ this . #router. addRoute ( ` surface ${ SurfaceId } page-up` , this . #surfacePageUp)
253
+ this . #router. addRoute ( ` surface ${ SurfaceId } page-down` , this . #surfacePageDown)
241
254
242
255
// control by location
243
- this . #router. addPath ( ' location :page/:row/:column press' , this . #locationPress)
244
- this . #router. addPath ( ' location :page/:row/:column down' , this . #locationDown)
245
- this . #router. addPath ( ' location :page/:row/:column up' , this . #locationUp)
246
- this . #router. addPath ( ' location :page/:row/:column rotate-left' , this . #locationRotateLeft)
247
- this . #router. addPath ( ' location :page/:row/:column rotate-right' , this . #locationRotateRight)
248
- this . #router. addPath ( ' location :page/:row/:column set-step :step' , this . #locationSetStep)
256
+ this . #router. addRoute ( ` location ${ Location } press` , this . #locationPress)
257
+ this . #router. addRoute ( ` location ${ Location } down` , this . #locationDown)
258
+ this . #router. addRoute ( ` location ${ Location } up` , this . #locationUp)
259
+ this . #router. addRoute ( ` location ${ Location } rotate-left` , this . #locationRotateLeft)
260
+ this . #router. addRoute ( ` location ${ Location } rotate-right` , this . #locationRotateRight)
261
+ this . #router. addRoute ( ` location ${ Location } set-step ${ Step } ` , this . #locationSetStep)
249
262
250
- this . #router. addPath ( ' location :page/:row/:column style text{ *text}' , this . #locationStyleText)
251
- this . #router. addPath ( ' location :page/:row/:column style color :color' , this . #locationStyleColor)
252
- this . #router. addPath ( ' location :page/:row/:column style bgcolor :bgcolor' , this . #locationStyleBgcolor)
263
+ this . #router. addRoute ( ` location ${ Location } style text ? ${ TextRequiringPrecedingSpace } ` , this . #locationStyleText)
264
+ this . #router. addRoute ( ` location ${ Location } style color ${ Color } ` , this . #locationStyleColor)
265
+ this . #router. addRoute ( ` location ${ Location } style bgcolor ${ Color } ` , this . #locationStyleBgcolor)
253
266
254
267
// surfaces
255
- this . #router. addPath ( 'surfaces rescan' , this . #surfacesRescan)
268
+ this . #router. addRoute ( 'surfaces rescan' , this . #surfacesRescan)
256
269
257
270
// custom variables
258
- this . #router. addPath ( ' custom-variable :name set-value {*value}' , this . #customVariableSetValue)
271
+ this . #router. addRoute ( ` custom-variable ${ VariableName } set-value ${ VariableValue } ` , this . #customVariableSetValue)
259
272
}
260
273
261
274
/**
@@ -440,7 +453,7 @@ export class ServiceTcpUdpApi extends CoreBase {
440
453
441
454
const control = this . controls . getControl ( controlId )
442
455
if ( control && control . supportsStyle ) {
443
- const color = parseColorToNumber ( match . bgcolor )
456
+ const color = parseColorToNumber ( match . color )
444
457
445
458
control . styleSetFields ( { bgcolor : color } )
446
459
} else {
0 commit comments