public List <string> Get_EDC_Label_Data(string _Serial_ID, string _GateWayID, string _DeviceID) { string _Dictkey = string.Concat(_Serial_ID, "_", _GateWayID, "_", _DeviceID); ConcurrentDictionary <string, int> _Sub_EDC_Labels = this._EDC_Label_Data.GetOrAdd(_Dictkey, new ConcurrentDictionary <string, int>()); //---判斷空的Label 馬上再去DB下載一次 if (_Sub_EDC_Labels.Count() == 0) { cls_DB_Info DB_info = ObjectManager.DBManager.dbconfig_list.Where(o => o.serial_id == _Serial_ID).FirstOrDefault(); if (DB_info != null) { using (var db = new DBContext.IOT_DbContext(DB_info.db_type, DB_info.connection_string)) { var vEDC_Label_Result = db.IOT_DEVICE_EDC_LABEL.AsQueryable().Where(o => o.device_id == _DeviceID).ToList(); _Sub_EDC_Labels = this._EDC_Label_Data.GetOrAdd(_Dictkey, new ConcurrentDictionary <string, int>()); foreach (DBContext.IOT_DEVICE_EDC_LABEL _EDC_Label_key in vEDC_Label_Result) { _Sub_EDC_Labels.AddOrUpdate(_EDC_Label_key.data_name, _EDC_Label_key.data_index, (key, oldvalue) => _EDC_Label_key.data_index); } this._EDC_Label_Data.AddOrUpdate(_Dictkey, _Sub_EDC_Labels, (key, oldvalue) => _Sub_EDC_Labels); } } } _Sub_EDC_Labels = this._EDC_Label_Data.GetOrAdd(_Dictkey, new ConcurrentDictionary <string, int>()); return(_Sub_EDC_Labels.Keys.ToList()); }
//Delegate function to setup DB Config Information void SetDBConfigInfo(cls_DB_Info db_info, bool edit_flag) { if (edit_flag) { int i = 0; if (dbm.dbconfig_list.Count > 0) { foreach (cls_DB_Info db in dbm.dbconfig_list) { if (db.serial_id == db_info.serial_id) { break; } i++; } dbm.dbconfig_list[i] = db_info; } } else { dbm.dbconfig_list.Add(db_info); } }
//Constructor to Edit DB Config Information public frmEditDBConfig(SetDBInfo set_db_info, GateWayManager gwm, cls_DB_Info db_info, string gateway, string device, bool copy_flag) { InitializeComponent(); this.isEdit = true; this.isCopy = copy_flag; this.gateway_mgr = gwm;; this.db_data = db_info; this.gateway_id = gateway; this.device_id = device; this.delgSetDBInfo = set_db_info; }
//Delegate function check duplicate Gateway ID + Device ID bool CheckDuplicateDBConfig(string gateway_id, string device_id) { if (dbm.dbconfig_list.Count > 0) { cls_DB_Info db = dbm.dbconfig_list.Where(p => p.gateway_id == gateway_id && p.device_id == device_id).FirstOrDefault(); if (db != null) { //MessageBox.Show("Duplicate Gateway ID + Device ID", "Error"); return(false); } } return(true); }
public void SendConfigDBReply(string GateWayID, string DeviceID) { cls_DB_Info db_info = ObjectManager.DBManager.dbconfig_list.Where(p => p.gateway_id == GateWayID && p.device_id == DeviceID).FirstOrDefault(); if (db_info == null) { MessageBox.Show("Can't find [" + GateWayID + "][" + DeviceID + "] DB Config to send to IoTClient", "Error"); return; } string json_msg = JsonConvert.SerializeObject(db_info, Newtonsoft.Json.Formatting.Indented); xmlMessage SendOutMsg = new xmlMessage(); SendOutMsg.LineID = GateWayID; SendOutMsg.DeviceID = DeviceID; SendOutMsg.MQTTTopic = "Cmd_Config_DB"; SendOutMsg.MQTTPayload = json_msg; SendMQTTData(SendOutMsg); }
private void lvDBConfigList_DoubleClick(object sender, EventArgs e) { string strSerial; string strGatewayID; string strDeviceID; cls_DB_Info dbTemp = new cls_DB_Info(); if (lvDBConfigList.SelectedItems.Count == 0) { MessageBox.Show("Please select the DB config first!", "Error"); return; } strSerial = lvDBConfigList.SelectedItems[0].Text.Trim(); strGatewayID = lvDBConfigList.SelectedItems[0].SubItems[1].Text.Trim(); strDeviceID = lvDBConfigList.SelectedItems[0].SubItems[2].Text.Trim(); int i = 0; foreach (cls_DB_Info db in this.dbm.dbconfig_list) { if (db.serial_id == strSerial) { dbTemp = this.dbm.dbconfig_list[i]; break; } i++; } frmEditDBConfig frm = new frmEditDBConfig(SetDBConfigInfo, this.gateway_mgr, dbTemp, strGatewayID, strDeviceID, false); frm.Owner = this; frm.ShowDialog(); dbTemp = null; delgDBManager(dbm); RefreshDBConfigList(); lvDBConfigList.Focus(); }
public void Routine_Update_DB_Information(object timerState) { if (_Initial_Finished == false) { return; } try { // 先upload 再 Download if (_Sync_EDC_Label_Data.Count != 0) { foreach (var Key in _Sync_EDC_Label_Data.Keys) { ConcurrentDictionary <string, int> _Process; _Sync_EDC_Label_Data.TryRemove(Key, out _Process); NLogManager.Logger.LogInfo(LogName, GetType().Name, MethodInfo.GetCurrentMethod().Name + "()", string.Format("Upload_DB_Info EDC_LABEL Info, (SerialNo,GatewayID,DeviceID ) = ({0})", Key)); string[] temp = Key.Split('_'); string SerialID = temp[0].ToString(); string GatewayID = temp[1].ToString(); string DeviceID = temp[2].ToString(); cls_DB_Info DB_info = ObjectManager.DBManager.dbconfig_list.Where(p => p.serial_id == SerialID).FirstOrDefault(); if (DB_info == null) { NLogManager.Logger.LogWarn(LogName, GetType().Name, MethodInfo.GetCurrentMethod().Name + "()", string.Format("Upload_DB_Info Faild DB Serial {0} not Exist in DBManager)", SerialID)); } else { using (var db = new DBContext.IOT_DbContext(DB_info.db_type, DB_info.connection_string)) { string InsertEDCLabelInfo = string.Empty; foreach (KeyValuePair <string, int> _EDC_item in _Process) { DBContext.IOT_DEVICE_EDC_LABEL oIoT_Device_EDC_label = new DBContext.IOT_DEVICE_EDC_LABEL(); oIoT_Device_EDC_label.device_id = DB_info.device_id; oIoT_Device_EDC_label.data_name = _EDC_item.Key; oIoT_Device_EDC_label.data_index = _EDC_item.Value; oIoT_Device_EDC_label.clm_date_time = DateTime.Now; oIoT_Device_EDC_label.clm_user = "******"; db.IOT_DEVICE_EDC_LABEL.Add(oIoT_Device_EDC_label); InsertEDCLabelInfo = string.Concat(InsertEDCLabelInfo, "_", string.Format("ItemName:{0}, ItemPosi:{1}.", _EDC_item.Key, _EDC_item.Value)); } db.SaveChanges(); NLogManager.Logger.LogTrx(LogName, string.Format("DB Insert EDC Label Trace {0}. ", InsertEDCLabelInfo)); } } } } //-------- Get Device and EDC Label information foreach (cls_DB_Info DB_info in ObjectManager.DBManager.dbconfig_list) { NLogManager.Logger.LogInfo(LogName, GetType().Name, MethodInfo.GetCurrentMethod().Name + "()", string.Format("Update_DB_Info IOT_DEVICE and EDC_LABEL Info, SerialNo = {0}, GatewayID = {1}, DeviceID = {2}", DB_info.serial_id, DB_info.gateway_id, DB_info.device_id)); string _IOT_Device_key = string.Empty; // "MS SQL" / "My SQL" ""MS SQL"", "server= localhost;database=IoTDB;user=root;password=qQ123456") using (var db = new DBContext.IOT_DbContext(DB_info.db_type, DB_info.connection_string)) { var _IOT_Status = db.IOT_DEVICE.AsQueryable().Where(p => p.gateway_id == DB_info.gateway_id && p.device_id == DB_info.device_id).ToList(); foreach (DBContext.IOT_DEVICE Device in _IOT_Status) { _IOT_Device_key = string.Concat(DB_info.serial_id, "_", DB_info.gateway_id, "_", DB_info.device_id); DBContext.IOT_DEVICE _IOT_Device_Value = (DBContext.IOT_DEVICE)Device.Clone(); this._IOT_Device.AddOrUpdate(_IOT_Device_key, _IOT_Device_Value, (key, oldvalue) => _IOT_Device_Value); } var vEDC_Label_Result = db.IOT_DEVICE_EDC_LABEL.AsQueryable().Where(o => o.device_id == DB_info.device_id).ToList(); _IOT_Device_key = string.Concat(DB_info.serial_id, "_", DB_info.gateway_id, "_", DB_info.device_id); ConcurrentDictionary <string, int> _Sub_EDC_Labels = this._EDC_Label_Data.GetOrAdd(DB_info.device_id, new ConcurrentDictionary <string, int>()); foreach (DBContext.IOT_DEVICE_EDC_LABEL _EDC_Label_key in vEDC_Label_Result) { _Sub_EDC_Labels.AddOrUpdate(_EDC_Label_key.data_name, _EDC_Label_key.data_index, (key, oldvalue) => _EDC_Label_key.data_index); } this._EDC_Label_Data.AddOrUpdate(_IOT_Device_key, _Sub_EDC_Labels, (key, oldvalue) => _Sub_EDC_Labels); } } } catch (Exception ex) { string ErrorLog = "Routine_Update_DB_Information, error :" + ex.Message; NLogManager.Logger.LogError(LogName, GetType().Name, MethodInfo.GetCurrentMethod().Name + "()", ErrorLog); } }
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(); }