/// <summary> /// 获取IOService的事件数据 /// </summary> /// <param name="lpszDbVarName"></param> /// <param name="lpszVal"></param> /// <param name="lpszdateTime"></param> /// <param name="errTxt"></param> //internal static void IOServiceEventCollect(string lpszDbVarName, string lpszVal, DateTime lpszdateTime, string errTxt) //{ // foreach (var s1 in NCSys.Pro) // { // string mkey = s1.Value.ProjectKey + lpszDbVarName; // CollectVModel collect = null; // if (NCSys.Result.TryGetValue(mkey, out collect) == false) continue; // if (collect.Module_id == 0) continue;/*没有映射关系不存储告警*/ // CommandVModel cmd = new CommandVModel() { Ledger = s1.Key, Module_id = collect.Module_id, ModuleAddr = collect.ModuleAddr, Fun_id = collect.Fun_id, FunType = collect.FunType, FunName = collect.FunName, LpszDbVarName = lpszDbVarName }; // AlarmCollect collectInfo = new AlarmCollect() { cmd = cmd, lpszVal = lpszVal, lpszdateTime = lpszdateTime, errCode = AlarmType.Err_OFlow, errTxt = errTxt }; // NCSys.AlarmCollect.Enqueue(collectInfo); /*更新数据库*/ // } //} internal static void IOServiceWriteFail(CommandVModel cmd, DateTime lpszdateTime, UInt32 dwUserData, bool rst, AlarmType errCode, string errTxt) {/*读写存在*/ SysPro pro; NCSys.Pro.TryGetValue(cmd.Ledger, out pro); if (pro == null) { FileLog.Error("IOServiceWriteFail 没有此项目ID号:" + cmd.Ledger); return; } //string mkey = pro.ProjectKey + cmd.LpszDbVarName; //CollectVModel collect = null; //if (NCSys.Result.TryGetValue(mkey, out collect) == false) //{ // collect = new CollectVModel() { Ledger = cmd.Ledger }; // FileLog.Error("IOServiceWriteFail 没有采集码:" + mkey); //} //else //{ //CommandVModel cmd = new CommandVModel() { Ledger = ledger, Module_id = collect.Module_id, ModuleAddr = collect.ModuleAddr, Fun_id = collect.Fun_id, FunType = collect.FunType, FunName = collect.FunName, LpszDbVarName = lpszDbVarName, DataValue = lpszVal, CollectTime = lpszdateTime, IsNDb = true }; if (cmd.Action == 1 && cmd.Fun_id != 0) {/*成功记录写的值*/ if (rst == true) { new DataProcessBLL(cmd.Ledger, Config.Uid).UpdataMap(cmd.Module_id, cmd.Fun_id, cmd.DataValue); } } //AlarmCollect collectInfo = new AlarmCollect() { cmd = cmd, lpszVal = cmd.DataValue, lpszdateTime = lpszdateTime, errCode = errCode, errTxt = errTxt }; //NCSys.AlarmCollect.Enqueue(collectInfo); /*更新数据库*/ //} AddUIResult(cmd.Ledger, cmd.Module_id, cmd.ModuleAddr, "IOService", "IOService", cmd.FunType, cmd.FunName + "(写值)", cmd.LpszDbVarName, cmd.DataValue, lpszdateTime, rst, null, null, errCode, errTxt); }
/// <summary> /// 下发命令 /// </summary> /// <param name="cmd"></param> /// <param name="errCode"></param> /// <param name="errTxt"></param> /// <returns></returns> //public long AddCmdLog(int module_id, int fun_id, string funType, DateTime collectTime, string dataValue, string descr, int errCode, string errTxt) public long AddCmdLog(CommandVModel cmd, int errCode, string errTxt) { object params01 = new { Ledger = this.Ledger, CDate = cmd.CollectTime, Co_id = cmd.Co_id, Module_id = cmd.Module_id, ModuleAddr = cmd.ModuleAddr, Fun_id = cmd.Fun_id, FunType = cmd.FunType, Action = cmd.Action, DataValue = cmd.DataValue, CollectTime = cmd.CollectTime, Descr = cmd.Descr, ErrCode = errCode, ErrTxt = errTxt, Create_by = cmd.Create_by, Create_dt = cmd.Create_dt, IsRn = cmd.IsRn, }; StringBuilder strSql = new StringBuilder(); strSql.Clear(); strSql.Append("insert into v2_command("); strSql.Append("CDate,Ledger,Co_id,Module_id,ModuleAddr,Fun_id,FunType,Action,DataValue,CollectTime,Create_by,Create_dt,ErrCode,ErrTxt,EndTime,Descr,IsRn)"); strSql.Append("values(@CDate,@Ledger,@Co_id,@Module_id,@ModuleAddr,@Fun_id,@FunType,@Action,@DataValue,@CollectTime,@Create_by,@Create_dt,@ErrCode,@ErrTxt,now(),@Descr,@IsRn);"); //strSql.Append(" from vp_mdinfo as a"); //strSql.Append(" where a.Ledger=@Ledger and a.Module_id=@Module_id;"); strSql.Append("select last_insert_id();"); object obj = SQLHelper.ExecuteScalar(strSql.ToString(), params01); return(CommFunc.ConvertDBNullToLong(obj)); }
// public APIRst GetYdMonitorOfControl(string cmd, int module_id) { APIRst rst = new APIRst(); if (string.IsNullOrEmpty(cmd)) { rst.rst = false; rst.err.code = (int)ResultCodeDefine.Error; rst.err.msg = "请发送指令"; return(rst); } try { //long log_id = bll.YdModuleOnAddCmd(module_id, 0, V0Fun.Ssr1.ToString(), cmd); long log_id = bll.YdModuleOnAddCmd(module_id, 0, V0Fun.Ssr.ToString(), cmd); CommandVModel command = bll.GetYdModuleOnSendCmd(log_id); ListenVModel vm = new ListenVModel(); vm.cfun = ListenCFun.cmd.ToString(); vm.content = JsonHelper.Serialize(command); string msg = ""; rst.rst = CacheMgr.SendCollectVal(vm, out msg); rst.err.msg = msg; } catch (Exception ex) { rst.rst = false; rst.err.code = (int)ResultCodeDefine.Error; rst.err.msg = ex.Message; FileLog.WriteLog("下发指令错误(GetYdMonitorOfControl):" + ex.Message + ex.StackTrace); } return(rst); }
/// <summary> /// 更新下发命令 /// </summary> /// <param name="command"></param> /// <param name="lpszVal"></param> /// <param name="lpszdateTime"></param> /// <param name="errCode"></param> /// <param name="errTxt"></param> public static void DbCmdRefresh(CommandVModel command, string lpszVal, DateTime lpszdateTime, AlarmType errCode, string errTxt) { if (command == null || lpszdateTime.Year < 2000) { return; } if (command.Module_id == 0) { return; } DataProcessBLL bll = new DataProcessBLL(command.Ledger, Config.Uid); if (command.Log_id != 0) { //FileLog.WriteLog(command.LpszDbVarName + " 后台Log:"+ command.Log_id + JsonHelper.Serialize(command)); bll.UpdataCmd(command, (int)errCode, errTxt); } else { if (command.IsNDb == true) { /*更新记录*/ //FileLog.WriteLog(command.LpszDbVarName + " 后台标示IsNDb:true :" + JsonHelper.Serialize(command)); bll.AddCmdLog(command, (int)errCode, errTxt); } } }
private bool YdToGwCmd(int module_id, int fun_id, V0Fun funType, string dataValue, bool backgroup, int isRn = 0) { long log_id = bll.YdToGwOfAddCmd(module_id, fun_id, funType.ToString(), dataValue, isRn); CommandVModel cmd = bll.GetYdToGwOfSendCmd(log_id); return(this.SendCmd(cmd, backgroup)); }
/// <summary> /// 更新采集命令 /// </summary> /// <param name="cmd"></param> /// <param name="errCode"></param> /// <param name="errTxt"></param> /// <returns></returns> public int UpdataCmd(CommandVModel cmd, int errCode, string errTxt) { StringBuilder strSql = new StringBuilder(); strSql.Append("update v2_command set CollectTime=@CollectTime, ErrCode=@ErrCode,ErrTxt=@ErrTxt,EndTime=now() where Log_id=@Log_id"); return(SQLHelper.Execute(strSql.ToString(), new { Log_id = cmd.Log_id, CollectTime = cmd.CollectTime, ErrCode = errCode, ErrTxt = errTxt })); }
// 批量下发命令 private bool YdBatchControlOfSsr(int module_id, string funType, string dataValue, int isRn) { int fun_id = bll.GetYdToFun_id(module_id, funType); long log_id = bll.YdToGwOfAddCmd(module_id, fun_id, funType.ToString(), dataValue, isRn); CommandVModel cmd = bll.GetYdToGwOfSendCmd(log_id); DataProcess.YdDrive.Collection.CollectionHelper.Instance(cmd); return(true); }
private void Notify(long log_id, int module_id) { string msg = ""; long cmd_log_id = 0; YDS6000.BLL.Exp.Charge.ChargeBLL chrage = new YDS6000.BLL.Exp.Charge.ChargeBLL("", this.ledger, this.uid); chrage.YdPrePayInMdOnRecallPay(log_id, 1, "", out msg, out cmd_log_id); if (cmd_log_id != 0) { try { CommandVModel cmd = gwBll.GetYdToGwOfSendCmd(cmd_log_id); cmd.IsUI = true; gwBll.SendCmd(cmd, true);/*发送充值命令*/ if (cmd.FunType.Equals(V0Fun.Pay.ToString())) { gwBll.BeginYdToGwCmd(cmd.Module_id, V0Fun.Ssr, "0"); /*发送合闸命令*/ } else if (cmd.FunType.Equals(V0Fun.Refund.ToString())) { gwBll.BeginYdToGwCmd(cmd.Module_id, V0Fun.Ssr, "1"); /*发送拉闸命令*/ } // gwBll.BeginYdToGwCmd(cmd.Module_id, V0Fun.RdVal, ""); /*发送读取剩余电量*/ } catch (Exception ex) { FileLog.WriteLog("ResultNotify : 发送远程命令错误:", ex.Message + ex.StackTrace); } } else { DataTable dtSource = chrage.GetYdPostPayInMdOnList(module_id); bool isClosed = false; decimal odValue = 0, syVal = 0; if (dtSource.Rows.Count > 0) { isClosed = CommFunc.ConvertDBNullToInt32(dtSource.Rows[0]["IsClosed"]) == 1 ? true : false; odValue = CommFunc.ConvertDBNullToDecimal(dtSource.Rows[0]["OdValue"]); syVal = CommFunc.ConvertDBNullToDecimal(dtSource.Rows[0]["RdVal"]); } FileLog.WriteLog("isClosed:" + isClosed + " syVal:" + syVal + " odValue:" + odValue); if (isClosed == true) { if (syVal <= odValue) /*拉闸*/ { gwBll.BeginYdToGwCmd(module_id, V0Fun.Ssr, "1", 1); /*发送拉闸命令*/ } else /*合闸*/ { gwBll.BeginYdToGwCmd(module_id, V0Fun.Ssr, "0", 2); /*发送合闸命令*/ } } } }
private void GetYdModelsSendCmd(DataTable dtSource) { foreach (DataRow dr in dtSource.Rows) { CommandVModel cmd = ModelHandler <CommandVModel> .FillModel(dr); ListenVModel vm = new ListenVModel(); vm.cfun = ListenCFun.cmd.ToString(); vm.content = JsonHelper.Serialize(cmd); CacheMgr.BeginSend(vm); } }
/// <summary> /// 获取命令 /// </summary> /// <param name="log_id"></param> /// <returns></returns> public CommandVModel GetYdToGwOfSendCmd(long log_id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select * from vp_cmdinfo where Log_id=@Log_id"); DataTable dt = SQLHelper.Query(strSql.ToString(), new { Log_id = log_id }); CommandVModel cmd = null; if (dt.Rows.Count > 0) { cmd = ModelHandler <CommandVModel> .FillModel(dt.Rows[0]); } return(cmd); }
/// <summary> /// 下发命令数据 /// </summary> /// <param name="tags"></param> /// <param name="dataValue"></param> /// <returns></returns> public APIRst SendVal(List <string> tags, string dataValue) { List <object> dd = new List <object>(); APIRst rst = new APIRst() { rst = true }; try { DataTable dtInfo = bll.GetTagInfo(string.Join(",", tags.ToArray())); foreach (var k in tags) { DataRow[] arr = dtInfo.Select("LpszDbVarName='" + k + "'"); if (arr.Count() == 0) { dd.Add(new { tag = k, rst = false, msg = "没有此采集点" }); continue; } CommandVModel cmd = ModelHandler <CommandVModel> .FillModel(arr[0]); cmd.Action = 1; cmd.LpszDbVarName = CommFunc.ConvertDBNullToString(arr[0]["TagName"]); cmd.DataValue = dataValue; cmd.IsNDb = true; bll.UpdateMapDataVal(cmd.Module_id, cmd.Fun_id, cmd.DataValue); // ListenVModel vm = new ListenVModel() { cfun = ListenCFun.cmd.ToString(), content = JsonHelper.Serialize(cmd) }; string msg = ""; bool rr = CacheMgr.SendCollectVal(vm, out msg); dd.Add(new { tag = k, rst = rr, msg = msg }); } rst.data = dd; } catch (Exception ex) { rst.rst = false; rst.data = ""; rst.err = new APIErr() { code = -1, msg = ex.Message }; } return(rst); }
public static void DbEventWpf(CommandVModel command, string lpszVal, DateTime lpszdateTime, AlarmType errCode, string errTxt) { if (lpszVal == null || lpszdateTime.Year < 2000) { return; } if (errCode != AlarmType.Sue) { return; } DataProcessBLL bll = new DataProcessBLL(command.Ledger, Config.Uid); SysPro pro; NCSys.Pro.TryGetValue(command.Ledger, out pro); if (pro == null) { FileLog.Error("CollectAlarm.DbEventWpf 模块没有项目:" + command.Ledger); return; } string mkey = pro.ProjectKey + command.LpszDbVarName; CollectVModel collect = null; if (NCSys.Result.TryGetValue(mkey, out collect) == false) { return; } RstVar rst = collect.RstVar;// MemcachedMgr.GetVal<RstVar>(mkey); bool isAdd = true; if (rst != null && !string.IsNullOrEmpty(rst.lpszVal)) { MalignantVModel pp = JsonHelper.Deserialize <MalignantVModel>(rst.lpszVal); if (collect.IsDb == false) { if (pp.CTime == lpszdateTime) { isAdd = false; } } collect.IsDb = false; } if (isAdd == true) { /*增加到数据库中*/ bll.AddAlarmOnCollect(command.Co_id, command.Module_id, command.ModuleAddr, command.FunType, command.Fun_id, lpszVal, (int)errCode, errTxt, lpszdateTime); } }
private void AddCmd(CommandVModel cmd) { if (string.IsNullOrEmpty(cmd.HandledBY)) { FileLog.Error("队列键值为空" + JsonHelper.Serialize(cmd)); return; } DataProcess.YdDrive.Collection.CollectionHelper.Instance(cmd); //if (cmd.Protocol.Trim().ToLower().Equals("modbus".ToLower())) // DataProcess.YdDrive.Collection.CollectionHelper.Instance(cmd); //else if (Config.Project.Trim().ToLower().Equals("YdDrive".ToLower())) // DataProcess.YdDrive.Collection.CollectionHelper.Instance(cmd); //else // DataProcess.YdDrive.Collection.CollectionHelper.Instance(cmd); //FileLog.Info("抄表类型错误"); }
/// <summary> /// 重发命令 /// </summary> /// <param name="log_id"></param> /// <param name="upDb">是否成功更新数据</param> /// <param name="msg">错误信息</param> /// <returns>是否成功下发命令</returns> public bool YdToGwAgain(long log_id, out bool upDb, out string msg) { upDb = false; msg = ""; v2_commandVModel rstCmd = bll.GetYdToGwOfCmd(log_id); if (rstCmd == null) { msg = "远程命令已撤销"; return(false); } if (rstCmd.ErrCode == 1) { msg = "远程命令已执行"; return(false); } CommandVModel cmd = bll.GetYdToGwOfSendCmd(log_id); bool rst = false; try { rst = this.SendCmd(cmd, false); } catch (Exception ex) { msg = ex.Message; FileLog.WriteLog("发送到设备错误(YdToGwAgain)", ex.Message + ex.StackTrace); bll.UpErrYdToGwOfCmd(log_id, msg); } int timeout = 1000; while (rst == true && timeout <= 5000) {/*等待是否更新成功*/ System.Threading.Thread.Sleep(1000); rstCmd = bll.GetYdToGwOfCmd(log_id); if (rstCmd != null && rstCmd.ErrCode == 1) { upDb = true; break; } timeout = timeout + 1000; } return(rst); }
private void AssData(string rst) { ListenVModel vm = JsonHelper.Deserialize <ListenVModel>(rst); if (vm == null) { FileLog.WriteLog("服务端获取的数据错误:" + rst); } if (vm.cfun.Trim().ToLower().Equals(ListenCFun.cmd.ToString().ToLower())) { CommandVModel cmd = JsonHelper.Deserialize <CommandVModel>(vm.content); if (cmd.TransferType == 4) {/*IOService通信*/ DataProcess.IOService.Collection.CollectionHelper.WriteVar(cmd); //DataProcess.IOService.Collection.CollectionHelper.WriteVar(cmd.Ledger, cmd.HandledBY, cmd.LpszDbVarName, cmd.DataValue); } else { DataProcess.YdDrive.Collection.CollectionHelper.Instance(cmd); } } else if (vm.cfun.Trim().ToLower().Equals(ListenCFun.collect.ToString().ToLower())) { CommandVModel cmd = JsonHelper.Deserialize <CommandVModel>(vm.content); if (cmd.TransferType == 4) { DataProcess.YdProcess.Helper.IOServiceSaveCollectCache(cmd.LpszDbVarName, cmd.DataValue, cmd.CollectTime.Value, 2, true, AlarmType.Sue, ""); } } else if (vm.cfun.Trim().ToLower().Equals(ListenCFun.addvar.ToString().ToLower())) { CommandVModel cmd = JsonHelper.Deserialize <CommandVModel>(vm.content); List <string> tag = JsonHelper.Deserialize <List <string> >(cmd.DataValue); DataProcess.IOService.Collection.CollectionHelper.Instance(cmd.HandledBY, tag); } else if (vm.cfun.Trim().ToLower().Equals(ListenCFun.config.ToString().ToLower())) { NCSys.Reset = false; } else { FileLog.WriteLog("下发命令类型未存在:" + rst); } }
/// <summary> /// 获取命令 /// </summary> /// <param name="log_id"></param> /// <returns></returns> public CommandVModel GetCommmand(long log_id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select a.Log_id,a.Esp_id,a.EspAddr,a.ComPort,a.Baud,a.DataBit,a.Parity,a.StopBit,a.Ip, a.TcpPort, a.TimeOut, a.HandledBY, a.ModuleType, a.ModulePwd, a.ModuleUid,"); strSql.Append("a.TransferType, a.Module_id, a.ModuleAddr, a.Fun_id, a.FunName, a.FunType,"); strSql.Append("a.Protocol, a.DataValue, a.Co_id, a.Action, a.LpszDbVarName"); strSql.Append(" from vp_cmdinfo as a"); strSql.Append(" where Log_id =@Log_id"); DataTable dtSource = SQLHelper.Query(strSql.ToString(), new { Log_id = log_id }); CommandVModel cmd = null; if (dtSource.Rows.Count > 0) { cmd = ModelHandler <CommandVModel> .FillModel(dtSource.Rows[0]); } return(cmd); }
private void SetSelectedIndices(DataTable dtSource) { if (dtSource == null) { return; } // foreach (DataRow dr in dtSource.Rows) { string funType = CommFunc.ConvertDBNullToString(dr["FunType"]); int action = CommFunc.ConvertDBNullToInt32(dr["Action"]); CommandVModel cmd = ModelHandler <CommandVModel> .FillModel(dr); cmd.IsUI = true; cmd.IsNDb = funType.Equals(V0Fun.E.ToString()) ? false : true; cmd.IsNDb = action == 1 ? true : cmd.IsNDb; DataProcess.YdDrive.Collection.CollectionHelper.Instance(cmd); } }
/// <summary> /// 拉闸数据 /// </summary> /// <returns></returns> public CommandVModel GetSsr1(int module_id) { StringBuilder strSql = new StringBuilder(); strSql.Clear(); strSql.Append("insert into v2_command("); strSql.Append("CDate,Ledger,Co_id,Module_id,ModuleAddr,Fun_id,FunType,Action,DataValue,Create_by,Create_dt,ErrCode,IsRn)"); strSql.Append("select now(),a.Ledger,a.Co_id,a.Module_id,a.ModuleAddr,a.Fun_id,a.FunType,a.Action,1,@SysUid,now(),0,1"); strSql.Append(" from vp_funinfo as a"); strSql.Append(" where a.Ledger=@Ledger and a.Module_id=@Module_id and a.FunType=@FunType;"); strSql.Append(" select * from vp_cmdinfo where Log_id = last_insert_id();"); DataTable dtSource = SQLHelper.Query(strSql.ToString(), new { Ledger = this.Ledger, Module_id = module_id, FunType = V0Fun.Ssr.ToString(), SysUid = this.SysUid }); CommandVModel cmd = null; if (dtSource.Rows.Count > 0) { cmd = ModelHandler <CommandVModel> .FillModel(dtSource.Rows[0]); } return(cmd); }
public bool SendCmd(CommandVModel cmd, bool backgroup = true) { string msg = ""; if (cmd == null) { throw new Exception("远程控制为空"); } //目的适应NB表 if (cmd.TransferType == 4 && !string.IsNullOrEmpty(cmd.DataValue)) { string fs = cmd.FunType + cmd.DataValue; string tagName = "", dataValue = ""; tagName = bll.GetYdToFun_idIOService(cmd.Module_id, fs, out dataValue); if (!string.IsNullOrEmpty(tagName) && !string.IsNullOrEmpty(dataValue)) { cmd.LpszDbVarName = tagName; cmd.DataValue = dataValue; } } // cmd.IsUI = true; ListenVModel vm = new ListenVModel(); cmd.IsUI = true; vm.cfun = ListenCFun.cmd.ToString(); vm.content = JsonHelper.Serialize(cmd); lock (ydToGwlock) { if (backgroup == true) { CacheMgr.BeginSend(vm); } else { CacheMgr.SendCollectVal(vm, out msg); } } return(true); }
internal static void WriteVar(CommandVModel cmd) { CollectionHelper threadHelper = null; //*如有配置地址* if (!string.IsNullOrEmpty(Config.IOHandledBY)) { cmd.HandledBY = Config.IOHandledBY; } // if (SysCol.TryGetValue(cmd.HandledBY, out threadHelper) == false) { FileLog.Error("CollectionHelper对象不存在"); return; } bool rst = false; AlarmType errCode = AlarmType.Err_Tx; string errTxt = ""; try { int result = threadHelper.rdc.WriteVar(cmd.LpszDbVarName, cmd.DataValue); rst = result == 0 ? true : false; errCode = rst == true ? AlarmType.Sue : AlarmType.Err_Tx; errTxt = rst == true ? "成功写入数据值:" + cmd.DataValue : "写数值:" + cmd.DataValue + "失败,IOSerivce返回错误码:" + result.ToString(); if (rst == false) { errCode = AlarmType.Err_Sb; } FileLog.WriteSysLog("电表:" + cmd.ModuleAddr + " 变量:" + cmd.LpszDbVarName + " 值:" + cmd.DataValue + " 执行结果:" + errTxt); } catch (Exception ex) { errTxt = ex.Message; FileLog.Error("IOService变量" + cmd.LpszDbVarName + "下发命令错误:" + ex.Message + ex.StackTrace); } cmd.CollectTime = DateTime.Now; DataProcess.YdProcess.Helper.IOServiceWriteFail(cmd, DateTime.Now, 0, rst, errCode, errTxt); }
private void CtorlSsr1(int module_id) {/*先下发命令*/ //YDS6000.BLL.CmdService.ProcessBLL gwBll = new YDS6000.BLL.CmdService.ProcessBLL(Config.Ledger, Config.Uid); //int fun_id = gwBll.GetYdToFun_id(module_id, V0Fun.Ssr); //long log_id = gwBll.YdToGwOfAddCmd(module_id, fun_id, V0Fun.Ssr.ToString(), "1"); //CommandVModel cmd = gwBll.GetYdToGwOfSendCmd(log_id); CommandVModel cmd = bll.GetSsr1(module_id); //ModelHandler<CommandVModel>.FillModel(dtSsr.Rows[0]); if (cmd == null) { FileLog.Error("CtorlSsr1没有数据"); return; } cmd.DataValue = "1"; cmd.IsNDb = true; cmd.Create_by = Config.Uid; cmd.IsRn = 1; if (cmd.TransferType == 4) {/*IOService通信*/ string fs = cmd.FunType + cmd.DataValue; string tagName = "", dataValue = ""; tagName = bll.GetSSrIOService(module_id, fs, out dataValue); if (!string.IsNullOrEmpty(tagName) && !string.IsNullOrEmpty(dataValue)) { cmd.LpszDbVarName = tagName; cmd.DataValue = dataValue; } DataProcess.IOService.Collection.CollectionHelper.WriteVar(cmd); //DataProcess.IOService.Collection.CollectionHelper.WriteVar(cmd.Ledger, cmd.HandledBY, cmd.LpszDbVarName, cmd.DataValue); } else { DataProcess.YdDrive.Collection.CollectionHelper.Instance(cmd); } //DataProcess.YdDrive.Collection.CollectionHelper.Instance(cmd); }
//初始化数据 internal static CollectionHelper Instance(CommandVModel command) { CollectionHelper ThreadHelper = null; try { V0Fun vfun; int priority = 0; if (Enum.TryParse <V0Fun>(command.FunType, out vfun) == true) { priority = (int)vfun; } ulong rid = 0; bool isRun = true; //命令功能运行次序(数字越小越先级越大)(内容来自FunPriority枚举) FirstDefine = 10 设置参数 ; SecondSsr0or1 lock (CollectionHelper.wholeLocker) { if (CollectionHelper.instance.TryGetValue(command.HandledBY, out ThreadHelper) == false) { ThreadHelper = new CollectionHelper(); ThreadHelper.HandledBY = command.HandledBY; ThreadHelper.Transfer = command.TransferType; ThreadHelper.Network = new NetworkResource(command.HandledBY, command.TransferType, command.TimeOut); if (CollectionHelper.instance.TryAdd(command.HandledBY, ThreadHelper) == false) { isRun = false; FileLog.Error("点:" + command.LpszDbVarName + "命令," + "队列:" + command.HandledBY + "新增内存失败"); } } if (isRun == true) { ThreadHelper.CmdId = ThreadHelper.CmdId + 1; rid = (command.IsUI == false ? (ulong)(priority * 100000000) : (ulong)(priority * 10000)) + ThreadHelper.CmdId; if (command.IsUI == false) { NCSys.BackgroundCount = NCSys.BackgroundCount + 1; } lock (ThreadHelper.locker) { ThreadHelper.ThreadCmd.Add(rid, command); } if (ThreadHelper.threadRun == false && command.IsUI == true) { isRun = true; ThreadHelper.threadRun = true; ThreadHelper.thread = new Thread((ThreadStart) delegate { ThreadHelper.Run(); }); ThreadHelper.thread.Name = command.HandledBY; ThreadHelper.thread.IsBackground = true; ThreadHelper.thread.Start(); CollectionHelper.ThreadCnt = CollectionHelper.ThreadCnt + 1; } } //ThreadHelper.CmdId = ThreadHelper.CmdId + 1; //ulong rid = (command.IsUI == false ? (ulong)(priority * 100000000) : (ulong)(priority * 10000)) + ThreadHelper.CmdId; //lock (ThreadHelper.locker) //{ // if (command.IsUI == false) // NCSys.BackgroundCount = NCSys.BackgroundCount + 1; // ThreadHelper.ThreadCmd.Add(rid, command); //} ////if (isAdd == true)//启动线程 //if (ThreadHelper.threadRun == false && command.IsUI == true) //{ // ThreadHelper.threadRun = true; // Thread th = new Thread((ThreadStart)delegate { ThreadHelper.Run(); }); // th.Name = command.HandledBY + "Th"; // th.IsBackground = true; // th.Start(); // CollectionHelper.ThreadCnt = CollectionHelper.ThreadCnt + 1; //} } } catch (Exception ex) { FileLog.Error("点:" + command.LpszDbVarName + "命令," + "队列:" + command.HandledBY + "反复错误次数:" + command.ErrCnt + "新增错误:" + ex.Message + ex.StackTrace); } return(ThreadHelper); }
/// <summary> /// 增加告警到数据库 /// </summary> /// <param name="lpszDbVarName"></param> /// <param name="lpszVal"></param> /// <param name="lpszdateTime"></param> /// <param name="errCode"></param> /// <param name="errTxt"></param> public static void DbAddAlarm(CommandVModel command, string lpszVal, DateTime lpszdateTime, AlarmType errCode, string errTxt, bool isSend) { DataProcessBLL bll = new DataProcessBLL(command.Ledger, Config.Uid); StringBuilder strContent = new StringBuilder(); DataTable dtSource = bll.GetMapInfo(command.LpszDbVarName); int ccc = dtSource.Rows.Count; if (ccc == 0) { FileLog.WriteLog("告警模块变量:" + command.LpszDbVarName + "不存在映射表中"); return; } if (ccc > 1) { FileLog.WriteLog("告警模块变量:" + command.LpszDbVarName + "在映射表中存在多个"); return; } int module_id = CommFunc.ConvertDBNullToInt32(dtSource.Rows[0]["Module_id"]); int co_id = CommFunc.ConvertDBNullToInt32(dtSource.Rows[0]["Co_id"]); int fun_id = CommFunc.ConvertDBNullToInt32(dtSource.Rows[0]["Fun_id"]); string moduleAddr = CommFunc.ConvertDBNullToString(dtSource.Rows[0]["ModuleAddr"]); string funType = CommFunc.ConvertDBNullToString(dtSource.Rows[0]["FunType"]); if (funType.Equals(V0Fun.LeakAlarm.ToString())) { if (lpszVal.Substring(0, 12).Contains("1")) { int cnt = 0; foreach (char c in lpszVal.Substring(0, 12).ToCharArray()) { string content = ""; ++cnt; if (c.ToString().Equals("1")) { if (cnt <= 8) { content = "漏电流" + cnt + "发生告警"; } else { content = "温度" + (cnt - 8) + "发生告警"; } } if (!string.IsNullOrEmpty(content)) { if (!string.IsNullOrEmpty(strContent.ToString())) { strContent.Append(";"); } strContent.Append(content); } } } } else { string content = ""; System.Reflection.FieldInfo info = typeof(V0Fun).GetField(funType); if (info != null) { var obj = info.GetCustomAttributes(typeof(DisplayAttribute), false); if (obj != null) { foreach (DisplayAttribute md in obj) { content = md.Name; } } } strContent.Append(content); } long log_id = bll.AddAlarm(co_id, module_id, moduleAddr, errCode.ToString(), fun_id, strContent.ToString(), lpszVal, lpszdateTime, (int)errCode, errTxt); if (isSend == true) {/*发送告警*/ SendHd(command.Ledger, log_id, errCode, HdType.AL_Sms, errTxt, module_id); SendHd(command.Ledger, log_id, errCode, HdType.AL_Email, errTxt, module_id); } }
private void AutoARM()//DataTable dtSource { DateTime? dtStart = null; DateTime? dtIsWrDate = null; DataTable dtSource = null; while (true) { Thread.Sleep(1000); //////////// if (NCSys.IsRun == false) { break; /*网关未启动*/ } bool running = (dtStart == null || DateTime.Now > dtStart.Value.AddMinutes(60)) ? true : false; /*15分钟没有运行过立刻再运行一次*/ if (NCSys.BackgroundCount > 0 && running == false) { continue; /*没有后台命令时才循环再发 /*防止过多的命令,导致内存不足*/ } if (NCSys.UIResult.Count >= 15000) { /*没有后台命令时才循环再发 /*防止过多的命令,导致内存不足*/ Thread.Sleep(1000 * 60 * 10); /*等10分钟*/ } if (NCSys.UIResult.Count >= 5000) {/*没有后台命令时才循环再发 /*防止过多的命令,导致内存不足*/ continue; } try { int frMd = 0; if (dtSource != null) { foreach (DataRow dr in dtSource.Rows) { frMd = CommFunc.ConvertDBNullToInt32(dr["FrMd"]); CommandVModel cmd = ModelHandler <CommandVModel> .FillModel(dr); this.AddCmd(cmd); } } frMd = frMd == 0 ? 15 : frMd; #region 获取读写的数据 int r1 = dtIsWrDate == null ? 0 : CommFunc.ConvertDBNullToInt32(dtIsWrDate.Value.ToString("yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo)); int r2 = CommFunc.ConvertDBNullToInt32(DateTime.Now.ToString("yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo)); bool isRun = (dtIsWrDate == null) ? true : false; if (isRun == false) { if (DateTime.Now.Hour <= 4) { isRun = (r1 != r2) ? true : false; } } //if (isRun == true && DateTime.Now.Hour <= 4) if (isRun == true && 1 == 2) { DataTable dtWr = bll.GetWrData(); foreach (DataRow dr in dtWr.Rows) { CommandVModel cmd = ModelHandler <CommandVModel> .FillModel(dr); this.AddCmd(cmd); } dtIsWrDate = DateTime.Now; } #endregion #region 更新抄表数据 if (DateTime.Now >= dtUpARM.AddMinutes(frMd)) { dtSource = bll.GetArmData(); dtUpARM = DateTime.Now; } #endregion dtStart = DateTime.Now; } catch (Exception ex) { FileLog.Error("自动抄表命令错误" + ex.Message + ex.StackTrace); } Thread.Sleep(1000 * 15); } }
/// <summary> /// 下发命令 /// </summary> /// <param name="cmd"></param> /// <param name="errCode"></param> /// <param name="errTxt"></param> public long AddCmdLog(CommandVModel cmd, int errCode, string errTxt) { return(dal.AddCmdLog(cmd, errCode, errTxt)); }
private void HandleCommand(CommandVModel command, NetworkResource Network) { if (Network == null) { FileLog.Error("ID:" + command.Module_id + "地址:" + command.ModuleAddr + "采集码:" + command.FunType + "采集ID:" + command.Fun_id + "采集名:" + command.FunName + "网络资源错误"); return; } string msg = ""; byte[] send = null, result = null; string lpszVal = null; AlarmType errCode = AlarmType.Sue; bool rst = false; command.CollectTime = DateTime.Now; DateTime lpszdateTime = DateTime.Now; if (command.FunType.Equals(V0Fun.E.ToString()))/*有功总电能*/ { rst = Drive.ARM(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.RdVal.ToString()))/*当前电表剩余电量*/ { rst = Drive.RdVal(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Ia.ToString()))/*当前电表剩余电量*/ { rst = Drive.GetIa(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Ib.ToString())) { rst = Drive.GetIb(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Ic.ToString())) { rst = Drive.GetIc(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Ua.ToString())) { rst = Drive.GetUa(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Ub.ToString())) { rst = Drive.GetUb(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Uc.ToString())) { rst = Drive.GetUc(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Psum.ToString())) { rst = Drive.GetPsum(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Pa.ToString())) { rst = Drive.GetPa(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Pb.ToString())) { rst = Drive.GetPb(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Pc.ToString())) { rst = Drive.GetPc(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Qsum.ToString())) { rst = Drive.GetQsum(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Qa.ToString())) { rst = Drive.GetQa(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Qb.ToString())) { rst = Drive.GetQb(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Qc.ToString())) { rst = Drive.GetQc(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Pfav.ToString())) { rst = Drive.GetPfav(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.PFa.ToString())) { rst = Drive.GetPFa(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.PFb.ToString())) { rst = Drive.GetPFb(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.PFc.ToString())) { rst = Drive.GetPFc(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Ssr.ToString()) && string.IsNullOrEmpty(command.DataValue))/*拉合闸状态*/ { rst = Drive.Ssr(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Ssr.ToString()) && CommFunc.ConvertDBNullToInt32(command.DataValue) == 0)/*合闸*/ { rst = Drive.Ssr0(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Ssr.ToString()) && CommFunc.ConvertDBNullToInt32(command.DataValue) == 1)/*拉闸*/ { rst = Drive.Ssr1(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Pay.ToString()))/*充值*/ { rst = Drive.Pay(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Refund.ToString()))/*退费*/ { rst = Drive.Refund(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.PaySb1.ToString()))/*充值*/ { rst = Drive.PaySb1(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.PaySb2.ToString()))/*充值*/ { rst = Drive.PaySb2(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.IsPaul.ToString()) && string.IsNullOrEmpty(command.DataValue))/*保电状态*/ { rst = Drive.SetPaul(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.IsPaul.ToString()) && !string.IsNullOrEmpty(command.DataValue))/*保电设置*/ { rst = Drive.SetPaul(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Nrp.ToString()))/*有功功率负荷限值(kW)*/ { rst = Drive.SetNrp(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.McNrp.ToString()))/*恶性负载判定有功功率增量(kW)(瞬时额定功率)*/ { rst = Drive.SetMcNrp(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Wpf.ToString()))/*恶性负载判定无功功率(kW)*/ { rst = Drive.SetWpf(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Uep.ToString()))/*违规用电判定功能的功率下限(kW)*/ { rst = Drive.SetUep(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Miss.ToString()))/*一天内违规断电次数限值(0~255)*/ { rst = Drive.SetMiss(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Rest.ToString()))/*违规断电后重启时间(S)*/ { rst = Drive.SetRest(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Saf.ToString()))/*单次功率增量限值(kW)*/ { rst = Drive.SetSaf(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Avlm.ToString()))/*过流门限值(A)*/ { rst = Drive.SetAvlm(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Phase.ToString()))/*启用移相器监测功率下限(kW)*/ { rst = Drive.SetPhase(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Resist.ToString()))/*设置阻行负载(kW)*/ { rst = Drive.SetResist(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.IsPhase.ToString()))/*移相器(=1启用)*/ { rst = Drive.SetIsPhase(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.IsRelay.ToString()))/*继电器参数(=0电平式A路继电器设置;=1电平式B路继电器设置;=127脉冲式设置)*/ { rst = Drive.SetIsRelay(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.IsStdby.ToString()))/*待机检测=0 不检测 =1 检测*/ { rst = Drive.SetStandby(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.TiVal.ToString()))/*获取定时断送电表*/ { rst = Drive.SetTiVal(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.ToLower().Length == 5 && command.FunType.ToLower().Contains("leak") && CommFunc.ConvertDBNullToInt32(command.FunType.Substring(command.FunType.Length - 1, 1)) > 0)/*漏电流*/ { rst = Drive.GetMeterLeak(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.ToLower().Length == 5 && command.FunType.ToLower().Contains("temp") && CommFunc.ConvertDBNullToInt32(command.FunType.Substring(command.FunType.Length - 1, 1)) > 0)/*漏电流*/ { rst = Drive.GetMeterTemp(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.ToLower().Length == 8 && command.FunType.ToLower().Contains("maxleak") && CommFunc.ConvertDBNullToInt32(command.FunType.Substring(command.FunType.Length - 1, 1)) > 0)/*电流阀值*/ { rst = Drive.SetMaxLeak(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.ToLower().Length == 8 && command.FunType.ToLower().Contains("maxtemp") && CommFunc.ConvertDBNullToInt32(command.FunType.Substring(command.FunType.Length - 1, 1)) > 0)/*温度阀值*/ { rst = Drive.SetMaxTemp(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.ToLower().Length >= 8 && command.FunType.ToLower().Contains("StatusLeak".ToLower()) && CommFunc.ConvertDBNullToInt32(command.FunType.Substring(command.FunType.Length - 1, 1)) > 0)/*电流使能*/ { rst = Drive.SetStatusLeak(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.ToLower().Length >= 8 && command.FunType.ToLower().Contains("StatusTemp".ToLower()) && CommFunc.ConvertDBNullToInt32(command.FunType.Substring(command.FunType.Length - 1, 1)) > 0)/*温度使能*/ { rst = Drive.SetStatusTemp(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Contains(V0Fun.LeakAlarm.ToString()))/*漏电设备告警*/ { rst = Drive.GetLeakAlarm(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.Alarm1.ToString()))/*获取报警电量1限值*/ { rst = Drive.GetAlarm1(command, Network, out send, out result, out lpszVal, out errCode, out msg); } else if (command.FunType.Equals(V0Fun.EventWpf.ToString()))/*记录恶性负载事件*/ { #region 记录恶性负载事件 Dictionary <string, string> list = new Dictionary <string, string>(); rst = Drive.GetEventWpf(command, Network, out send, out result, out list, out errCode, out msg); if (rst == true) { MalignantVModel mv = new MalignantVModel(); mv.CTime = new DateTime(1900, 1, 1); foreach (var item in list) { if (item.Key.Equals("CTime")) { mv.CTime = CommFunc.ConvertDBNullToDateTime(item.Value); } else if (item.Key.Equals("SsrS")) { mv.Status = item.Value; } else if (item.Key.Equals("Ua")) { mv.Ua = CommFunc.ConvertDBNullToDecimal(item.Value); } else if (item.Key.Equals("Ia")) { mv.Ia = CommFunc.ConvertDBNullToDecimal(item.Value); } else if (item.Key.Equals("Psum1")) { mv.Psum1 = CommFunc.ConvertDBNullToDecimal(item.Value); } else if (item.Key.Equals("Psum2")) { mv.Psum2 = CommFunc.ConvertDBNullToDecimal(item.Value); } else if (item.Key.Equals("Psum3")) { mv.Psum3 = CommFunc.ConvertDBNullToDecimal(item.Value); } else if (item.Key.Equals("Psum4")) { mv.Psum4 = CommFunc.ConvertDBNullToDecimal(item.Value); } } lpszVal = null; lpszdateTime = mv.CTime; if (mv.CTime.Year != 1900) { lpszVal = JsonHelper.Serialize(mv); lpszdateTime = mv.CTime; } } #endregion } else { errCode = AlarmType.Err_Nd; } try { DataProcess.YdProcess.Helper.YdDriveSaveCollectCache(command, lpszVal, lpszdateTime, 0, rst, errCode, msg, send, result); } catch (Exception ex) { FileLog.Error("处理ID号:" + this.HandledBY + "处理采集数据错误:" + ex.Message + ex.StackTrace); } }
/// <summary> /// 保存采集项数据 /// </summary> /// </summary> /// <param name="command">下发的命令</param> /// <param name="lpszVal">采集值</param> /// <param name="lpszdateTime">采集时间</param> /// <param name="rst">采集结果</param> /// <param name="errCode">采集错误码</param> /// <param name="errTxt">采集错误信息</param> internal static void YdDriveSaveCollectCache(CommandVModel command, string lpszVal, DateTime lpszdateTime, UInt32 dwUserData, bool rst, AlarmType errCode, string errTxt, byte[] send, byte[] result) { AlarmCollect collectInfo = new AlarmCollect() { cmd = command, lpszVal = lpszVal, lpszdateTime = lpszdateTime, errCode = errCode, errTxt = errTxt }; string msg = errTxt; CollectVModel collect = null; SysPro pro; NCSys.Pro.TryGetValue(command.Ledger, out pro); if (pro == null) { FileLog.Error("YdDriveSaveCollectCache模块没有还未有本项目:" + command.Ledger); return; } //if (command.LpszDbVarName == ("5419.166610222091.EventWpf")) //{ // FileLog.Error("YdDriveSaveCollectCache:" + command.LpszDbVarName); //} if (rst == false || command.FunType.Equals(V0Fun.EventWpf.ToString()) || command.Log_id != 0 || command.IsNDb == true || (command.Action == 1 && command.Fun_id != 0)) { if (rst == false && (errCode == AlarmType.Err_Tx || errCode == AlarmType.Err_Sb)) { collectInfo.isSend = 0; } bool isAddQueue = true; if (rst == false && errCode == AlarmType.Err_Tx) {/*铜通讯错误*//*通讯异常,需要发告警,并且距离上次30分钟还是连接不上出告警*/ string mkey = pro.ProjectKey + command.LpszDbVarName; collect = null; NCSys.Result.TryGetValue(mkey, out collect); //DateTime collectTime = collect == null ? Helper.LoadTime : collect.RstVar.lpszdateTime; //if (collectTime.AddMinutes(30) > lpszdateTime) //{/*不加入队列*/ // isAddQueue = false; //} } if (isAddQueue == true) { if (command.FunType.Equals(V0Fun.Ssr.ToString()) && !string.IsNullOrEmpty(command.DataValue)) { //if (command.Log_id == 0 && command.IsNDb != true) // FileLog.WriteLog("没有入库拉合闸命令:" + JsonHelper.Serialize(command)); //if (command.Log_id > 0) // FileLog.WriteLog("入库拉合闸命令2:" + JsonHelper.Serialize(command)); } //NCSys.AlarmCollect.Enqueue(collectInfo); /*更新数据库*/ } } if (rst == true) { RefreshCached(command, lpszVal, errCode);/*更新缓存,需优化*/ //if (command.Fun_id != 0 && (command.Action == 0 || command.Action == 3)) if (command.Fun_id != 0 && (command.Action != 1)) {/*写、跟事件不存缓存*/ //SaveCollectCache(command.Ledger,command.LpszDbVarName, lpszVal, lpszdateTime, dwUserData, ref command); SaveCollectCache(command.Ledger, command.LpszDbVarName, lpszVal, lpszdateTime, dwUserData); } } else { if (lpszdateTime.Year > 2000) { /*有采集信息*//*如果命令错误重新下发命令*/ if (CommFunc.ConvertDBNullToInt32(command.ErrCnt) < 3) { CommandVModel restCmd = command.Clone(); restCmd.ErrCnt = CommFunc.ConvertDBNullToInt32(restCmd.ErrCnt) + 1; DataProcess.YdDrive.Collection.CollectionHelper.Instance(restCmd); } } } //if (command.Log_id != 0 || command.IsNDb == true || (command.Action == 1 && command.Fun_id != 0)) if (command.FunType.Equals(V0Fun.Ssr.ToString()) && !string.IsNullOrEmpty(command.DataValue)) {/*立刻更新*/ //ZpAlarm.DbCmdRefresh(command, lpszVal, lpszdateTime, errCode, errTxt); //if (command.Log_id == 0 && command.IsNDb != true) // FileLog.WriteLog("没有入库拉合闸命令1:" + JsonHelper.Serialize(command)); //if (command.Log_id >= 0 && command.IsNDb != true) // FileLog.WriteLog("没有入库拉合闸命令2:" + JsonHelper.Serialize(command)); } AddUIResult(command.Ledger, command.Module_id, command.ModuleAddr, command.HandledBY, command.ModuleType, command.FunType, command.FunName, command.ModuleAddr, lpszVal, lpszdateTime, rst, send, result, errCode, msg, command.IsUI, command.IsNDb);/*输出到界面信息*/ }
/// <summary> /// 更新缓存信息 /// </summary> /// <param name="command"></param> /// <param name="errCode"></param> /// <param name="errTxt"></param> private static void RefreshCached(CommandVModel command, string lpszVal, AlarmType errCode) { if (errCode != AlarmType.Sue) { return; /*不成功则返回;成功则继续往下操作*/ } if (command.Action == 1 && command.Fun_id != 0) {/*记录写的值*/ new DataProcessBLL(command.Ledger, Config.Uid).UpdataMap(command.Module_id, command.Fun_id, lpszVal); } SysPro pro = null; NCSys.Pro.TryGetValue(command.Ledger, out pro); if (pro == null) { return; } string ss1 = pro.ProjectKey + command.Module_id.ToString() + "." + command.ModuleAddr + "."; if (command.FunType.Equals(V0Fun.Ssr.ToString()) && !string.IsNullOrEmpty(command.DataValue)) { // 立刻更新拉合闸 string key = ss1 + V0Fun.Ssr.ToString(); RstVar rst = MemcachedMgr.GetVal <RstVar>(key); if (rst == null) { return; } rst.lpszVal = command.DataValue; rst.lpszdateTime = command.CollectTime.Value; rst.dwUserData = 0; /////////////// CollectVModel collect = null; if (NCSys.Result.TryGetValue(key, out collect) == true) { //collect.RstVar = rst; //collect.IsDb = true; //collect.IsRn = command.IsRn; } /////////////// MemcachedMgr.SetVal(key, rst); } if (command.FunType.Equals(V0Fun.Pay.ToString()) || command.FunType.Equals(V0Fun.Refund.ToString())) { // 充值;退费 string key = ss1 + V0Fun.RdVal.ToString(); RstVar rst = MemcachedMgr.GetVal <RstVar>(key); if (rst == null) { return; } decimal lpszPay = CommFunc.ConvertDBNullToDecimal(rst.lpszVal); if (command.FunType.Equals(V0Fun.Pay.ToString())) { lpszPay = lpszPay + CommFunc.ConvertDBNullToDecimal(command.DataValue); } else if (command.FunType.Equals(V0Fun.Refund.ToString())) { lpszPay = lpszPay - CommFunc.ConvertDBNullToDecimal(command.DataValue); } rst.lpszVal = lpszPay.ToString(); rst.lpszdateTime = command.CollectTime.Value; rst.dwUserData = 0; /////////////// CollectVModel collect = null; if (NCSys.Result.TryGetValue(key, out collect) == true) { //collect.RstVar = rst; //collect.IsDb = true; } /////////////// MemcachedMgr.SetVal(key, rst); #region 2018.07.02 //if (command.ModuleType.Equals("YD-ZB20-4PL2-SB")) //{ // key = ss1 + V0Fun.E.ToString(); // collect = null; // if (NCSys.Result.TryGetValue(key, out collect) == true) // collect.IsDb = true; //} #endregion } }
/// <summary> /// 线程运行 /// </summary> internal void Run() { Thread.Sleep(100); DateTime runningTime = DateTime.Now;/*有命令的运行时间*/ #region 打开连接 bool isOnLine = false; try { isOnLine = this.Network.Open(3);/*打开端口*/ } catch { FileLog.WriteLog("处理ID号:" + this.HandledBY + " 方式:" + this.Transfer + " 打开失败"); isOnLine = false; } #endregion bool restCache = true; int errTime = 0; while (true) { Thread.Sleep(500); CommandVModel cmd = null; #region 获取一个命令 try { lock (this.locker) { foreach (KeyValuePair <ulong, CommandVModel> item in this.ThreadCmd.Take(1)) { cmd = item.Value; ThreadCmd.Remove(item.Key);//移除命令 } } lock (CollectionHelper.wholeLocker) { if (cmd != null && cmd.IsUI == false) { NCSys.BackgroundCount = NCSys.BackgroundCount - 1; } } if (cmd != null && isOnLine == false) {/*连接不上*/ //if (restCache==true) // Helper.SaveCollectCache(cmd, null, new DateTime(1900, 1, 1), false, AlarmType.Err_Tx, "连接错误", null, null); if (restCache == true) { DataProcess.YdProcess.Helper.YdDriveSaveCollectCache(cmd, "", DateTime.Now, 0, false, AlarmType.Err_Tx, "连接错误", null, null); } restCache = false; } errTime = 0; } catch (Exception ex) { if (errTime <= 5) { FileLog.Error("读取内存数据错误:" + ex.Message + ex.StackTrace); } ++errTime; } #endregion ////////////////////////////////////////////////////////////////////// #region 处理命令 if (cmd != null && isOnLine == true) { if (NCSys.IsRun == true) { try { this.HandleCommand(cmd, this.Network); errTime = 0; } catch (Exception ex) { if (errTime <= 5) { FileLog.Error("ID:" + cmd.Module_id + "地址:" + cmd.ModuleAddr + "采集码:" + cmd.FunType + "采集ID:" + cmd.Fun_id + "采集名:" + cmd.FunName + "命令运行错误:" + ex.Message + ex.StackTrace); } ++errTime; } } } #endregion #region 移除资源 int cnt = this.ThreadCmd.Count; if (cnt == 0) { Thread.Sleep(3000);/*等3秒*/ lock (CollectionHelper.wholeLocker) { cnt = this.ThreadCmd.Count; if (cnt == 0) { try { #region 移除资源 CollectionHelper.ThreadCnt = CollectionHelper.ThreadCnt - 1; CollectionHelper del = null; this.Network.RemoveInstance();/*关闭端口并移除出内存*/ int i = 0; while (++i <= 5) { if (CollectionHelper.instance.TryRemove(this.HandledBY, out del) == false) { FileLog.WriteLog("处理ID号:" + this.HandledBY + "第" + i + "次移除命令失败"); Thread.Sleep(1000); } else { //FileLog.WriteLog("处理ID号:" + this.HandledBY + "第" + i + "移除命令成功"); break; } } #endregion } catch (Exception ex) { FileLog.WriteLog("移除资源错误:" + ex.Message + ex.StackTrace); } break; } } } #endregion } }
private bool YdDbMidRun() { DataTable dtSource = bll.GetDk_buy(); if (dtSource == null) { return(false); } foreach (DataRow dr in dtSource.Rows) { long recno = CommFunc.ConvertDBNullToLong(dr["recno"]); int co_id = CommFunc.ConvertDBNullToInt32(dr["FJID"]); decimal payAmt = CommFunc.ConvertDBNullToDecimal(dr["tranamt"]) / 100; DateTime endatatime = CommFunc.ConvertDBNullToDateTime(dr["endatatime"]); if (co_id == 0) { FileLog.WriteLog("中间库房间ID号为空"); continue; } if (payAmt == 0) { FileLog.WriteLog("中间库充值金额为0"); continue; } DataTable dtPay = bll.GetPayInfo(co_id); if (dtPay.Rows.Count == 0) { FileLog.WriteLog("中间库的房间ID:" + co_id + "没有表信息存在"); continue; } if (dtPay.Rows.Count > 1) { FileLog.WriteLog("中间库的房间ID:" + co_id + "存在多个表"); continue; } decimal price = CommFunc.ConvertDBNullToDecimal(dtPay.Rows[0]["Price"]); if (price == 0) { FileLog.WriteLog("中间库的房间ID:" + co_id + "单价不能为零"); continue; } v4_pay_logVModel pay = new v4_pay_logVModel(); pay.Co_id = CommFunc.ConvertDBNullToInt32(dtPay.Rows[0]["Co_id"]); pay.Module_id = CommFunc.ConvertDBNullToInt32(dtPay.Rows[0]["Module_id"]); pay.ModuleAddr = CommFunc.ConvertDBNullToString(dtPay.Rows[0]["ModuleAddr"]);; // CommFunc.ConvertDBNullToString(Request["ModuleAddr"]); pay.Fun_id = CommFunc.ConvertDBNullToInt32(dtPay.Rows[0]["Fun_id"]); pay.PayAmt = payAmt; pay.PayVal = Math.Round(pay.PayAmt / price, 2, MidpointRounding.AwayFromZero); pay.PayType = 1; pay.IsWrong = 0; pay.IsPay = 30; pay.PayStartTime = DateTime.Now; pay.PayEndTime = DateTime.Now; pay.Price = price; bll.YdDbMidAddPayLog(pay, endatatime, recno); string msg = ""; long cmd_log_id = 0; bll.PayVal(pay.Log_id, recno, 1, "", out msg, out cmd_log_id); if (cmd_log_id != 0)//立刻发送命令到网关 { CommandVModel cmd = bll.GetYdToGwOfSendCmd(cmd_log_id); DataProcess.YdDrive.Collection.CollectionHelper.Instance(cmd); } DataTable dtMM = bll.GetYdPostPayInMdOnList(pay.Module_id); decimal chrgVal = 0, useVal = 0, rdVal = 0, syZsVal = 0; if (dtMM.Rows.Count > 0) { chrgVal = CommFunc.ConvertDBNullToDecimal(dtMM.Rows[0]["ChrgVal"]); useVal = CommFunc.ConvertDBNullToDecimal(dtMM.Rows[0]["UseVal"]); rdVal = CommFunc.ConvertDBNullToDecimal(dtMM.Rows[0]["RdVal"]); syZsVal = CommFunc.ConvertDBNullToDecimal(dtMM.Rows[0]["SyZsVal"]); } //判断是否拉闸 YDS6000.BLL.Exp.Syscont.ParameterBLL sysBll = new YDS6000.BLL.Exp.Syscont.ParameterBLL(0, 1); var dt = sysBll.GetAlarmCfg(); if (dt != null && dt.Rows.Count > 0) { decimal odValue = CommFunc.ConvertDBNullToDecimal(dt.Rows[0]["CfValue"]); int isClosed = CommFunc.ConvertDBNullToInt32(dt.Rows[0]["Rule"]); if (isClosed == 1) { if (rdVal <= odValue) { this.YdBatchControlOfSsr(pay.Module_id, "Ssr", "1", 30); } else { this.YdBatchControlOfSsr(pay.Module_id, "Ssr", "0", 30); } } } } return(true); }