void TestConnectionTask() { while (true) { SecureDBEntities1 db = new SecureDBEntities1(); Ping ping = new Ping(); var q = db.tblNVRConfig; foreach (tblNVRConfig nvr in q) { try { if (ping.Send(nvr.IP).Status == IPStatus.Success) { if (nvr.Comm_state != 1) nvr.Comm_state = 1; } else { if (nvr.Comm_state != 0) nvr.Comm_state = 0; } } catch { ;} } try { db.SaveChanges(); } catch { ;} System.Threading.Thread.Sleep(60000); } }
void DoRepeatSched() { using (SecureDBEntities1 db = new SecureDBEntities1()) { tblSchConfig sch = db.tblSchConfig.Where(n => n.SchID == config.SchID).FirstOrDefault(); if (sch.NextStartTime == null || ((DateTime)sch.NextStartTime).DayOfWeek != DateTime.Now.DayOfWeek) { try { ReportAutoPrint.ReportServer.PrintRoport(config.ReportID); Console.WriteLine("invoke rptid:" + config.ReportID); tblSchLog log = new tblSchLog() { TimeStamp = DateTime.Now, ReportID = sch.ReportID, SchID = sch.SchID, Result = true }; db.tblSchLog.Add(log); // db.SaveChanges(); } catch (Exception ex) { tblSchLog log = new tblSchLog() { TimeStamp = DateTime.Now, ReportID = sch.ReportID, SchID = sch.SchID, Result = false ,Memo=ex.Message}; db.tblSchLog.Add(log); // db.SaveChanges(); } Console.WriteLine("Do report:" + config.ReportID); sch.NextStartTime = DateTime.Now; db.SaveChanges(); } } }
public void CheckSchedule() { if (config.SchType == 0) //執行一次 { if (DateTime.Now >= config.StartTime && config.NextStartTime==null) { config.NextStartTime = config.StartTime; SecureServer.SecureDBEntities1 db = new SecureDBEntities1(); tblSchConfig sch= db.tblSchConfig.Where(n => n.SchID == config.SchID).FirstOrDefault(); if (sch != null) { sch.NextStartTime = config.NextStartTime; // sch.Enable = false; db.SaveChanges(); } try { ReportAutoPrint.ReportServer.PrintRoport(config.ReportID); Console.WriteLine("invoke rptid:" + config.ReportID); tblSchLog log = new tblSchLog() { TimeStamp = DateTime.Now, ReportID = sch.ReportID, SchID = sch.SchID, Result = true }; db.tblSchLog.Add(log); db.SaveChanges(); } catch (Exception ex) { tblSchLog log = new tblSchLog() { TimeStamp = DateTime.Now, ReportID = sch.ReportID, SchID = sch.SchID, Result = false,Memo=ex.Message }; db.tblSchLog.Add(log); db.SaveChanges(); } db.Dispose(); } } else if (config.SchType == 1)//重複執行 { DayOfWeek dayofweek = DateTime.Now.DayOfWeek; double diffmin=DiffMin( ); if(diffmin >=0 && diffmin < 10) DoRepeatSched(); return; if(dayofweek== DayOfWeek.Sunday && config.Week1==true && diffmin >=0 && diffmin < 10) DoRepeatSched(); else if(dayofweek== DayOfWeek.Monday && config.Week2==true && diffmin >=0 && diffmin < 10) DoRepeatSched(); else if(dayofweek== DayOfWeek.Tuesday && config.Week3==true && diffmin >=0 && diffmin < 10) DoRepeatSched(); else if (dayofweek == DayOfWeek.Wednesday&& config.Week4 == true && diffmin >= 0 && diffmin < 10) DoRepeatSched(); else if (dayofweek == DayOfWeek.Thursday && config.Week5 == true && diffmin >= 0 && diffmin < 10) DoRepeatSched(); else if (dayofweek == DayOfWeek.Friday && config.Week6 == true && diffmin >= 0 && diffmin < 10) DoRepeatSched(); else if (dayofweek == DayOfWeek.Saturday && config.Week7 == true && diffmin >= 0 && diffmin < 10) DoRepeatSched(); } }
void CheckDataChange(byte[] temp) { string description=""; if (data[0] == temp[0] && data[1] == temp[1]) { // Console.WriteLine("沒變化!"); return; } Console.WriteLine(this.PDName); SecureDBEntities1 db = new SecureDBEntities1(); tblPDConfig tblpd= db.tblPDConfig.Where(n=>n.PDName==this.PDName).FirstOrDefault(); if(tblpd==null) return; PDStatus d= new PDStatus(data); PDStatus t = new PDStatus(temp); if(tblPDConfig.R0!=-1) if (d.R0 != t.R0 ) { tblpd.R0 = t.R0; if (t.R0 == 0) //normal { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "R0", Timestamp = DateTime.Now, PDName = this.PDName, Status = 1}; db.tblPDAlarmLog.Add(log); } else //abnormal { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "R0", Timestamp = DateTime.Now, PDName = this.PDName, Status = 0, Memo = GetPDStatusDescription(0, tblpd), UserMemo=tblpd.Memo }; db.tblPDAlarmLog.Add(log); description += "R0 "+GetPDStatusDescription(0, tblpd); } } if (tblPDConfig.S0 != -1) if (d.S0 != t.S0) { tblpd.S0 = t.S0; if (t.S0 == 0) { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "S0", Timestamp = DateTime.Now, PDName = this.PDName, Status = 1 }; db.tblPDAlarmLog.Add(log); } else { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "S0", Timestamp = DateTime.Now, PDName = this.PDName, Status = 0, Memo = GetPDStatusDescription(0, tblpd), UserMemo = tblpd.Memo }; db.tblPDAlarmLog.Add(log); description += "S0 " + GetPDStatusDescription(0, tblpd); ; } } if (tblPDConfig.T0 != -1) if (d.T0 != t.T0 ) { tblpd.T0 =t.T0; if (t.T0 == 0) { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "T0", Timestamp = DateTime.Now, PDName = this.PDName, Status = 1 }; db.tblPDAlarmLog.Add(log); } else { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "T0", Timestamp = DateTime.Now, PDName = this.PDName, Status = 0, Memo = GetPDStatusDescription(0, tblpd), UserMemo = tblpd.Memo }; db.tblPDAlarmLog.Add(log); description += "T0 " + GetPDStatusDescription(0, tblpd); ; } } if (d.R1 != t.R1) { tblpd.R1 = t.R1; if (t.R1 == 0) { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "R1", Timestamp = DateTime.Now, PDName = this.PDName, Status = 1 }; db.tblPDAlarmLog.Add(log); } else { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "R1", Timestamp = DateTime.Now, PDName = this.PDName, Status = 0, Memo = GetPDStatusDescription(1, tblpd), UserMemo = tblpd.Memo }; db.tblPDAlarmLog.Add(log); description += "R1 "+GetPDStatusDescription(1, tblpd); } } if (d.S1 != t.S1) { tblpd.S1 = t.S1; if (t.S1 == 0) { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "S1", Timestamp = DateTime.Now, PDName = this.PDName, Status = 1 }; db.tblPDAlarmLog.Add(log); } else { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "S1", Timestamp = DateTime.Now, PDName = this.PDName, Status = 0, Memo = GetPDStatusDescription(1, tblpd), UserMemo = tblpd.Memo }; db.tblPDAlarmLog.Add(log); description += "S1 " + GetPDStatusDescription(1, tblpd); } } if (d.T1 != t.T1) { tblpd.T1 = t.T1; if (t.T1 == 0) { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "T1", Timestamp = DateTime.Now, PDName = this.PDName, Status = 1 }; db.tblPDAlarmLog.Add(log); } else { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "T1", Timestamp = DateTime.Now, PDName = this.PDName, Status = 0, Memo = GetPDStatusDescription(1, tblpd), UserMemo = tblpd.Memo }; db.tblPDAlarmLog.Add(log); description += "T1 " + GetPDStatusDescription(1, tblpd); } } if(tblPDConfig.L0!=-1) if (d.L0 != t.L0 ) { tblpd.L0 = t.L0; if (t.L0 == 0) { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "L0", Timestamp = DateTime.Now, PDName = this.PDName,Status=1 }; db.tblPDAlarmLog.Add(log); } else { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "L0", Timestamp = DateTime.Now, PDName = this.PDName, Status = 0, UserMemo = tblpd.Memo }; db.tblPDAlarmLog.Add(log); description += "L0 "; } } if (tblPDConfig.L1 != -1) if (d.L1 != t.L1) { tblpd.L1 = t.L1; if (t.L1 == 0) { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "L1", Timestamp = DateTime.Now, PDName = this.PDName,Status=1 }; db.tblPDAlarmLog.Add(log); } else { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "L1", Timestamp = DateTime.Now, PDName = this.PDName, Status = 0, UserMemo = tblpd.Memo }; db.tblPDAlarmLog.Add(log); description += "L1 "; } } if (tblPDConfig.L2 != -1) if (d.L2 != t.L2) { tblpd.L2 = t.L2; if (t.L2 == 0) { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "L2", Timestamp = DateTime.Now, PDName = this.PDName, Status = 1 }; db.tblPDAlarmLog.Add(log); } else { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "L2", Timestamp = DateTime.Now, PDName = this.PDName, Status = 0, UserMemo = tblpd.Memo }; db.tblPDAlarmLog.Add(log); description += "L2 "; } } if (tblPDConfig.L3 != -1) if (d.L3 != t.L3) { tblpd.L3 = t.L3; if (t.L3 == 0) { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "L3", Timestamp = DateTime.Now, PDName = this.PDName, Status = 1 }; db.tblPDAlarmLog.Add(log); } else { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "L3", Timestamp = DateTime.Now, PDName = this.PDName, Status = 0, UserMemo = tblpd.Memo }; db.tblPDAlarmLog.Add(log); description += "L3 "; } } if (tblPDConfig.L4 != -1) if (d.L4 != t.L4) { tblpd.L4 = t.L4; if (t.L4 == 0) { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "L4", Timestamp = DateTime.Now, PDName = this.PDName, Status = 1 }; db.tblPDAlarmLog.Add(log); } else { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "L4", Timestamp = DateTime.Now, PDName = this.PDName, Status = 0, UserMemo = tblpd.Memo }; db.tblPDAlarmLog.Add(log); description += "L4 "; } } if (d.Cabinet != t.Cabinet) { tblpd.Cabinet = t.Cabinet; if (t.Cabinet == 1) //close { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "Cabinet", Timestamp = DateTime.Now, PDName = this.PDName, Memo = "箱門關閉", Status = 1 }; db.tblPDAlarmLog.Add(log); } else //open { tblPDAlarmLog log = new tblPDAlarmLog() { PDItem = "Cabinet", Timestamp = DateTime.Now, PDName = this.PDName, Memo = "箱門開啟", Status = 0 }; db.tblPDAlarmLog.Add(log); if(description !="") description += "異常 " + "箱門開啟 "; else description += "箱門開啟 "; } } if (!description.Contains("箱門") && description != "") description += "異常"; if (description != "") { AlarmData alarmdata = new AlarmData() { TimeStamp = DateTime.Now, AlarmType = AlarmType.PD, ColorString = "Red", Description = description, // PlaneID = sender.PlaneID, IsForkCCTVEvent = false, PlaneName = this.tblPDConfig.PDName//Global.GetPlaneNameByPlaneID(this.PDName) }; try { Program.MyServiceObject.DispatchAlarmEvent(alarmdata); } catch { ;} } // Console.WriteLine("save change"); db.SaveChanges(); db.Dispose(); }
void ReadingTask() { byte[] tempdata = new byte[data.Length]; while (true) { try { if (RTUDevice!=null && RTUDevice.connected != IsConnected) { IsConnected = RTUDevice.connected; using( SecureDBEntities1 db = new SecureDBEntities1()) { tblPDConfig pdc = db.tblPDConfig.Where(n => n.PDName == this.PDName).FirstOrDefault(); if (pdc != null) { pdc.Comm_state = IsConnected ? 1 : 0; db.SaveChanges(); } db.tblPDAlarmLog.Add(new tblPDAlarmLog() { Timestamp = DateTime.Now, PDItem = "Comm_state", PDName = this.PDName, Status = IsConnected ? 1 : 0 , UserMemo=pdc.Memo}); db.SaveChanges(); } } if (RTUDevice != null && RTUDevice.connected) { lock (lockobj) { RTUDevice.ReadDiscreteInputs(1, 0, 0, 12, ref tempdata); if (tempdata == null) { Console.WriteLine(this.PDName + "讀取資料失敗"); reading_fail_cnt++; continue; } else reading_fail_cnt = 0; if ((tblPDConfig.type ?? 1) == 2) //R11 { for (int i = 0; i < tempdata.Length; i++) tempdata[i] = (byte)(~tempdata[i]); System.Collections.BitArray baryD = new System.Collections.BitArray(new byte[2]); System.Collections.BitArray baryS = new System.Collections.BitArray(tempdata); baryD.Set(0, baryS.Get(4)); //r0 baryD.Set(1, baryS.Get(5)); //s0 baryD.Set(2, baryS.Get(6)); // t0 baryD.Set(3, baryS.Get(0)); //r1 baryD.Set(4, baryS.Get(1)); //s1 baryD.Set(5, baryS.Get(2)); //t1 baryD.Set(6, baryS.Get(7)); // L0 baryD.Set(7, baryS.Get(8)); // L1 baryD.Set(8, baryS.Get(9)); // L2 baryD.Set(9, baryS.Get(10)); // L3 baryD.Set(10, baryS.Get(11)); // L4 baryD.Set(11, baryS.Get(3)); // cab baryD.CopyTo(tempdata, 0); } else if((tblPDConfig.type ?? 1) == 7) { //for (int i = 0; i < tempdata.Length; i++) // tempdata[i] = (byte)(~tempdata[i]); System.Collections.BitArray baryD = new System.Collections.BitArray(new byte[2]); System.Collections.BitArray baryS = new System.Collections.BitArray(tempdata); baryD.Set(0, baryS.Get(4)); //r0 baryD.Set(1, baryS.Get(5)); //s0 baryD.Set(2, baryS.Get(6)); // t0 baryD.Set(3, baryS.Get(0)); //r1 baryD.Set(4, baryS.Get(1)); //s1 baryD.Set(5, baryS.Get(2)); //t1 baryD.Set(6, baryS.Get(7)); // L0 baryD.Set(7, baryS.Get(8)); // L1 baryD.Set(8, baryS.Get(9)); // L2 baryD.Set(9, baryS.Get(10)); // L3 baryD.Set(10, baryS.Get(11)); // L4 baryD.Set(11, !baryS.Get(3)); // cab baryD.CopyTo(tempdata, 0); } else if ((tblPDConfig.type ?? 1) == 5) //R13 { for (int i = 0; i < tempdata.Length; i++) tempdata[i] = (byte)(~tempdata[i]); System.Collections.BitArray baryD = new System.Collections.BitArray(new byte[2]); System.Collections.BitArray baryS = new System.Collections.BitArray(tempdata); baryD.Set(0, baryS.Get(4)); //r0 baryD.Set(1, baryS.Get(5)); //s0 baryD.Set(2, baryS.Get(6)); // t0 baryD.Set(3, baryS.Get(0)); //r1 baryD.Set(4, baryS.Get(1)); //s1 baryD.Set(5, baryS.Get(2)); //t1 baryD.Set(6, baryS.Get(7)); // L0 baryD.Set(7, baryS.Get(8)); // L1 baryD.Set(8, baryS.Get(9)); // L2 baryD.Set(9, baryS.Get(10)); // L3 baryD.Set(10, baryS.Get(11)); // L4 baryD.Set(11, !baryS.Get(3)); // cab baryD.CopyTo(tempdata, 0); } else if ((tblPDConfig.type ?? 1) == 3) //R12 { for (int i = 0; i < tempdata.Length; i++) tempdata[i] = (byte)(~tempdata[i]); System.Collections.BitArray baryD = new System.Collections.BitArray(new byte[2]); System.Collections.BitArray baryS = new System.Collections.BitArray(tempdata); baryD.Set(0, baryS.Get(6)); //r0 baryD.Set(1, baryS.Get(7)); //s0 baryD.Set(2, baryS.Get(8)); // t0 baryD.Set(3, baryS.Get(0)); //r1 baryD.Set(4, baryS.Get(1)); //s1 baryD.Set(5, baryS.Get(2)); //t1 baryD.Set(6, baryS.Get(9)); // L0 baryD.Set(7, baryS.Get(10)); // L1 baryD.Set(8, baryS.Get(11)); // L2 //baryD.Set(9, 0baryS.Get(10)); // L3 //baryD.Set(10,0 baryS.Get(11)); // L4 baryD.Set(11, baryS.Get(3) && baryS.Get(4) && baryS.Get(5)); // cab baryD.CopyTo(tempdata, 0); } else if ((tblPDConfig.type ?? 1) == 4) //R23 T74 { System.Collections.BitArray baryD = new System.Collections.BitArray(tempdata); baryD.Set(11,! baryD.Get(11)); baryD.CopyTo(tempdata, 0); } else if ((tblPDConfig.type ?? 1) == 6) { System.Collections.BitArray bard = new System.Collections.BitArray(tempdata); for (int i = 0; i < 11; i++) bard.Set(i, !bard.Get(i)); bard.CopyTo(tempdata, 0); } // RTUDevice.ReadHoldingRegister((ushort)this.DevID, (byte)255, (ushort)(StartAddress - 1), this.RegisterLength, ref tempdata); if (tempdata != null && tempdata.Length != 0) { CheckDataChange(tempdata); System.Array.Copy(tempdata, data, 2); //for (int i = 0; i < tempdata.Length; i++) //{ // data[i] = tempdata[i]; //} } } } } catch (Exception ex) { Console.WriteLine(this.PDName+","+((tempdata==null)?"突然斷線":ex.Message) + "," + ex.StackTrace); } finally { System.Threading.Thread.Sleep(1000); } } }
void rtu_OnCommStateChanged(ModbusTCP.IRTU sender, int comm_state) { SecureDBEntities1 db = new SecureDBEntities1(); tblControllerConfig ctl= db.tblControllerConfig.Where(n => n.ControlID == sender.ControlID).FirstOrDefault(); if (ctl != null) { ctl.Comm_state = comm_state; db.tblDeviceStateLog.Add( new tblDeviceStateLog() { TypeID=10, TypeCode=(short)comm_state, TimeStamp=DateTime.Now, ControlID=sender.ControlID } ); db.SaveChanges(); } db.Dispose(); //throw new NotImplementedException(); }
//void DownloadSuperPassword() //{ // SecureDBEntities1 db = new SecureDBEntities1(); // DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); // tblPasswordEveryDayDifference tbl = (from n in db.tblPasswordEveryDayDifference // where n.Timestamp // == dt // select n).FirstOrDefault(); // if (tbl == null) // return; // foreach (CardReader reader in dictCardReaders.Values) // { // try // { // if (reader.IsConnected) // { // reader.SetSuperOpenDoorPassword(int.Parse(tbl.DoorPassword)); // Console.WriteLine(reader.ControllerID + "設定每日開門密碼成功"); // db.tblEngineRoomLog.Add( // new tblEngineRoomLog() // { // ControlID = reader.ControllerID, // Result = 0, // StartTime = DateTime.Now, // ABA = "0", // TypeID = 8, // TypeCode = 40, // Memo = tbl.DoorPassword // } // ); // } // } // catch (Exception ex) // { // Console.WriteLine(ex.Message + "," + ex.StackTrace); // } // } // db.SaveChanges(); //} void CheckAndGenerateDailySuperPassword() { #if R23 return; #endif bool haschanges = false; using (SecureDBEntities1 db = new SecureDBEntities1()) { Random rnd = new Random(); var q = from n in db.tblControllerConfig where n.ControlType == 1 && n.IsEnable == true select n; DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0); foreach (tblControllerConfig control in q) { tblPasswordEveryDayDifference tbl = (from n in db.tblPasswordEveryDayDifference where n.Timestamp == dt && n.ControlID == control.ControlID select n).FirstOrDefault(); if (tbl == null) { int passwd = rnd.Next(0, 10000); string pwdString = passwd.ToString("0000"); // tblERDoorPassword tbl1 = //foreach (CardReader reader in dictCardReaders.Values) //{ try { ICardReader reader = dictCardReaders[control.ControlID]; reader.SetSuperOpenDoorPassword(passwd); Console.WriteLine(reader.ControllerID + "設定每日開門密碼成功"); db.tblPasswordEveryDayDifference.Add(new tblPasswordEveryDayDifference() { Timestamp = dt, DoorPassword = passwd.ToString("0000"), ControlID = control.ControlID }); db.tblEngineRoomLog.Add( new tblEngineRoomLog() { ControlID = reader.ControllerID, Result = 0, StartTime = DateTime.Now, ABA = "0", TypeID = 8, TypeCode = 40, Memo = pwdString } ); haschanges = true; } catch (Exception ex) { Console.WriteLine(ex.Message + "," + ex.StackTrace); } //} } } if (haschanges) { Console.WriteLine("開始寫入每日密碼!"); db.SaveChanges(); } } }
void DownloadSuperPassword(string readerID) { SecureDBEntities1 db = new SecureDBEntities1(); DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); tblERDoorPassword tbl = (from n in db.tblERDoorPassword where n.Timestamp == dt select n).FirstOrDefault(); if (tbl == null) return; dictCardReaders[readerID].SetSuperOpenDoorPassword(int.Parse(tbl.DoorPassword)); db.tblEngineRoomLog.Add( new tblEngineRoomLog() { ControlID = readerID,//reader.ControllerID, Result = 0, StartTime = DateTime.Now, ABA = "0", TypeID = 8, TypeCode = 40, Memo = tbl.DoorPassword } ); db.SaveChanges(); }
//void cardreader_OnAlarmEvent(CardReader reder, AlarmData alarmdata) //{ // if (this.OnAlarmEvent != null) // this.OnAlarmEvent(reder, alarmdata); //} void cardreader_OnDoorEvent(ICardReader reader, DoorEventType enumEventType) { if (this.OnDoorEvent != null) { try { this.OnDoorEvent(reader, enumEventType); } catch { ;} } if (enumEventType == DoorEventType.Connected) { SecureDBEntities1 db = new SecureDBEntities1(); tblControllerConfig config = db.tblControllerConfig.Where(n => n.ControlID == reader.ControllerID).FirstOrDefault(); if (config != null) config.Comm_state = 1; tblEngineRoomLog log = new tblEngineRoomLog() { ControlID = reader.ControllerID, ABA = "0", StartTime = DateTime.Now, TypeID = 8, TypeCode = 31, Result = 1 }; db.tblEngineRoomLog.Add(log); db.SaveChanges(); db.Dispose(); AlarmData data = new AlarmData() { TimeStamp = DateTime.Now, AlarmType = AlarmType.Secure, ColorString = "Green", Description = reader.ControllerID + "復線", PlaneID = reader.PlaneID, IsForkCCTVEvent = false, PlaneName = Global.GetPlaneNameByPlaneID(reader.PlaneID) // CCTVBindingData =cctv.ToBindingData( }; Program.MyServiceObject.DispatchAlarmEvent(data); this.DownloadSuperPassword(reader.ControllerID); } if (enumEventType == DoorEventType.DisConnected) { SecureDBEntities1 db = new SecureDBEntities1(); tblControllerConfig config = db.tblControllerConfig.Where(n => n.ControlID == reader.ControllerID).FirstOrDefault(); if (config != null) config.Comm_state = 0; tblEngineRoomLog log=new tblEngineRoomLog(){ ControlID=reader.ControllerID, ABA="0", StartTime=DateTime.Now, TypeID=8, TypeCode=30, Result=0}; db.tblEngineRoomLog.Add(log); db.SaveChanges(); db.Dispose(); AlarmData data = new AlarmData() { TimeStamp = DateTime.Now, AlarmType = AlarmType.Secure, ColorString = "Red", Description = reader.ControllerID + "斷線警報", PlaneID = reader.PlaneID, IsForkCCTVEvent = false, PlaneName = Global.GetPlaneNameByPlaneID(reader.PlaneID) // CCTVBindingData =cctv.ToBindingData( }; Program.MyServiceObject.DispatchAlarmEvent(data); } }
void cardreader_OnStatusChanged(ICardReader reader, CardReaderEventReport rpt) { try { if (rpt.Status == (int)CardReaderStatusEnum.卡號連續錯誤 && this.EventInvalidCardAlarm || rpt.Status == (int)CardReaderStatusEnum.外力破壞 && this.EventExternalForceAlarm || rpt.Status == (int)CardReaderStatusEnum.異常入侵 && this.EventIntrusionAlarm || rpt.Status == (int)CardReaderStatusEnum.開門超時 && this.EventDoorOpenOverTimeAlarm || rpt.Status == (int)CardReaderStatusEnum.開鎖 && this.EventDoorOpenAlarm || rpt.Status == (int)CardReaderStatusEnum.號碼錯誤 && this.R23InvalidCardAlarm ) { if (this.OnAlarmEvent != null) { ICCTV cctv = (ICCTV)SecureService.cctv_mgr[reader.TriggerCCTVID]; AlarmData data = new AlarmData() { TimeStamp = DateTime.Now, AlarmType = AlarmType.Secure, ColorString = "Red", Description = reader.ControllerID + "," + rpt.StatusString, PlaneID = reader.PlaneID, IsForkCCTVEvent = true, PlaneName = Global.GetPlaneNameByPlaneID(reader.PlaneID), CCTVBindingData = cctv!=null?cctv.ToBindingData():null }; this.OnAlarmEvent(reader, data); } } if (rpt.Status == (int)CardReaderStatusEnum.開鎖 || rpt.Status == (int)CardReaderStatusEnum.按鈕開門 || rpt.Status == (int)CardReaderStatusEnum.密碼開門 || rpt.Status == (int)CardReaderStatusEnum.系統開門 || rpt.Status == (int)CardReaderStatusEnum.異常入侵 || rpt.Status == (int)CardReaderStatusEnum.開門超時) { SecureDBEntities1 db = new SecureDBEntities1(); tblEngineRoomLog log = new tblEngineRoomLog() { ControlID = reader.ControllerID, ABA = rpt.CardNo.ToString(), StartTime = DateTime.Now, TypeID = 8, Memo = rpt.StatusString, TypeCode = (short)rpt.Status, ERNo = reader.PlaneID.ToString() }; db.tblEngineRoomLog.Add( log ); db.SaveChanges(); //開門錄影 if (rpt.Status == (int)CardReaderStatusEnum.開鎖 || rpt.Status == (int)CardReaderStatusEnum.按鈕開門 || rpt.Status == (int)CardReaderStatusEnum.密碼開門 || rpt.Status == (int)CardReaderStatusEnum.系統開門) { if (reader.NVRID == -1) return; #region 擷取錄影 #if R23 #else Task task = Task.Factory.StartNew(() => { DateTime dt = DateTime.Now; System.Threading.Thread.Sleep(1000 * 20); long flowid = log.FlowID; Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("nvrid:" + reader.NVRID); try { NVR.INVR nvr = SecureService.nvr_mgr[reader.NVRID]; if (nvr == null) { Console.WriteLine(reader.NVRID + " is null"); return; } bool success = nvr.SaveRecord( // reader.NVRChNo, dt.AddSeconds(-10), dt.AddSeconds(10), @"C:\web\Secure\ClientBin\VideoRecord\" + flowid + ".avi"); reader.NVRChNo, dt.AddSeconds(-10), dt.AddSeconds(10), @"E:\web\Secure\ClientBin\VideoRecord\" + flowid + ".avi"); //bool success = nvr.SaveRecord( //reader.NVRChNo, dt.AddSeconds(-10), dt.AddSeconds(10), @"D:\" + flowid + ".avi"); log.NVRFile = flowid + ".wmv"; db.SaveChanges(); Console.WriteLine(success); } catch (Exception ex) { Console.WriteLine(ex.Message + "," + ex.StackTrace); } finally { Console.ResetColor(); } }); #endif #endregion } if (this.serivce != null && reader.TriggerCCTVID != 0) { if (InOperationCCTV.Where(n => n == reader.TriggerCCTVID).Count() > 0) { Task.Factory.StartNew(() => { try { Console.WriteLine("Trigger " + reader.TriggerCCTVID); SecureService.cctv_mgr[reader.TriggerCCTVID].Preset(2); System.Threading.Thread.Sleep(1000 * 10); SecureService.cctv_mgr[reader.TriggerCCTVID].Preset(1); } catch (Exception ex) { Console.WriteLine("May be trigger cctv not found" + ex.Message + "," + ex.StackTrace); } InOperationCCTV.Remove(reader.TriggerCCTVID); }); } } } if (rpt.Status == (int)CardReaderStatusEnum.號碼錯誤 || rpt.Status == (int)CardReaderStatusEnum.卡號連續錯誤 || rpt.Status == (int)CardReaderStatusEnum.外力破壞 ) { SecureDBEntities1 db = new SecureDBEntities1(); db.tblEngineRoomLog.Add( new tblEngineRoomLog() { ControlID = reader.ControllerID, ABA = rpt.CardNo.ToString(), StartTime = DateTime.Now, TypeID = 8, Memo = rpt.StatusString, TypeCode = (short)rpt.Status, ERNo = reader.PlaneID.ToString() } ); db.SaveChanges(); } } catch (Exception ex) { Console.WriteLine("CardManager OnStatus Change:" + ex.Message + "," + ex.StackTrace); } }
void item_ItemDegreeChanged(Item sender, int? NewValue) { SecureDBEntities1 db = new SecureDBEntities1(); if (NewValue == 2 && sender.AlarmMode=="Y") { AlarmData data=new AlarmData() { TimeStamp = DateTime.Now, AlarmType = AlarmType.RTU, ColorString = "Red", Description = sender.ItemConfig.ItemName+"警報", PlaneID = sender.PlaneID, IsForkCCTVEvent = false, PlaneName =Global.GetPlaneNameByPlaneID(sender.PlaneID) // CCTVBindingData =cctv.ToBindingData( }; if(!(sender.ItemConfig.Suppress??false)) Program.MyServiceObject.DispatchAlarmEvent(data); int typecode = 0; switch(sender.ItemType) { case "AI": typecode=3; break; case "DI": typecode=0; break; } tblAlarmLog tblalarmlog = new tblAlarmLog() { ControlID = sender.ItemConfig.ControlID, ItemID = sender.ItemID, Timestamp = DateTime.Now, TypeID = 5, TypeCode = (short)typecode, Value = sender.Value }; db.tblAlarmLog.Add(tblalarmlog); } else if (NewValue == 1 && sender.ItemType == "AI" && sender.AlarmMode == "Y") { int typecode = 0; switch (sender.ItemType) { case "AI": typecode = 2; break; //case "DI": // typecode = 1; // break; } tblAlarmLog tblalarmlog = new tblAlarmLog() { ControlID = sender.ItemConfig.ControlID, ItemID = sender.ItemID, Timestamp = DateTime.Now, TypeID = 5, TypeCode = (short)typecode, Value = sender.Value }; db.tblAlarmLog.Add(tblalarmlog); } if (NewValue == 0) { sender.ItemConfig.Suppress = false; tblItemConfig item = db.tblItemConfig.Where(n => n.ItemID == sender.ItemID).FirstOrDefault(); if (item != null) item.Suppress = false; } if( NewValue==0 && sender.AlarmMode=="Y") { int typecode=0; switch(sender.ItemType) { case "AI": typecode=4; break; case "DI": typecode=1; break; } tblAlarmLog tblalarmlog = new tblAlarmLog() { ControlID = sender.ItemConfig.ControlID, ItemID = sender.ItemID, Timestamp = DateTime.Now, TypeID = 5, TypeCode = (short)typecode, Value = sender.Value }; db.tblAlarmLog.Add(tblalarmlog); } tblItemConfig tbl = db.tblItemConfig.Where(n => n.ItemID == sender.ItemID).FirstOrDefault(); if (tbl != null) { tbl.Degree = NewValue; } db.SaveChanges(); db.Dispose(); }
void item_ItemValueChanged(Item sender, double NewValue) { SecureDBEntities1 db = new SecureDBEntities1(); try { tblItemConfig tbl = db.tblItemConfig.Where(n => n.ItemID == sender.ItemID).FirstOrDefault(); if (tbl != null) tbl.Value = NewValue; db.SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex.Message + "," + ex.StackTrace); } finally { db.Dispose(); } if(Program.MyServiceObject!=null) Program.MyServiceObject.DispatchItemValueChangedEvent(sender.ToBindingData()); }
void OneHourTmr_OnElapsed(object sender) { DateTime dt=DateTime.Now; dt= dt.AddMinutes(-dt.Minute).AddSeconds(-dt.Second).AddMilliseconds(-dt.Millisecond); using (SecureDBEntities1 db1 = new SecureDBEntities1()) { foreach (Item item in Items.Values) { try { if (item.ItemType == "AI" && item.IsConnected ) { tblAIItem1HourLog tbl = new tblAIItem1HourLog() { ItemID = item.ItemID, Value = item.Value, Timestamp = dt, Memo=item.ItemConfig.Lable }; db1.tblAIItem1HourLog.Add(tbl); db1.SaveChanges(); } } catch { ;} } } //throw new NotImplementedException(); }
void ReadingTask() { while (true) { foreach (Meter.R23PowerMeter meter in list) { try { SecureDBEntities1 db = new SecureDBEntities1(); if (!meter.IsValid) continue; tblPowerMeter tbl = db.tblPowerMeter.Where(n => n.ERID == meter.ERID).FirstOrDefault(); if (tbl != null) { tbl.VA = meter.VA; tbl.VB = meter.VB; tbl.VC = meter.VC; tbl.AVGV = meter.AVGV; tbl.IA = meter.IA; tbl.IB = meter.IB; tbl.IC = meter.IC; tbl.AVGI = meter.AVGI; tbl.KW = meter.KW; tbl.PF = meter.PF; tbl.CumulateValue = meter.CumulateValue; tbl.InstantaneousValue = meter.InstantaneousValue; tbl.UpdateDate = DateTime.Now; tbl.kWh = meter.kwh; db.SaveChanges(); db.Dispose(); } } catch (Exception ex) { Console.WriteLine(ex.Message + "," + ex.StackTrace); } } System.Threading.Thread.Sleep(10 * 60 * 1000); } }
void OneHourTmr_OnElapsed(object sender) { // throw new NotImplementedException(); foreach (Meter.R23PowerMeter meter in list) { try { SecureDBEntities1 db = new SecureDBEntities1(); if (!meter.IsValid) continue; tblPowerMeter tbl = db.tblPowerMeter.Where(n => n.ERID == meter.ERID).FirstOrDefault(); if (tbl != null) { DateTime dt=DateTime.Now; // DateTime lastbeg=DateTime.Now.Subtract(TimeSpan.FromHours(24 )); tblPowerMeter1HourLog lastlog= db.tblPowerMeter1HourLog.Where(n=>n.ERID==tbl.ERID).OrderByDescending(n=>n.Timestamp).Take(1).FirstOrDefault(); double? wateCurrent = (tbl.CumulateValue - lastlog.CumulateValue); double? PowerCurrent = ((tbl.kWh ?? 0) - (lastlog.kWh ?? 0)); bool IsModify = false; if (wateCurrent <-1|| wateCurrent > 100) { wateCurrent = lastlog.WaterConsume; tbl.CumulateValue=lastlog.CumulateValue + lastlog.WaterConsume; IsModify = true; } if (PowerCurrent < 0 || PowerCurrent > 5000) { PowerCurrent = lastlog.PowerConsume; tbl.kWh = lastlog.kWh + lastlog.PowerConsume; IsModify = true; } double? PowerBase = GetPowerBase(tbl.ERID); //db.tblPowerMeter1HourLog.Where(n =>/* n.Timestamp >= lastbeg && */ n.ERID==tbl.ERID ).Average(n => n.KW); double? WaterBase = GetWaterBase(tbl.ERID);//db.tblPowerMeter1HourLog.Where(n => /*n.Timestamp >= lastbeg && */ n.ERID==tbl.ERID ).Average(n => n.WaterConsume); double? Kwh24Hour= db.tblPowerMeter1HourLog.Where(n=>n.ERID==tbl.ERID).OrderByDescending(n => n.Timestamp ).Take(23).Sum(n=>n.PowerConsume)+PowerCurrent; //db.tblPowerMeter1HourLog.Where(n=>n.ERID==tbl.ERID).OrderByDescending(n => n.Timestamp ).Take(24).Average(n=>n.KW); double? water24Hour = db.tblPowerMeter1HourLog.Where(n => n.ERID == tbl.ERID).OrderByDescending(n => n.Timestamp).Take(23).Sum(n => n.WaterConsume)+wateCurrent; double? PowerLastDayBase = GetLastDayPowerBase(tbl.ERID); double? PowerLastHourBase = GetLastHourPowerBase(tbl.ERID); bool PowerAlarm = false, WaterAlarm = false, PowerLastDayAlarm = false, PowerLastHourAlarm = false ; //if(PowerAvg!=null && (tbl.KW> PowerAvg*(1+tbl.PowerAlarmUpper/100) || tbl.KW< PowerAvg*(1-tbl.PowerAlarmLower/100 )) ) if (PowerBase != null && (Kwh24Hour > PowerBase * (1 + tbl.PowerAlarmUpper / 100) || Kwh24Hour < PowerBase * (1 - tbl.PowerAlarmLower / 100))) PowerAlarm=true; if (WaterBase != null && (water24Hour > WaterBase * (1 + tbl.WaterAlarmUpper / 100) /* || wateCurrent< WaterAvg*(1-tbl.WaterAlarmLower/100 )*/)) WaterAlarm=true; if(PowerLastDayBase!=null && (Kwh24Hour > PowerLastDayBase * (1 + tbl.PowerAlarmUpper / 100) || Kwh24Hour < PowerLastDayBase * (1 - tbl.PowerAlarmLower / 100))) PowerLastDayAlarm=true; if (PowerLastHourBase != null && (PowerCurrent > PowerLastHourBase * (1 + tbl.PowerAlarmUpper / 100) || PowerCurrent < PowerLastHourBase * (1 - tbl.PowerAlarmLower / 100))) PowerLastHourAlarm = true; tbl.PowerAlarm = PowerAlarm; tbl.WaterAlarm = WaterAlarm; bool PowerAlarmChanged, WaterAlarmChanged; PowerAlarmChanged = (tbl.PowerAlarm ?? false) ^ PowerAlarm; WaterAlarmChanged = (tbl.WaterAlarm ?? false) ^ WaterAlarm; // tbl.KW24Avg = Kw24avg; tblPowerMeter1HourLog log = new tblPowerMeter1HourLog() { ERID = tbl.ERID, KW = tbl.KW, Timestamp = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, 0, 0), CumulateValue = tbl.CumulateValue, //(lastlog==null)?tbl.CumulateValue:tbl.CumulateValue-lastlog.CumulateValue, WaterAlarm = WaterAlarm, PowerAlarm = (PowerLastDayAlarm || PowerLastHourAlarm || PowerAlarm), // PowerAlarm = PowerAlarm, // PowerAlarmAvg = PowerAvg ?? tbl.KW, // WaterAlarmAvg = WaterAvg ?? ((lastlog == null) ? tbl.CumulateValue : tbl.CumulateValue - lastlog.CumulateValue), WaterConsume = wateCurrent,//(lastlog == null) ? tbl.CumulateValue : tbl.CumulateValue - lastlog.CumulateValue, //KW24Avg=Kw24avg, PowerConsume =PowerCurrent, //(lastlog == null) ? tbl.PowerConsume: tbl.kWh - (lastlog.PowerConsume??0), PowerAlarm_LastHour=(short)(PowerLastHourAlarm?(PowerCurrent>PowerLastHourBase?2:1):0), PowerAlarm_LastYear = (short)(PowerAlarm ? (Kwh24Hour > PowerBase ? 2 : 1):0), PowerAlarm_Yesterday=(short)(PowerLastDayAlarm ? (Kwh24Hour > PowerLastDayBase ? 2 : 1):0), Power24Consume=Kwh24Hour, kWh=tbl.kWh, PowerAlarmBaseValue_LastHour=PowerLastHourBase, PowerAlarmBaseValue_LastYear=PowerBase, PowerAlarmBaseValue_Yesterday=PowerLastDayBase, WaterAlarmBaseValue_LastYear=WaterBase, WaterAlarm_LastYear=(short)(WaterAlarm ? (water24Hour > WaterBase ? 2 : 1):0), Water24Consume=water24Hour, Memo=(IsModify?"Modify":null) }; db.tblPowerMeter1HourLog.Add(log); tbl.WaterConsume = log.WaterConsume; tbl.WaterAlarm_LastYear = log.WaterAlarm_LastYear; tbl.Water24Consume = log.Water24Consume; tbl.WaterAlarmBaseValue_LastYear = log.WaterAlarmBaseValue_LastYear; tbl.WaterAlarmAvg = log.WaterAlarmAvg; tbl.PowerAlarmAvg = log.PowerAlarmAvg; tbl.Power24Consume = log.Power24Consume; tbl.PowerAlarm = log.PowerAlarm; tbl.PowerAlarm_LastHour = log.PowerAlarm_LastHour; tbl.PowerAlarm_LastYear = log.PowerAlarm_LastYear; tbl.PowerAlarm_Yesterday = log.PowerAlarm_Yesterday; tbl.PowerAlarmBaseValue_LastHour = log.PowerAlarmBaseValue_LastHour; tbl.PowerAlarmBaseValue_LastYear = log.PowerAlarmBaseValue_LastYear; tbl.PowerAlarmBaseValue_Yesterday = log.PowerAlarmBaseValue_Yesterday; tbl.PowerConsume = log.PowerConsume; if ((log.PowerAlarm ?? false) && (log.PowerAlarm_LastHour == 2 || log.PowerAlarm_LastYear == 2 || log.PowerAlarm_Yesterday == 2)) tbl.PowerAlarmDesc = "用電量過高"; else if ((log.PowerAlarm ?? false) && (log.PowerAlarm_LastHour == 1 || log.PowerAlarm_LastYear == 1 || log.PowerAlarm_Yesterday == 1)) tbl.PowerAlarmDesc = "用電量過低"; if ((log.WaterAlarm ?? false) && log.WaterAlarm_LastYear==2) tbl.WaterAlarmDesc = "用水量過高"; else if ((log.WaterAlarm ?? false) && log.WaterAlarm_LastYear==1) tbl.WaterAlarmDesc = "用水量過低 "; //if ((log.PowerAlarm ?? false) && log.KW > log.PowerAlarmAvg) // tbl.PowerAlarmDesc = "用電量過高"; //else if ((log.PowerAlarm ?? false) && log.KW <= log.PowerAlarmAvg) // tbl.PowerAlarmDesc = "用電量過低 "; //if ((log.WaterAlarm ?? false) && log.WaterConsume > log.WaterAlarmAvg) // tbl.WaterAlarmDesc = "用水量過高"; //else if ((log.WaterAlarm ?? false) && log.WaterConsume <= log.WaterAlarmAvg) // tbl.WaterAlarmDesc = "用水量過低 "; if (!(log.PowerAlarm ?? false)) tbl.PowerAlarmDesc = ""; if (!(log.WaterAlarm ?? false)) tbl.WaterAlarmDesc = ""; db.SaveChanges(); if (PowerAlarm /*&& PowerAlarmChanged*/) { AlarmData data = new AlarmData() { AlarmType = AlarmType.PowerMeter, ColorString = "Red", TimeStamp = DateTime.Now, PlaneName = tbl.ERName, TimeStampString = string.Format("HH:mm"), IsForkCCTVEvent=false, Description=tbl.PowerAlarmDesc }; Program.MyServiceObject.DispatchAlarmEvent(data); } if (WaterAlarm /*&& WaterAlarmChanged*/) { AlarmData data = new AlarmData() { AlarmType = AlarmType.WaterMeter, ColorString = "Red", TimeStamp = DateTime.Now, PlaneName = tbl.ERName, TimeStampString = string.Format("HH:mm"), IsForkCCTVEvent=false, Description=tbl.WaterAlarmDesc }; Program.MyServiceObject.DispatchAlarmEvent(data); } //tbl.VA = meter.VA; //tbl.VB = meter.VB; //tbl.VC = meter.VC; //tbl.AVGV = meter.AVGV; //tbl.IA = meter.IA; //tbl.IB = meter.IB; //tbl.IC = meter.IC; //tbl.AVGI = meter.AVGI; //tbl.KW = meter.KW; //tbl.PF = meter.PF; //tbl.CumulateValue = meter.CumulateValue; //tbl.InstantaneousValue = meter.InstantaneousValue; //tbl.UpdateDate = DateTime.Now; //db.SaveChanges(); //db.Dispose(); } } catch (Exception ex) { Console.WriteLine(ex.Message + "," + ex.StackTrace); } } }