コード例 #1
0
        public void _First_Initial_DB_Information()
        {
            try
            {
                foreach (cls_DB_Info DB_info in _objectmanager.DBManager.dbconfig_list)
                {
                    _logger.LogInformation(string.Format("Initial_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))
                    {
                        _logger.LogInformation(string.Format("Initial DB Context Object"));

                        _logger.LogInformation(string.Format("Download IOT Device Object start"));
                        // 建置更新 IOT Device Information
                        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);
                        }

                        _logger.LogInformation(string.Format("Download IOT Device Object end"));

                        _logger.LogInformation(string.Format("Download IOT EDC Level Object start"));
                        // 建置更新 EDC Label to Local  (DB)
                        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);

                        _logger.LogInformation(string.Format("Download IOT EDC Level Object end"));

                        _logger.LogInformation(string.Format("Upload IOT EDC Level Object start"));

                        // 建置 CCS Config 設定資料
                        List <string> ConfigSetting_NormalTag = DB_info.tag_info.Select(o => o.Item1).ToList();
                        List <string> ConfigSetting_CalcTag   = DB_info.calc_tag_info.Select(o => o.Item1).ToList();
                        List <string> ConfigSetting_Tag       = ConfigSetting_NormalTag.Concat(ConfigSetting_CalcTag).ToList();

                        ConcurrentDictionary <string, int> _DB_EDC_Labels = this._EDC_Label_Data.GetOrAdd(DB_info.device_id, new ConcurrentDictionary <string, int>());
                        List <string> DBSetting_Tag = _DB_EDC_Labels.Keys.ToList();

                        List <string> Diff = ConfigSetting_Tag.Except(DBSetting_Tag).ToList();

                        if (Diff.Count > 0)
                        {
                            ConcurrentDictionary <string, int> _Sync_Device_EDC_Label = this._Sync_EDC_Label_Data.GetOrAdd(_IOT_Device_key, new ConcurrentDictionary <string, int>());
                            List <int> _lstIndex = _DB_EDC_Labels.Select(t => t.Value).ToList();

                            //---- 找出最大值 --
                            int _CurrentIndex = 0;
                            if (_lstIndex.Count() == 0)
                            {
                                _CurrentIndex = 0;
                            }
                            else
                            {
                                _CurrentIndex = _lstIndex.Max();
                            }

                            foreach (string New_EDC_items in Diff)
                            {
                                _CurrentIndex++;
                                _DB_EDC_Labels.AddOrUpdate(New_EDC_items, _CurrentIndex, (key, oldvalue) => _CurrentIndex);
                                _Sync_Device_EDC_Label.AddOrUpdate(New_EDC_items, _CurrentIndex, (key, oldvalue) => _CurrentIndex);
                            }
                            this._EDC_Label_Data.AddOrUpdate(_IOT_Device_key, _DB_EDC_Labels, (key, oldvalue) => _DB_EDC_Labels);
                            this._Sync_EDC_Label_Data.AddOrUpdate(_IOT_Device_key, _Sync_Device_EDC_Label, (key, oldvalue) => _Sync_Device_EDC_Label);
                        }

                        _logger.LogInformation(string.Format("Upload IOT EDC Level Object end"));
                    }
                }
            }

            catch (Exception ex)
            {
                string ErrorLog = "First_Initial_DB_Information, error :" + ex.Message;
                _logger.LogError(ErrorLog);
                ReportAlarm(_dic_Basicsetting[_GatewayID], _dic_Basicsetting[_DeviceID], "0002", ErrorLog, "ERROR");
            }
        }
コード例 #2
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);

                        _logger.LogInformation(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)
                        {
                            _logger.LogWarning(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.Add(oIoT_Device_EDC_label);
                                    InsertEDCLabelInfo = string.Concat(InsertEDCLabelInfo, "_", string.Format("ItemName:{0}, ItemPosi:{1}.", _EDC_item.Key, _EDC_item.Value));
                                }
                                db.SaveChanges();
                                _logger.LogTrace(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)
                {
                    _logger.LogTrace(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;
                _logger.LogError(ErrorLog);
                ReportAlarm(_dic_Basicsetting[_GatewayID], _dic_Basicsetting[_DeviceID], "0003", ErrorLog, "ERROR");
            }
        }
コード例 #3
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")));
                                            }
                                        }

                                        _logger.LogInformation(string.Format("DB Service Insert DB : {0}, Device : {1}. ", key, remove_key));
                                        _logger.LogTrace(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();
                        _logger.LogInformation(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);
                _logger.LogError(ErrorLog);
                ReportAlarm(_dic_Basicsetting[_GatewayID], _dic_Basicsetting[_DeviceID], "0001", ErrorLog, "ERROR");
            }
        }