Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
        // 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);
            }
        }