|
|
@ -85,6 +85,17 @@ import java.util.UUID; |
|
|
@Slf4j |
|
|
@Slf4j |
|
|
public class DefaultIotHubService implements IotHubService { |
|
|
public class DefaultIotHubService implements IotHubService { |
|
|
|
|
|
|
|
|
|
|
|
private static final String ACTION_INSTALL = "install"; |
|
|
|
|
|
private static final String ACTION_UPDATE = "update"; |
|
|
|
|
|
private static final String SECTION_PACKAGE_DATA = "package data"; |
|
|
|
|
|
private static final String SECTION_RULE_CHAIN_METADATA = "rule chain metadata"; |
|
|
|
|
|
|
|
|
|
|
|
private static final String ITEM_TYPE_WIDGET = "widget"; |
|
|
|
|
|
private static final String ITEM_TYPE_DASHBOARD = "dashboard"; |
|
|
|
|
|
private static final String ITEM_TYPE_CALCULATED_FIELD = "calculated field"; |
|
|
|
|
|
private static final String ITEM_TYPE_RULE_CHAIN = "rule chain"; |
|
|
|
|
|
private static final String ITEM_TYPE_DEVICE_PROFILE = "device profile"; |
|
|
|
|
|
|
|
|
private final IotHubRestClient iotHubRestClient; |
|
|
private final IotHubRestClient iotHubRestClient; |
|
|
private final TbWidgetTypeService tbWidgetTypeService; |
|
|
private final TbWidgetTypeService tbWidgetTypeService; |
|
|
private final TbDashboardService tbDashboardService; |
|
|
private final TbDashboardService tbDashboardService; |
|
|
@ -161,7 +172,7 @@ public class DefaultIotHubService implements IotHubService { |
|
|
try { |
|
|
try { |
|
|
widgetTypeDetails = JacksonUtil.fromString(new String(fileData), WidgetTypeDetails.class, true); |
|
|
widgetTypeDetails = JacksonUtil.fromString(new String(fileData), WidgetTypeDetails.class, true); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
throw new Exception("Failed to parse widget data: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); |
|
|
throw parseFailure(ACTION_INSTALL, ITEM_TYPE_WIDGET, e); |
|
|
} |
|
|
} |
|
|
widgetTypeDetails.setId(null); |
|
|
widgetTypeDetails.setId(null); |
|
|
widgetTypeDetails.setTenantId(tenantId); |
|
|
widgetTypeDetails.setTenantId(tenantId); |
|
|
@ -177,7 +188,7 @@ public class DefaultIotHubService implements IotHubService { |
|
|
try { |
|
|
try { |
|
|
dashboard = JacksonUtil.fromString(new String(fileData), Dashboard.class, true); |
|
|
dashboard = JacksonUtil.fromString(new String(fileData), Dashboard.class, true); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
throw new Exception("Failed to parse dashboard data: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); |
|
|
throw parseFailure(ACTION_INSTALL, ITEM_TYPE_DASHBOARD, e); |
|
|
} |
|
|
} |
|
|
dashboard.setId(null); |
|
|
dashboard.setId(null); |
|
|
dashboard.setTenantId(tenantId); |
|
|
dashboard.setTenantId(tenantId); |
|
|
@ -193,7 +204,7 @@ public class DefaultIotHubService implements IotHubService { |
|
|
try { |
|
|
try { |
|
|
calculatedField = JacksonUtil.fromString(new String(fileData), CalculatedField.class, true); |
|
|
calculatedField = JacksonUtil.fromString(new String(fileData), CalculatedField.class, true); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
throw new Exception("Failed to parse calculated field data: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); |
|
|
throw parseFailure(ACTION_INSTALL, ITEM_TYPE_CALCULATED_FIELD, e); |
|
|
} |
|
|
} |
|
|
calculatedField.setId(null); |
|
|
calculatedField.setId(null); |
|
|
calculatedField.setTenantId(tenantId); |
|
|
calculatedField.setTenantId(tenantId); |
|
|
@ -241,14 +252,14 @@ public class DefaultIotHubService implements IotHubService { |
|
|
try { |
|
|
try { |
|
|
ruleChain = JacksonUtil.fromString(json.get("ruleChain").toString(), RuleChain.class, true); |
|
|
ruleChain = JacksonUtil.fromString(json.get("ruleChain").toString(), RuleChain.class, true); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
throw new Exception("Failed to parse rule chain: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); |
|
|
throw parseFailure(ACTION_INSTALL, ITEM_TYPE_RULE_CHAIN, e); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
RuleChainMetaData metadata; |
|
|
RuleChainMetaData metadata; |
|
|
try { |
|
|
try { |
|
|
metadata = JacksonUtil.fromString(json.get("metadata").toString(), RuleChainMetaData.class, true); |
|
|
metadata = JacksonUtil.fromString(json.get("metadata").toString(), RuleChainMetaData.class, true); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
throw new Exception("Failed to parse rule chain metadata: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); |
|
|
throw parseFailure(ACTION_INSTALL, ITEM_TYPE_RULE_CHAIN, SECTION_RULE_CHAIN_METADATA, e); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
ruleChain.setId(null); |
|
|
ruleChain.setId(null); |
|
|
@ -311,7 +322,7 @@ public class DefaultIotHubService implements IotHubService { |
|
|
try { |
|
|
try { |
|
|
deviceProfile = JacksonUtil.fromString(new String(fileData), DeviceProfile.class, true); |
|
|
deviceProfile = JacksonUtil.fromString(new String(fileData), DeviceProfile.class, true); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
throw new Exception("Failed to parse device profile data: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); |
|
|
throw parseFailure(ACTION_INSTALL, ITEM_TYPE_DEVICE_PROFILE, e); |
|
|
} |
|
|
} |
|
|
deviceProfile.setId(null); |
|
|
deviceProfile.setId(null); |
|
|
deviceProfile.setTenantId(tenantId); |
|
|
deviceProfile.setTenantId(tenantId); |
|
|
@ -436,7 +447,7 @@ public class DefaultIotHubService implements IotHubService { |
|
|
try { |
|
|
try { |
|
|
newWidgetType = JacksonUtil.fromString(new String(fileData), WidgetTypeDetails.class, true); |
|
|
newWidgetType = JacksonUtil.fromString(new String(fileData), WidgetTypeDetails.class, true); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
throw new Exception("Failed to parse widget data: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); |
|
|
throw parseFailure(ACTION_UPDATE, ITEM_TYPE_WIDGET, e); |
|
|
} |
|
|
} |
|
|
WidgetTypeDetails existing = widgetTypeService.findWidgetTypeDetailsById(tenantId, descriptor.getWidgetTypeId()); |
|
|
WidgetTypeDetails existing = widgetTypeService.findWidgetTypeDetailsById(tenantId, descriptor.getWidgetTypeId()); |
|
|
if (existing == null) { |
|
|
if (existing == null) { |
|
|
@ -452,7 +463,7 @@ public class DefaultIotHubService implements IotHubService { |
|
|
try { |
|
|
try { |
|
|
newDashboard = JacksonUtil.fromString(new String(fileData), Dashboard.class, true); |
|
|
newDashboard = JacksonUtil.fromString(new String(fileData), Dashboard.class, true); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
throw new Exception("Failed to parse dashboard data: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); |
|
|
throw parseFailure(ACTION_UPDATE, ITEM_TYPE_DASHBOARD, e); |
|
|
} |
|
|
} |
|
|
Dashboard existing = dashboardService.findDashboardById(tenantId, descriptor.getDashboardId()); |
|
|
Dashboard existing = dashboardService.findDashboardById(tenantId, descriptor.getDashboardId()); |
|
|
if (existing == null) { |
|
|
if (existing == null) { |
|
|
@ -468,7 +479,7 @@ public class DefaultIotHubService implements IotHubService { |
|
|
try { |
|
|
try { |
|
|
newCf = JacksonUtil.fromString(new String(fileData), CalculatedField.class, true); |
|
|
newCf = JacksonUtil.fromString(new String(fileData), CalculatedField.class, true); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
throw new Exception("Failed to parse calculated field data: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); |
|
|
throw parseFailure(ACTION_UPDATE, ITEM_TYPE_CALCULATED_FIELD, e); |
|
|
} |
|
|
} |
|
|
CalculatedField existing = calculatedFieldService.findById(tenantId, descriptor.getCalculatedFieldId()); |
|
|
CalculatedField existing = calculatedFieldService.findById(tenantId, descriptor.getCalculatedFieldId()); |
|
|
if (existing == null) { |
|
|
if (existing == null) { |
|
|
@ -506,7 +517,7 @@ public class DefaultIotHubService implements IotHubService { |
|
|
try { |
|
|
try { |
|
|
metadata = JacksonUtil.fromString(json.get("metadata").toString(), RuleChainMetaData.class, true); |
|
|
metadata = JacksonUtil.fromString(json.get("metadata").toString(), RuleChainMetaData.class, true); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
throw new Exception("Failed to parse rule chain metadata: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); |
|
|
throw parseFailure(ACTION_UPDATE, ITEM_TYPE_RULE_CHAIN, SECTION_RULE_CHAIN_METADATA, e); |
|
|
} |
|
|
} |
|
|
RuleChain existing = ruleChainService.findRuleChainById(tenantId, descriptor.getRuleChainId()); |
|
|
RuleChain existing = ruleChainService.findRuleChainById(tenantId, descriptor.getRuleChainId()); |
|
|
if (existing == null) { |
|
|
if (existing == null) { |
|
|
@ -516,7 +527,7 @@ public class DefaultIotHubService implements IotHubService { |
|
|
try { |
|
|
try { |
|
|
newRuleChain = JacksonUtil.fromString(json.get("ruleChain").toString(), RuleChain.class, true); |
|
|
newRuleChain = JacksonUtil.fromString(json.get("ruleChain").toString(), RuleChain.class, true); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
throw new Exception("Failed to parse rule chain: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); |
|
|
throw parseFailure(ACTION_UPDATE, ITEM_TYPE_RULE_CHAIN, e); |
|
|
} |
|
|
} |
|
|
existing.setName(newRuleChain.getName()); |
|
|
existing.setName(newRuleChain.getName()); |
|
|
RuleChain savedRuleChain = ruleChainService.saveRuleChain(existing); |
|
|
RuleChain savedRuleChain = ruleChainService.saveRuleChain(existing); |
|
|
@ -731,4 +742,13 @@ public class DefaultIotHubService implements IotHubService { |
|
|
iotHubInstalledItemService.deleteById(tenantId, installedItemId); |
|
|
iotHubInstalledItemService.deleteById(tenantId, installedItemId); |
|
|
log.info("[{}] Deleted installed IoT Hub item: {}", tenantId, installedItem.getItemName()); |
|
|
log.info("[{}] Deleted installed IoT Hub item: {}", tenantId, installedItem.getItemName()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static Exception parseFailure(String action, String itemTypeName, Exception cause) { |
|
|
|
|
|
return parseFailure(action, itemTypeName, SECTION_PACKAGE_DATA, cause); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static Exception parseFailure(String action, String itemTypeName, String section, Exception cause) { |
|
|
|
|
|
return new Exception("Unable to " + action + " this " + itemTypeName + " — the " + section + " could not be parsed. " + |
|
|
|
|
|
"The package may be corrupted or in an unexpected format; please contact the creator to have it re-published.", cause); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|