[성현모] CPXV2 Init
This commit is contained in:
210
SystemX.Net.CP.Platform/SystemX.PLC/Model/ModelCtrl.cs
Normal file
210
SystemX.Net.CP.Platform/SystemX.PLC/Model/ModelCtrl.cs
Normal file
@ -0,0 +1,210 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using SystemX.Net.Platform.Common.Util;
|
||||
using static SystemX.PLC.Model.ModelSetupTemplate;
|
||||
|
||||
namespace SystemX.PLC.Model
|
||||
{
|
||||
public class ModelCtrl
|
||||
{
|
||||
public Dictionary<string, ModelSetupTemplate> ModelDataResult { get; set; } = null;
|
||||
public ModelSetupTemplate ModelTmpStd { get; set; }
|
||||
public DataTable TemplateTable { get; set; } = null;
|
||||
public DataTable ModelTable { get; set; } = null;
|
||||
public string ModelPath { get; set; }
|
||||
|
||||
public ModelCtrl(string strModelTmpPath, string strModelPath)
|
||||
{
|
||||
ModelTmpStd = new ModelSetupTemplate(strModelTmpPath);
|
||||
TemplateTable = CreateDescDataTable(ModelTmpStd);
|
||||
ModelTable = OpenModelData(strModelPath);
|
||||
ModelPath = strModelPath;
|
||||
}
|
||||
|
||||
#region Initialization
|
||||
DataTable OpenModelData(string strModelPath)
|
||||
{
|
||||
DataTable dtSetup = new DataTable();
|
||||
List<string> vReadData = null;
|
||||
|
||||
CreateDTTemplate(dtSetup, ModelTmpStd);
|
||||
|
||||
if (File.Exists(strModelPath))
|
||||
vReadData = OpenModelFile(strModelPath);
|
||||
|
||||
ModelDataResult = ReadModelData(vReadData, ModelTmpStd);
|
||||
|
||||
CreateModelDatatable(dtSetup, ModelDataResult);
|
||||
|
||||
return dtSetup;
|
||||
}
|
||||
|
||||
DataTable CreateDescDataTable(ModelSetupTemplate model_tmpstd)
|
||||
{
|
||||
DataTable dtSetup = new DataTable();
|
||||
|
||||
dtSetup.Columns.Add(ModelTemplateDefinition.DescTableName);
|
||||
dtSetup.Columns.Add(ModelTemplateDefinition.DescTableValue);
|
||||
|
||||
foreach (string strKey in model_tmpstd.ModelDescriptionDictionary.Keys)
|
||||
{
|
||||
DataRow dtRow = dtSetup.NewRow();
|
||||
dtRow[ModelTemplateDefinition.DescTableName] = model_tmpstd.ModelDescriptionDictionary[strKey].Name;
|
||||
dtRow[ModelTemplateDefinition.DescTableValue] = model_tmpstd.ModelDescriptionDictionary[strKey].Desc;
|
||||
dtSetup.Rows.Add(dtRow);
|
||||
}
|
||||
|
||||
return dtSetup;
|
||||
}
|
||||
|
||||
void CreateDTTemplate(DataTable dtResult, ModelSetupTemplate model_tmpstd)
|
||||
{
|
||||
foreach (string strKey in model_tmpstd.ModelDataDictionary.Keys)
|
||||
{
|
||||
DataRow dtRow = dtResult.NewRow();
|
||||
Type typeTarget = CommonUtil.GetType(model_tmpstd.ModelDataDictionary[strKey].DataType);
|
||||
|
||||
if (typeTarget == null) return;
|
||||
|
||||
dtResult.Columns.Add(model_tmpstd.ModelDataDictionary[strKey].Name, typeTarget, model_tmpstd.ModelDataDictionary[strKey].Desc);
|
||||
|
||||
if (model_tmpstd.ModelDataDictionary[strKey].IsIDKey)
|
||||
dtResult.Columns[model_tmpstd.ModelDataDictionary[strKey].Name].Unique = true;
|
||||
}
|
||||
}
|
||||
|
||||
void CreateModelDatatable(DataTable dtResult, Dictionary<string, ModelSetupTemplate> vModelData)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (vModelData == null)
|
||||
return;
|
||||
|
||||
foreach (ModelSetupTemplate vModel in vModelData.Values)
|
||||
{
|
||||
DataRow dtRow = dtResult.NewRow();
|
||||
|
||||
foreach (string strKey in vModel.ModelDataDictionary.Keys)
|
||||
{
|
||||
ModelElementData elemData = vModel.ModelDataDictionary[strKey];
|
||||
object objValue = elemData.RValue;
|
||||
|
||||
if (objValue == null)
|
||||
continue;
|
||||
|
||||
dtRow[strKey] = objValue;
|
||||
}
|
||||
|
||||
dtResult.Rows.Add(dtRow);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"Error in CreateModelDatatable. Error Message: {ex.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateModelDataTable()
|
||||
{
|
||||
ModelTable.Clear();
|
||||
|
||||
CreateModelDatatable(ModelTable, ModelDataResult);
|
||||
}
|
||||
|
||||
Dictionary<string, ModelSetupTemplate> ReadModelData(List<string> vstrModel, ModelSetupTemplate model_tmpstd)
|
||||
{
|
||||
if (vstrModel == null || vstrModel.Count <= 0)
|
||||
return null;
|
||||
Dictionary<string, ModelSetupTemplate> dicResultModel = new Dictionary<string, ModelSetupTemplate>();
|
||||
List<string> vstrColumn = vstrModel[0].Split(ModelTemplateDefinition.ModelFileDelimiter.ToCharArray()).Where(x => x != string.Empty).ToList();
|
||||
|
||||
vstrModel.RemoveAt(0);
|
||||
|
||||
try
|
||||
{
|
||||
foreach (string strModel in vstrModel)
|
||||
{
|
||||
List<string> vstrModelLine = strModel.Split(ModelTemplateDefinition.ModelFileDelimiter.ToCharArray()).ToList();
|
||||
ModelSetupTemplate modelstr = model_tmpstd.GetNewModelStructure();
|
||||
|
||||
if (!CreateModelData(vstrColumn, vstrModelLine, modelstr))
|
||||
continue;
|
||||
|
||||
dicResultModel.Add(modelstr.GetPrimaryKey().Value, modelstr);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"Error occured in ModelCtrl.ReadModelData. Error Message: {ex.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
}
|
||||
return dicResultModel;
|
||||
}
|
||||
|
||||
bool CreateModelData(List<string> vstrColumn, List<string> vstrModelLine, ModelSetupTemplate modelstr)
|
||||
{
|
||||
string strColumnName = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < vstrColumn.Count; i++)
|
||||
{
|
||||
strColumnName = vstrColumn[i];
|
||||
modelstr.ModelDataDictionary[strColumnName].Value = vstrModelLine[i];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite("Error to find a column or a value. Column Name: " + strColumnName + "\n - Error Message: " + ex.Message, ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
List<string> OpenModelFile(string strPath)
|
||||
{
|
||||
List<string> vstrData = new List<string>();
|
||||
//IntPtr safeADSHandle = NativeMethods.CreateFile(strPath, FileAccess.Read, FileShare.Read, IntPtr.Zero, FileMode.Open, FileAttributes.Normal, IntPtr.Zero);
|
||||
FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.Read);
|
||||
StreamReader sr = new StreamReader(fs);
|
||||
|
||||
while (!sr.EndOfStream)
|
||||
vstrData.Add(sr.ReadLine());
|
||||
|
||||
sr.Close();
|
||||
fs.Close();
|
||||
|
||||
return vstrData;
|
||||
}
|
||||
#endregion
|
||||
|
||||
public ModelSetupTemplate FindElement(string strKey)
|
||||
{
|
||||
return ModelDataResult[strKey];
|
||||
}
|
||||
|
||||
public void SaveModel(string strPath)
|
||||
{
|
||||
string strSavePath = string.IsNullOrEmpty(strPath) ? ModelPath : strPath;
|
||||
//IntPtr safeADSHandle = NativeMethods.CreateFile(strSavePath, FileAccess.Read, FileShare.Read, IntPtr.Zero, FileMode.Open, FileAttributes.Normal, IntPtr.Zero);
|
||||
FileStream fs = new FileStream(strSavePath, FileMode.Truncate, FileAccess.ReadWrite);
|
||||
StreamWriter sr = new StreamWriter(fs);
|
||||
|
||||
sr.WriteLine(ModelTmpStd.GetSaveModelColName());
|
||||
|
||||
foreach (ModelSetupTemplate model in ModelDataResult.Values)
|
||||
{
|
||||
string strLine = model.GetSaveModelValue();
|
||||
|
||||
sr.WriteLine(strLine);
|
||||
}
|
||||
|
||||
sr.Close();
|
||||
fs.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
218
SystemX.Net.CP.Platform/SystemX.PLC/Model/ModelSetupTemplate.cs
Normal file
218
SystemX.Net.CP.Platform/SystemX.PLC/Model/ModelSetupTemplate.cs
Normal file
@ -0,0 +1,218 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using SystemX.Net.Platform.Common.Util;
|
||||
|
||||
namespace SystemX.PLC.Model
|
||||
{
|
||||
public class ModelSetupTemplate
|
||||
{
|
||||
public class ModelElementData
|
||||
{
|
||||
public ModelElementData() { }
|
||||
public ModelElementData(ModelElementData model)
|
||||
{
|
||||
Name = model.Name;
|
||||
DataType = model.DataType;
|
||||
Desc = model.Desc;
|
||||
Value = model.Value;
|
||||
IsIDKey = model.IsIDKey;
|
||||
|
||||
ChangeType();
|
||||
}
|
||||
string InternalValue { get; set; } = string.Empty;
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string DataType { get; set; } = string.Empty;
|
||||
public string Desc { get; set; } = string.Empty;
|
||||
public string Value
|
||||
{
|
||||
get { return InternalValue; }
|
||||
set
|
||||
{
|
||||
string strChangineValue = value;
|
||||
Type tResult = CommonUtil.GetType(DataType);
|
||||
|
||||
if (tResult == typeof(System.Boolean) && !string.IsNullOrWhiteSpace(Value))
|
||||
{
|
||||
int nValue = strChangineValue == true.ToString() ? 1 : 0;
|
||||
|
||||
InternalValue = nValue.ToString();
|
||||
}
|
||||
else
|
||||
InternalValue = strChangineValue;
|
||||
}
|
||||
}
|
||||
public object RValue { get { ChangeType(); return objConvertedValue; } }
|
||||
object objConvertedValue = null;
|
||||
public bool IsIDKey { get; set; } = false;
|
||||
|
||||
void ChangeType()
|
||||
{
|
||||
try
|
||||
{
|
||||
Type tResult = CommonUtil.GetType(DataType);
|
||||
string strChangineValue = Value;
|
||||
|
||||
if (string.IsNullOrEmpty(strChangineValue) && tResult != typeof(System.Boolean))
|
||||
return;
|
||||
|
||||
if (tResult == typeof(System.Boolean))
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(Value))
|
||||
{
|
||||
bool bValue = false;
|
||||
int nValue = 0;
|
||||
|
||||
if (Boolean.TryParse(Value, out bValue))
|
||||
strChangineValue = bValue.ToString();
|
||||
else if (Int32.TryParse(Value, out nValue))
|
||||
strChangineValue = nValue == 0 ? false.ToString() : true.ToString();
|
||||
}
|
||||
else
|
||||
strChangineValue = false.ToString();
|
||||
}
|
||||
|
||||
objConvertedValue = Convert.ChangeType(strChangineValue, tResult);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"Converting Error on <{Name}>, Value: {Value}. \n - Error Message: {ex.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Dictionary<string, ModelElementData> ModelDescriptionDictionary { get; set; }
|
||||
public Dictionary<string, ModelElementData> ModelDataDictionary { get; set; }
|
||||
|
||||
public ModelSetupTemplate(ModelSetupTemplate model)
|
||||
{
|
||||
ModelDescriptionDictionary = CloneDictionary(model.ModelDescriptionDictionary);
|
||||
ModelDataDictionary = CloneDictionary(model.ModelDataDictionary);
|
||||
}
|
||||
|
||||
public ModelSetupTemplate(string strTemplatePath)
|
||||
{
|
||||
ModelDescriptionDictionary = new Dictionary<string, ModelElementData>();
|
||||
ModelDataDictionary = new Dictionary<string, ModelElementData>();
|
||||
|
||||
LoadXmlConfig(strTemplatePath);
|
||||
}
|
||||
|
||||
public ModelElementData GetPrimaryKey()
|
||||
{
|
||||
List<ModelElementData> vModelelemdata = ModelDataDictionary.Values.Where(x => x.IsIDKey == true).ToList();
|
||||
|
||||
return vModelelemdata.Count > 0 ? vModelelemdata[0] : null;
|
||||
}
|
||||
|
||||
public ModelElementData FindKey(string strName)
|
||||
{
|
||||
return ModelDataDictionary[strName];
|
||||
}
|
||||
|
||||
public string GetSaveModelColName()
|
||||
{
|
||||
string strResult = string.Empty;
|
||||
|
||||
foreach (ModelElementData element in ModelDataDictionary.Values)
|
||||
strResult += element.Name + ModelTemplateDefinition.ModelFileDelimiter;
|
||||
|
||||
strResult = strResult.Remove((strResult.Length - 1), 1);
|
||||
|
||||
return strResult;
|
||||
}
|
||||
|
||||
public string GetSaveModelValue()
|
||||
{
|
||||
string strResult = string.Empty;
|
||||
|
||||
foreach (ModelElementData element in ModelDataDictionary.Values)
|
||||
strResult += element.Value + ModelTemplateDefinition.ModelFileDelimiter;
|
||||
|
||||
strResult = strResult.Remove((strResult.Length - 1), 1);
|
||||
|
||||
return strResult;
|
||||
}
|
||||
|
||||
#region Initialization
|
||||
void LoadXmlConfig(string strXmlPath)
|
||||
{
|
||||
if (!File.Exists(strXmlPath))
|
||||
return;
|
||||
|
||||
XDocument xDoc = XDocument.Load(strXmlPath);
|
||||
|
||||
var xElement = xDoc.Element(ModelTemplateDefinition.ModelRoot);
|
||||
|
||||
if (xElement == null) return;
|
||||
|
||||
var xelemDescRoot = xElement.Element(ModelTemplateDefinition.DescriptionRoot);
|
||||
var xelemDSRoot = xElement.Element(ModelTemplateDefinition.DSRoot);
|
||||
|
||||
LoadXmlDataForTemplate(xelemDescRoot, ModelDescriptionDictionary);
|
||||
LoadXmlDataForTemplate(xelemDSRoot, ModelDataDictionary);
|
||||
}
|
||||
|
||||
public void LoadXmlDataForTemplate(XElement xElemRoot, Dictionary<string, ModelElementData> dicResult)
|
||||
{
|
||||
foreach (XElement elem in xElemRoot.Elements())
|
||||
{
|
||||
if (elem.Name != ModelTemplateDefinition.Element)
|
||||
continue;
|
||||
|
||||
string strName = string.Empty;
|
||||
string strDataType = string.Empty;
|
||||
string strDesc = string.Empty;
|
||||
string strValue = elem.Value;
|
||||
bool bIDKey = strValue == ModelTemplateDefinition.IDKeyMark ? true : false;
|
||||
|
||||
GetAttributes(elem, ref strName, ref strDataType, ref strDesc);
|
||||
dicResult.Add(strName, new ModelElementData() { Name = strName, DataType = strDataType, Desc = strDesc, IsIDKey = bIDKey });
|
||||
}
|
||||
|
||||
if (GetPrimaryKey() == null)
|
||||
dicResult[dicResult.Keys.ElementAt(0)].IsIDKey = true;
|
||||
}
|
||||
|
||||
void GetAttributes(XElement xElem, ref string strName, ref string strType, ref string strDesc)
|
||||
{
|
||||
strName = string.Empty;
|
||||
strType = string.Empty;
|
||||
strDesc = string.Empty;
|
||||
|
||||
foreach (XAttribute xattrb in xElem.Attributes())
|
||||
{
|
||||
if (xattrb.Name == ModelTemplateDefinition.AttrbName)
|
||||
strName = xattrb.Value;
|
||||
else if (xattrb.Name == ModelTemplateDefinition.AttrbDataType)
|
||||
strType = xattrb.Value;
|
||||
else if (xattrb.Name == ModelTemplateDefinition.AttrbDesc)
|
||||
strDesc = xattrb.Value;
|
||||
}
|
||||
}
|
||||
|
||||
public ModelSetupTemplate GetNewModelStructure()
|
||||
{
|
||||
ModelSetupTemplate model = new ModelSetupTemplate(this);
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
Dictionary<string, ModelElementData> CloneDictionary(Dictionary<string, ModelElementData> dicOrg)
|
||||
{
|
||||
Dictionary<string, ModelElementData> dicClone = new Dictionary<string, ModelElementData>();
|
||||
|
||||
foreach (string strKey in dicOrg.Keys)
|
||||
{
|
||||
ModelElementData modeldata = new ModelElementData(dicOrg[strKey]);
|
||||
|
||||
dicClone.Add(strKey, modeldata);
|
||||
}
|
||||
|
||||
return dicClone;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
namespace SystemX.PLC.Model
|
||||
{
|
||||
public static class ModelTemplateDefinition
|
||||
{
|
||||
public static string ModelRoot = "ModelTemplate";
|
||||
|
||||
public static string DescriptionRoot = "Description";
|
||||
public static string DSRoot = "DataStructure";
|
||||
public static string Element = "Element";
|
||||
public static string AttrbName = "Name";
|
||||
public static string AttrbDataType = "DataType";
|
||||
public static string AttrbDesc = "Desc";
|
||||
|
||||
public static string DescTableName = "Name";
|
||||
public static string DescTableValue = "Description";
|
||||
|
||||
public static string IDKeyMark = "IDKey";
|
||||
|
||||
public static string ModelFileDelimiter = ";";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user