Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 69 additions & 26 deletions src/Moryx.FactoryMonitor.Endpoints/Converter/Converter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Copyright (c) 2026 Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using Microsoft.Extensions.Logging;
using Moryx.AbstractionLayer.Resources;
using Moryx.ControlSystem.Cells;
using Moryx.Factory;
using Moryx.FactoryMonitor.Endpoints.Models;
using Moryx.Logging;
using Moryx.Orders;
using Moryx.Serialization;
using Moryx.Tools;
Expand All @@ -16,21 +18,30 @@ namespace Moryx.FactoryMonitor.Endpoints.Converter;
/// </summary>
internal class Converter
{
/// <summary>
/// Logger for this resource
/// </summary>
public IModuleLogger Logger { get; set; }

/// <summary>
/// Constructor
/// </summary>
/// <param name="serialization"></param>
public Converter(ICustomSerialization serialization)

public Converter(ICustomSerialization serialization, IModuleLogger logger = null)
{
Serialization = serialization;
Logger = logger;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an internal component. Why this nullability? Complexity could be reduced here when you don't allow null. The logger can also be a private field instead of a public property.

}

protected ICustomSerialization Serialization { get; }

public ResourceChangedModel ToResourceChangedModel(Resource current)
{
if (current == null) return null;
if (current == null)
{
return null;
}

var cellEntry = EntryConvert.EncodeObject(current.Descriptor, Serialization);

Expand All @@ -44,22 +55,22 @@ public ResourceChangedModel ToResourceChangedModel(Resource current)

public static ActivityChangedModel ToActivityChangedModel(ICell current)
{
if (current == null) return null;

return new ActivityChangedModel
{
ResourceId = current.Id,
};
return current == null
? null
: new ActivityChangedModel
{
ResourceId = current.Id,
};
Comment on lines -47 to +63
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer readability in such cases.

if (current == null) 
{
    return null;
}

return new ActivityChangedModel 
{
    ResourceId = current.Id
}

}

public static CellStateChangedModel ToCellStateChangedModel(Resource current)
{
if (current == null) return null;

return new CellStateChangedModel
{
Id = current.Id,
};
return current == null
? null
: new CellStateChangedModel
{
Id = current.Id,
};
}

/// <summary>
Expand All @@ -69,34 +80,55 @@ public static CellStateChangedModel ToCellStateChangedModel(Resource current)
/// <param name="baseType">Type of the resource</param>
internal Dictionary<string, CellPropertySettings> ToCellPropertySettings(Entry cellEntry, Type baseType)
{
if (cellEntry == null) return null;
if (cellEntry == null)
{
return null;
}

var cellProperties = new Dictionary<string, CellPropertySettings>();
if (cellEntry.GetType().IsArray || cellEntry.SubEntries.Count > 0)
{
foreach (var subEntry in cellEntry.SubEntries)
{
var results = ToCellPropertySettings(subEntry, baseType);
if (results == null) continue;
if (results == null)
{
continue;
}

foreach (var item in results)
{
cellProperties.Add(item.Key, item.Value);
if (cellProperties.ContainsKey(item.Key))
{
Logger?.LogWarning($"Duplicate DisplayName '{item.Key}' in resource '{baseType.Name}'. Overwriting previous value. Ensure unique DisplayNames.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Logger.LogWarning("Duplicate DisplayName '{itemKey}' in resource '{baseTypeName}'. Overwriting previous value. Ensure unique DisplayNames.", item.Key, baseType.Name);


cellProperties[item.Key] = item.Value;
}
else
{
cellProperties[item.Key] = item.Value;
}
}
}
}
else
{
var entryVisualizer = Serialization.GetProperties(baseType)
.FirstOrDefault(x => x.Name == cellEntry.Identifier)?.GetCustomAttribute<EntryVisualizationAttribute>();
if (entryVisualizer == null) return null;
var property = new CellPropertySettings
if (entryVisualizer == null)
{
IsDisplayed = false,
CurrentValue = cellEntry.Value.Current,
IconName = entryVisualizer?.Icon,
ValueUnit = entryVisualizer?.Unit,
};
cellProperties.Add(cellEntry.DisplayName, property);
return null;
}

var property = CreateCellPropertySettings(cellEntry, entryVisualizer);

var key = cellEntry.Identifier;

if (cellProperties.ContainsKey(key))
{
Logger?.LogWarning($"Duplicate DisplayName '{key}' in resource '{baseType.Name}'. Overwriting previous value.");
}
cellProperties[key] = property;
}
return cellProperties;
}
Expand Down Expand Up @@ -131,6 +163,17 @@ internal static OrderReferenceModel ToOrderReferenceModel(OrderModel orderModel)
};
}

private static CellPropertySettings CreateCellPropertySettings(Entry entry, EntryVisualizationAttribute entryVisualizer)
{
return new CellPropertySettings
{
IsDisplayed = false,
CurrentValue = entry.Value.Current,
IconName = entryVisualizer?.Icon,
ValueUnit = entryVisualizer?.Unit,
DisplayName = entry.DisplayName
};
}
private static InternalOperationClassification GetCorrectOperationState(Operation operation)
{
if (operation.Progress.SuccessCount + operation.Progress.FailureCount >= operation.TargetAmount)
Expand Down Expand Up @@ -189,4 +232,4 @@ public static TransportRouteModel ToTransportRouteModel(TransportPathModel pathM
}

public static FactoryStateModel ToFactoryStateModel(IManufacturingFactory factory) => new() { Id = factory.Id };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ public class CellPropertySettings
public virtual bool IsDisplayed { get; set; }
[DataMember]
public virtual object CurrentValue { get; set; }
}
[DataMember]
public string DisplayName { get; set; }

}
Loading