protected override void AfterMyWorkStateChanged(object sender, EventArgs e)
 {
     if (myWorkState)
     {
         CaseSettings     = Ewatch_MySqlMethod.CaseLoad();
         AiSettings       = Ewatch_MySqlMethod.AiLoad();
         ElectricSettings = Ewatch_MySqlMethod.ElectricLoad();
         ConnectionThread = new Thread(Connection_Mysql);
         ConnectionThread.Start();
     }
     else
     {
         if (ConnectionThread != null)
         {
             ConnectionThread.Abort();
         }
     }
 }
Ejemplo n.º 2
0
        private void Connection_Mysql()
        {
            while (myWorkState)
            {
                TimeSpan timeSpan = DateTime.Now.Subtract(ConnectionTime);
                if (timeSpan.TotalMilliseconds > 5000)
                {
                    CaseSettings = Ewatch_MySqlMethod.CaseLoad();
                    #region AI上下限
                    var Alarmdata = Ewatch_MySqlMethod.AiConfig_Compare_Load();
                    foreach (var item in Alarmdata)
                    {
                        var caseSetting = CaseSettings.Where(g => g.CaseNo == item.CaseNo).ToList()[0];
                        var value       = Ewatch_MySqlMethod.Ai64web(item);
                        if (value != null)
                        {
                            var nowdata = Ewatch_MySqlMethod.Alarm_Procedure(item, value);

                            var Adata = AiConfigs.Where(g => g.PK == item.PK).ToList();
                            if (Adata.Count > 0)
                            {
                                if (item.CompareType != Adata[0].CompareType)
                                {
                                    NotifyTypeEnum notifyTypeEnum = (NotifyTypeEnum)caseSetting.NotifyTypeEnum;
                                    switch (notifyTypeEnum)
                                    {
                                    case NotifyTypeEnum.None:
                                        break;

                                    case NotifyTypeEnum.Line:
                                    {
                                        switch (item.CompareType)
                                        {
                                        case 0:             //上限
                                        {
                                            LineNotifyClass lineNotifyClass = new LineNotifyClass();
                                            lineNotifyClass.LineNotifyFunction(caseSetting.NotifyToken, $"\r案場名稱: {caseSetting.TitleName}\r點位名稱: {item.AiName}\r超過上限值: {item.AiMax}\r目前數值為 {nowdata}");
                                        }
                                        break;

                                        case 1:             //正常
                                        {
                                            LineNotifyClass lineNotifyClass = new LineNotifyClass();
                                            lineNotifyClass.LineNotifyFunction(caseSetting.NotifyToken, $"\r案場名稱: {caseSetting.TitleName}\r點位名稱: {item.AiName}\r上限值: {item.AiMax}\r下限值: {item.AiMin}\r恢復正常,目前數值為 {nowdata}");
                                        }
                                        break;

                                        case 2:             //下限
                                        {
                                            LineNotifyClass lineNotifyClass = new LineNotifyClass();
                                            lineNotifyClass.LineNotifyFunction(caseSetting.NotifyToken, $"\r案場名稱: {caseSetting.TitleName}\r點位名稱: {item.AiName}\r低於下限值: {item.AiMin}\r目前數值為 {nowdata}");
                                        }
                                        break;
                                        }
                                    }
                                    break;

                                    case NotifyTypeEnum.Telegram:
                                    {
                                        switch (item.CompareType)
                                        {
                                        case 0:             //上限
                                        {
                                            TelegramBotClass telegramBotClass = new TelegramBotClass(caseSetting.NotifyApi)
                                            {
                                                Chat_ID = caseSetting.NotifyToken
                                            };
                                            telegramBotClass.Send_Message_Group($"\r案場名稱: {caseSetting.TitleName}\r點位名稱: {item.AiName}\r超過上限值: {item.AiMax}\r目前數值為 {nowdata}");
                                        }
                                        break;

                                        case 1:             //正常
                                        {
                                            TelegramBotClass telegramBotClass = new TelegramBotClass(caseSetting.NotifyApi)
                                            {
                                                Chat_ID = caseSetting.NotifyToken
                                            };
                                            telegramBotClass.Send_Message_Group($"\r案場名稱: {caseSetting.TitleName}\r點位名稱: {item.AiName}\r上限值: {item.AiMax}\r下限值: {item.AiMin}\r恢復正常,目前數值為 {nowdata}");
                                        }
                                        break;

                                        case 2:             //下限
                                        {
                                            TelegramBotClass telegramBotClass = new TelegramBotClass(caseSetting.NotifyApi)
                                            {
                                                Chat_ID = caseSetting.NotifyToken
                                            };
                                            telegramBotClass.Send_Message_Group($"\r案場名稱: {caseSetting.TitleName}\r點位名稱: {item.AiName}\r低於下限值: {item.AiMin}\r目前數值為 {nowdata}");
                                        }
                                        break;
                                        }
                                    }
                                    break;
                                    }
                                }
                            }
                        }
                    }
                    #endregion
                    AiConfigs      = Alarmdata;
                    ConnectionTime = DateTime.Now;
                }
                else
                {
                    Thread.Sleep(80);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 查詢案場
        /// </summary>
        public override void Search_Setting()
        {
            var CaseSetting = Ewatch_MySqlMethod.CaseLoad();

            gridControl1.DataSource = CaseSetting;
            if (!Flag)
            {
                gridView1.CustomColumnDisplayText += (s, ex) =>
                {
                    if (ex.Column.FieldName == "NotifyTypeEnum")
                    {
                        int val = (int)ex.Value;
                        switch (val)
                        {
                        case 0:
                        {
                            ex.DisplayText = "None";
                        }
                        break;

                        case 1:
                        {
                            ex.DisplayText = "LineNotify";
                        }
                        break;

                        case 2:
                        {
                            ex.DisplayText = "TelegramBot";
                        }
                        break;
                        }
                    }
                };
                gridView1.OptionsBehavior.Editable = false;
                gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
                for (int i = 0; i < gridView1.Columns.Count; i++)
                {
                    gridView1.Columns[i].BestFit();
                }
                gridView1.Columns["PK"].Visible             = false;
                gridView1.Columns["CaseNo"].Caption         = "案場編號";
                gridView1.Columns["Address"].Caption        = "地址";
                gridView1.Columns["Contacter"].Caption      = "聯絡人";
                gridView1.Columns["Phone"].Caption          = "聯絡電話";
                gridView1.Columns["TitleName"].Caption      = "案場名稱";
                gridView1.Columns["NotifyTypeEnum"].Caption = "推播類型";
                gridView1.Columns["NotifyApi"].Caption      = "推播網址";
                gridView1.Columns["NotifyToken"].Caption    = "推播權杖";
                gridView1.Columns["Longitude"].Caption      = "經度";
                gridView1.Columns["Latitude"].Caption       = "緯度";
                #region 報表行聚焦
                gridView1.FocusedRowChanged += (s, ex) =>
                {
                    ColumnView view = (ColumnView)s;
                    if ((view.FindFilterText == "" || view.ActiveFilterString != "") & !SortGlyphFlag)
                    {
                        if (ex.FocusedRowHandle > -1)
                        {
                            CaseNotextEdit.Text    = view.GetListSourceRowCellValue(ex.FocusedRowHandle, "CaseNo").ToString();
                            AddresstextEdit.Text   = view.GetListSourceRowCellValue(ex.FocusedRowHandle, "Address").ToString();
                            ContactertextEdit.Text = view.GetListSourceRowCellValue(ex.FocusedRowHandle, "Contacter").ToString();
                            PhonetextEdit.Text     = view.GetListSourceRowCellValue(ex.FocusedRowHandle, "Phone").ToString();
                            TitleNametextEdit.Text = view.GetListSourceRowCellValue(ex.FocusedRowHandle, "TitleName").ToString();
                            NotifyTypeEnumcomboBoxEdit.SelectedIndex = Convert.ToInt32(view.GetListSourceRowCellValue(ex.FocusedRowHandle, "NotifyTypeEnum"));
                            NotifyApitextEdit.Text   = view.GetListSourceRowCellValue(ex.FocusedRowHandle, "NotifyApi").ToString();
                            NotifyTokentextEdit.Text = view.GetListSourceRowCellValue(ex.FocusedRowHandle, "NotifyToken").ToString();
                            LongitudetextEdit.Text   = view.GetListSourceRowCellValue(ex.FocusedRowHandle, "Longitude").ToString();
                            LatitudetextEdit.Text    = view.GetListSourceRowCellValue(ex.FocusedRowHandle, "Latitude").ToString();
                        }
                    }
                    else
                    {
                        if (ex.FocusedRowHandle > -1)
                        {
                            if (FilterCaseSetting.Count > 0 && FilterCaseSetting.Count > ex.FocusedRowHandle)
                            {
                                CaseNotextEdit.Text    = FilterCaseSetting[ex.FocusedRowHandle].CaseNo;
                                AddresstextEdit.Text   = FilterCaseSetting[ex.FocusedRowHandle].Address;
                                ContactertextEdit.Text = FilterCaseSetting[ex.FocusedRowHandle].Contacter;
                                PhonetextEdit.Text     = FilterCaseSetting[ex.FocusedRowHandle].Phone;
                                TitleNametextEdit.Text = FilterCaseSetting[ex.FocusedRowHandle].TitleName;
                                NotifyTypeEnumcomboBoxEdit.SelectedIndex = FilterCaseSetting[ex.FocusedRowHandle].NotifyTypeEnum;
                                NotifyApitextEdit.Text   = FilterCaseSetting[ex.FocusedRowHandle].NotifyApi;
                                NotifyTokentextEdit.Text = FilterCaseSetting[ex.FocusedRowHandle].NotifyToken;
                                LongitudetextEdit.Text   = FilterCaseSetting[ex.FocusedRowHandle].Longitude.ToString();
                                LatitudetextEdit.Text    = FilterCaseSetting[ex.FocusedRowHandle].Latitude.ToString();
                            }
                        }
                    }
                };
                #endregion
                #region 報表行刪除
                gridView1.RowDeleting += (s, ex) =>
                {
                    ColumnView view   = (ColumnView)s;
                    string     CaseNo = CaseNotextEdit.Text;
                    Ewatch_MySqlMethod.Delete_CaseSetting(CaseNo);
                };
                #endregion
                #region 關鍵字搜尋
                gridView1.ColumnFilterChanged += (s, e) =>
                {
                    GridView view = s as GridView;
                    if (view.FindFilterText != "" || view.ActiveFilterString != "")
                    {
                        FilterCaseSetting = new List <CaseSetting>();
                        for (int i = 0; i < view.RowCount; i++)
                        {
                            if (view.IsGroupRow(i))
                            {
                                continue;
                            }
                            var entity = view.GetRow(i) as CaseSetting;
                            if (entity == null)
                            {
                                continue;
                            }
                            FilterCaseSetting.Add(entity);
                        }
                    }
                    if (FilterCaseSetting.Count > 0)
                    {
                        CaseNotextEdit.Text    = FilterCaseSetting[0].CaseNo;
                        AddresstextEdit.Text   = FilterCaseSetting[0].Address;
                        ContactertextEdit.Text = FilterCaseSetting[0].Contacter;
                        PhonetextEdit.Text     = FilterCaseSetting[0].Phone;
                        TitleNametextEdit.Text = FilterCaseSetting[0].TitleName;
                        NotifyTypeEnumcomboBoxEdit.SelectedIndex = FilterCaseSetting[0].NotifyTypeEnum;
                        NotifyApitextEdit.Text   = FilterCaseSetting[0].NotifyApi;
                        NotifyTokentextEdit.Text = FilterCaseSetting[0].NotifyToken;
                        LongitudetextEdit.Text   = FilterCaseSetting[0].Longitude.ToString();
                        LatitudetextEdit.Text    = FilterCaseSetting[0].Latitude.ToString();
                    }
                };
                #endregion
                #region 表頭篩選
                gridView1.EndSorting += (s, e) =>
                {
                    GridView view = s as GridView;
                    FilterCaseSetting = new List <CaseSetting>();
                    for (int i = 0; i < view.RowCount; i++)
                    {
                        if (view.IsGroupRow(i))
                        {
                            continue;
                        }
                        var entity = view.GetRow(i) as CaseSetting;
                        if (entity == null)
                        {
                            continue;
                        }
                        FilterCaseSetting.Add(entity);
                    }
                    SortGlyphFlag = true;
                    gridView1.FocusedRowHandle = 0;
                };
                #endregion
                gridView1.FocusedRowHandle = 1;
                Flag = true;
            }
            else
            {
                gridControl1.Refresh();
            }
        }
        private void Connection_Mysql()
        {
            while (myWorkState)
            {
                TimeSpan timeSpan = DateTime.Now.Subtract(ConnectionTime);
                if (timeSpan.TotalMilliseconds > 5000) //每一秒查詢動作
                {
                    var NewCaseSettings     = Ewatch_MySqlMethod.CaseLoad();
                    var NewAiSettings       = Ewatch_MySqlMethod.AiLoad();
                    var NewElectricSettings = Ewatch_MySqlMethod.ElectricLoad();
                    var casevalue           = Ewatch_MySqlMethod.CaseLoad();
                    foreach (var caseitem in casevalue)
                    {
                        #region AI接收
                        var aivalue = Ewatch_MySqlMethod.AiLoad(caseitem);
                        foreach (var aiitem in aivalue)
                        {
                            if (aiitem.NotifyFlag)                                      //AI點位需要發報
                            {
                                if (Ewatch_MySqlMethod.AI64Load(aiitem))                //檢查即時數值是否超過時間
                                {
                                    var TimeValue = Ewatch_MySqlMethod.Ai_Time(aiitem); //檢查發報數值是否達到
                                    if (TimeValue == null || TimeValue >= aiitem.TimeoutSpan)
                                    {
                                        if (NewAiSettings.SingleOrDefault(g => g.PK == aiitem.PK) != null)
                                        {
                                            NewAiSettings.Single(g => g.PK == aiitem.PK).ConnectionFlag = "斷線";
                                            NotifyTypeEnum notifyTypeEnum = (NotifyTypeEnum)caseitem.NotifyTypeEnum;
                                            switch (notifyTypeEnum)
                                            {
                                            case NotifyTypeEnum.None:
                                                break;

                                            case NotifyTypeEnum.Line:
                                            {
                                                LineNotifyClass lineNotifyClass = new LineNotifyClass();
                                                lineNotifyClass.LineNotifyFunction(caseitem.NotifyToken, $"設備名稱:{aiitem.AiName} \r最後上傳時間 : {Ewatch_MySqlMethod.AI_LastTime(aiitem)}\r上傳逾時請檢查");
                                            }
                                            break;

                                            case NotifyTypeEnum.Telegram:
                                            {
                                                TelegramBotClass telegramBotClass = new TelegramBotClass(caseitem.NotifyApi)
                                                {
                                                    Chat_ID = caseitem.NotifyToken
                                                };
                                                telegramBotClass.Send_Message_Group($"設備名稱:{aiitem.AiName} \r最後上傳時間 : {Ewatch_MySqlMethod.AI_LastTime(aiitem)}\r上傳逾時請檢查");
                                            }
                                            break;
                                            }
                                            Ewatch_MySqlMethod.UpdataAi_Time(aiitem, true);
                                        }
                                    }
                                    else
                                    {
                                        NewAiSettings.Single(g => g.PK == aiitem.PK).ConnectionFlag = "斷線";
                                    }
                                }
                                else
                                {
                                    if (NewAiSettings.SingleOrDefault(g => g.PK == aiitem.PK) != null)
                                    {
                                        NewAiSettings.Single(g => g.PK == aiitem.PK).ConnectionFlag = "連線";
                                        Ewatch_MySqlMethod.UpdataAi_Time(aiitem, false);
                                    }
                                }
                            }
                            else
                            {
                                //AiSettings.Single(g => g.PK == aiitem.PK).ConnectionFlag = "不使用";
                                if (Ewatch_MySqlMethod.AI64Load(aiitem))//檢查即時數值是否超過時間
                                {
                                    if (NewAiSettings.SingleOrDefault(g => g.PK == aiitem.PK) != null)
                                    {
                                        NewAiSettings.Single(g => g.PK == aiitem.PK).ConnectionFlag = "斷線";
                                        //Ewatch_MySqlMethod.UpdataAi_Time(aiitem, true);
                                    }
                                }
                                else
                                {
                                    if (NewAiSettings.SingleOrDefault(g => g.PK == aiitem.PK) != null)
                                    {
                                        NewAiSettings.Single(g => g.PK == aiitem.PK).ConnectionFlag = "連線";
                                        Ewatch_MySqlMethod.UpdataAi_Time(aiitem, false);
                                    }
                                }
                            }
                        }
                        #endregion

                        #region 電表接收
                        var electricvalue = Ewatch_MySqlMethod.ElectricLoad(caseitem);
                        foreach (var electricitem in electricvalue)
                        {
                            if (electricitem.NotifyFlag)//電表點位需要發報
                            {
                                if (Ewatch_MySqlMethod.ElectricMeterLoad(electricitem))
                                {
                                    var TimeValue = Ewatch_MySqlMethod.ElectricMeter_Time(electricitem);
                                    if (TimeValue == null || TimeValue >= electricitem.TimeoutSpan)
                                    {
                                        if (NewElectricSettings.SingleOrDefault(g => g.PK == electricitem.PK) != null)
                                        {
                                            NewElectricSettings.Single(g => g.PK == electricitem.PK).ConnectionFlag = "斷線";
                                            NotifyTypeEnum notifyTypeEnum = (NotifyTypeEnum)caseitem.NotifyTypeEnum;
                                            switch (notifyTypeEnum)
                                            {
                                            case NotifyTypeEnum.None:
                                                break;

                                            case NotifyTypeEnum.Line:
                                            {
                                                LineNotifyClass lineNotifyClass = new LineNotifyClass();
                                                lineNotifyClass.LineNotifyFunction(caseitem.NotifyToken, $"設備名稱:{electricitem.ElectricName} \r最後上傳時間 : {Ewatch_MySqlMethod.ElectricMeter_LastTime(electricitem)}\r上傳逾時請檢查");
                                            }
                                            break;

                                            case NotifyTypeEnum.Telegram:
                                            {
                                                TelegramBotClass telegramBotClass = new TelegramBotClass(caseitem.NotifyApi)
                                                {
                                                    Chat_ID = caseitem.NotifyToken
                                                };
                                                telegramBotClass.Send_Message_Group($"設備名稱:{electricitem.ElectricName} \r最後上傳時間 : {Ewatch_MySqlMethod.ElectricMeter_LastTime(electricitem)}\r上傳逾時請檢查");
                                            }
                                            break;
                                            }
                                            Ewatch_MySqlMethod.UpdataElectricMeter_Time(electricitem, true);
                                        }
                                    }
                                    else
                                    {
                                        NewElectricSettings.Single(g => g.PK == electricitem.PK).ConnectionFlag = "斷線";
                                    }
                                }
                                else
                                {
                                    if (NewElectricSettings.SingleOrDefault(g => g.PK == electricitem.PK) != null)
                                    {
                                        NewElectricSettings.Single(g => g.PK == electricitem.PK).ConnectionFlag = "連線";
                                        Ewatch_MySqlMethod.UpdataElectricMeter_Time(electricitem, false);
                                    }
                                }
                            }
                            else
                            {
                                //ElectricSettings.Single(g => g.PK == electricitem.PK).ConnectionFlag = "不使用";
                                var TimeValue = Ewatch_MySqlMethod.ElectricMeter_Time(electricitem);
                                if (TimeValue == null || TimeValue >= electricitem.TimeoutSpan)
                                {
                                    if (NewElectricSettings.SingleOrDefault(g => g.PK == electricitem.PK) != null)
                                    {
                                        NewElectricSettings.Single(g => g.PK == electricitem.PK).ConnectionFlag = "斷線";
                                    }
                                }
                                else
                                {
                                    if (NewElectricSettings.SingleOrDefault(g => g.PK == electricitem.PK) != null)
                                    {
                                        NewElectricSettings.Single(g => g.PK == electricitem.PK).ConnectionFlag = "連線";
                                        Ewatch_MySqlMethod.UpdataElectricMeter_Time(electricitem, false);
                                    }
                                }
                            }
                        }
                        #endregion
                    }
                    CaseSettings     = NewCaseSettings;
                    AiSettings       = NewAiSettings;
                    ElectricSettings = NewElectricSettings;
                    ConnectionTime   = DateTime.Now;
                }
                else
                {
                    Thread.Sleep(80);
                }
            }
        }