tbf/TBF/Rig/Modbus/TempControl/Easytherm/TControlCfg.cs

282 lines
11 KiB
C#

///
/// Copyright (c) 2017-2022 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using Common;
using Config.Entities;
using SchematicDrawing;
using TBF.Resources;
using TBF.Rig.Generic;
namespace TBF.Rig.Modbus.TempControl.Easytherm
{
public class TControlCfg : ComponentCfgBase, IChildComponentCfg, IParamsProvider,
IDrawingItemWithSetpoint, IDrawingItemWithMeasuredVal, IDrawingItemWithCustomBmp
{
public static XmlSerializer Serializer = XmlSerializer.FromTypes(new[] { typeof(TControlCfg) })[0];
public override XmlSerializer GetSerializer() { return Serializer; }
public IComponentCfgCtrl GetControl(IList<Component> cmpntEntities)
{
var parents = cmpntEntities.Where(x => x.ClassName == "Modbus.Common");
return new Configs.ParamsProvider.ComponentCfgCtrl(this, parents);
}
///
/// Serialized parameters
///
public int ModbusAddress; /// 0: 1..254
/// Schematic drawing info
public Shape Shape { get; set; }
public int X { get; set; }
public int Y { get; set; }
public Sz Sz { get; set; }
public Orient Orient { get; set; }
public bool Flip { get; set; }
public int LblX { get; set; }
public int LblY { get; set; }
public Orient LblOrient { get; set; }
public int MsrdX { get; set; }
public int MsrdY { get; set; }
public Orient MsrdOrient { get; set; }
public string MsrdFormat { get; set; } /// 1
public Unit MsrdUnit { get; set; } /// 2
public double MsrdValLimLo { get; set; }
public double MsrdValLimHi { get; set; }
public int SetpX { get; set; }
public int SetpY { get; set; }
public Orient SetpOrient { get; set; }
public string SetpFormat { get; set; } /// 3
public Unit SetpUnit { get; set; } /// 4
public double SetpStep { get; set; } /// 5
public bool RoundToSetpStep { get; set; } /// 6
[XmlIgnore]
public IList<GNode> GNodes { get; set; }
/// <summary> Procedure parameters </summary>
[XmlIgnore]
public ProcParams ProcParams;
public override IParamsProvider GetRuntimeProcParamsProvider() { return ProcParams; }
public override IParamsProvider CreateProcParamsProvider() { return new ProcParams(true); }
[XmlIgnore]
public bool IsOffline { get; set; }
/// Private parameterless constructor invoked by all other (public) constructors
TControlCfg()
{
ProcParams = new ProcParams(true);
GNodes = new List<GNode>();
}
public TControlCfg(IComponentFactory factory)
: this()
{
this.Factory = factory;
Name = "Novus";
ParentName = "Modbus";
Shape = Shape.Custom;
Sz = Sz.M;
MsrdValLimLo = 5;
MsrdValLimHi = 95;
MsrdY = 22;
InitializeAll();
}
public string ComponentName { get { return Name; } }
public void InitializeAll()
{
ModbusAddress = 49;
MsrdFormat = "{0:F1} °C";
MsrdUnit = Unit.C;
SetpFormat = "{0:F1} °C";
SetpUnit = Unit.C;
SetpStep = 1.0;
RoundToSetpStep = true;
}
string[] paramNames = new string[]
{
"Modbus address", /// 0
"Measured temperature format", /// 1
"Units of measured temperature", /// 2
"Setpoint format", /// 3
"Units of temperature setpoint", /// 4
"Setpoint step", /// 5
"Round to setpoint step", /// 6
};
public string ParamName(int i) { return paramNames[i]; }
public int ParamsCount() { return paramNames.Length; }
public ICollection<string> ParamValues(int i)
{
switch (i)
{
case 2:
case 4:
return new string[] { "°C", "°F", "K" };
case 6:
return new string[] { Strings.yes, Strings.no };
default:
return null;
}
}
public string ToString(int i)
{
switch (i)
{
case 0: return ModbusAddress.ToString();
case 1: return MsrdFormat;
case 2: return (MsrdUnit == Unit.K) ? "K" : (MsrdUnit == Unit.F) ? "°F" : "°C";
case 3: return SetpFormat;
case 4: return (SetpUnit == Unit.K) ? "K" : (SetpUnit == Unit.F) ? "°F" : "°C";
case 5: return SetpStep.ToString();
case 6: return RoundToSetpStep ? Strings.yes : Strings.no;
default:
return string.Format("{0}({1}) address={2}", Name, string.IsNullOrEmpty(ParentName) ? "-" : ParentName, ModbusAddress);
}
}
public CfgUpdateFlags UpdateParam(int i, string str)
{
switch (i)
{
case 0: ModbusAddress = int.Parse(str); return CfgUpdateFlags.RestartRqrd;
case 1: MsrdFormat = str; return CfgUpdateFlags.RestartRqrd;
case 2: MsrdUnit = (str == "°K" || str == "K") ? Unit.K
: (str == "°F" || str == "F") ? Unit.F
: Unit.C;
return CfgUpdateFlags.RestartRqrd;
case 3: SetpFormat = str; return CfgUpdateFlags.RestartRqrd;
case 4: SetpUnit = (str == "°K" || str == "K") ? Unit.K
: (str == "°F" || str == "F") ? Unit.F
: Unit.C;
return CfgUpdateFlags.RestartRqrd;
case 5: SetpStep = Utils.ParseUDouble(str); return CfgUpdateFlags.RestartRqrd;
case 6: RoundToSetpStep = (str == Strings.yes); return CfgUpdateFlags.RestartRqrd;
default: return CfgUpdateFlags.None;
}
}
public bool ValidateParam(int i, string str, out string message)
{
message = string.Empty;
int idummy;
double ddummy;
switch (i)
{
case 0:
if (int.TryParse(str, out idummy) && idummy >= 1 && idummy <= 254) return true;
break;
case 1:
case 3:
return true;
case 2:
case 4:
if (str == "°K" || str == "K" || str == "°F" || str == "F" || str == "°C" || str == "C") return true;
break;
case 5:
if (Utils.TryParseUDouble(str, out ddummy)) return true;
break;
case 6:
if (str == Strings.yes || str == Strings.no) return true;
break;
default:
message = "Invalid index";
return false;
}
message = ParamName(i) + " is invalid";
return false;
}
void CopyContentTo(TControlCfg prms)
{
prms.ParentName = ParentName;
prms.ModbusAddress = ModbusAddress;
prms.Shape = Shape;
prms.Sz = Sz;
prms.Orient = Orient;
prms.Flip = Flip;
prms.LblX = LblX;
prms.LblY = LblY;
prms.LblOrient = LblOrient;
prms.SetpX = SetpX;
prms.SetpY = SetpY;
prms.SetpOrient = SetpOrient;
prms.SetpFormat = SetpFormat;
prms.SetpUnit = SetpUnit;
prms.MsrdX = MsrdX;
prms.MsrdY = MsrdY;
prms.MsrdOrient = MsrdOrient;
prms.MsrdFormat = MsrdFormat;
prms.MsrdUnit = MsrdUnit;
prms.MsrdValLimLo = MsrdValLimLo;
prms.MsrdValLimHi = MsrdValLimHi;
prms.SetpStep = SetpStep;
prms.RoundToSetpStep = RoundToSetpStep;
}
public IParamsProvider Clone()
{
TControlCfg pars = new TControlCfg();
CopyContentTo(pars);
return pars;
}
public bool UpdateEmbeddedDbEntity()
{
return true; /// =OK, do nothing
}
///
/// SchematicDrawing support
///
public DrawingShape GetSampleBitmap()
{
return (Sz == Sz.S) ? DSCoolingS : (Sz == Sz.M) ? DSCoolingM : (Sz == Sz.XL) ? DSCoolingXL : DSCoolingL;
}
///
/// Static part, DrawingShape constructor arguments depend on bitmap properties
///
public static DrawingShape DSIdleS, DSHeatingS, DSCoolingS, DSBothS;
public static DrawingShape DSIdleM, DSHeatingM, DSCoolingM, DSBothM;
public static DrawingShape DSIdleL, DSHeatingL, DSCoolingL, DSBothL;
public static DrawingShape DSIdleXL, DSHeatingXL, DSCoolingXL, DSBothXL;
///
static TControlCfg()
{
DSIdleS = new DrawingShape(Shape.Custom, Sz.S, 2, 2, null, null, 0, 0, 20, 20, Properties.Resources.TControl_S_idle);
DSHeatingS = new DrawingShape(Shape.Custom, Sz.S, 2, 2, null, null, 0, 0, 20, 20, Properties.Resources.TControl_S_heating);
DSCoolingS = new DrawingShape(Shape.Custom, Sz.S, 2, 2, null, null, 0, 0, 20, 20, Properties.Resources.TControl_S_cooling);
DSBothS = new DrawingShape(Shape.Custom, Sz.S, 2, 2, null, null, 0, 0, 20, 20, Properties.Resources.TControl_S_both);
DSIdleM = new DrawingShape(Shape.Custom, Sz.S, 3, 4, null, null, 0, 5, 30, 30, Properties.Resources.TControl_M_idle);
DSHeatingM = new DrawingShape(Shape.Custom, Sz.S, 3, 4, null, null, 0, 5, 30, 30, Properties.Resources.TControl_M_heating);
DSCoolingM = new DrawingShape(Shape.Custom, Sz.S, 3, 4, null, null, 0, 5, 30, 30, Properties.Resources.TControl_M_cooling);
DSBothM = new DrawingShape(Shape.Custom, Sz.S, 3, 4, null, null, 0, 5, 30, 30, Properties.Resources.TControl_M_both);
DSIdleL = new DrawingShape(Shape.Custom, Sz.S, 4, 4, null, null, 0, 0, 40, 40, Properties.Resources.TControl_L_idle);
DSHeatingL = new DrawingShape(Shape.Custom, Sz.S, 4, 4, null, null, 0, 0, 40, 40, Properties.Resources.TControl_L_heating);
DSCoolingL = new DrawingShape(Shape.Custom, Sz.S, 4, 4, null, null, 0, 0, 40, 40, Properties.Resources.TControl_L_cooling);
DSBothL = new DrawingShape(Shape.Custom, Sz.S, 4, 4, null, null, 0, 0, 40, 40, Properties.Resources.TControl_L_both);
DSIdleXL = new DrawingShape(Shape.Custom, Sz.S, 5, 6, null, null, 0, 5, 50, 50, Properties.Resources.TControl_XL_idle);
DSHeatingXL = new DrawingShape(Shape.Custom, Sz.S, 5, 6, null, null, 0, 5, 50, 50, Properties.Resources.TControl_XL_heating);
DSCoolingXL = new DrawingShape(Shape.Custom, Sz.S, 5, 6, null, null, 0, 5, 50, 50, Properties.Resources.TControl_XL_cooling);
DSBothXL = new DrawingShape(Shape.Custom, Sz.S, 5, 6, null, null, 0, 5, 50, 50, Properties.Resources.TControl_XL_both);
}
}
}