I’m writing the migration code for the 8.3 version of my module. I’m using this migration strategy code:
@Override
public List<IdbMigrationStrategy> getRecordMigrationStrategies() {
return List.of(new SingletonRecordMigrationStrategy(
SeeqSettingsRecord.META,
SeeqSettingsRecord.Id,
IgnitionModuleConfigurationV2.RESOURCE_TYPE,
DefaultRecordEncodingDelegate.newBuilder().build()
));
}
In the Ignition 8.3.0 SDK Migration Plan PDF I got from support, it says this:
I’m trying to figure out how these “ignored” fields work. Specifically, I need to access the Enabled field programmatically. I see that it’s stored on disk in resource.json
, but where can I get at the enabled value from the code, and how can I write to it programmatically?
If you have access to a raw Resource
instance (probably as part of a DecodedResource<?>
) then the easy, supported pattern is to use com.inductiveautomation.ignition.common.util.ResourceUtil#isEnabled
- note that this method is defined to "fail true" - the absence of an enabled key or a malformed enabled key will return true, not false.
But essentially all the method is doing is asking the Resource
object for its defined attribute with the com.inductiveautomation.ignition.common.util.ResourceUtil#ENABLED_KEY
.
Setting enabled status is the inverse operation - push a new modification operation that has the resource's enabled attribute set to the state you want. Exactly how to best do that depends on what level of abstraction you're working at, which will vary depending on whether you're creating an extension point, using our new SingletonResourceHandler
/NamedResourceHandler
, etc.
OK that helps Paul, I am indeed using SingletonResourceHandler
, so here’s what my read code looks like based on your input:
IgnitionModuleConfigurationV2 settings = this.settingsHandler.getResource();
Optional<Resource> settingsResource =
this.context.getConfigurationManager()
.getSingletonResource(IgnitionModuleConfigurationV2.RESOURCE_TYPE);
boolean enabled = true;
if (settingsResource.isPresent()) {
enabled = ResourceUtil.isEnabled(settingsResource.get());
}
I’ve confirmed this works. I can manually change the attributes.enabled
property in my resource’s resource.json
file and see that it is reflected at runtime.
I need help on the inverse. I’m guessing maybe there’s some pattern involving ConfigurationManager#push(PushOperation) but I’m not seeing anything in the forum, docs or in the SDK examples. Can you help me with that?
Here's the code used by the system.device.setDeviceEnabled
scripting function, I think it should demonstrate what you need:
Resource resource = context.getConfigurationManager()
.getResource(new ResourcePath(DeviceExtensionPoint.DEVICE_RESOURCE_TYPE, deviceName))
.orElseThrow(() -> new Exception("Device '" + deviceName + "' doesn't exist"));
Resource modifiedResource = resource.toBuilder()
.setResourceCollectionName(resource.getDefiningCollectionName())
.putAttribute("enabled", enabled)
.build();
ChangeOperation modifyOp = ChangeOperation.newModifyOp(
LastModification.update(modifiedResource, "unknown"),
ResourceUtil.getTrueSignature(resource)
);
context.getConfigurationManager().push(List.of(modifyOp));
Thanks Kevin, this worked.
Here’s my final set of helper functions:
public static boolean isEnabled(GatewayContext context) {
Optional<Resource> settingsResource =
context.getConfigurationManager()
.getSingletonResource(IgnitionModuleConfigurationV2.RESOURCE_TYPE);
return settingsResource.map(ResourceUtil::isEnabled).orElse(true);
}
public static void setEnabled(GatewayContext context, boolean enabled) {
Resource resource = context.getConfigurationManager()
.getSingletonResource(IgnitionModuleConfigurationV2.RESOURCE_TYPE)
.orElseThrow(() -> new RuntimeException("Settings resource not found"));
Resource modifiedResource = resource.toBuilder()
.setResourceCollectionName(resource.getDefiningCollectionName())
.putAttribute("enabled", enabled)
.build();
ChangeOperation modifyOp = ChangeOperation.newModifyOp(
LastModification.update(modifiedResource, "Seeq Settings UI"),
ResourceUtil.getTrueSignature(resource)
);
try {
context.getConfigurationManager().push(List.of(modifyOp));
} catch (PushException e) {
throw new RuntimeException(e);
}
}