public void Organize_DBPartaker(string GateWayID, string Device_ID) { //--- Check DB List information if (_objectmanager.DBManager == null) { return; } List <ObjectManager.cls_DB_Info> lst_DBInfo = _objectmanager.DBManager.dbconfig_list.Where(p => p.gateway_id == GateWayID && p.device_id == Device_ID && p.enable == true).ToList(); foreach (cls_DB_Info _DB in lst_DBInfo) { DBPartaker DBReporter = new DBPartaker(_DB); 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) { // Assembly Normal Tag info foreach (Tuple <string, string> _Items in _DB.tag_info) { string ReportItemName = _Items.Item1; string ReportItemValue = string.Empty; if (device.tag_info.ContainsKey(_Items.Item2)) { ReportItemValue = device.tag_info[_Items.Item2].Value; DBReporter.Report_Item.Add(Tuple.Create(ReportItemName, ReportItemValue, device.tag_info[_Items.Item2].Expression)); } } // Assembly Calc Tag info foreach (Tuple <string, string> _Items in _DB.calc_tag_info) { string ReportItemName = _Items.Item1; string ReportItemValue = string.Empty; if (device.tag_info.ContainsKey(_Items.Item2)) { ReportItemValue = device.tag_info[_Items.Item2].Value; DBReporter.Report_Item.Add(Tuple.Create(ReportItemName, ReportItemValue, device.tag_info[_Items.Item2].Expression)); } } } } //----- Send MQTT----- xmlMessage SendOutMsg = new xmlMessage(); SendOutMsg.GatewayID = GateWayID; // GateID SendOutMsg.DeviceID = Device_ID; // DeviceID SendOutMsg.MQTTTopic = "DBService"; SendOutMsg.MQTTPayload = JsonConvert.SerializeObject(DBReporter, Newtonsoft.Json.Formatting.Indented); _QueueManager.PutMessage(SendOutMsg); } }
public ProcDBData(string inputdata, DBService.Add_DBPartaker_to_dict_Event Add_DBPartakertoDict) { try { this.objDB = JsonConvert.DeserializeObject <DBPartaker>(inputdata.ToString()); _Add_DBPartakertoDict = Add_DBPartakertoDict; } catch { this.objDB = null; } }
public void Add_DBPartaker_to_dict(DBPartaker DBP) { if (_Initial_Finished == false) { return; } string Key = string.Concat(DBP.db_type, "_", DBP.connection_string); ConcurrentQueue <DBPartaker> _Current = this._dic_DB_Partaker.GetOrAdd(Key, new ConcurrentQueue <DBPartaker>()); _Current.Enqueue(DBP); this._dic_DB_Partaker.AddOrUpdate(Key, _Current, (key, oldvalue) => _Current); }
// Real Insert DB 的部分 public void DB_TimerTask(object timerState) { try { foreach (var key in _dic_DB_Partaker.Keys) { ConcurrentQueue <DBPartaker> _DBProcess; _dic_DB_Partaker.TryRemove(key, out _DBProcess); string[] temp = key.Split('_'); string Provider = temp[0].ToString(); string ConnectionStr = temp[1].ToString(); if (_DBProcess.Count > 0) { DBPartaker DBP = null; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); int _DBProcess_Deep = _DBProcess.Count(); using (var db = new DBContext.IOT_DbContext(Provider, ConnectionStr)) { while (_DBProcess.TryDequeue(out DBP)) { DBContext.IOT_DEVICE Device = null; ConcurrentDictionary <string, int> Dict_EDC_Label = null; string remove_key = string.Concat(DBP.serial_id, "_", DBP.gateway_id, "_", DBP.device_id); if (_IOT_Device.TryGetValue(remove_key, out Device)) { if (_EDC_Label_Data.TryGetValue(remove_key, out Dict_EDC_Label)) { DBContext.IOT_DEVICE_EDC oIoT_DeviceEDC = new DBContext.IOT_DEVICE_EDC(); oIoT_DeviceEDC.device_id = DBP.device_id; string InsertDBInfo = string.Empty; int ReportIndex = 0; foreach (Tuple <string, string, string> items in DBP.Report_Item) { if (Dict_EDC_Label.TryGetValue(items.Item1, out ReportIndex)) { string ReportValue = string.Empty; switch (items.Item3) { case "ASC": ReportValue = (items.Item2.Length > 16) ? items.Item2.Substring(16) : items.Item2; break; case "DATETIME": DateTime parsedDate; if (DateTime.TryParse(items.Item2, out parsedDate)) { ReportValue = parsedDate.ToString("yyyyMMddHHmmss"); } else { ReportValue = "999999"; } break; default: ReportValue = (IsNumeric(items.Item2) == true) ? items.Item2 : "999999"; break; } string ReportPropertyName = string.Concat("data_value_", ReportIndex.ToString("00")); oIoT_DeviceEDC.SetPropertyValue(ReportPropertyName, ReportValue); InsertDBInfo = string.Concat(InsertDBInfo, "_", string.Format("ItemName:{0}, ItemValue:{1}, ItemPosi:{2}.", items.Item1, items.Item2, ReportIndex.ToString("00"))); } } NLogManager.Logger.LogInfo(LogName, GetType().Name, MethodInfo.GetCurrentMethod().Name + "()", string.Format("DB Service Insert DB : {0}, Device : {1}. ", key, remove_key)); NLogManager.Logger.LogTrx(LogName, string.Format("DB Insert Trace {0}. ", InsertDBInfo)); oIoT_DeviceEDC.clm_date_time = DateTime.Now; oIoT_DeviceEDC.clm_user = "******"; oIoT_DeviceEDC.AddDB(db, Device, oIoT_DeviceEDC); db.SaveChanges(); } } } } stopwatch.Stop(); NLogManager.Logger.LogInfo(LogName, GetType().Name, MethodInfo.GetCurrentMethod().Name + "()", string.Format("Execute Update DB Key {0} DataCount : {1}, Spect Time {2} ms > ", key, _DBProcess_Deep, stopwatch.ElapsedMilliseconds)); } } } catch (Exception ex) { string ErrorLog = string.Format("Insert_DB Faild ex :{0}. ", ex.Message); NLogManager.Logger.LogError(LogName, GetType().Name, MethodInfo.GetCurrentMethod().Name + "()", ErrorLog); } }