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 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 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(); }
public RTUManager() { SecureDBEntities1 db = new SecureDBEntities1(); var q = from n in db.tblControllerConfig where n.IsEnable == true &&( n.ControlType == 3 || n.ControlType==5 || n.ControlType==6 || n.ControlType==7 || n.ControlType==8 || n.ControlType==9||n.ControlType==10) select n; //RTU control type=3 8: for TowerRTU //var q = from n in db.tblControllerConfig where n.ControlID == "AC-RTU-1" && n.ControlType == 3 && n.IsEnable==true select n; foreach (tblControllerConfig tbl in q) { ModbusTCP.IRTU rtu = null; ; if (tbl.ControlType == 3) //normal rtu { rtu = new ModbusTCP.RTU(tbl.ControlID, 1, tbl.IP, tbl.Port, (int)tbl.RTUBaseAddress, (int)tbl.RTURegisterLength, tbl.Comm_state ?? 0); rtu.OnCommStateChanged += rtu_OnCommStateChanged; } else if(tbl.ControlType==5) { rtu = new SecureServer.RTU.R23AdamRTU(tbl.ControlID, 1, tbl.IP, tbl.Port, (int)tbl.RTUBaseAddress, (int)tbl.RTURegisterLength, tbl.Comm_state ?? 0,tbl.R23_ADAM ); rtu.OnCommStateChanged += rtu_OnCommStateChanged; } else if (tbl.ControlType == 6) { rtu = new SecureServer.RTU.R13SmrRTU(tbl.ControlID, 1, tbl.IP, tbl.Port, (int)tbl.RTUBaseAddress, (int)tbl.RTURegisterLength, tbl.Comm_state ?? 0 ); rtu.OnCommStateChanged += rtu_OnCommStateChanged; } else if (tbl.ControlType == 7) //IED { int devid = 1; if (tbl.Port == 503) devid = 2; rtu = new SecureServer.RTU.R13IEDRTU(tbl.ControlID, devid, tbl.IP, tbl.Port, (int)tbl.RTUBaseAddress, (int)tbl.RTURegisterLength, tbl.Comm_state ?? 0); rtu.OnCommStateChanged += rtu_OnCommStateChanged; } else if (tbl.ControlType == 8) //TowerRTU { rtu = new SecureServer.RTU.R13TowerRTU(tbl.ControlID, 1, tbl.IP, tbl.Port, (int)tbl.RTUBaseAddress, (int)tbl.RTURegisterLength, tbl.Comm_state ?? 0); rtu.OnCommStateChanged += rtu_OnCommStateChanged; } else if (tbl.ControlType == 9) //rtu device id=3 { rtu = new SecureServer.RTU.R13BatteryPackRTU(tbl.ControlID, 1, tbl.IP, tbl.Port, (int)tbl.RTUBaseAddress, (int)tbl.RTURegisterLength, tbl.Comm_state ?? 0); rtu.OnCommStateChanged += rtu_OnCommStateChanged; //rtu = new ModbusTCP.RTU(tbl.ControlID, 3, tbl.IP, tbl.Port, (int)tbl.RTUBaseAddress, (int)tbl.RTURegisterLength, tbl.Comm_state ?? 0); //rtu.OnCommStateChanged += rtu_OnCommStateChanged; } else if (tbl.ControlType == 10) //rtu device id=3 { rtu = new SecureServer.RTU.R13NewSmrRTU(tbl.ControlID, 1, tbl.IP, tbl.Port, (int)tbl.RTUBaseAddress, (int)tbl.RTURegisterLength, tbl.Comm_state ?? 0); rtu.OnCommStateChanged += rtu_OnCommStateChanged; } if (!dictRTUs.ContainsKey(tbl.ControlID)) { dictRTUs.Add(tbl.ControlID, rtu); Console.WriteLine("Add RTU" + rtu.ControlID + ",base:" + tbl.RTUBaseAddress + ",Length:" + tbl.RTURegisterLength); } } db.Dispose(); }
//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 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 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); } }