using LB_VisionProcesses.Communicators;
|
using Newtonsoft.Json;
|
using Newtonsoft.Json.Serialization;
|
using System;
|
using System.Collections.Concurrent;
|
using System.Collections.Generic;
|
using System.Diagnostics;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace LB_VisionProcesses.MotionControl
|
{
|
[Serializable]
|
[Process("运动控制模块", Category = "运动控制工具", Description = "创建运动控制工具")]
|
public class MotionControlConfig : IProcess
|
{
|
/// <summary>
|
/// 运动控制模块集合(Key:通讯名,Value:通讯句柄)
|
/// </summary>
|
public ConcurrentDictionary<string, BaseMotionControl> dicMotionControls { get; set; }
|
|
/// <summary>
|
/// 通讯集合(Key:通讯名,Value:通讯句柄)
|
/// </summary>
|
public ConcurrentDictionary<string, BaseCommunicator> dicCommunicators { get; set; }
|
|
public MotionControlConfig(ConcurrentDictionary<string, BaseMotionControl> dicMotionControl)
|
{
|
this.dicMotionControls = dicMotionControl;
|
strProcessClass = "LB_VisionProcesses.Communicators.MotionControlConfig";
|
|
Params.Inputs.Add("运动控制名称", "");
|
//Params.Inputs.Add("X轴PLC地址", "");
|
//Params.Inputs.Add("X轴检测位置", "");
|
//Params.Inputs.Add("Y轴PLC地址", "");
|
//Params.Inputs.Add("Z轴PLC地址", "");
|
//Params.Inputs.Add("W轴PLC地址", "");
|
Params.Outputs.Add("收到的信息", "");
|
}
|
|
public override object Clone()
|
{
|
return MemberwiseClone();
|
}
|
|
public override void Dispose()
|
{
|
return;
|
}
|
|
public override void InitRunParams()
|
{
|
Result = true;
|
Msg = "";
|
|
if (Record != null)
|
{
|
Record.Dispose();
|
}
|
}
|
/// <summary>
|
/// 加载算法
|
/// </summary>
|
/// <param name="fullPath">完整路径带.json</param>
|
/// <returns></returns>
|
public override bool Load(string fullPath)
|
{
|
try
|
{
|
if (string.IsNullOrEmpty(fullPath))
|
return false;
|
|
if (!fullPath.Contains(".json"))
|
{
|
Debug.WriteLine("文件路径不完整");
|
return false;
|
}
|
if (string.IsNullOrEmpty(fullPath) || fullPath.Trim() == "")
|
{
|
Debug.WriteLine("文件路径不完整");
|
return false;
|
}
|
|
// 获取不带文件名的目录路径
|
string directoryPath = Path.GetDirectoryName(fullPath);
|
strProcessName = Path.GetFileNameWithoutExtension(fullPath);
|
|
if (!File.Exists(fullPath))
|
{
|
Debug.WriteLine("文件不存在创建空文件");
|
Save(directoryPath);
|
return true;
|
}
|
|
string strJson = string.Empty;
|
using (StreamReader streamReader = new StreamReader(fullPath, Encoding.UTF8))
|
{
|
strJson = streamReader.ReadToEnd();
|
streamReader.Close();
|
}
|
Params = JsonConvert.DeserializeObject<ProcessParams>(strJson);
|
if (Params == null)
|
return false;
|
|
Params.FixDeserializedData();
|
return true;
|
}
|
catch { return false; }
|
}
|
|
public override bool Run()
|
{
|
InitRunParams();
|
Params.Outputs["收到消息"] = "";
|
string MotionControlName = Params.Inputs["运动控制名称"].ToString();
|
if (!dicMotionControls.ContainsKey(MotionControlName))
|
{
|
Msg = $"运动控制[{MotionControlName}]不存在";
|
Result = false;
|
return Result;
|
}
|
BaseMotionControl BaseMotionControl = dicMotionControls[MotionControlName];
|
|
if (BaseMotionControl == null)
|
{
|
Msg = $"运动控制[{MotionControlName}]未实例化";
|
Result = false;
|
return Result;
|
}
|
|
if (!BaseMotionControl.bConnected)
|
{
|
Msg = $"运动控制[{MotionControlName}]未连接";
|
Result = false;
|
return Result;
|
}
|
|
return true;
|
}
|
|
/// <summary>
|
/// 保存算法
|
/// </summary>
|
/// <param name="filePath">不带.json</param>
|
/// <returns></returns>
|
public override bool Save(string filePath = null)
|
{
|
try
|
{
|
if (string.IsNullOrEmpty(filePath) || filePath.Trim() == "")
|
{
|
Debug.WriteLine("文件路径不完整");
|
return false;
|
}
|
|
string strJson = string.Empty;
|
var settings = new JsonSerializerSettings
|
{
|
Formatting = Newtonsoft.Json.Formatting.Indented,
|
// 自定义缩进(4空格)
|
ContractResolver = new DefaultContractResolver
|
{
|
NamingStrategy = new CamelCaseNamingStrategy()
|
}
|
};
|
strJson = JsonConvert.SerializeObject(Params, settings);
|
|
Params = JsonConvert.DeserializeObject<ProcessParams>(strJson);
|
if (Params == null)
|
return false;
|
|
//判断文件夹是否存在,防呆输入为文件名称
|
if (!Directory.Exists(filePath))
|
{
|
try
|
{
|
Directory.CreateDirectory(filePath);
|
}
|
catch (Exception)
|
{ }
|
}
|
File.WriteAllText(filePath + "//" + strProcessName + ".json", strJson, Encoding.UTF8);
|
return true;
|
}
|
catch { return false; }
|
}
|
}
|
}
|