using System.Text.Json;
using PLCSharp.Core.Tools;
using PLCSharp.Core.Prism;
using Prism.Commands;
using Prism.Mvvm;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Windows;
namespace PLCSharp.VVMs.Robots
{
///
/// 机器人
///
public class Robot : BindableBase
{
[NotMapped]
public RobotModel Model { get; set; }
///
/// 唯一标识
///
[Key]
public Guid ID { get; set; } = Guid.NewGuid();
private string _Name;
///
/// 机器人名称
///
public string Name
{
get { return _Name; }
set
{
if (_Name != value)
{
Prompt = "已修改,请保存";
}
SetProperty(ref _Name, value);
}
}
private RobotType _Type;
///
/// 机器人品牌类型
///
public RobotType Type
{
get { return _Type; }
set
{
if (_Type == RobotType.Undefined)
{
SetProperty(ref _Type, value);
}
else
{
MessageBox.Show("选定型号后不可改变!");
}
}
}
private string _IP;
///
/// IP地址
///
public string IP
{
get { return _IP; }
set { SetProperty(ref _IP, value); }
}
private int _Port;
///
/// 控制器端口
///
public int Port
{
get { return _Port; }
set { SetProperty(ref _Port, value); }
}
private int _CommanPort;
///
/// 指令交互端口
///
public int CommanPort
{
get { return _CommanPort; }
set { SetProperty(ref _CommanPort, value); }
}
private int _Speed = 100;
///
/// 总速度百分比 1-100
///
public int Speed
{
get { return _Speed; }
set
{
if (value > 100) value = 100;
if (value < 1) value = 1;
SetProperty(ref _Speed, value);
}
}
private string _Comment;
///
/// 备注
///
public string Comment
{
get { return _Comment; }
set
{
if (_Comment != value)
{
Prompt = "已修改,请保存";
}
SetProperty(ref _Comment, value);
}
}
///
/// 序列化Params
///
[Column("Params")]
public string SerializedParams
{
get => JsonSerializer.Serialize(Params, JsonOptions.Default);
set => Params = JsonSerializer.Deserialize(value, JsonOptions.Default);
}
private string _Prompt;
///
/// 提示
///
[NotMapped]
public string Prompt
{
get { return _Prompt; }
set { SetProperty(ref _Prompt, value); }
}
private ObservableCollection _Status = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false];
///
/// 状态
///
[NotMapped]
public ObservableCollection Status
{
get { return _Status; }
set { SetProperty(ref _Status, value); }
}
private ObservableCollection _StatusInfo = ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""];
///
/// 状态信息
///
[NotMapped]
public ObservableCollection StatusInfo
{
get { return _StatusInfo; }
set { SetProperty(ref _StatusInfo, value); }
}
private RobotParams _Params;
///
/// 参数集合
///
[NotMapped]
public RobotParams Params
{
get
{
_Params ??= new RobotParams();
return _Params;
}
set
{
SetProperty(ref _Params, value);
}
}
///
/// 机器人Params
///
public class RobotParams : BindableBase
{
private RobotPoint _ToolP1;
///
/// 工具P1
///
public RobotPoint ToolP1
{
get { return _ToolP1; }
set { SetProperty(ref _ToolP1, value); }
}
private RobotPoint _ToolP2;
///
/// 工具P2
///
public RobotPoint ToolP2
{
get { return _ToolP2; }
set { SetProperty(ref _ToolP2, value); }
}
}
private ObservableCollection _Points = [];
///
/// 坐标点集合
///
[NotMapped]
public ObservableCollection Points
{
get { return _Points; }
set { SetProperty(ref _Points, value); }
}
private ObservableDictionary _Tools = [];
///
/// Tools
///
[NotMapped]
public ObservableDictionary Tools
{
get { return _Tools; }
set { SetProperty(ref _Tools, value); }
}
///
/// 序列化Tools
///
[Column("Tools")]
public string SerializedTools
{
get
{
var str = JsonSerializer.Serialize(Tools, JsonOptions.Default);
return str;
}
set => Tools = value != null ? JsonSerializer.Deserialize>(value, JsonOptions.Default) : [];
}
private int _ToolTarget;
///
/// 工具Target
///
[NotMapped]
public int ToolTarget
{
get { return _ToolTarget; }
set { SetProperty(ref _ToolTarget, value); }
}
private RobotPoint _SelectedPoint;
///
/// 选中点
///
[NotMapped]
public RobotPoint SelectedPoint
{
get { return _SelectedPoint; }
set { SetProperty(ref _SelectedPoint, value); }
}
private RobotPoint _CurrPoint = new();
///
/// Curr点位
///
[NotMapped]
public RobotPoint CurrPoint
{
get { return _CurrPoint; }
set { SetProperty(ref _CurrPoint, value); }
}
private RobotPoint _CurrToolPoint = new();
///
/// Curr工具点位
///
[NotMapped]
public RobotPoint CurrToolPoint
{
get { return _CurrToolPoint; }
set { SetProperty(ref _CurrToolPoint, value); }
}
private int _CurrTool;
///
/// Curr工具
///
[NotMapped]
public int CurrTool
{
get { return _CurrTool; }
set { SetProperty(ref _CurrTool, value); }
}
private string _RcvShowInfo;
///
/// Rcv显示信息
///
[NotMapped]
public string RcvShowInfo
{
get { return _RcvShowInfo; }
set { SetProperty(ref _RcvShowInfo, value); }
}
private string _ConnStatus;
///
/// Conn状态
///
[NotMapped]
public string ConnStatus
{
get { return _ConnStatus; }
set { SetProperty(ref _ConnStatus, value); }
}
private string _ErrorInfo;
///
/// 错误信息
///
[NotMapped]
public string ErrorInfo
{
get { return _ErrorInfo; }
set { SetProperty(ref _ErrorInfo, value); }
}
private DelegateCommand _StartCmd;
///
/// 启动Cmd
///
public DelegateCommand StartCmd =>
_StartCmd ??= new DelegateCommand(ExecuteStartCmd);
void ExecuteStartCmd()
{
Start();
}
///
/// 启动
///
public virtual void Start()
{
System.Diagnostics.Debug.WriteLine($"[{Name}] Start");
}
private DelegateCommand _ResetCmd;
///
/// 重置Cmd
///
public DelegateCommand ResetCmd =>
_ResetCmd ??= new DelegateCommand(ExecuteResetCmd);
void ExecuteResetCmd()
{
}
///
/// 重置
///
public virtual void Reset()
{
}
private DelegateCommand _StopCmd;
///
/// 停止Cmd
///
public DelegateCommand StopCmd =>
_StopCmd ??= new DelegateCommand(ExecuteStopCmd);
void ExecuteStopCmd()
{
Stop();
}
///
/// 停止
///
public virtual void Stop()
{
}
///
/// 点位Command
///
[NotMapped]
public RobotPoint PointCommand { get; set; }
///
/// 运行点位
///
/// 机器人点位
public virtual void RunPoint(string pointName)
{
}
///
/// Jog
///
/// 点位
/// 命令参数
/// dist
/// rate
/// 返回布尔值
public virtual void Jog(string pointName, string cmd, double dist, double rate = 0)
{
}
///
/// 点位运行完成
///
[NotMapped]
public bool PointDone { get; set; }
private DelegateCommand _HomeCmd;
///
/// 回零Cmd
///
public DelegateCommand HomeCmd =>
_HomeCmd ??= new DelegateCommand(ExecuteHomeCmd);
void ExecuteHomeCmd()
{
Home();
}
///
/// 回零
///
public virtual void Home()
{
}
private DelegateCommand