mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-25 11:45:49 +01:00
Upgrade tool: Add upgrade task for script profile changes (#4117)
* Upgrade tool: Add upgrade task for script profile changes Refs #4058. Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
This commit is contained in:
parent
3f27104e76
commit
45f8bff876
@ -15,7 +15,7 @@
|
|||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>openHAB Core :: Tools :: Upgrade tool</name>
|
<name>openHAB Core :: Tools :: Upgrade tool</name>
|
||||||
<description>A tool for upgrading openHAB from 3.4 to 4.0</description>
|
<description>A tool for upgrading openHAB</description>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -82,10 +82,15 @@ public class UpgradeTool {
|
|||||||
|| LINK_UPGRADE_JS_PROFILE.equals(commandLine.getOptionValue(OPT_COMMAND))) {
|
|| LINK_UPGRADE_JS_PROFILE.equals(commandLine.getOptionValue(OPT_COMMAND))) {
|
||||||
upgrader.linkUpgradeJsProfile();
|
upgrader.linkUpgradeJsProfile();
|
||||||
}
|
}
|
||||||
|
if (!commandLine.hasOption(OPT_COMMAND)
|
||||||
|
|| LINK_UPGRADE_SCRIPT_PROFILE.equals(commandLine.getOptionValue(OPT_COMMAND))) {
|
||||||
|
upgrader.linkUpgradeScriptProfile();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
HelpFormatter formatter = new HelpFormatter();
|
HelpFormatter formatter = new HelpFormatter();
|
||||||
String commands = Set.of(ITEM_COPY_UNIT_TO_METADATA, LINK_UPGRADE_JS_PROFILE).toString();
|
String commands = Set.of(ITEM_COPY_UNIT_TO_METADATA, LINK_UPGRADE_JS_PROFILE, LINK_UPGRADE_SCRIPT_PROFILE)
|
||||||
|
.toString();
|
||||||
formatter.printHelp("upgradetool", "", options, "Available commands: " + commands, true);
|
formatter.printHelp("upgradetool", "", options, "Available commands: " + commands, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,11 +42,13 @@ import org.slf4j.LoggerFactory;
|
|||||||
* The {@link Upgrader} contains the implementation of the upgrade methods
|
* The {@link Upgrader} contains the implementation of the upgrade methods
|
||||||
*
|
*
|
||||||
* @author Jan N. Klug - Initial contribution
|
* @author Jan N. Klug - Initial contribution
|
||||||
|
* @author Florian Hotze - Add script profile upgrade
|
||||||
*/
|
*/
|
||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
public class Upgrader {
|
public class Upgrader {
|
||||||
public static final String ITEM_COPY_UNIT_TO_METADATA = "itemCopyUnitToMetadata";
|
public static final String ITEM_COPY_UNIT_TO_METADATA = "itemCopyUnitToMetadata";
|
||||||
public static final String LINK_UPGRADE_JS_PROFILE = "linkUpgradeJsProfile";
|
public static final String LINK_UPGRADE_JS_PROFILE = "linkUpgradeJsProfile";
|
||||||
|
public static final String LINK_UPGRADE_SCRIPT_PROFILE = "linkUpgradeScriptProfile";
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(Upgrader.class);
|
private final Logger logger = LoggerFactory.getLogger(Upgrader.class);
|
||||||
private final String baseDir;
|
private final String baseDir;
|
||||||
@ -234,6 +236,49 @@ public class Upgrader {
|
|||||||
upgradeRecords.flush();
|
upgradeRecords.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upgrades the ItemChannelLink database for the separation of {@code toHandlerScript} into
|
||||||
|
* {@code commandFromItemScript} and {@code stateFromItemScript}.
|
||||||
|
* See <a href="https://github.com/openhab/openhab-core/pull/4058">openhab/openhab-core#4058</a>.
|
||||||
|
*/
|
||||||
|
public void linkUpgradeScriptProfile() {
|
||||||
|
if (!checkUpgradeRecord(LINK_UPGRADE_SCRIPT_PROFILE)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Path linkJsonDatabasePath = Path.of(baseDir, "jsondb", "org.openhab.core.thing.link.ItemChannelLink.json");
|
||||||
|
logger.info("Upgrading script profile configuration in database '{}'", linkJsonDatabasePath);
|
||||||
|
|
||||||
|
if (!Files.isWritable(linkJsonDatabasePath)) {
|
||||||
|
logger.error("Cannot access link database '{}', check path and access rights.", linkJsonDatabasePath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
JsonStorage<ItemChannelLink> linkStorage = new JsonStorage<>(linkJsonDatabasePath.toFile(), null, 5, 0, 0,
|
||||||
|
List.of());
|
||||||
|
|
||||||
|
List.copyOf(linkStorage.getKeys()).forEach(linkUid -> {
|
||||||
|
ItemChannelLink link = Objects.requireNonNull(linkStorage.get(linkUid));
|
||||||
|
Configuration configuration = link.getConfiguration();
|
||||||
|
String profileName = (String) configuration.get(ItemChannelLinkConfigDescriptionProvider.PARAM_PROFILE);
|
||||||
|
if (profileName.matches("^transform:*")) {
|
||||||
|
String toHandlerScript = (String) configuration.get("toHandlerScript");
|
||||||
|
if (toHandlerScript != null) {
|
||||||
|
configuration.put("commandFromItemScript", toHandlerScript);
|
||||||
|
configuration.remove("toHandlerScript");
|
||||||
|
|
||||||
|
linkStorage.put(linkUid, link);
|
||||||
|
logger.info("{}: rewrote script profile link to new format", linkUid);
|
||||||
|
} else {
|
||||||
|
logger.info("{}: link already has correct configuration", linkUid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
linkStorage.flush();
|
||||||
|
upgradeRecords.put(LINK_UPGRADE_SCRIPT_PROFILE, new UpgradeRecord(ZonedDateTime.now()));
|
||||||
|
upgradeRecords.flush();
|
||||||
|
}
|
||||||
|
|
||||||
private static class UpgradeRecord {
|
private static class UpgradeRecord {
|
||||||
public final String executionDate;
|
public final String executionDate;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user