-
Notifications
You must be signed in to change notification settings - Fork 389
/
Browser.go
457 lines (435 loc) · 16.3 KB
/
Browser.go
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to 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. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package cbus
import (
"context"
"fmt"
"runtime/debug"
"sync"
"time"
plc4go "github.com/apache/plc4x/plc4go/pkg/api"
apiModel "github.com/apache/plc4x/plc4go/pkg/api/model"
"github.com/apache/plc4x/plc4go/pkg/api/values"
readWriteModel "github.com/apache/plc4x/plc4go/protocols/cbus/readwrite/model"
_default "github.com/apache/plc4x/plc4go/spi/default"
spiModel "github.com/apache/plc4x/plc4go/spi/model"
"github.com/apache/plc4x/plc4go/spi/options"
"github.com/pkg/errors"
"github.com/rs/zerolog"
)
type Browser struct {
_default.DefaultBrowser
connection plc4go.PlcConnection
sequenceCounter uint8
log zerolog.Logger
}
func NewBrowser(connection plc4go.PlcConnection, _options ...options.WithOption) *Browser {
customLogger := options.ExtractCustomLoggerOrDefaultToGlobal(_options...)
browser := &Browser{
connection: connection,
sequenceCounter: 0,
log: customLogger,
}
browser.DefaultBrowser = _default.NewDefaultBrowser(browser, _options...)
return browser
}
func (m *Browser) BrowseQuery(ctx context.Context, interceptor func(result apiModel.PlcBrowseItem) bool, queryName string, query apiModel.PlcQuery) (responseCode apiModel.PlcResponseCode, queryResults []apiModel.PlcBrowseItem) {
switch query := query.(type) {
case *unitInfoQuery:
return m.browseUnitInfo(ctx, interceptor, queryName, query)
default:
m.log.Warn().Type("query", query).Msg("unsupported query type supplied %T")
return apiModel.PlcResponseCode_INVALID_ADDRESS, nil
}
}
func (m *Browser) browseUnitInfo(ctx context.Context, interceptor func(result apiModel.PlcBrowseItem) bool, queryName string, query *unitInfoQuery) (responseCode apiModel.PlcResponseCode, queryResults []apiModel.PlcBrowseItem) {
m.log.Trace().Msg("extract units")
units, allUnits, err := m.extractUnits(ctx, query, m.getInstalledUnitAddressBytes)
if err != nil {
m.log.Error().Err(err).Msg("Error extracting units")
return apiModel.PlcResponseCode_INTERNAL_ERROR, nil
}
attributes, allAttributes := m.extractAttributes(query)
if allUnits {
m.log.Info().Msg("Querying all (available) units")
} else {
m.log.Debug().Interface("units", units).Msg("Querying units")
}
unitLoop:
for _, unit := range units {
unitLog := m.log.With().Stringer("unit", unit).Logger()
unitLog.Trace().Msg("checking unit")
if err := ctx.Err(); err != nil {
unitLog.Info().Err(err).Msg("Aborting scan at unit")
return apiModel.PlcResponseCode_INVALID_ADDRESS, nil
}
unitAddress := unit.GetAddress()
if !allUnits && allAttributes {
m.log.Info().
Uint8("unitAddress", unitAddress).
Msg("Querying all attributes of unit")
}
level := zerolog.InfoLevel
if allUnits {
level = zerolog.DebugLevel
}
m.log.WithLevel(level).Uint8("unitAddress", unitAddress).Msg("Query unit")
for _, attribute := range attributes {
if err := ctx.Err(); err != nil {
unitLog.Info().Err(err).Msg("Aborting scan at unit")
return apiModel.PlcResponseCode_INVALID_ADDRESS, nil
}
if !allUnits && !allAttributes {
m.log.Info().
Uint8("unitAddress", unitAddress).
Stringer("attribute", attribute).
Msg("Querying attribute of unit")
} else {
m.log.WithLevel(level).Uint8("unitAddress", unitAddress).
Stringer("attribute", attribute).
Msg("unit unitAddress: Query attribute")
}
m.log.Trace().Msg("Building request")
readTagName := fmt.Sprintf("%s/%d/%s", queryName, unitAddress, attribute)
readRequest, _ := m.connection.ReadRequestBuilder().
AddTag(readTagName, NewCALIdentifyTag(unit, nil /*TODO: add bridge support*/, attribute, 1)).
Build()
timeout := 5 * time.Second
timeoutCtx, timeoutCancel := context.WithTimeout(ctx, timeout)
m.log.Trace().
Stringer("readRequest", readRequest).
Dur("timeout", timeout).
Msg("Executing readRequest with timeout")
requestResult := <-readRequest.ExecuteWithContext(timeoutCtx)
m.log.Trace().Stringer("requestResult", requestResult).Msg("got a response")
timeoutCancel()
if err := requestResult.GetErr(); err != nil {
if allUnits || allAttributes {
level = zerolog.TraceLevel
}
m.log.WithLevel(level).Err(err).
Uint8("unitAddress", unitAddress).
Stringer("attribute", attribute).
Msg("unit unitAddress: Can't read attribute attribute")
continue unitLoop
}
response := requestResult.GetResponse()
if code := response.GetResponseCode(readTagName); code != apiModel.PlcResponseCode_OK {
m.log.WithLevel(level).
Uint8("unitAddress", unitAddress).
Stringer("attribute", attribute).
Stringer("code", code).
Msg("unit unitAddress: error reading tag attribute. Code %s")
continue unitLoop
}
queryResult := spiModel.NewDefaultPlcBrowseItem(
NewCALIdentifyTag(unit, nil /*TODO: add bridge support*/, attribute, 1),
queryName,
"",
true,
false,
false,
nil,
map[string]values.PlcValue{
"CurrentValue": response.GetValue(readTagName),
},
)
if interceptor != nil {
m.log.Trace().Msg("forwarding query result to interceptor")
interceptor(queryResult)
}
queryResults = append(queryResults, queryResult)
}
}
return apiModel.PlcResponseCode_OK, queryResults
}
func (m *Browser) extractUnits(ctx context.Context, query *unitInfoQuery, getInstalledUnitAddressBytes func(ctx context.Context) (map[byte]any, error)) ([]readWriteModel.UnitAddress, bool, error) {
if unitAddress := query.unitAddress; unitAddress != nil {
return []readWriteModel.UnitAddress{unitAddress}, false, nil
}
// TODO: check if we still want the option to brute force all addresses
installedUnitAddressBytes, err := getInstalledUnitAddressBytes(ctx)
if err != nil {
return nil, false, errors.New("Unable to get installed uints")
}
var units []readWriteModel.UnitAddress
for i := 0; i <= 0xFF; i++ {
unitAddressByte := byte(i)
if _, ok := installedUnitAddressBytes[unitAddressByte]; ok {
units = append(units, readWriteModel.NewUnitAddress(unitAddressByte))
}
}
return units, true, nil
}
func (m *Browser) extractAttributes(query *unitInfoQuery) ([]readWriteModel.Attribute, bool) {
if attribute := query.attribute; attribute != nil {
return []readWriteModel.Attribute{*attribute}, false
} else {
var attributes []readWriteModel.Attribute
for _, attribute := range readWriteModel.AttributeValues {
attributes = append(attributes, attribute)
}
return attributes, true
}
}
func (m *Browser) getInstalledUnitAddressBytes(ctx context.Context) (map[byte]any, error) {
start := time.Now()
defer func() {
m.log.Debug().TimeDiff("duration", time.Now(), start).Msg("Ending unit address acquiring after duration")
}()
// We need to pre-subscribe to catch the 2 followup responses
subscriptionRequest, err := m.connection.SubscriptionRequestBuilder().
AddEventTagAddress("installationMMIMonitor", "mmimonitor/*/NETWORK_CONTROL").
Build()
if err != nil {
return nil, errors.Wrap(err, "Error subscribing to the installation MMI")
}
subCtx, subCtxCancel := context.WithTimeout(ctx, 2*time.Second)
defer subCtxCancel()
subscriptionResult := <-subscriptionRequest.ExecuteWithContext(subCtx)
if err := subscriptionResult.GetErr(); err != nil {
return nil, errors.Wrap(err, "Error subscribing to the mmi")
}
response := subscriptionResult.GetResponse()
if responseCode := response.GetResponseCode("installationMMIMonitor"); responseCode != apiModel.PlcResponseCode_OK {
return nil, errors.Errorf("Got %s", responseCode)
}
subscriptionHandle, err := response.GetSubscriptionHandle("installationMMIMonitor")
if err != nil {
return nil, errors.Wrap(err, "Error getting the subscription handle")
}
build, err := m.connection.UnsubscriptionRequestBuilder().AddHandles(subscriptionHandle).Build()
if err != nil {
return nil, errors.Wrap(err, "Error building unsubscription request")
}
defer build.ExecuteWithContext(ctx)
blockOffset0Received := false
blockOffset0ReceivedChan := make(chan any, 100) // We only expect one, but we make it a bit bigger to no clog up
blockOffset88Received := false
blockOffset88ReceivedChan := make(chan any, 100) // We only expect one, but we make it a bit bigger to no clog up
blockOffset176Received := false
blockOffset176ReceivedChan := make(chan any, 100) // We only expect one, but we make it a bit bigger to no clog up
result := make(map[byte]any)
plcConsumerRegistration := subscriptionHandle.Register(func(event apiModel.PlcSubscriptionEvent) {
m.log.Trace().Stringer("event", event).Msg("handling event")
if responseCode := event.GetResponseCode("installationMMIMonitor"); responseCode != apiModel.PlcResponseCode_OK {
m.log.Warn().Stringer("event", event).Msg("Ignoring")
return
}
rootValue := event.GetValue("installationMMIMonitor")
if !rootValue.IsStruct() {
m.log.Warn().Stringer("rootValue", rootValue).Msg("Ignoring rootValue should be a struct")
return
}
rootStruct := rootValue.GetStruct()
if applicationValue := rootStruct["application"]; applicationValue == nil || !applicationValue.IsString() || applicationValue.GetString() != "NETWORK_CONTROL" {
m.log.Warn().
Interface("rootStruct", rootStruct).
Msg("Ignoring rootStruct should contain a application tag of type string with value NETWORK_CONTROL")
return
}
var blockStart int
if blockStartValue := rootStruct["blockStart"]; blockStartValue == nil || !blockStartValue.IsByte() {
m.log.Warn().
Interface("rootStruct", rootStruct).
Msg("Ignoring rootStruct should contain a blockStart tag of type byte")
return
} else {
blockStart = int(blockStartValue.GetByte())
}
if plcListValue := rootStruct["values"]; plcListValue == nil || !plcListValue.IsList() {
m.log.Warn().
Interface("rootStruct", rootStruct).
Msg("Ignoring rootStruct should contain a values tag of type list")
return
} else {
for unitByteAddress, plcValue := range plcListValue.GetList() {
unitByteAddress = blockStart + unitByteAddress
if !plcValue.IsString() {
m.log.Warn().
Stringer("plcValue", plcValue).
Int("unitByteAddress", unitByteAddress).
Msg("Ignoring plcValue at unitByteAddress should be a string")
return
}
switch plcValue.GetString() {
case readWriteModel.GAVState_ON.PLC4XEnumName(), readWriteModel.GAVState_OFF.PLC4XEnumName():
m.log.Debug().
Int("unitByteAddress", unitByteAddress).
Msg("unit does exists")
result[byte(unitByteAddress)] = true
case readWriteModel.GAVState_DOES_NOT_EXIST.PLC4XEnumName():
m.log.Debug().
Int("unitByteAddress", unitByteAddress).
Msg("unit does not exists")
case readWriteModel.GAVState_ERROR.PLC4XEnumName():
m.log.Warn().
Int("unitByteAddress", unitByteAddress).
Msg("unit is in error state")
}
}
}
// We notify here so we don't exit to early
switch blockStart {
case 0:
select {
case blockOffset0ReceivedChan <- true:
m.log.Trace().Msg("0 notified")
default:
m.log.Warn().Msg("0 blocked")
}
case 88:
select {
case blockOffset88ReceivedChan <- true:
m.log.Trace().Msg("88 notified")
default:
m.log.Warn().Msg("88 blocked")
}
case 176:
select {
case blockOffset176ReceivedChan <- true:
m.log.Trace().Msg("176 notified")
default:
m.log.Warn().Msg("176 blocked")
}
}
})
defer plcConsumerRegistration.Unregister()
readRequest, err := m.connection.ReadRequestBuilder().
AddTagAddress("installationMMI", "status/binary/0xFF").
Build()
if err != nil {
return nil, errors.Wrap(err, "Error building the installation MMI")
}
readCtx, readCtxCancel := context.WithTimeout(ctx, 2*time.Second)
defer readCtxCancel()
readWg := sync.WaitGroup{}
readWg.Add(1)
go func() {
defer readWg.Done()
defer func() {
if err := recover(); err != nil {
m.log.Error().
Str("stack", string(debug.Stack())).
Interface("err", err).
Msg("panic-ed")
}
}()
defer readCtxCancel()
m.log.Debug().Stringer("readRequest", readRequest).Msg("sending read request")
readRequestResult := <-readRequest.ExecuteWithContext(readCtx)
if err := readRequestResult.GetErr(); err != nil {
m.log.Warn().Err(err).Msg("Error reading the mmi")
return
}
response := readRequestResult.GetResponse()
if responseCode := response.GetResponseCode("installationMMI"); responseCode == apiModel.PlcResponseCode_OK {
rootValue := response.GetValue("installationMMI")
if !rootValue.IsStruct() {
m.log.Warn().Err(err).Stringer("rootValue", rootValue).Msg("%v should be a struct")
return
}
rootStruct := rootValue.GetStruct()
if applicationValue := rootStruct["application"]; applicationValue == nil || !applicationValue.IsString() || applicationValue.GetString() != "NETWORK_CONTROL" {
m.log.Warn().Err(err).
Interface("rootStruct", rootStruct).
Msg("%v should contain a application tag of type string with value NETWORK_CONTROL")
return
}
var blockStart int
if blockStartValue := rootStruct["blockStart"]; blockStartValue == nil || !blockStartValue.IsByte() || blockStartValue.GetByte() != 0 {
m.log.Warn().Err(err).
Interface("rootStruct", rootStruct).
Msg("rootStruct should contain a blockStart tag of type byte with value 0")
return
} else {
blockStart = int(blockStartValue.GetByte())
}
m.log.Debug().Int("blockStart", blockStart).Msg("Read MMI with block start")
if plcListValue := rootStruct["values"]; plcListValue == nil || !plcListValue.IsList() {
m.log.Warn().Err(err).
Interface("rootStruct", rootStruct).
Msg("rootStruct should contain a values tag of type list")
return
} else {
for unitByteAddress, plcValue := range plcListValue.GetList() {
unitByteAddress = blockStart + unitByteAddress
if !plcValue.IsString() {
m.log.Warn().Err(err).
Stringer("plcValue", plcValue).
Int("unitByteAddress", unitByteAddress).
Msg("plcValue at unitByteAddress should be a string")
return
}
switch plcValue.GetString() {
case readWriteModel.GAVState_ON.PLC4XEnumName(), readWriteModel.GAVState_OFF.PLC4XEnumName():
m.log.Debug().
Int("unitByteAddress", unitByteAddress).
Msg("unit does exists")
result[byte(unitByteAddress)] = true
case readWriteModel.GAVState_DOES_NOT_EXIST.PLC4XEnumName():
m.log.Debug().
Int("unitByteAddress", unitByteAddress).
Msg("unit does not exists")
case readWriteModel.GAVState_ERROR.PLC4XEnumName():
m.log.Warn().
Int("unitByteAddress", unitByteAddress).
Msg("unit is in error state")
}
}
}
switch blockStart {
case 0:
blockOffset0Received = true
m.log.Trace().Msg("block 0 read by read")
case 88:
blockOffset88Received = true
m.log.Trace().Msg("block 88 read by read")
case 176:
blockOffset176Received = true
m.log.Trace().Msg("block 176 read by read")
}
} else {
m.log.Warn().
Stringer("responseCode", responseCode).
Msg("We got responseCode as response code for installation mmi so we rely on getting it via subscription")
}
}()
syncCtx, syncCtxCancel := context.WithTimeout(ctx, 6*time.Second)
defer syncCtxCancel()
for !blockOffset0Received || !blockOffset88Received || !blockOffset176Received {
select {
case <-blockOffset0ReceivedChan:
m.log.Trace().Msg("Offset 0 received")
blockOffset0Received = true
case <-blockOffset88ReceivedChan:
m.log.Trace().Msg("Offset 88 received")
blockOffset88Received = true
case <-blockOffset176ReceivedChan:
m.log.Trace().Msg("Offset 176 received")
blockOffset176Received = true
case <-syncCtx.Done():
err = syncCtx.Err()
m.log.Trace().Err(err).Msg("Ending prematurely")
return nil, errors.Wrap(err, "error waiting for other offsets")
}
}
readWg.Wait()
return result, nil
}