-
Notifications
You must be signed in to change notification settings - Fork 6
Load and save controller configuration
You can use the ConfigurationOptimizer and the Customizer to load configuration values from a controller or save change configuration values back to it.
You can find the following steps combined in the "customizer" example application which can be found here:
-
Discover LAN-enabled devices found a
DataSourceto open a connection to -
Create and establish a connection returned a
Connectionto receive and transmit VBus data over
Since the VBus is a single master bus and the master role is provided by the controller you cannot simply start sending VBus commands. Instead you have to wait for the master to offer the bus timing control to you. You can use the Connection#waitForFreeBus() method for that. The method take a timeout parameter in milliseconds. If no bus offer is received before the timeout, the method returns null. Otherwise it returns the Datagram containing the bus offer.
Connection connection = ...; // see Prerequisites section
Datagram dgram = connection.waitForFreeBus(20000);
The remaining code assumes, that the method returned a non-null result.
You can get the VBus address of the controller that offered the bus:
int deviceAddress = dgram.getSourceAddress();
If you want to configure a special controller only you can check whether the right one was detected.
The next step is optional. You can query the ConfigurationOptimizerFactory whether is knows a ConfigurationOptimizer that matches the controller's device address you just received.
ConfigurationOptimizer optimizer = ConfigurationOptimizerFactory.matchOptimizer(deviceAddress, null, null);
The function returns a ConfigurationOptimizer if possible and null otherwise. ConfigurationOptimizer instances offer multiple benefits:
- they know all values supported by their controller
- they allow you to specify values by their ID (a internal name) instead of their numeric index
- they allow you to only transfer the minimum set of values for load and save operations because it known which values are not needed for your configuration
- they allow you to generate a list of values to set the date / time of the controller correctly.
Not all controllers have corresponding ConfigurationOptimizer classes yet so matchOptimizer might return null for those controllers.
The next step is to create a ConnectionCustomizer. Customizer subclasses allow you to load and save configuration from and to controllers. The ConnectionCustomizer is a specialized version that uses a Connection to perform its task.
ConnectionCustomizer customizer = new ConnectionCustomizer(deviceAddress, optimizer, connection);
If a ConfigurationOptimizer was found above, the following line loads all values from the controller that are relevant for its current configuration:
ConfigurationValue[] values = customizer.loadConfiguration(null, true);
The first argument is an array of ConfigurationValue instances. It may be null only if a ConfigurationOptimizer was given that can be used to create a complete set of values. Otherwise you have to provide that array yourself.
The Customizer#loadConfiguration method returns an array of ConfigurationValue instances that were received from the controller.
To save this configuration back into the controller the Customizer#saveConfiguration can be used:
customizer.saveConfiguration(values, null, true);
To set the clock to the correct date / time you can use a combination of:
long now = System.currentTimeMillis();
TimeZone timeZone = TimeZone.getDefault();
values = optimizer.generateClockConfiguration(now, timeZone);
customizer.saveConfiguration(values, null, false);