using FLY.Modbus;
using FLY.OBJComponents.Common;
using FLY.OBJComponents.IService;
using FLY.OBJComponents.Server;
using FLY.Winder.Common;
using FLY.Winder.IService;
using FLY.Winder.Server.Model;
using FObjBase;
using Misc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace FLY.Winder.Server
{
public class WinderSystem : IWinderSystemService
{
///
/// 收卷其它附件
///
public WinderAccessory Accessory { get; private set; } = new WinderAccessory();
///
/// 内收卷 外收卷
///
public List Items { get; private set; } = new List() { new WinderInsideOutside(), new WinderInsideOutside() };
private PLCProxySystem plcos = new PLCProxySystem();
///
/// PLC代理系统
///
public IPLCProxySystemService PLCos { get { return plcos; } }
///
/// 报警系统
///
WarningSystem2 warning;
///
/// 记录到数据库
///
HistoryDb historyDb;
///
/// 报警配置
///
ErrorConf errorConf;
///
/// 周期保存收卷数据
///
PeriodicallySaveData psdWinderInfo;
public event PropertyChangedEventHandler PropertyChanged;
public WinderSystem()
{
}
public void Init(HistoryDb historyDb, WarningSystem2 warning)
{
this.historyDb = historyDb;
this.warning = warning;
//--------------------------------------------------------------------------------
//step 1 加载PLC寄存器 文件
plcos.AddConfigFile(
"plcgroup.json",
(plcgroup) =>
{
//PLCos.ObjNames[n].Key 必须与 plcgroup.json 中的 OwnerName 一致!!!!
var objNames = new Dictionary();
objNames.Add(nameof(Accessory), Accessory);
for (int i = 0; i < Items.Count(); i++)
objNames.Add($"{nameof(Items)}[{i}]", Items[i]);
return objNames;
}
);
//--------------------------------------------------------------------------------
//step 2 报警配置
errorConf = new ErrorConf(plcos, this.warning, "收卷");
//反射找出全部是报警的property
errorConf.AddErrorAction(Accessory);
errorConf.AddErrorAction(
Items[0],
(ref string description, object state) => { description = "内" + description; }
);
errorConf.AddErrorAction(
Items[1],
(ref string description, object state) => { description = "外" + description; }
);
errorConf.InitError();
//--------------------------------------------------------------------------------
//step 3 历史数据记录
//添加任务
PLCos.SetPlan(nameof(Accessory), new string[] {
nameof(Accessory.Velocity),
nameof(Accessory.Traction1Current),
nameof(Accessory.Traction2Current),
nameof(Accessory.Traction2TensionKg),
nameof(Accessory.RotaryCurrent),
nameof(Accessory.RotaryFreq),
nameof(Accessory.RotaryFreqSet),
nameof(Accessory.IsRotaryForw),
nameof(Accessory.IsRotaryBackw),
nameof(Accessory.IsRotaryBackwTurn),
nameof(Accessory.IsRotaryForwTurn),
nameof(Accessory.IsRotaryOrgSign)
}, 0);
for (int i = 0; i < Items.Count(); i++)
PLCos.SetPlan($"{nameof(Items)}[{i}]", new string[] {
nameof(WinderInsideOutside.TensionKg),
nameof(WinderInsideOutside.Current)
}, 0);
psdWinderInfo = new PeriodicallySaveData();
psdWinderInfo.Init(
10,
this.historyDb.AddWinderInfos,
() => {
if (!PLCos.IsConnectedWithPLC)//连接断开
return null;
if (Accessory.Velocity < 2)//没有生产
return null;
//记录数据
return new Db_WinderInfo
{
Time = DateTime.Now,
Velocity = Math.Round(Accessory.Velocity, 1),
Traction1Current = Math.Round(Accessory.Traction1Current, 1),
Traction2Current = Math.Round(Accessory.Traction2Current, 1),
Winder0Current = Math.Round(Items[0].Current, 1),
Winder1Current = Math.Round(Items[1].Current, 1),
Traction2TensionKg = Math.Round(Accessory.Traction2TensionKg, 1),
Winder0TensionKg = Math.Round(Items[0].TensionKg, 1),
Winder1TensionKg = Math.Round(Items[1].TensionKg, 1),
RotaryCurrent = Math.Round(Accessory.RotaryCurrent, 1),
RotaryFreq = Math.Round(Accessory.RotaryFreq, 1),
IsRotaryForw = Accessory.IsRotaryForw,
IsRotaryBackw = Accessory.IsRotaryBackw
};
});
//--------------------------------------------------------------------------------
//last step PLC 更新计划服务初始化
plcos.Init();
}
}
}