//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);
            }
               
           
           
        }