Beispiel #1
0
        void InitServerByDatabase()
        {
            try
            {
                using (var dataReader = DataHelper.Instance.ExecuteProcedureReader("InitServer", DataHelper.CreateParam("@TYPE", System.Data.SqlDbType.Int, 1)))
                {
                    if (dataReader == null)
                    {
                        Environment.Exit(0);
                    }
                    //dataReader.Read();
                    dataReader.Read();
                    int count = dataReader.GetInt32(0);
                    _list    = new List <TagMetaData>(count);
                    _mapping = new Dictionary <string, ITag>(count);
                    dataReader.NextResult();
                    while (dataReader.Read())
                    {
                        _list.Add(new TagMetaData(dataReader.GetInt16(0), dataReader.GetInt16(1), dataReader.GetString(2), dataReader.GetString(3), (DataType)dataReader.GetByte(4),
                                                  (ushort)dataReader.GetInt16(5), dataReader.GetBoolean(6), dataReader.GetFloat(7), dataReader.GetFloat(8), dataReader.GetInt32(9)));
                        //_list[i].Description = dataReader.GetSqlString(6).Value;
                    }
                    _list.Sort();
                    if (reader != null && group == null)
                    {
                        group = reader.AddGroup("Group1", 1, 0, 0, true) as ClientGroup;
                        group.AddItems(_list);
                    }
                    dataReader.NextResult();
                    _conditions    = new List <ICondition>();
                    _conditionList = new ObservableCollection <ICondition>();
                    while (dataReader.Read())
                    {
                        int        id   = dataReader.GetInt32(0);
                        AlarmType  type = (AlarmType)dataReader.GetInt32(2);
                        ICondition cond;
                        string     source = dataReader.GetString(1);
                        if (_conditions.Count > 0)
                        {
                            cond = _conditions[_conditions.Count - 1];
                            if (cond.ID == id)
                            {
                                cond.AddSubCondition(new SubCondition((SubAlarmType)dataReader.GetInt32(9), dataReader.GetFloat(10),
                                                                      (Severity)dataReader.GetByte(11), dataReader.GetString(12), dataReader.GetBoolean(13)));
                                continue;
                            }
                        }
                        switch (type)
                        {
                        case AlarmType.Complex:
                            cond = new ComplexCondition(id, source, dataReader.GetString(6), dataReader.GetFloat(7), dataReader.GetInt32(8));
                            break;

                        case AlarmType.Level:
                            cond = new LevelAlarm(id, source, dataReader.GetString(6), dataReader.GetFloat(7), dataReader.GetInt32(8));
                            break;

                        case AlarmType.Dev:
                            cond = new DevAlarm(id, (ConditionType)dataReader.GetByte(4), source, dataReader.GetString(6),
                                                dataReader.GetFloat(5), dataReader.GetFloat(7), dataReader.GetInt32(8));
                            break;

                        case AlarmType.ROC:
                            cond = new ROCAlarm(id, source, dataReader.GetString(6), dataReader.GetFloat(7), dataReader.GetInt32(8));
                            break;

                        case AlarmType.Quality:
                            cond = new QualitiesAlarm(id, source, dataReader.GetString(6));
                            break;

                        default:
                            cond = new DigitAlarm(id, source, dataReader.GetString(6), dataReader.GetInt32(8));
                            break;
                        }
                        cond.AddSubCondition(new SubCondition((SubAlarmType)dataReader.GetInt32(9), dataReader.GetFloat(10),
                                                              (Severity)dataReader.GetByte(11), dataReader.GetString(12), dataReader.GetBoolean(13)));

                        cond.IsEnabled = dataReader.GetBoolean(3);
                        var simpcond = cond as SimpleCondition;
                        if (simpcond != null)
                        {
                            simpcond.Tag = this[source];
                        }
                        else
                        {
                            var complexcond = cond as ComplexCondition;
                            if (complexcond != null)
                            {
                                var action = complexcond.SetFunction(reval.Eval(source));
                                if (action != null)
                                {
                                    ValueChangedEventHandler handle = (s1, e1) => { action(); };
                                    foreach (ITag tag in reval.TagList)
                                    {
                                        tag.ValueChanged += handle;// tag.Refresh();
                                    }
                                }
                            }
                        }
                        cond.AlarmActive += new AlarmEventHandler(cond_SendAlarm);
                        cond.AlarmAck    += new EventHandler(cond_AckAlarm);
                        //_conditions.Add(cond);// UpdateCondition(cond);
                        _conditions.Add(cond);
                    }
                    dataReader.NextResult();
                    while (dataReader.Read())
                    {
                        _scales.Add(new Scaling(dataReader.GetInt16(0), (ScaleType)dataReader.GetByte(1),
                                                dataReader.GetFloat(2), dataReader.GetFloat(3), dataReader.GetFloat(4), dataReader.GetFloat(5)));
                    }
                }
                reval.Clear();
                _scales.Sort();
                _compare = new CompareCondBySource();
                _conditions.Sort(_compare);
            }
            catch (Exception e)
            {
                App.AddErrorLog(e);
                Environment.Exit(0);
            }
        }