using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.ComponentModel;
using Misc;
using FObjBase;
using FlyADBase;
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using FLY.Thick.Base.Server;
using FLY.Thick.RemoteHistory;
using FLY.Thick.BulkDataModule;
using FLY.Thick.Blowing.Server.Model;
namespace FLY.Thick.Blowing.Server
{
public class TDGage
{
#region 延时推送 MARKNO
const int MARKNO_UPDATEV = 1;
const int MARKNO_INITHISTORY = 2;
const int MARKNO_PUSH_ACCESS = 3;
#endregion
#region 参数--------------------------------------------------------------------
#region 初始化------------------------------------------------------------------
///
/// 参数目录 m_path;数据文件的 根目录默认是 "Gage1", 不能有 @"\"
/// 目前也是对外的服务器目录
///
public string mParamDirectory;
///
/// 名字 如:gage0
///
public string mName;
#endregion
#region 数据--------------------------------------------------------------------
///
/// 当前基本简单参数
///
public DynArea dynArea;
///
/// 运行参数
///
public BlowingFixProfile profile;
///
/// AD曲线
///
public CurveCollection curve;
///
/// 报警系统
///
public FLY.OBJComponents.Server.WarningSystem warning;
#region 运行模式
///
/// AD卡
///
public FlyADBase.FlyAD7 flyAd;
///
/// GM管理器
///
GageModeManager gmManager;
///
/// 定点模式
///
public GM_Fix gmFix;
///
/// 吹膜测厚模式.定点
///
public GM_BlowingFix gmRenZiJiaFix;
///
/// 扫描报警系统
///
public ScanWarning scanWarning;
///
/// 设备参数
///
public InitParam initParam;
#endregion
///
/// 系统参数
///
public SysParam sysParam;
#region BULKDATA
///
/// 数据库对象
///
DbModel dbModel;
/////
///// 原始数据 数据库对象
/////
//OrgDbModel orgDbModel;
///
/// 数据库写操作
///
public HistoryDb historyDb;
//OrgHistoryDb orgHistoryDb;
///
/// 本地数据库
///
LocalDb localDb;
///
/// 数据库读操作
///
public BulkDb bulkDb;
#endregion
#endregion
#endregion
public TDGage(string nam)
{
mName = nam;
mParamDirectory = mName;// @"D:\" + m_name;
if (!System.IO.Directory.Exists(mParamDirectory))
{
System.IO.Directory.CreateDirectory(mParamDirectory);
}
System.Environment.CurrentDirectory = mParamDirectory;
#region create
sysParam = new SysParam(null);//系统参数
profile = new BlowingFixProfile(null);//运行配置参数
curve = new CurveCollection(null);//AD转thick
initParam = new InitParam(null);
dbModel = new DbModel();
//orgDbModel = new OrgDbModel();
localDb = new LocalDb();
historyDb = new HistoryDb();
//orgHistoryDb = new OrgHistoryDb();
bulkDb = new BulkDb();
dynArea = new DynArea();
warning = new FLY.OBJComponents.Server.WarningSystem();
scanWarning = new ScanWarning();
flyAd = new FlyAD7();
gmManager = new GageModeManager();
gmFix = new GM_Fix(flyAd);
gmRenZiJiaFix = new GM_BlowingFix(flyAd);
#endregion
#region 注入初始化
//---------------------------------------------------------------------------------------------------------------
//数据库
dbModel.Init();
historyDb.Init(dbModel, localDb);
historyDb.KeepDbSize(initParam.DBKeepMonth);
//orgDbModel.Init();
//orgHistoryDb.Init(orgDbModel);
//orgHistoryDb.KeepDbSize(3);
//PollModule.Current.Poll_Config(PollModule.POLL_CONFIG.ADD, () =>
//{
// //避免1个月都不关程序,每天检查一次
// orgHistoryDb.KeepDbSize(3);
//}, TimeSpan.FromDays(1));
bulkDb.Init(historyDb, localDb, dbModel);
//---------------------------------------------------------------------------------------------------------------
//dynarea
Misc.BindingOperations.SetBinding(profile.Param, "PName", dynArea, "ProductName");
Misc.BindingOperations.SetBinding(profile.Param, "Target", () =>
{
dynArea.Target = (int)(profile.Param.Target * 100);
});
Misc.BindingOperations.SetBinding(profile.Param, new string[] { "Target", "TolerancePercent" }, () =>
{
dynArea.Alarm = (int)(profile.Param.Target * profile.Param.TolerancePercent * 100 );
});
Misc.BindingOperations.SetBinding(profile.Param, "K", dynArea, "Comp");
dynArea.PropertyChanged += new PropertyChangedEventHandler(mDynArea_PropertyChanged);
//---------------------------------------------------------------------------------------------------------------
//GageModeManager_Create
Misc.BindingOperations.SetBinding(gmManager, "State", dynArea, "ControllerState");
//---------------------------------------------------------------------------------------------------------------
//FlyAD7_Create
flyAd.Load();
flyAd.Connect();
Misc.BindingOperations.SetBinding(flyAd, "IStatus", dynArea, "IStatus");
Misc.BindingOperations.SetBinding(flyAd, "OStatus", dynArea, "OStatus");
Misc.BindingOperations.SetBinding(flyAd, "Position", dynArea, "Position");
Misc.BindingOperations.SetBinding(flyAd, "Position2", dynArea, "Position2");
Misc.BindingOperations.SetBinding(flyAd, "Surplus", dynArea, "Hrs");
Misc.BindingOperations.SetBinding(flyAd, "IsConnected", dynArea, "FLYADIsConnect");
//---------------------------------------------------------------------------------------------------------------
//Warning_Create
warning.Init(historyDb.ErrorBuffer);
Misc.BindingOperations.SetBinding(warning, "IsRinging", () =>
{
if (warning.IsRinging)
{
flyAd.SetOutput(
(UInt16)Misc.MyBase.BIT(4 - 1),
(UInt16)~Misc.MyBase.BIT(4 - 1));
}
else
{
flyAd.SetOutput(
(UInt16)Misc.MyBase.BIT(4 - 1),
(UInt16)Misc.MyBase.BIT(4 - 1));
}
});
//---------------------------------------------------------------------------------------------------------------
//ScanWarning_Create
scanWarning.Init(warning,historyDb);
//---------------------------------------------------------------------------------------------------------------
//GM_Fix_Create
gmFix.Init(dynArea, AD2Thick);
gmFix.IsIgnoreDataWhenIdle = false;
gmManager.AddGM(gmFix);
//---------------------------------------------------------------------------------------------------------------
//GM_RenZiJiaFix_Create
gmManager.AddGM(gmRenZiJiaFix);
//---------------------------------------------------------------------------------------------------------------
//Bulkdate_Create
//gmRenZiJiaFix.Init(Ad2Thk, dynArea, profile.Param, historyDb,orgHistoryDb, bulkDb );
gmRenZiJiaFix.Init(Ad2Thk, dynArea, profile.Param, historyDb, bulkDb);
gmRenZiJiaFix.Start();
ErrNoForCheckInit();
#endregion
}
void mGMRenZiJiaFix_EPCSampled(int ad)
{
CurveCollection pCurve = curve;
dynArea.SampleAD[0] = ad;
List list = new List();
if (pCurve.Curves.Count == 0)
return;
list.Add(new CurveCollection.ExChange() { OrgAD = pCurve.Curves[0].AD, CurrAD = ad });
pCurve.ModifyExChange(list);
pCurve.ReviseCurve();
}
#region 异常检测
class ErrNoForCheckItem
{
public FLY.OBJComponents.Common.ERRNO errno;
public Func conditional;
///
/// 持续时间,单位s
///
public int cnt;
int timer = -1;
public FLY.OBJComponents.Server.WarningSystem mWarning;
public void Clear()
{
if (timer == 0)
{
mWarning.Add(
errno.Code,
errno.Descrption,
FLY.OBJComponents.Common.ERR_STATE.OFF);
}
timer = -1;
}
public void OnPoll()
{
if (conditional())
{
if (timer < 0)
timer = cnt;
else
{
if (timer > 0)
timer--;
if (timer == 0)
{
mWarning.Add(
errno.Code,
errno.Descrption,
FLY.OBJComponents.Common.ERR_STATE.ON);
}
}
}
else
{
Clear();
}
}
}
List ErrNoForCheckItems = new List();
void ErrNoForCheckInit()
{
ErrNoForCheckItems.Add(//AD值太小
new ErrNoForCheckItem()
{
errno = FLY.Thick.Blowing.Common.ERRNOs.BASE_ERRNO_AD_MIN,
cnt = 20,
conditional = delegate()
{
return dynArea.AD < (dynArea.ADMax * 0.01);
}
});
ErrNoForCheckItems.Add(//AD值太大
new ErrNoForCheckItem()
{
errno = FLY.Thick.Blowing.Common.ERRNOs.BASE_ERRNO_AD_MAX,
cnt = 20,
conditional = delegate()
{
return (dynArea.AD > (dynArea.ADMax * 0.98));
}
});
foreach (ErrNoForCheckItem item in ErrNoForCheckItems)
{
item.mWarning = warning;
}
FObjBase.PollModule.Current.Poll_Config(
FObjBase.PollModule.POLL_CONFIG.ADD,
onpoll_checkErrNo, TimeSpan.FromSeconds(1));
}
bool isInit_onpoll_checkErrNo;
void onpoll_checkErrNo() //1秒执行一次
{
if (!isInit_onpoll_checkErrNo)
{
isInit_onpoll_checkErrNo = true;
checkDynAreaError();
}
if (!dynArea.FLYADIsConnect)
{
foreach (ErrNoForCheckItem item in ErrNoForCheckItems)
item.Clear();
}
else
{
foreach (ErrNoForCheckItem item in ErrNoForCheckItems)
item.OnPoll();
}
}
#endregion
void mDynArea_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(dynArea.Hrs))
{
if (dynArea.Hrs < 48 && dynArea.Hrs > 0)//授权限制提醒
{
warning.Add(
FLY.Thick.Blowing.Common.ERRNOs.BASE_ERRNO_LICENSE_TIP.Code,
FLY.Thick.Blowing.Common.ERRNOs.BASE_ERRNO_LICENSE_TIP.Descrption,
OBJComponents.Common.ERR_STATE.ON);
}
}
else if (e.PropertyName == nameof(dynArea.SecuteLock)) //授权限制
{
if (dynArea.SecuteLock)
{
warning.Add(
FLY.Thick.Blowing.Common.ERRNOs.BASE_ERRNO_LICENSE.Code,
FLY.Thick.Blowing.Common.ERRNOs.BASE_ERRNO_LICENSE.Descrption,
OBJComponents.Common.ERR_STATE.ON);
}
else
{
warning.Add(
FLY.Thick.Blowing.Common.ERRNOs.BASE_ERRNO_LICENSE.Code,
FLY.Thick.Blowing.Common.ERRNOs.BASE_ERRNO_LICENSE.Descrption,
OBJComponents.Common.ERR_STATE.ON);
}
}
else if (e.PropertyName == nameof(dynArea.FLYADIsConnect))//连接断开
{
if (!dynArea.FLYADIsConnect)
{
warning.Add(
FLY.Thick.Blowing.Common.ERRNOs.BASE_ERRNO_FLYAD7DISCONNECTED.Code,
FLY.Thick.Blowing.Common.ERRNOs.BASE_ERRNO_FLYAD7DISCONNECTED.Descrption,
OBJComponents.Common.ERR_STATE.ON);
}
else
{
warning.Add(
FLY.Thick.Blowing.Common.ERRNOs.BASE_ERRNO_FLYAD7DISCONNECTED.Code,
FLY.Thick.Blowing.Common.ERRNOs.BASE_ERRNO_FLYAD7DISCONNECTED.Descrption,
OBJComponents.Common.ERR_STATE.OFF);
}
}
}
void checkDynAreaError()
{
if (dynArea.Hrs < 48 && dynArea.Hrs > 0)//授权限制提醒
{
warning.Add(
Common.ERRNOs.BASE_ERRNO_LICENSE_TIP.Code,
Common.ERRNOs.BASE_ERRNO_LICENSE_TIP.Descrption,
OBJComponents.Common.ERR_STATE.ON);
}
if (dynArea.SecuteLock)
{
warning.Add(
Common.ERRNOs.BASE_ERRNO_LICENSE.Code,
Common.ERRNOs.BASE_ERRNO_LICENSE.Descrption,
OBJComponents.Common.ERR_STATE.ON);
}
if (!dynArea.FLYADIsConnect)
{
warning.Add(
Common.ERRNOs.BASE_ERRNO_FLYAD7DISCONNECTED.Code,
Common.ERRNOs.BASE_ERRNO_FLYAD7DISCONNECTED.Descrption,
OBJComponents.Common.ERR_STATE.ON);
}
}
#region AD卡密码操作,主机版本号操作--------------------------------------------------------
public string Version//获取版本号
{
get
{
System.Reflection.Assembly ma = System.Reflection.Assembly.GetEntryAssembly();
FileInfo fi = new FileInfo(ma.Location);
FileVersionInfo mfv = FileVersionInfo.GetVersionInfo(ma.Location);
return mfv.FileVersion;
}
}
#endregion
#region 推送处理
public int AD2Thick(int ad)
{
int thick = Misc.MyBase.NULL_VALUE;
if (Misc.MyBase.ISVALIDATA(ad))
{
thick = curve.AD2Value(ad, AD2ValueFlag.Revised);
thick = (int)(thick * profile.Param.K);
}
return thick;
}
public double Ad2Thk(int ad)
{
double thk = double.NaN;
if (Misc.MyBase.ISVALIDATA(ad))
{
thk = curve.AD2Value(ad, AD2ValueFlag.Revised) / 100.0;
thk = thk * profile.Param.K;
}
return thk;
}
#endregion
}
}