4
4
5
5
import java .util .HashMap ;
6
6
import java .util .Map ;
7
- import java .util .Optional ;
8
7
import java .util .concurrent .atomic .AtomicReference ;
9
8
import java .util .function .Function ;
10
9
17
16
import io .openems .common .types .OpenemsType ;
18
17
import io .openems .edge .bridge .modbus .api .element .AbstractModbusElement ;
19
18
import io .openems .edge .bridge .modbus .api .element .BitsWordElement ;
20
- import io .openems .edge .bridge .modbus .api .element .ModbusCoilElement ;
19
+ import io .openems .edge .bridge .modbus .api .element .CoilElement ;
20
+ import io .openems .edge .bridge .modbus .api .element .ModbusElement ;
21
21
import io .openems .edge .bridge .modbus .api .element .ModbusRegisterElement ;
22
22
import io .openems .edge .bridge .modbus .api .task .ReadTask ;
23
23
import io .openems .edge .bridge .modbus .api .task .WriteTask ;
@@ -260,7 +260,7 @@ public void retryModbusCommunication() {
260
260
* Maps an Element to one or more ModbusChannels using converters, that convert
261
261
* the value forward and backwards.
262
262
*/
263
- public class ChannelMapper <ELEMENT extends AbstractModbusElement <?, ?> > {
263
+ public class ChannelMapper <ELEMENT extends ModbusElement > {
264
264
265
265
private final ELEMENT element ;
266
266
private final Map <Channel <?>, ElementToChannelConverter > channelMaps = new HashMap <>();
@@ -278,7 +278,7 @@ public ChannelMapper(ELEMENT element) {
278
278
*/
279
279
public ChannelMapper <ELEMENT > m (io .openems .edge .common .channel .ChannelId channelId ,
280
280
ElementToChannelConverter converter ) {
281
- return this .m (channelId , converter , new ChannelMetaInfo (this .element .getStartAddress () ));
281
+ return this .m (channelId , converter , new ChannelMetaInfo (this .element .startAddress ));
282
282
}
283
283
284
284
/**
@@ -324,7 +324,8 @@ public ELEMENT build() {
324
324
/*
325
325
* Forward Element Read-Value to Channel
326
326
*/
327
- this .element .onUpdateCallback (value -> { //
327
+ // This is guaranteed to work because of sealed abstract classes
328
+ ((AbstractModbusElement <?, ?, ?>) this .element ).onUpdateCallback (value -> { //
328
329
/*
329
330
* Applies the updated value on every Channel in ChannelMaps using the given
330
331
* Converter. If the converter returns an Optional.empty, the value is ignored.
@@ -349,13 +350,13 @@ public ELEMENT build() {
349
350
// dynamically get the Converter; this allows the converter to be changed
350
351
var converter = this .channelMaps .get (channel );
351
352
var convertedValue = converter .channelToElement (value );
352
- if (this .element instanceof ModbusRegisterElement <?> registerElement ) {
353
+ if (this .element instanceof ModbusRegisterElement <?, ? > registerElement ) {
353
354
try {
354
- registerElement .setNextWriteValue ( Optional . ofNullable ( convertedValue ) );
355
- } catch (OpenemsException | IllegalArgumentException e ) {
355
+ registerElement .setNextWriteValueFromObject ( convertedValue );
356
+ } catch (IllegalArgumentException e ) {
356
357
AbstractOpenemsModbusComponent .this .logWarn (AbstractOpenemsModbusComponent .this .log ,
357
358
"Unable to write to ModbusRegisterElement. " //
358
- + "Address [" + this .element .getStartAddress () + "] " //
359
+ + "Address [" + this .element .startAddress + "] " //
359
360
+ "Channel [" + channel .address () + "]. " //
360
361
+ "Exception [" + e .getClass ().getSimpleName () + "] " //
361
362
+ ": " + e .getMessage ());
@@ -364,19 +365,20 @@ public ELEMENT build() {
364
365
e .printStackTrace ();
365
366
}
366
367
}
367
- } else if (this .element instanceof ModbusCoilElement coilElement ) {
368
+
369
+ } else if (this .element instanceof CoilElement coilElement ) {
368
370
try {
369
- coilElement .setNextWriteValue (
370
- Optional .ofNullable (TypeUtils .getAsType (OpenemsType .BOOLEAN , convertedValue )));
371
- } catch (OpenemsException e ) {
371
+ coilElement .setNextWriteValue (TypeUtils .getAsType (OpenemsType .BOOLEAN , convertedValue ));
372
+ } catch (IllegalArgumentException e ) {
372
373
AbstractOpenemsModbusComponent .this .logWarn (AbstractOpenemsModbusComponent .this .log ,
373
374
"Unable to write to ModbusCoilElement " //
374
- + "[" + this .element .getStartAddress () + "]: " + e .getMessage ());
375
+ + "[" + this .element .startAddress + "]: " + e .getMessage ());
375
376
}
377
+
376
378
} else {
377
379
AbstractOpenemsModbusComponent .this .logWarn (AbstractOpenemsModbusComponent .this .log ,
378
380
"Unable to write to Element " //
379
- + "[" + this .element .getStartAddress () + "]: it is not a ModbusElement" );
381
+ + "[" + this .element .startAddress + "]: it is not a ModbusElement" );
380
382
}
381
383
});
382
384
}
@@ -390,11 +392,11 @@ public ELEMENT build() {
390
392
* Creates a ChannelMapper that can be used with builder pattern inside the
391
393
* protocol definition.
392
394
*
393
- * @param <T> the type of the {@link AbstractModbusElement}d
395
+ * @param <T> the type of the {@link ModbusElement}
394
396
* @param element the ModbusElement
395
397
* @return a {@link ChannelMapper}
396
398
*/
397
- protected final <T extends AbstractModbusElement <?, ?> > ChannelMapper <T > m (T element ) {
399
+ protected final <T extends ModbusElement > ChannelMapper <T > m (T element ) {
398
400
return new ChannelMapper <>(element );
399
401
}
400
402
@@ -411,43 +413,42 @@ protected final BitsWordElement m(BitsWordElement bitsWordElement) {
411
413
/**
412
414
* Maps the given element 1-to-1 to the Channel identified by channelId.
413
415
*
414
- * @param <T> the type of the {@link AbstractModbusElement}d
416
+ * @param <T> the type of the {@link ModbusElement}
415
417
* @param channelId the Channel-ID
416
418
* @param element the ModbusElement
417
419
* @return the element parameter
418
420
*/
419
- protected final <T extends AbstractModbusElement <?, ?>> T m (io .openems .edge .common .channel .ChannelId channelId ,
420
- T element ) {
421
+ protected final <T extends ModbusElement > T m (io .openems .edge .common .channel .ChannelId channelId , T element ) {
421
422
return this .m (channelId , element , DIRECT_1_TO_1 );
422
423
}
423
424
424
425
/**
425
426
* Maps the given element 1-to-1 to the Channel identified by channelId.
426
427
*
427
- * @param <T> the type of the {@link AbstractModbusElement}d
428
+ * @param <T> the type of the {@link ModbusElement}
428
429
* @param channelId the Channel-ID
429
430
* @param element the ModbusElement
430
431
* @param channelMetaInfo an object that holds meta information about the
431
432
* Channel
432
433
* @return the element parameter
433
434
*/
434
- protected final <T extends AbstractModbusElement <?, ?>> T m (io .openems .edge .common .channel .ChannelId channelId ,
435
- T element , ChannelMetaInfo channelMetaInfo ) {
435
+ protected final <T extends ModbusElement > T m (io .openems .edge .common .channel .ChannelId channelId , T element ,
436
+ ChannelMetaInfo channelMetaInfo ) {
436
437
return this .m (channelId , element , DIRECT_1_TO_1 , channelMetaInfo );
437
438
}
438
439
439
440
/**
440
441
* Maps the given element to the Channel identified by channelId, applying the
441
442
* given @link{ElementToChannelConverter}.
442
443
*
443
- * @param <T> the type of the {@link AbstractModbusElement}d
444
+ * @param <T> the type of the {@link ModbusElement}
444
445
* @param channelId the Channel-ID
445
446
* @param element the ModbusElement
446
447
* @param converter the ElementToChannelConverter
447
448
* @return the element parameter
448
449
*/
449
- protected final <T extends AbstractModbusElement <?, ?>> T m (io .openems .edge .common .channel .ChannelId channelId ,
450
- T element , ElementToChannelConverter converter ) {
450
+ protected final <T extends ModbusElement > T m (io .openems .edge .common .channel .ChannelId channelId , T element ,
451
+ ElementToChannelConverter converter ) {
451
452
return new ChannelMapper <>(element ) //
452
453
.m (channelId , converter ) //
453
454
.build ();
@@ -457,16 +458,16 @@ protected final BitsWordElement m(BitsWordElement bitsWordElement) {
457
458
* Maps the given element to the Channel identified by channelId, applying the
458
459
* given @link{ElementToChannelConverter}.
459
460
*
460
- * @param <T> the type of the {@link AbstractModbusElement}d
461
+ * @param <T> the type of the {@link ModbusElement}
461
462
* @param channelId the Channel-ID
462
463
* @param element the ModbusElement
463
464
* @param converter the ElementToChannelConverter
464
465
* @param channelMetaInfo an object that holds meta information about the
465
466
* Channel
466
467
* @return the element parameter
467
468
*/
468
- protected final <T extends AbstractModbusElement <?, ?>> T m (io .openems .edge .common .channel .ChannelId channelId ,
469
- T element , ElementToChannelConverter converter , ChannelMetaInfo channelMetaInfo ) {
469
+ protected final <T extends ModbusElement > T m (io .openems .edge .common .channel .ChannelId channelId , T element ,
470
+ ElementToChannelConverter converter , ChannelMetaInfo channelMetaInfo ) {
470
471
return new ChannelMapper <>(element ) //
471
472
.m (channelId , converter , channelMetaInfo ) //
472
473
.build ();
0 commit comments