|
@@ -3,6 +3,8 @@ using BizService;
|
|
|
using Model.Dto;
|
|
|
using Model.Entities;
|
|
|
using PLCTool.Common;
|
|
|
+using PLCTool.Controls;
|
|
|
+using Prism.Commands;
|
|
|
using Prism.Mvvm;
|
|
|
using System;
|
|
|
using System.CodeDom;
|
|
@@ -27,9 +29,17 @@ namespace PLCTool.ViewModels.MonitorManageViewModel
|
|
|
|
|
|
_iBasPlcItemConfigService = iBasPlcItemConfigService;
|
|
|
_mapper = mapper;
|
|
|
- GetPLCConfig();
|
|
|
+ OnLoadCommand = new DelegateCommand(OnLoad);
|
|
|
+
|
|
|
ReadPLCRealValue();
|
|
|
}
|
|
|
+
|
|
|
+ private void OnLoad()
|
|
|
+ {
|
|
|
+ GetPLCConfig();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
#region 私有方法
|
|
|
/// <summary>
|
|
|
/// 获取PLC配置
|
|
@@ -40,61 +50,69 @@ namespace PLCTool.ViewModels.MonitorManageViewModel
|
|
|
plcConfigs = _mapper.Map<List<bas_plc_item_config>, List<BasPlcItemConfigDto>>(plclist);
|
|
|
foreach (var plc in plcConfigs)
|
|
|
{
|
|
|
- PLCItemList.Add(plc);
|
|
|
+ var find = PLCItemList.FirstOrDefault(X => X.PlcAddress == plc.PlcAddress);
|
|
|
+ if(find == null)
|
|
|
+ {
|
|
|
+ PLCItemList.Add(plc);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- #endregion
|
|
|
- #region 命令绑定
|
|
|
- private ObservableCollection<BasPlcItemConfigDto> plcItemList=new ObservableCollection<BasPlcItemConfigDto>();
|
|
|
- public ObservableCollection<BasPlcItemConfigDto> PLCItemList
|
|
|
- {
|
|
|
- get { return plcItemList; }
|
|
|
- set { plcItemList = value; RaisePropertyChanged(); }
|
|
|
- }
|
|
|
private void ReadPLCRealValue()
|
|
|
{
|
|
|
- Task.Run(async () =>
|
|
|
+ Task.Run(async () =>
|
|
|
+ {
|
|
|
+ while (true)
|
|
|
{
|
|
|
- while(true)
|
|
|
+ //更新界面上的值
|
|
|
+ Application.Current.Dispatcher.Invoke(() =>
|
|
|
{
|
|
|
- //更新界面上的值
|
|
|
- Application.Current.Dispatcher.Invoke(() =>
|
|
|
+ foreach (var item in PLCItemList)
|
|
|
{
|
|
|
- foreach (var item in PLCItemList)
|
|
|
+ //没有连接 plc,直接退出
|
|
|
+ if (!PLCCom.GetInstance().IsConnectPLC())
|
|
|
{
|
|
|
- //没有连接 plc,直接退出
|
|
|
- if(!PLCCom.GetInstance().IsConnectPLC())
|
|
|
- {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ string plcAddr = item.PlcAddress;
|
|
|
+ string plcAddType = item.PlcAddType;
|
|
|
+ switch (plcAddType)
|
|
|
+ {
|
|
|
+ case "bool":
|
|
|
+ item.RealValue = PLCCom.GetInstance().ReadPlcObject(plcAddr, VarType.Bit);
|
|
|
+ break;
|
|
|
+ case "word":
|
|
|
+ item.RealValue = PLCCom.GetInstance().ReadPlcObject(plcAddr, VarType.Word);
|
|
|
+ break;
|
|
|
+ case "dword":
|
|
|
+ item.RealValue = PLCCom.GetInstance().ReadPlcObject(plcAddr, VarType.DWord);
|
|
|
+ break;
|
|
|
+ case "real":
|
|
|
+ item.RealValue = PLCCom.GetInstance().ReadPlcObject(plcAddr, VarType.Real);
|
|
|
break;
|
|
|
- }
|
|
|
- string plcAddr = item.PlcAddress;
|
|
|
- string plcAddType = item.PlcAddType;
|
|
|
- switch (plcAddType)
|
|
|
- {
|
|
|
- case "bool":
|
|
|
- item.RealValue = PLCCom.GetInstance().ReadPlcObject(plcAddr, VarType.Bit);
|
|
|
- break;
|
|
|
- case "word":
|
|
|
- item.RealValue = PLCCom.GetInstance().ReadPlcObject(plcAddr, VarType.Word);
|
|
|
- break;
|
|
|
- case "dword":
|
|
|
- item.RealValue = PLCCom.GetInstance().ReadPlcObject(plcAddr, VarType.DWord);
|
|
|
- break;
|
|
|
- case "real":
|
|
|
- item.RealValue = PLCCom.GetInstance().ReadPlcObject(plcAddr, VarType.Real);
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
}
|
|
|
- });
|
|
|
|
|
|
- await Task.Delay(5000);
|
|
|
- }
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
- });
|
|
|
+ await Task.Delay(5000);
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+ #region 数据绑定
|
|
|
+ private AsyncObservableCollection<BasPlcItemConfigDto> plcItemList=new AsyncObservableCollection<BasPlcItemConfigDto>();
|
|
|
+ public AsyncObservableCollection<BasPlcItemConfigDto> PLCItemList
|
|
|
+ {
|
|
|
+ get { return plcItemList; }
|
|
|
+ set { plcItemList = value; RaisePropertyChanged(); }
|
|
|
}
|
|
|
+
|
|
|
+ #endregion
|
|
|
+ #region 命令绑定
|
|
|
+ public DelegateCommand OnLoadCommand { set; get; }
|
|
|
#endregion
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
}
|