ibms-dome/z01_WinAPP/fmCheckTagName.cs

490 lines
20 KiB
C#
Raw Normal View History

2022-10-14 16:08:54 +08:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using tpDomeWinAPP.Service;
using tpDomeWinAPP.Models;
using System.Linq;
using System.Collections;
namespace tpDomeWinAPP
{
public partial class fmCheckTagName : Form
{
string url_slot = string.Empty;
public string building { get; set; }
string top100 = " top 100 ";
List<device_value2> ds;
List<NiagaraTags> ds2;
procCompare svcCompare = new procCompare();
public fmCheckTagName()
{
InitializeComponent();
}
private void fmCheckTagName_Load(object sender, EventArgs e)
{
// MessageBox.Show(tabControl1.TabPages[2].Text);
#region DB -> Niagara
fmDB2Niagara f2 = new fmDB2Niagara();
f2.TopLevel = false;
f2.Parent = tabControl1.TabPages[2];
f2.FormBorderStyle = FormBorderStyle.None;
f2.Dock = DockStyle.Fill;
f2.Show();
#endregion
#region Excel
fmImportXls f3 = new fmImportXls();
f3.TopLevel = false;
f3.Parent = tabControl1.TabPages[3];
f3.FormBorderStyle = FormBorderStyle.None;
f3.Dock = DockStyle.Fill;
f3.Show();
#endregion
#region
fmObixStatus f4 = new fmObixStatus();
f4.TopLevel = false;
f4.Parent = tabControl1.TabPages[4];
f4.FormBorderStyle = FormBorderStyle.None;
f4.Dock = DockStyle.Fill;
f4.Show();
#endregion
}
async private void btLoad_Click(object sender, EventArgs e)
{
ds = null;
dataGridView1.DataSource = null;
loadData();
//load form
//var tab = tabControl1.TabPages[2];
//tabControl1.TabPages[2].Controls.
//if (dsDevice.Count == 0) loadData();
//var ds = from data in dsDevice
// where data.kind == 4
// select data;
//if (ds.Count() > 0)
// dataGridView1.DataSource = ds.ToList();
//getFireAsync(1, ds, dataGridView1);
}
async private void rbM_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = (sender as RadioButton);
if (rb.Checked)
{
switch (rb.Name)
{
case "rbH": url_slot = "slot:/Arena/H|"; break;
case "rbOffice": url_slot = "slot:/Arena/O|"; break;
case "rbCinema": url_slot = "slot:/Arena/C|"; break;
case "rbDome2": url_slot = "slot:/Arena/D2|"; break;
case "rbDome3": url_slot = "slot:/Arena/D3|"; break;
case "rbM": url_slot = "slot:/Arena/M|"; break;
}
building = rb.Tag.ToString();
// DB -> Niagara form
//tabControl1.TabPages[2].Controls[0]
//btLoad.PerformClick();
}
}
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
int index = 0;
int spliteCell = 1;//每三行換一個顏色
DataGridView dg = (sender as DataGridView);
//foreach (DataGridViewRow dgv in dataGridView1.Rows)
foreach (DataGridViewRow dgv in dg.Rows)
{
if (index % 2 == 0)
dgv.DefaultCellStyle.BackColor = SystemColors.GradientActiveCaption; //改row的顏色
else
dgv.DefaultCellStyle.BackColor = Color.White; //改row的顏色
index++;
}
}
private void rb100_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = (sender as RadioButton);
if (rb.Checked)
{
switch (rb.Name)
{
case "rb100": top100 = " top 100 "; break;
default : top100 = ""; break;
}
//btLoad.PerformClick();
}
}
private void btTagCompare_Click(object sender, EventArgs e)
{
if (ds != null && ds.Count > 0)
{
svcCompare.InsertNiagaraTagList(ds, building);
lbMsg2.Text = "Done!" + Environment.NewLine + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
else
MessageBox.Show("查無資料 無法比對!");
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
//switch ((sender as TabControl).SelectedIndex)
//{
// case 0:
// break;
// case 1:
// break;
//}
}
public void loadData()
{
lbMsg.Text = "Loading ...";
if (tabControl1.SelectedIndex == 0)
{
webRequestSvc svc = new webRequestSvc();
string bql = url_slot + "bql:select " + top100 + " * from control:ControlPoint ";
2022-10-19 13:24:45 +08:00
//ds = svc.obixQuery("http://60.251.164.125:8080/obix/config/Arena/Program/ObixQuery/query/", bql);//原本的
ds = svc.obixQuery("http://localhost:8080/obix/config/Program/ObixQuery/query/", bql);
2022-10-14 16:08:54 +08:00
dataGridView1.DataSource = ds;
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns[1].Width = 400;
dataGridView1.Columns[2].Width = 200;
lbMsg.Text = "共 " + ds.Count.ToString() + " 筆資料 " + Environment.NewLine + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
else if (tabControl1.SelectedIndex == 1)
{
ds2 = null;
dataGridView2.DataSource = null;
//procCompare svc = new procCompare();
ds2 = svcCompare.GetNiagaraTags(building);
dataGridView2.DataSource = ds2;
dataGridView2.Columns[0].Visible = false;
dataGridView2.Columns[1].Width = 200;
dataGridView2.Columns[2].Width = 200;
dataGridView2.Columns[3].Width = 150;
lbMsg2.Text = "Done!" + Environment.NewLine + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
else if (tabControl1.SelectedIndex == 2)
{
//procCompare svc = new procCompare();
// ref https://stackoverflow.com/questions/3419159/how-to-get-all-child-controls-of-a-windows-forms-form-of-a-specific-type-button?noredirect=1&lq=1
var dgs = GetAll(tabControl1.TabPages[2].Controls[0], typeof(DataGridView));
DataGridView grid = (DataGridView)dgs.Where(x => x.Name == "dataGridView2").FirstOrDefault();
grid.DataSource = null;
var dt = svcCompare.SearchDBTags(building);
//Set AutoGenerateColumns False
grid.AutoGenerateColumns = false;
grid.EnableHeadersVisualStyles = true;
//Set Columns Count
grid.ColumnCount = 12;
grid.AllowUserToOrderColumns = true;
#region Grid column Setting
grid.Columns[0].Name = "Device_guid";
grid.Columns[0].HeaderText = "Device_guid";
grid.Columns[0].DataPropertyName = "Device_guid";
grid.Columns[1].Name = "device_name";
grid.Columns[1].HeaderText = "device_name";
grid.Columns[1].DataPropertyName = "full_name";
grid.Columns[2].Name = "db_tag";
grid.Columns[2].HeaderText = "db_tag";
grid.Columns[2].DataPropertyName = "device_number";
grid.Columns[2].Width = 200;
grid.Columns[3].Name = "niagaraTag";
grid.Columns[3].HeaderText = "Niagara Tag";
grid.Columns[3].DataPropertyName = "niagaraTag";
grid.Columns[3].Width = 200;
//grid.Columns[4].Name = "db_tag_old";
//grid.Columns[4].HeaderText = "db_tag_old";
//grid.Columns[4].DataPropertyName = "device_number_old";
grid.Columns[4].Name = "building";
grid.Columns[4].HeaderText = "building";
grid.Columns[4].DataPropertyName = "device_building_tag";
grid.Columns[4].Width = 80;
grid.Columns[5].Name = "system_code1";
grid.Columns[5].HeaderText = "system_code1";
grid.Columns[5].DataPropertyName = "device_system_tag";
grid.Columns[5].Width = 100;
grid.Columns[6].Name = "old_system";
grid.Columns[6].HeaderText = "old_system_code1";
grid.Columns[6].DataPropertyName = "device_system_tag_old";
grid.Columns[6].Width = 100;
grid.Columns[7].Name = "floor";
grid.Columns[7].HeaderText = "floor";
grid.Columns[7].DataPropertyName = "device_floor_tag";
grid.Columns[7].Width = 80;
grid.Columns[8].Name = "system_code2";
grid.Columns[8].HeaderText = "system_code2";
grid.Columns[8].DataPropertyName = "device_name_tag";
grid.Columns[8].Width = 80;
grid.Columns[9].Name = "serial";
grid.Columns[9].HeaderText = "serial";
grid.Columns[9].DataPropertyName = "device_serial_tag";
grid.Columns[9].Width = 80;
grid.Columns[10].Name = "coordinate";
grid.Columns[10].HeaderText = "coordinate";
grid.Columns[10].DataPropertyName = "device_coordinate";
grid.Columns[10].Width = 80;
grid.Columns[11].Name = "atDateTime";
grid.Columns[11].HeaderText = "atDateTime";
grid.Columns[11].DataPropertyName = "atDateTime";
grid.Columns[11].Width = 100;
grid.DataBindingComplete += dataGridView1_DataBindingComplete;
grid.DataSource = dt;
#endregion
var labels = GetAll(tabControl1.TabPages[2].Controls[0], typeof(Label));
Label lbMsg2 = (Label)labels.Where(x => x.Name == "lbMsg").FirstOrDefault();
lbMsg2.Text = "共 " + dt.Count.ToString() + " 筆資料 " + Environment.NewLine + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
else if (tabControl1.SelectedIndex == 4)
{ // 取得即時狀態
var dgs = GetAll(tabControl1.TabPages[4].Controls[0], typeof(DataGridView));
DataGridView grid = (DataGridView)dgs.Where(x => x.Name == "dgv").FirstOrDefault();
grid.DataSource = null;
var dt = svcCompare.SearchDBTags(building);
//Set AutoGenerateColumns False
grid.AutoGenerateColumns = false;
grid.EnableHeadersVisualStyles = true;
//Set Columns Count
grid.ColumnCount = 2;
grid.AllowUserToOrderColumns = true;
#region Grid column Setting
//grid.Columns[0].Name = "Device_guid";
//grid.Columns[0].HeaderText = "Device_guid";
//grid.Columns[0].DataPropertyName = "Device_guid";
grid.Columns[0].Name = "device_name";
grid.Columns[0].HeaderText = "device_name";
grid.Columns[0].DataPropertyName = "full_name";
grid.Columns[1].Name = "db_tag";
grid.Columns[1].HeaderText = "db_tag";
grid.Columns[1].DataPropertyName = "device_number";
grid.Columns[1].Width = 200;
//grid.Columns[3].Name = "niagaraTag";
//grid.Columns[3].HeaderText = "Niagara Tag";
//grid.Columns[3].DataPropertyName = "niagaraTag";
//grid.Columns[3].Width = 200;
//grid.Columns[4].Name = "db_tag_old";
//grid.Columns[4].HeaderText = "db_tag_old";
//grid.Columns[4].DataPropertyName = "device_number_old";
//grid.Columns[4].Name = "building";
//grid.Columns[4].HeaderText = "building";
//grid.Columns[4].DataPropertyName = "device_building_tag";
//grid.Columns[4].Width = 80;
//grid.Columns[5].Name = "system_code1";
//grid.Columns[5].HeaderText = "system_code1";
//grid.Columns[5].DataPropertyName = "device_system_tag";
//grid.Columns[5].Width = 100;
//grid.Columns[6].Name = "old_system";
//grid.Columns[6].HeaderText = "old_system_code1";
//grid.Columns[6].DataPropertyName = "device_system_tag_old";
//grid.Columns[6].Width = 100;
//grid.Columns[7].Name = "floor";
//grid.Columns[7].HeaderText = "floor";
//grid.Columns[7].DataPropertyName = "device_floor_tag";
//grid.Columns[7].Width = 80;
//grid.Columns[8].Name = "system_code2";
//grid.Columns[8].HeaderText = "system_code2";
//grid.Columns[8].DataPropertyName = "device_name_tag";
//grid.Columns[8].Width = 80;
//grid.Columns[9].Name = "serial";
//grid.Columns[9].HeaderText = "serial";
//grid.Columns[9].DataPropertyName = "device_serial_tag";
//grid.Columns[9].Width = 80;
//grid.Columns[10].Name = "coordinate";
//grid.Columns[10].HeaderText = "coordinate";
//grid.Columns[10].DataPropertyName = "device_coordinate";
//grid.Columns[10].Width = 80;
//grid.Columns[11].Name = "atDateTime";
//grid.Columns[11].HeaderText = "atDateTime";
//grid.Columns[11].DataPropertyName = "atDateTime";
//grid.Columns[11].Width = 100;
grid.DataBindingComplete += dataGridView1_DataBindingComplete;
grid.DataSource = dt;
#endregion
var labels = GetAll(tabControl1.TabPages[4].Controls[0], typeof(Label));
Label lbMsg2 = (Label)labels.Where(x => x.Name == "lbMsg").FirstOrDefault();
lbMsg2.Text = "共 " + dt.Count.ToString() + " 筆資料 " + Environment.NewLine + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
}
public IEnumerable<Control> GetAll(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetAll(ctrl, type))
.Concat(controls)
.Where(c => c.GetType() == type);
}
/// <summary>
/// Niagara tag vs DB tag
/// 僅顯示 DBtag 缺失部分
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cbShowEmpty_CheckedChanged(object sender, EventArgs e)
{
//if (cbShowEmpty.Checked)
//{
// if (ds2.Count > 0)
// {
// var dds2 = ds2.Where(x => string.IsNullOrEmpty(x.db_tags));
// dataGridView2.DataSource = dds2;
// lbMsg2.Text = "共 " + dds2.Count().ToString() + " 筆資料 " + Environment.NewLine + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
// }
//}
}
//enum tag_sec
//{
// building = 1,
// system1
//}
/// <summary>
/// Niagara tag vs DB tag
/// 查詢 2 3 4 5碼 關鍵字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btSearch_N4vsDB_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (Control c in tableLayoutPanel1.Controls)
{
if (c is TextBox)
{
if (c.Text.Trim().Length > 0)
{
string num = c.Name.Substring(c.Name.Length - 1, 1);//取出最後的數字
switch (num)
{
//case "1": sb.Append($@" and building like '%{c.Text.Trim()}%' "); break;
case "2": sb.Append($@" and system_code1 like '%{c.Text.Trim()}%' "); break;
case "3": sb.Append($@" and [floor] like '%{c.Text.Trim()}%' "); break;
case "4": sb.Append($@" and system_code2 like '%{c.Text.Trim()}%' "); break;
case "5": sb.Append($@" and device_serial like '%{c.Text.Trim()}%' "); break;
}
}
}
}
2022-10-19 13:24:45 +08:00
sb.Insert(0, $@"select * from import_niagara_tag where building = '{building}' ");
2022-10-14 16:08:54 +08:00
if (cbShowEmpty.Checked)
{
sb.Append( $@" and db_tags is null;");
}
//procCompare svc = new procCompare();
ds2 = svcCompare.SearchNiagaraTags(sb.ToString());
dataGridView2.DataSource = ds2;
lbMsg2.Text = "共 " + ds2.Count().ToString() + " 筆資料 " + Environment.NewLine + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
2022-10-19 13:24:45 +08:00
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
2022-10-14 16:08:54 +08:00
//public async void getFireAsync(int deviceKind, IEnumerable<device> ds, DataGridView gv)
//{
// ///消防設備一次報警1http://greencloud.fic.com.tw:8080/obix/config/NHI/Taipei/Fire/Fire1Level1/out
// //消防設備一次報警2 http://greencloud.fic.com.tw:8080/obix/config/NHI/Taipei/Fire/Fire2Level1/out
// //消防設備二次報警1 http://greencloud.fic.com.tw:8080/obix/config/NHI/Taipei/Fire/Fire1Level2/out
// //消防設備二次報警2 http://greencloud.fic.com.tw:8080/obix/config/NHI/Taipei/Fire/Fire2Level2/out
// //消防設備故障報警1 http://greencloud.fic.com.tw:8080/obix/config/NHI/Taipei/Fire/Fire1Trouble/out
// //消防設備故障報警2 http://greencloud.fic.com.tw:8080/obix/config/NHI/Taipei/Fire/Fire2Trouble/out
// string url; //http://greencloud.fic.com.tw:8080/obix/config/NHI/Taipei/Fire/ Fire1Level1/out
// url = (rbTest.Checked) ? "http://greencloud.fic.com.tw:8080/obix/config/NHI/Taipei/Fire/" : "http://192.168.10.6:8081/obix/config/NHI/Taipei/Fire/";
// // List<string> urls = new List<string>();
// List<device_value> device = new List<device_value>();
// for (int i = 0; i <= 5; i++)
// {
// string ss = string.Empty;
// switch (i)
// {
// case 0: ss = url + "Fire1Level1/out"; break;
// case 1: ss = url + "Fire2Level1/out"; break;
// case 2: ss = url + "Fire1Level2/out"; break;
// case 3: ss = url + "Fire2Level2/out"; break;
// case 4: ss = url + "Fire1Trouble/out"; break;
// case 5: ss = url + "Fire2Trouble/out"; break;
// }
// //urls.Add(ss);
// int itemNo = ((i + 1) % 2 == 1) ? 1 : 2;
// await proc.GetIotData(ss, device, "FireNo" + itemNo.ToString());
// }
// gv.DataSource = device;
// //return device;
//}
//public async void getAPIResultAsync(int deviceKind, IEnumerable<device> ds, DataGridView gv)
//{
// List<device_value> device = new List<device_value>();
// foreach (var item in ds)
// {
// await proc.GetIotData(item.API_URL, device, item.device_name);
// }
// gv.DataSource = device;
//}
}
}