Hello Dojo community !
I have been working on a DataMiner Automation script to update some tables based on JSON data using DIS. The deserialization logic is based on the example provided in the DataMiner documents website.
Here's a brief overview of my script:
- Reading JSON data from a file located at
C:\\Skyline DataMiner\\Documents\\DMA_COMMON_DOCUMENTS\\Power_Model\\DeviceUpdateSet.json
. - Deserializing the JSON data into objects. (How to handle XML and JSON data in QActions | DataMiner Docs- logic taken from here)
- Filling DataMiner tables with the deserialized data.
The same JSON data contains PowerSettings
which will be added to one table and PowerThreshold
which will go to another table (for each device ID) The parameters on these tables are write parameters.
The script runs successfully, but I do not see any changes in the tables. Below is a simplified version of my script:
Code:
namespace DeviceDefaultsMod
{
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Skyline.DataMiner.Automation;
using Skyline.DataMiner.Core.DataMinerSystem.Automation;
using Skyline.DataMiner.Core.DataMinerSystem.Common;
public class UpdateSet
{
public int DeviceId { get; set; }
public PowerSettings PowerSettings { get; set; }
public List<PowerThreshold> PowerThresholds { get; set; }
}public class PowerThreshold
{
public int Alpha { get; set; }
public int SinglePhaseLimit { get; set; }
public int MultiplePhasesLimit { get; set; }
}public class PowerSettings
{
public double InitialPower { get; set; }
public double DefaultIdlePowerConsumption { get; set; }
public double ActivePowerConsumption { get; set; }
public double LhcpPowerConsumption { get; set; }
public double RhcpPowerConsumption { get; set; }
public double DutyCycleConsumption { get; set; }
public double DutyCycleAsymmetry { get; set; }
}public class RootObject
{
public string Version { get; set; }
public List<UpdateSet> UpdateSets { get; set; }
}public class Script
{
public void Run(Engine engine)
{
try
{
// Path to the JSON file needs to be altered
string filePath = "C:\\Skyline DataMiner\\Documents\\DMA_COMMON_DOCUMENTS\\Power_Model\\DeviceUpdateSet.json";if (!File.Exists(filePath))
{
engine.GenerateInformation($"File not found: {filePath}");
return;
}string jsonData = File.ReadAllText(filePath);
if (string.IsNullOrWhiteSpace(jsonData))
{
engine.GenerateInformation($"File is empty: {filePath}");
return;
}RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(jsonData);
if (rootObject == null || rootObject.UpdateSets == null)
{
engine.GenerateInformation("Deserialized object is null or does not contain any update sets.");
return;
}List<object[]> powerSettingsInstances = new List<object[]>();
List<object[]> powerThresholdInstances = new List<object[]>();
List<int> indexesList = new List<int>();// Example: Collect indices from existing table rows (if needed)
// Assuming you have a method to get existing indices from the table
// indexesList.AddRange(existingIndices);// Extract indices from JSON and process data
foreach (UpdateSet updateSet in rootObject.UpdateSets)
{
indexesList.Add(updateSet.DeviceId);var powerSettings = updateSet.PowerSettings;
if (powerSettings != null)
{
var powerSettingsRow = new List<object> { updateSet.DeviceId };if (powerSettings.InitialPower != default)
{
powerSettingsRow.Add(powerSettings.InitialPower);
}if (powerSettings.DefaultIdlePowerConsumption != default)
{
powerSettingsRow.Add(powerSettings.DefaultIdlePowerConsumption);
}if (powerSettings.ActivePowerConsumption != default)
{
powerSettingsRow.Add(powerSettings.ActivePowerConsumption);
}if (powerSettings.LhcpPowerConsumption != default)
{
powerSettingsRow.Add(powerSettings.LhcpPowerConsumption);
}if (powerSettings.RhcpPowerConsumption != default)
{
powerSettingsRow.Add(powerSettings.RhcpPowerConsumption);
}if (powerSettings.DutyCycleConsumption != default)
{
powerSettingsRow.Add(powerSettings.DutyCycleConsumption);
}if (powerSettings.DutyCycleAsymmetry != default)
{
powerSettingsRow.Add(powerSettings.DutyCycleAsymmetry);
}powerSettingsInstances.Add(powerSettingsRow.ToArray());
}if (updateSet.PowerThresholds != null)
{
foreach (PowerThreshold threshold in updateSet.PowerThresholds)
{
var powerThresholdRow = new List<object> { updateSet.DeviceId };if (threshold.Alpha != default)
{
powerThresholdRow.Add(threshold.Alpha);
}if (threshold.SinglePhaseLimit != default)
{
powerThresholdRow.Add(threshold.SinglePhaseLimit);
}if (threshold.MultiplePhasesLimit != default)
{
powerThresholdRow.Add(threshold.MultiplePhasesLimit);
}powerThresholdInstances.Add(powerThresholdRow.ToArray());
}
}
}// Determine the next index
int nextIndex = indexesList.Max() + 1;// Insert data into the DataMiner tables
IDms dms = engine.GetDms();
if (dms == null)
{
engine.GenerateInformation("DMS is null.");
return;
}Element element = engine.FindElement("NetworkManager");
if (element == null)
{
engine.GenerateInformation("Element 'NetworkManager' not found.");
return;
}IDmsElement dmsElement = dms.GetElement(element.ElementName);
if (dmsElement == null)
{
engine.GenerateInformation($"DMS Element '{element.ElementName}' not found.");
return;
}IDmsTable powerSettingsTable = dmsElement.GetTable(11800);
if (powerSettingsTable == null)
{
engine.GenerateInformation("DMS Table with ID 11800 not found.");
return;
}IDmsTable powerThresholdTable = dmsElement.GetTable(12400);
if (powerThresholdTable == null)
{
engine.GenerateInformation("DMS Table with ID 12400 not found.");
return;
}FillTable(powerSettingsTable, powerSettingsInstances);
FillTable(powerThresholdTable, powerThresholdInstances);
}
catch (Exception ex)
{
engine.GenerateInformation($"Error: {ex.Message}\nStack Trace: {ex.StackTrace}");
}
}
private void FillTable(IDmsTable table, List<object[]> instances)
{
foreach (var instance in instances)
{
table.AddRow(instance);
}
}
}
}
I am seeking guidance on the following points:
- Is there any specific method or logic I might be missing that could cause the tables to not update correctly despite the script executing successfully?
- Are there any best practices or common pitfalls related to deserializing JSON and updating DataMiner tables that I should be aware of?
I'm wanting an automation script so that the operator has control over when to execute and what file to read. This is my first time working on an automation script that handles JSON data .Any insights or suggestions/help would be greatly appreciated!
Thank you so much for your support !
Hi A B M Sidddique,
At first sight, it seems you are doing things correctly.
The only point of attention might be the table ID itself.
IDmsTable powerSettingsTable = dmsElement.GetTable(1001);
if (powerSettingsTable == null)
{
engine.GenerateInformation("DMS Table with ID 11800 not found.");
return;
}
Here you indicate that the Power Settings Table has ParameterID 1001 and the information line mentions 11800.
Would it be possible to verify that 1001 is the correct ParameterID?
Most likely the same remark for the Power Thresholds table where 1002 might be the wrong ParamterID.
Kind regards,
Some of the column not all require to be updated from the json data , I am not sure if the script needs to be specified which column using Get/SetParameter method. I am looping through the IDs to match the identification of rows that need to be changed.
Hi, I believe the Index column of the row needs to be of type string as well. Would it be possible to try changing the type to string (as it’s currently of type int)?
Eg.
var powerSettingsRow = new List
{
updateSet.DeviceId.ToString(),
powerSettings.InitialPower,
powerSettings.DefaultIdlePowerConsumption,
powerSettings.ActivePowerConsumption,
powerSettings.LhcpPowerConsumption,
powerSettings.RhcpPowerConsumption,
powerSettings.DutyCycleConsumption,
powerSettings.DutyCycleAsymmetry,
};
powerSettingsInstances.Add(powerSettingsRow.ToArray());
Some other things to verify:
– Verify in the Information events if there is indeed no error being logged.
– Try to print out the number of rows, if it’s an empty collection the table will indeed be empty
– Try to print out the deserialized JSON again to see if the content indeed matches with what is defined in the file engine.GenerateInformation(“json: ” + JsonConvert.SerializeObject(jsonData));
Hi ,
I have modified the indexing to be a string and added the checks to see if its collecting and showing the right json format data in the information events . Also added the checks to see if no error is being logged. I get the following information events regarding the table :
“Number of power limit rows: 0 (Script ‘Device_defaults_mod’)”
“Number of power model rows: 0 (Script ‘Satellite_defaults_mod’)”
The JSon doesnot have the full data for the whole table but some particular columns
Hi Robin , Thank you for your answer .I have modified the The tableIDs for easier understanding but forgot to remove the comments . I am using 11800 as the tableID. But I am not sure if I should use the setparameter to update the table data ?