//Delegate function to check duplicated Tag/CalcTag ID from frmEditTag form bool CheckDuplicateTag(string tag_name, string type) { if (type == "TAG") { if (tag_list.Count > 0) { cls_Tag tag = tag_list.Where(p => p.TagName == tag_name).FirstOrDefault(); if (tag != null) { MessageBox.Show("Duplicate Tag Name!", "Error"); return(false); } } } else if (type == "CALC_TAG") { if (calc_tag_list.Count > 0) { cls_CalcTag calc_tag = calc_tag_list.Where(p => p.TagName == tag_name).FirstOrDefault(); if (calc_tag != null) { MessageBox.Show("Duplicate Calculation Tag Name!", "Error"); return(false); } } } else { MessageBox.Show("Wrong type string to check duplicated tag name!", "Error"); return(false); } return(true); }
private void lvTagList_DoubleClick(object sender, EventArgs e) { string strTagName; cls_Tag tag = new cls_Tag(); if (lvTagList.SelectedItems.Count == 0) { MessageBox.Show("Please select the tag first!", "Error"); return; } strTagName = lvTagList.SelectedItems[0].Text; int i = 0; foreach (cls_Tag t in tag_list) { if (t.TagName == strTagName) { tag = t; break; } i++; } this.tag_index = i; frmEditTag frm = new frmEditTag(SetTagInformation, tag); frm.Owner = this; frm.ShowDialog(); DisplayTagList(); }
private void btnTagRemove_Click(object sender, EventArgs e) { string strTagName; cls_Tag tmp = new cls_Tag(); if (lvTagList.SelectedItems.Count == 0) { MessageBox.Show("Please select the tag first!", "Error"); return; } if (MessageBox.Show("Are you sure to delete the tag?", "Confirm Message", MessageBoxButtons.OKCancel) != DialogResult.OK) { lvTagList.Focus(); return; } strTagName = lvTagList.SelectedItems[0].Text; if (!taglist_data.TryRemove(strTagName, out tmp)) { MessageBox.Show("Tag Remove failed", "Error"); return; } RefreshDeviceTagList(); }
public frmEditTag(SetTag set_tag, cls_Tag tag) { InitializeComponent(); this.isEdit = true; this.tag_data = tag; this.delgSetTag = set_tag; //this.delgCheckDuplicate = check_tag; }
//Delegate function to receive tag set data from frmEditTag form void SetTagInformation(cls_Tag tag, bool edit) { if (edit) { tag_list[tag_index] = tag; return; } else { tag_list.Add(tag); return; } }
private void lvTagList_DoubleClick(object sender, EventArgs e) { string strTagName; cls_Tag tag = new cls_Tag(); if (lvTagList.SelectedItems.Count == 0) { MessageBox.Show("Please select the tag first!", "Error"); return; } strTagName = lvTagList.SelectedItems[0].Text; if (!taglist_data.TryGetValue(strTagName, out tag)) { MessageBox.Show("Get tag[" + strTagName + "] information error", "Error"); return; } frmEditTag frm = new frmEditTag(SetTagInformation, tag); frm.Owner = this; frm.ShowDialog(); RefreshDeviceTagList(); }
//Delegate function to recieve tag data from frmEditTag form void SetTagInformation(cls_Tag tag, bool edit) { if (edit) { cls_Tag tmp = new cls_Tag(); if (taglist_data.TryGetValue(tag.TagName, out tmp)) { if (!taglist_data.TryUpdate(tag.TagName, tag, tmp)) { MessageBox.Show("Tag Update failed!", "Error"); return; } } } else { if (!taglist_data.TryAdd(tag.TagName, tag)) { MessageBox.Show("Tag Add failed!", "Error"); return; } } }
private void btnSaveEDCXml_Click(object sender, EventArgs e) { cls_EDC_Info tmpEDC = new cls_EDC_Info(); List <Tuple <string, string> > tmp_tag_info = new List <Tuple <string, string> >(); List <Tuple <string, string> > tmp_calc_tag_info = new List <Tuple <string, string> >(); if (txtSerial.Text.Trim() == "") { MessageBox.Show("Please input the serial id!", "Error"); return; } if (cmbGateway.Text.Trim() == "") { MessageBox.Show("Please select the gateway id!", "Error"); return; } if (cmbDevice.Text.Trim() == "") { MessageBox.Show("Please select the device id!", "Error"); return; } if (cmbReportType.Text.Trim() == "") { MessageBox.Show("Please select the report type!", "Error"); return; } if (cmbReportType.Text.Trim() == "interval") { if (txtReportInterval.Text.Trim() == "") { MessageBox.Show("Please input the report interval!", "Error"); return; } else { int value; if (!int.TryParse(txtReportInterval.Text.Trim(), out value)) { MessageBox.Show("Report Interval is number only!", "Error"); return; } tmpEDC.report_interval = int.Parse(txtReportInterval.Text.Trim()); if (chkAVG.Checked) { tmpEDC.interval_function.Add("AVG"); } if (chkMAX.Checked) { tmpEDC.interval_function.Add("MAX"); } if (chkMIN.Checked) { tmpEDC.interval_function.Add("MIN"); } if (tmpEDC.interval_function.Count == 0) { MessageBox.Show("Please checked at lease one interval function!", "Error"); return; } } } if (txtReportPath.Text.Trim() == "") { MessageBox.Show("Please input the report path!", "Error"); return; } if (!this.isEdit) { if (cmbEDCHeaderSet.Text.Trim() == "") { MessageBox.Show("Please select the EDC Header set first!", "Error"); return; } } tmpEDC.serial_id = txtSerial.Text.Trim(); tmpEDC.gateway_id = cmbGateway.Text.Trim(); tmpEDC.device_id = cmbDevice.Text.Trim(); tmpEDC.report_tpye = cmbReportType.Text.Trim(); tmpEDC.ReportEDCPath = txtReportPath.Text.Trim(); if (chkEnable.Checked) { tmpEDC.enable = true; } else { tmpEDC.enable = false; } if (this.isEdit) { tmpEDC.edchead_info = this.edc_data.edchead_info; } else { foreach (cls_EDC_Header h in this.edc_header_list.head_set_list) { if (h.set_name == cmbEDCHeaderSet.Text.Trim()) { tmpEDC.edchead_info = h.head_set; } } } foreach (ListViewItem item in lvTagList.Items) { cls_Tag t = this.gateway_mgr.gateway_list[gateway_index].device_info[device_index].tag_info[item.Text.Trim()]; if (t != null) { if (item.Checked) { this.gateway_mgr.gateway_list[gateway_index].device_info[device_index].tag_info[item.Text.Trim()].report_flag = "Y"; } else { this.gateway_mgr.gateway_list[gateway_index].device_info[device_index].tag_info[item.Text.Trim()].report_flag = "N"; } } } foreach (KeyValuePair <string, cls_Tag> tag in this.gateway_mgr.gateway_list[gateway_index].device_info[device_index].tag_info) { if (tag.Value.report_flag == "Y") { tmp_tag_info.Add(Tuple.Create(tag.Key, tag.Key)); } } foreach (ListViewItem item in lvCalcTagList.Items) { if (item.Checked) { cls_CalcTag ct = this.gateway_mgr.gateway_list[gateway_index].device_info[device_index].calc_tag_info[item.Text.Trim()]; if (ct != null) { tmp_calc_tag_info.Add(Tuple.Create(ct.TagName, ct.TagName)); } } } tmpEDC.tag_info = tmp_tag_info; tmpEDC.calc_tag_info = tmp_calc_tag_info; if (!this.isEdit) { delgSetSerial(this.serial_index); } if (this.isCopy) { delgSetEDCXmlInfo(tmpEDC, false); } else { delgSetEDCXmlInfo(tmpEDC, this.isEdit); } if (!this.gateway_mgr.gateway_list[this.gateway_index].function_list.Contains("EDC")) { this.gateway_mgr.gateway_list[this.gateway_index].function_list.Add("EDC"); } this.Close(); }
public void ThreadPool_Proc_Physical(string msg) { try { cls_read_data_reply CollectData = null; CollectData = JsonConvert.DeserializeObject <cls_read_data_reply>(msg.ToString()); cls_ProcRecv_CollectData ProcRecv_CollectData = new cls_ProcRecv_CollectData(); ProcRecv_CollectData.GateWayID = _GatewayID; ProcRecv_CollectData.Device_ID = _DeviceID; if (CollectData != null) { // 直接平行處理對應所有Tag Parallel.ForEach(CollectData.EDC_Data, p => { cls_Tag Device_Tag = _Device.tag_info[p.DATA_NAME]; string Tag_Value = p.DATA_VALUE; int BitPoints = 0; double tmp_output = 0; string Output = string.Empty; if (Device_Tag.Type == "PLC") { if (CheckWordType.Any(x => Device_Tag.UUID_Address.Contains(x))) { if (Tag_Value.Length % 4 != 0) { return; } switch (Device_Tag.Expression) { case "BIT": BitPoints = CalcBitPoints(Device_Tag.UUID_Address); if (BitPoints != 0) { Output = HexToBit(0, BitPoints, Tag_Value); } else { Output = string.Empty; } break; case "UINT": BitPoints = 16; // Int固定16 bit ; tmp_output = HexToInt(BitPoints, Tag_Value); tmp_output = (Device_Tag.scale * tmp_output) + Device_Tag.offset; Output = tmp_output.ToString(); break; case "ULONG": BitPoints = 32; tmp_output = HexToLong(BitPoints, Tag_Value); tmp_output = (Device_Tag.scale * tmp_output) + Device_Tag.offset; Output = tmp_output.ToString(); break; case "SINT": BitPoints = 16; tmp_output = HexToSInt(BitPoints, Tag_Value); tmp_output = (Device_Tag.scale * tmp_output) + Device_Tag.offset; Output = tmp_output.ToString(); break; case "SLONG": BitPoints = 32; tmp_output = HexToSLong(BitPoints, Tag_Value); tmp_output = (Device_Tag.scale * tmp_output) + Device_Tag.offset; Output = tmp_output.ToString(); break; case "ASC": Output = HexToASCII(Tag_Value); // 目前Decode 全部都解析解析完再考慮長度 break; default: Output = string.Empty; break; } } } if (Output != string.Empty) { ProcRecv_CollectData.Prod_EDC_Data.Enqueue(Tuple.Create(p.DATA_NAME, Output)); } }); } this.Put_ProcRecv_CollectData(ProcRecv_CollectData); } catch (Exception ex) { throw new Exception(string.Format("Handle ProcCollectData Message Error [{0}].", ex.Message)); } }
public void ProcCalcTag(string GateWayID, string Device_ID) { cls_Gateway_Info gateway = _objectmanager.GatewayManager.gateway_list.Where(p => p.gateway_id == GateWayID).FirstOrDefault(); if (gateway != null) { cls_Device_Info device = gateway.device_info.Where(p => p.device_name == Device_ID).FirstOrDefault(); if (device != null) { foreach (KeyValuePair <string, cls_CalcTag> kvp in device.calc_tag_info) { cls_Tag tagA = null; cls_Tag tagB = null; cls_Tag tagC = null; cls_Tag tagD = null; cls_Tag tagE = null; cls_Tag tagF = null; cls_Tag tagG = null; cls_Tag tagH = null; Double douA = -1; Double douB = -1; Double douC = -1; Double douD = -1; Double douE = -1; Double douF = -1; Double douG = -1; Double douH = -1; Double douResult = -999999.999; if (kvp.Value.ParamA.Trim() != "") { // for vic verify dictionary key exist or not. if (device.tag_info.ContainsKey(kvp.Value.ParamA)) { tagA = device.tag_info[kvp.Value.ParamA]; } } if (kvp.Value.ParamB.Trim() != "") { tagB = device.tag_info[kvp.Value.ParamB]; } if (kvp.Value.ParamC.Trim() != "") { tagC = device.tag_info[kvp.Value.ParamC]; } if (kvp.Value.ParamD.Trim() != "") { tagD = device.tag_info[kvp.Value.ParamD]; } if (kvp.Value.ParamE.Trim() != "") { tagE = device.tag_info[kvp.Value.ParamE]; } if (kvp.Value.ParamF.Trim() != "") { tagF = device.tag_info[kvp.Value.ParamF]; } if (kvp.Value.ParamG.Trim() != "") { tagG = device.tag_info[kvp.Value.ParamG]; } if (kvp.Value.ParamH.Trim() != "") { tagH = device.tag_info[kvp.Value.ParamH]; } try { douA = Convert.ToDouble(tagA.Value); douB = Convert.ToDouble(tagB.Value); douC = Convert.ToDouble(tagC.Value); douD = Convert.ToDouble(tagD.Value); douE = Convert.ToDouble(tagE.Value); douF = Convert.ToDouble(tagF.Value); douG = Convert.ToDouble(tagG.Value); douH = Convert.ToDouble(tagH.Value); ExpressionCalculator exp_calc = new ExpressionCalculator(kvp.Value.Expression, douA, douB, douC, douD, douE, douF, douG, douH); douResult = exp_calc.Evaluate(); kvp.Value.Value = douResult.ToString(); } catch (Exception ex) { douResult = -999999.999; } } } } }
private void btnSaveDBConfig_Click(object sender, EventArgs e) { cls_DB_Info tmpDB = new cls_DB_Info(); List <Tuple <string, string> > tmp_tag_info = new List <Tuple <string, string> >(); List <Tuple <string, string> > tmp_calc_tag_info = new List <Tuple <string, string> >(); if (cmbGateway.Text.Trim() == "") { MessageBox.Show("Please select the gateway id!", "Error"); return; } if (cmbDevice.Text.Trim() == "") { MessageBox.Show("Please select the device id!", "Error"); return; } if (!this.isEdit) { if (!delgCheckDuplicate(cmbGateway.Text.Trim(), cmbDevice.Text.Trim())) { MessageBox.Show("Gateway ID + Device ID should be an unique key!", "Error"); return; } } if (cmbDBType.Text.Trim() == "") { MessageBox.Show("Please select the DB type!", "Error"); return; } if (txtDataSource.Text.Trim() == "") { MessageBox.Show("Please input the data source!", "Error"); return; } if (txtPortID.Text.Trim() == "") { MessageBox.Show("Please input the port id!", "Error"); return; } if (txtConnectDB.Text.Trim() == "") { MessageBox.Show("Please input the database name!", "Error"); return; } if (txtDataSource.Text.Trim() == "") { MessageBox.Show("Please input the data source!", "Error"); return; } if (txtUserName.Text.Trim() == "") { MessageBox.Show("Please input the user name!", "Error"); return; } if (txtPassword.Text.Trim() == "") { MessageBox.Show("Please input the password!", "Error"); return; } tmpDB.serial_id = txtSerial.Text.Trim(); tmpDB.gateway_id = cmbGateway.Text.Trim(); tmpDB.device_id = cmbDevice.Text.Trim(); tmpDB.db_type = cmbDBType.Text.Trim(); tmpDB.data_source = txtDataSource.Text.Trim(); tmpDB.port_id = txtPortID.Text.Trim(); tmpDB.db_name = txtConnectDB.Text.Trim(); tmpDB.user_name = txtUserName.Text.Trim(); //tmpDB.password = EncryptionHelper.Encrypt(txtPassword.Text.Trim()); tmpDB.password = txtPassword.Text.Trim(); if (generate_connection_string(cmbDBType.Text.Trim()) == null) { MessageBox.Show("DB type not supported yet", "Error"); return; } else { tmpDB.connection_string = generate_connection_string(cmbDBType.Text.Trim()); } if (get_provider_name(cmbDBType.Text.Trim()) == null) { MessageBox.Show("DB type not supported yet", "Error"); return; } else { tmpDB.provider_name = get_provider_name(cmbDBType.Text.Trim()); } if (chkEnable.Checked) { tmpDB.enable = true; } else { tmpDB.enable = false; } foreach (ListViewItem item in lvTagList.Items) { cls_Tag t = this.gateway_mgr.gateway_list[gateway_index].device_info[device_index].tag_info[item.Text.Trim()]; if (t != null) { if (item.Checked) { this.gateway_mgr.gateway_list[gateway_index].device_info[device_index].tag_info[item.Text.Trim()].db_report_flag = "Y"; } else { this.gateway_mgr.gateway_list[gateway_index].device_info[device_index].tag_info[item.Text.Trim()].db_report_flag = "N"; } } } foreach (KeyValuePair <string, cls_Tag> tag in this.gateway_mgr.gateway_list[gateway_index].device_info[device_index].tag_info) { if (tag.Value.db_report_flag == "Y") { tmp_tag_info.Add(Tuple.Create(tag.Key, tag.Key)); } } foreach (ListViewItem item in lvCalcTagList.Items) { if (item.Checked) { cls_CalcTag ct = this.gateway_mgr.gateway_list[gateway_index].device_info[device_index].calc_tag_info[item.Text.Trim()]; if (ct != null) { tmp_calc_tag_info.Add(Tuple.Create(ct.TagName, ct.TagName)); } } } /* * foreach (KeyValuePair<string, cls_CalcTag> calc_tag in this.gateway_mgr.gateway_list[gateway_index].device_info[device_index].calc_tag_info) * { * tmp_calc_tag_info.Add(Tuple.Create(calc_tag.Key, calc_tag.Key)); * } */ tmpDB.tag_info = tmp_tag_info; tmpDB.calc_tag_info = tmp_calc_tag_info; if (!this.isEdit) { delgSetDBSerial(this.serial_index); } if (this.isCopy) { delgSetDBInfo(tmpDB, false); } else { delgSetDBInfo(tmpDB, this.isEdit); } if (!this.gateway_mgr.gateway_list[this.gateway_index].function_list.Contains("DB")) { this.gateway_mgr.gateway_list[this.gateway_index].function_list.Add("DB"); } this.Close(); }
private void btnTagSave_Click(object sender, EventArgs e) { cls_Tag tmpTag = new cls_Tag(); int iValue; string end_address; if (txtTagName.Text == "") { MessageBox.Show("Please enter Tag Name!", "Error"); return; } else { if (!this.isEdit) //Check duplicated id { if (!delgCheckDuplicate(txtTagName.Text.Trim(), "TAG")) { return; } } } tmpTag.TagName = txtTagName.Text.Trim(); if (cmbDataType.Text == "") { MessageBox.Show("Please select the data type!", "Error"); return; } tmpTag.Type = cmbDataType.Text.Trim(); if (cmbWordBit.Text == "") { MessageBox.Show("Please select the Word/Bit information!", "Error"); return; } if (cmbDataType.Text.Trim() == "Virtual") { double dScale; double dOffset; if (cmbWordBit.Text.Trim() == "INT" || cmbWordBit.Text.Trim() == "DOUBLE") { if (double.TryParse(txtScale.Text.Trim(), out dScale)) { tmpTag.scale = dScale; } else { MessageBox.Show("Scale must be a double value", "Error"); return; } if (double.TryParse(txtOffset.Text.Trim(), out dOffset)) { tmpTag.offset = dOffset; } else { MessageBox.Show("Offset must be a double value", "Error"); return; } } } else { if (txtStartAddress.Text == "") { MessageBox.Show("Please enter start address!", "Error"); return; } else { // Check if the first character equals to 'B', 'D', 'M', 'W', "ZR" if (ALLOWED_ADDRESS.Contains(txtStartAddress.Text.Trim().Substring(0, 1))) { if (txtStartAddress.Text.Trim().Substring(0, 1) == "Z") { if (txtStartAddress.Text.Trim().Substring(1, 1) != "R") { MessageBox.Show("The first character of Start Address should be B, D, M, W, ZR", "Error"); return; } } } else { MessageBox.Show("The first character of Start Address should be B, D, M, W, ZR", "Error"); return; } int start_index; if (txtStartAddress.Text.Trim().Substring(0, 1) == "Z") { start_index = 2; } else { start_index = 1; } if (!int.TryParse(txtStartAddress.Text.Trim().Substring(start_index), out iValue)) { MessageBox.Show("Start Address format is wrong!", "Error"); return; } } if (cmbWordBit.Text == "BIT") { if (txtStartBit.Text == "") { MessageBox.Show("Please enter the start bit!", "Error"); return; } else { if (int.TryParse(txtStartBit.Text, out iValue)) { if (iValue < 0 || iValue > 15) { MessageBox.Show("Please enter the correct start bit!", "Error"); return; } } else { MessageBox.Show("Number only for Start Bit!", "Error"); return; } } if (txtEndBit.Text == "") { MessageBox.Show("Please enter the end bit!", "Error"); return; } else { if (int.TryParse(txtEndBit.Text, out iValue)) { if (iValue < 0 || iValue > 15) { MessageBox.Show("Please enter the correct end bit!", "Error"); return; } } else { MessageBox.Show("Number only for End Bit!", "Error"); return; } } tmpTag.UUID_Address = txtStartAddress.Text.Trim() + "." + txtStartBit.Text.Trim() + ":" + txtEndBit.Text.Trim(); } else if (cmbWordBit.Text == "ASC") { if (txtLength.Text.Trim() == "") { MessageBox.Show("Please enter the length", "Error"); return; } if (int.TryParse(txtLength.Text.Trim(), out iValue)) { if (iValue <= 0) { MessageBox.Show("Length must > 1", "Error"); return; } } else { MessageBox.Show("Number only for Length!", "Error"); return; } end_address = get_end_address(txtStartAddress.Text.Trim(), int.Parse(txtLength.Text.Trim())); tmpTag.UUID_Address = txtStartAddress.Text.Trim() + ":" + end_address; } else { switch (cmbWordBit.Text.Trim()) { case "SINT": end_address = get_end_address(txtStartAddress.Text.Trim(), SINT_LENGTH); tmpTag.UUID_Address = txtStartAddress.Text.Trim() + ":" + end_address; break; case "UINT": end_address = get_end_address(txtStartAddress.Text.Trim(), UINT_LENGTH); tmpTag.UUID_Address = txtStartAddress.Text.Trim() + ":" + end_address; break; case "SLONG": end_address = get_end_address(txtStartAddress.Text.Trim(), SLONG_LENGTH); tmpTag.UUID_Address = txtStartAddress.Text.Trim() + ":" + end_address; break; case "ULONG": end_address = get_end_address(txtStartAddress.Text.Trim(), ULONG_LENGTH); tmpTag.UUID_Address = txtStartAddress.Text.Trim() + ":" + end_address; break; default: break; } double dScale; double dOffset; if (double.TryParse(txtScale.Text.Trim(), out dScale)) { tmpTag.scale = dScale; } else { MessageBox.Show("Scale must be a double value", "Error"); return; } if (double.TryParse(txtOffset.Text.Trim(), out dOffset)) { tmpTag.offset = dOffset; } else { MessageBox.Show("Offset must be a double value", "Error"); return; } } } tmpTag.Expression = cmbWordBit.Text.Trim(); tmpTag.Description = txtDescription.Text.Trim(); tmpTag.LastUpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); delgSetTag(tmpTag, this.isEdit); this.Close(); }