using Google.Protobuf;
using Google.Protobuf.Reflection;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Tango.PMR.Common;
using Tango.PMR.DataStore;
using Tango.PMR.Integration;
using Tango.PMR.Printing;
///
/// Contains PMR extension methods.
///
public static class ExtensionMethods
{
///
/// Gets the protobuf attribute value from the message type.
///
/// The value.
///
public static String ToOriginalName(this MessageType value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
OriginalNameAttribute[] attributes = (OriginalNameAttribute[])fi.GetCustomAttributes(typeof(OriginalNameAttribute), false);
if (attributes != null &&
attributes.Length > 0)
return attributes[0].Name;
else
return value.ToString();
}
public static String ToOriginalName(this DataStoreMessageType value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
OriginalNameAttribute[] attributes = (OriginalNameAttribute[])fi.GetCustomAttributes(typeof(OriginalNameAttribute), false);
if (attributes != null &&
attributes.Length > 0)
return attributes[0].Name;
else
return value.ToString();
}
///
/// Gets the message parser.
///
/// The message.
///
public static MessageParser GetParser(this IMessage message)
{
MessageParser parser = message.GetType().GetProperty("Parser").GetValue(message) as MessageParser;
return parser;
}
///
/// Determines whether the specified intent requires a password.
///
/// The intent.
///
public static bool RequiresPassword(this ExternalBridgeLoginIntent intent)
{
return
intent == ExternalBridgeLoginIntent.Diagnostics
||
intent == ExternalBridgeLoginIntent.FullControl;
}
///
/// Gets the name of the generic request type.
///
/// The request.
///
public static String GetTypeName(this GenericRequest request)
{
try
{
return request.Type.Split(',').First().Split('.').Last();
}
catch
{
return request.Type;
}
}
///
/// Gets the name of the generic response type.
///
/// The response.
///
public static String GetTypeName(this GenericResponse response)
{
try
{
return response.Type.Split(',').First().Split('.').Last();
}
catch
{
return response.Type;
}
}
///
/// Determines whether this segment is a group.
///
/// The segment.
///
/// true if the specified segment is group; otherwise, false.
///
public static bool IsGroup(this Tango.PMR.Exports.JobFileSegment segment)
{
return segment.Segments != null && segment.Segments.Count > 0;
}
///
/// Gets the total nanoliter per centimeter for this stop.
///
/// The stop.
///
public static double GetTotalNanoliterPerCentimeter(this JobBrushStop stop)
{
return stop.Dispensers.Where(x => x.DispenserLiquidType != DispenserLiquidType.Lubricant && x.DispenserLiquidType != DispenserLiquidType.Cleaner).Sum(x => x.NanoliterPerCentimeter);
}
///
/// Sets the transparent ink nanoliter per centimeter.
///
/// The stop.
/// The nl per cm.
/// The minimum ink uptake.
/// The dyeing speed.
/// The nl per step.
public static void NormalizeStop(this JobBrushStop stop, double nlPerCM, double minInkUptake, double dyeingSpeed)
{
double nlPcmSum = stop.Dispensers.Where(x => x.DispenserLiquidType != DispenserLiquidType.Cleaner && x.DispenserLiquidType != DispenserLiquidType.Lubricant && x.DispenserLiquidType != DispenserLiquidType.Transparent).Sum(x => x.NanoliterPerCentimeter);
var ti = stop.Dispensers.FirstOrDefault(x => x.DispenserLiquidType == DispenserLiquidType.Transparent);
if (ti == null) return;
//if (Math.Max(0, minInkUptake - nlPcmSum) < minInkUptake * 0.02d) return;
if (Math.Max(0, nlPerCM - nlPcmSum) < nlPerCM * 0.02d) return; //Daria change
double newNlCM = Math.Max(0, nlPerCM - nlPcmSum);
ti.NanoliterPerCentimeter = newNlCM;
ti.NanolitterPerSecond = newNlCM * dyeingSpeed;
}
}