Esempio n. 1
0
        private void CreateSignalDict(Opc.Hda.Server Hda, List <string> tagsLst, string[] propID = null, string dtBegin = null, string dtEnd = null, bool restrict = false)
        {
            Dictionary = new Dictionary <string, Signal>();
            DateTime dt1    = new DateTime();
            DateTime dt2    = new DateTime();
            var      memRec = new OpcRecord();

            if (dtBegin != null)
            {
                if (serverHda == null)
                {
                    serverHda = Hda;
                }
            }



            for (int i = 0; i < tagsLst.Count; i++)
            {
                var signal = new Signal(serverDa, tagsLst[i], propID);

                if (dtBegin != null)
                {
                    try
                    {
                        signal.ValueCollection = new List <OpcRecord>();
                        dt1 = System.Convert.ToDateTime(dtBegin);
                        dt2 = DateTime.Today;
                        DateTime lastdt = dt1;

                        if (dtEnd == null)
                        {
                            dtEnd = dt2.AddDays(1).ToString();
                        }
                        dt2 = System.Convert.ToDateTime(dtEnd);

                        signal.DtBegin = dt1;
                        signal.DtEnd   = dt2;

                        var identifiers = new ItemIdentifier[1];
                        identifiers[0] = new Opc.ItemIdentifier(tagsLst[i]);

                        Opc.IdentifiedResult[] items = serverHda.CreateItems(identifiers);
                        OpcRecord rec  = null;
                        bool      exit = false;
                        Opc.Hda.ItemValueCollection[] vForPrev = serverHda.ReadRaw(new Opc.Hda.Time(dt1),
                                                                                   new Opc.Hda.Time(dt1.AddSeconds(1)), 0, true, items);
                        signal.PrevValue = new OpcRecord();
                        if (vForPrev.Length > 0)
                        {
                            if (vForPrev[0].Count > 0)
                            {
                                signal.PrevValue.Value     = vForPrev[0][0].Value;
                                signal.PrevValue.Quality   = vForPrev[0][0].Quality.GetCode();
                                signal.PrevValue.Timestamp = vForPrev[0][0].Timestamp.ToString();
                            }
                        }
                        Opc.Hda.ItemValueCollection[] vForNext =
                            serverHda.ReadRaw(new Opc.Hda.Time(dt2.AddMilliseconds(-1)), new Opc.Hda.Time(dt2), 0, true,
                                              items);
                        signal.NextValue = new OpcRecord();
                        if (vForNext.Length > 0)
                        {
                            if (vForNext[0].Count > 0)
                            {
                                signal.NextValue.Value     = vForNext[0][vForNext[0].Count - 1].Value;
                                signal.NextValue.Quality   = vForNext[0][vForNext[0].Count - 1].Quality.GetCode();
                                signal.NextValue.Timestamp = vForNext[0][vForNext[0].Count - 1].Timestamp.ToString();
                            }
                        }

                        while (!exit)
                        {
                            Opc.Hda.ItemValueCollection[] values = serverHda.ReadRaw(new Opc.Hda.Time(lastdt),
                                                                                     new Opc.Hda.Time(dt2), 0, false, items);

                            for (int j = 0; j < values.Length; j++)
                            {
                                if (values[j].Count <= 1)
                                {
                                    exit = true;
                                }
                                for (int k = 0; k < values[j].Count; k++)
                                {
                                    rec = new OpcRecord();
                                    if (signal.ValueType == "long")
                                    {
                                        if (values[j][k].Value != null)
                                        {
                                            rec.Value = System.Convert.ToInt64(values[j][k].Value.ToString());
                                        }
                                        else
                                        {
                                            rec.Value = 0;
                                        }
                                    }
                                    if (signal.ValueType == "double")
                                    {
                                        if (values[j][k].Value != null)
                                        {
                                            rec.Value = System.Convert.ToDouble(values[j][k].Value.ToString());
                                        }
                                        else
                                        {
                                            rec.Value = 0.0;
                                        }
                                    }
                                    if (signal.ValueType == "string")
                                    {
                                        if (values[j][k].Value != null)
                                        {
                                            rec.Value = values[j][k].Value.ToString();
                                        }
                                        else
                                        {
                                            rec.Value = "";
                                        }
                                    }
                                    if (signal.ValueType == "bool")
                                    {
                                        if (values[j][k].Value != null)
                                        {
                                            rec.Value = values[j][k].Value.ToString().ToLower().Trim() == "true";
                                        }
                                        else
                                        {
                                            rec.Value = false;
                                        }
                                    }

                                    rec.Quality   = values[j][k].Quality.GetCode();
                                    rec.Timestamp = values[j][k].Timestamp.ToString();
                                    rec.Value     = values[j][k].Value;
                                    lastdt        = values[j][k].Timestamp;
                                    if (values[j].Count > 1)
                                    {
                                        if (k == values[j].Count - 1 &&
                                            values[j][k].Timestamp == values[j][k - 1].Timestamp)
                                        {
                                            lastdt = lastdt.AddMilliseconds(1);
                                        }
                                    }
                                    if (k != values[j].Count - 1)
                                    {
                                        if (restrict & signal.ValueCollection.Count > 0)
                                        {
                                            if (
                                                System.Convert.ToDateTime(
                                                    signal.ValueCollection[signal.ValueCollection.Count - 1].Timestamp)
                                                .ToString() ==
                                                System.Convert.ToDateTime(rec.Timestamp).ToString() &&
                                                rec.Quality >= 192)
                                            {
                                                signal.ValueCollection[signal.ValueCollection.Count - 1] = rec;
                                            }
                                            else
                                            {
                                                signal.ValueCollection.Add(rec);
                                            }
                                        }
                                        else
                                        {
                                            signal.ValueCollection.Add(rec);
                                        }
                                    }
                                }
                            }
                        }

                        if (restrict & signal.ValueCollection.Count > 0)
                        {
                            if (
                                System.Convert.ToDateTime(
                                    signal.ValueCollection[signal.ValueCollection.Count - 1].Timestamp).ToString() ==
                                System.Convert.ToDateTime(rec.Timestamp).ToString() && rec.Quality >= 192)
                            {
                                signal.ValueCollection[signal.ValueCollection.Count - 1] = rec;
                            }
                            else
                            {
                                signal.ValueCollection.Add(rec);
                            }
                        }
                        else
                        {
                            signal.ValueCollection.Add(rec);
                        }
                    }

                    catch (Exception)
                    {
                        MessageBox.Show(@"Отсутствует лицензия или проверьте подключения к серверу C:\Tag.ini", @"Ошибка подключения сервера", MessageBoxButton.OK, MessageBoxImage.Error);

                        Environment.Exit(0);
                    }
                }

                if (restrict)
                {
                    if (signal.ValueCollection.Count > 0)
                    {
                        if (signal.ValueCollection[0] == null)
                        {
                            signal.ValueCollection.Remove(signal.ValueCollection[0]);
                        }
                    }

                    var restrictLeft = new OpcRecord();
                    restrictLeft.Value     = signal.PrevValue.Value;
                    restrictLeft.Quality   = signal.PrevValue.Quality;
                    restrictLeft.Timestamp = dt1.ToString();
                    signal.ValueCollection.Insert(0, restrictLeft);

                    var restrictRight = new OpcRecord();
                    restrictRight.Value     = signal.ValueCollection[signal.ValueCollection.Count - 1].Value;
                    restrictRight.Quality   = signal.ValueCollection[signal.ValueCollection.Count - 1].Quality;
                    restrictRight.Timestamp = dt2.ToString();
                    signal.ValueCollection.Add(restrictRight);
                }
                Dictionary.Add(tagsLst[i], signal);
            }
        }
Esempio n. 2
0
        private static Signal Union2Signals(Signal s1, Signal s2)
        {
            var exit   = false;
            var s      = new Signal(null, "");
            var newLst = new List <OpcRecord>();

            s.ValueCollection = newLst;

            if (s1.DtBegin < s2.DtBegin)
            {
                s.DtBegin = s1.DtBegin;
            }
            else
            {
                s.DtBegin = s2.DtBegin;
            }
            if (s1.DtEnd > s2.DtEnd)
            {
                s.DtEnd = s1.DtEnd;
            }
            else
            {
                s.DtEnd = s2.DtEnd;
            }

            var prevS1 = new OpcRecord();

            prevS1.Value     = s1.PrevValue.Value;
            prevS1.Timestamp = s1.PrevValue.Timestamp;
            prevS1.Quality   = s1.PrevValue.Quality;

            var prevS2 = new OpcRecord();

            prevS2.Value     = s2.PrevValue.Value;
            prevS2.Timestamp = s2.PrevValue.Timestamp;
            prevS2.Quality   = s2.PrevValue.Quality;

            var nextS1 = new OpcRecord();

            nextS1.Value     = s1.NextValue.Value;
            nextS1.Timestamp = s1.NextValue.Timestamp;
            nextS1.Quality   = s1.NextValue.Quality;

            var nextS2 = new OpcRecord();

            nextS2.Value     = s2.NextValue.Value;
            nextS2.Timestamp = s2.NextValue.Timestamp;
            nextS2.Quality   = s2.NextValue.Quality;

            OpcRecord val_s1_prev = prevS1;
            OpcRecord val_s2_prev = prevS2;
            OpcRecord val_s2      = prevS2;
            OpcRecord val_s1      = prevS1;
            bool      endOne      = false;

            if (prevS1.Quality < 192)
            {
                prevS1.Value = 0;
            }
            if (nextS1.Quality < 192)
            {
                nextS1.Value = 0;
            }
            if (prevS2.Quality < 192)
            {
                prevS2.Value = 0;
            }
            if (nextS2.Quality < 192)
            {
                nextS2.Value = 0;
            }

            if (prevS1.Timestamp == null)
            {
                prevS1.Timestamp = s.DtBegin.ToString();
            }
            if (prevS2.Timestamp == null)
            {
                prevS2.Timestamp = s.DtBegin.ToString();
            }
            if (nextS1.Timestamp == null)
            {
                nextS1.Timestamp = s.DtEnd.ToString();
            }
            if (nextS2.Timestamp == null)
            {
                nextS2.Timestamp = s.DtEnd.ToString();
            }


            if (System.Convert.ToDateTime(prevS1.Timestamp) > System.Convert.ToDateTime(prevS2.Timestamp))
            {
                s.PrevValue = prevS1;
            }
            else
            {
                s.PrevValue = prevS2;
            }

            if (prevS1.Quality < 192 && prevS2.Quality >= 192)
            {
                s.PrevValue = prevS2;
            }
            if (prevS1.Quality >= 192 && prevS2.Quality > 192)
            {
                s.PrevValue = prevS1;
            }

            if (System.Convert.ToDateTime(nextS1.Timestamp) <= System.Convert.ToDateTime(nextS2.Timestamp))
            {
                s.NextValue = nextS1;
            }
            else
            {
                s.NextValue = nextS2;
            }

            if (nextS1.Quality < 192 && nextS2.Quality >= 192)
            {
                s.NextValue = nextS2;
            }
            if (nextS1.Quality >= 192 && nextS2.Quality > 192)
            {
                s.NextValue = nextS1;
            }

            s.PrevValue.Value     = System.Convert.ToInt32(prevS1.Value) + System.Convert.ToInt32(prevS2.Value);
            s.PrevValue.Timestamp = s.DtBegin.ToString();

            try
            {
                if (s1.ValueCollection[s1.ValueCollection.Count - 1] != null && s2.ValueCollection[s2.ValueCollection.Count - 1] != null)
                {
                    s.NextValue.Value = System.Convert.ToInt32(s1.ValueCollection[s1.ValueCollection.Count - 1].Value) + System.Convert.ToInt32(s2.ValueCollection[s2.ValueCollection.Count - 1].Value);
                }

                if (s1.ValueCollection[s1.ValueCollection.Count - 1] == null && s2.ValueCollection[s2.ValueCollection.Count - 1] != null)
                {
                    s.NextValue.Value = s2.ValueCollection[s2.ValueCollection.Count - 1].Value;
                }
                if (s1.ValueCollection[s1.ValueCollection.Count - 1] != null && s2.ValueCollection[s2.ValueCollection.Count - 1] == null)
                {
                    s.NextValue.Value = s1.ValueCollection[s1.ValueCollection.Count - 1].Value;
                }

                s.NextValue.Timestamp = s.DtEnd.ToString();
            }
            catch { }

            if (s1.ValueCollection[0] == null && s2.ValueCollection[0] == null)
            {
                s.ValueCollection.Add(null);
                return(s);
            }

            if (s1.ValueCollection[0] == null)
            {
                var prev   = 0;
                var lastDt = s2.DtEnd;
                lastDt = System.Convert.ToDateTime(nextS1.Timestamp);
                if (prevS1.Quality > 192)
                {
                    prev = System.Convert.ToInt32(prevS1.Value);
                }

                foreach (var r in s2.ValueCollection)
                {
                    var rec = new OpcRecord();
                    rec.Timestamp = r.Timestamp;
                    var s1val = 0;
                    if (r.Quality >= 192)
                    {
                        s1val = System.Convert.ToInt32(r.Value);
                    }

                    if (System.Convert.ToDateTime(r.Timestamp) < lastDt)
                    {
                        rec.Value = s1val + prev;
                    }

                    rec.Quality = r.Quality;
                    if (r.Quality < 192 && System.Convert.ToInt32(r.Value) > 0)
                    {
                        rec.Quality = 192;
                    }

                    s.ValueCollection.Add(rec);
                }
                return(s);
            }

            if (s2.ValueCollection[0] == null)
            {
                var prev   = 0;
                var lastDt = s1.DtEnd;
                lastDt = System.Convert.ToDateTime(nextS2.Timestamp);
                if (prevS2.Quality > 192)
                {
                    prev = System.Convert.ToInt32(prevS2.Value);
                }

                foreach (var r in s1.ValueCollection)
                {
                    var rec = new OpcRecord();
                    rec.Timestamp = r.Timestamp;
                    var s2val = 0;
                    if (r.Quality >= 192)
                    {
                        s2val = System.Convert.ToInt32(r.Value);
                    }

                    if (System.Convert.ToDateTime(r.Timestamp) < lastDt)
                    {
                        rec.Value = s2val + prev;
                    }

                    rec.Quality = r.Quality;
                    if (r.Quality < 192 && System.Convert.ToInt32(r.Value) > 0)
                    {
                        rec.Quality = 192;
                    }

                    s.ValueCollection.Add(rec);
                }
                return(s);
            }

            var i = 0;
            var j = 0;

            while (!exit)
            {
                if (i < s1.ValueCollection.Count)
                {
                    val_s1 = s1.ValueCollection[i];
                }
                else
                {
                    val_s1 = val_s2;
                    endOne = true;
                }
                if (j < s2.ValueCollection.Count)
                {
                    val_s2 = s2.ValueCollection[j];
                }
                else
                {
                    val_s2 = val_s1;
                    endOne = true;
                }

                if (i != 0 && i - 1 < s1.ValueCollection.Count)
                {
                    val_s1_prev = s1.ValueCollection[i - 1];
                }
                if (j != 0 && j - 1 < s2.ValueCollection.Count)
                {
                    val_s2_prev = s2.ValueCollection[j - 1];
                }

                var newRec = new OpcRecord();
                if (System.Convert.ToDateTime(val_s1.Timestamp) < System.Convert.ToDateTime(val_s2.Timestamp))
                {
                    newRec.Value     = val_s1.Value;
                    newRec.Quality   = val_s1.Quality;
                    newRec.Timestamp = val_s1.Timestamp;

                    if (val_s2_prev != null)
                    {
                        if (System.Convert.ToDateTime(val_s2_prev.Timestamp) <= System.Convert.ToDateTime(val_s1.Timestamp))
                        {
                            var v1 = 0;
                            if (val_s2_prev.Quality >= 192)
                            {
                                v1 = System.Convert.ToInt32(val_s2_prev.Value);
                            }
                            var v2 = 0;
                            if (val_s1.Quality >= 192)
                            {
                                v2 = System.Convert.ToInt32(val_s1.Value);
                            }
                            newRec.Value = v1 + v2;
                        }
                    }

                    newLst.Add(newRec);
                    i++;
                }
                if (System.Convert.ToDateTime(val_s1.Timestamp) > System.Convert.ToDateTime(val_s2.Timestamp))
                {
                    newRec.Value     = val_s2.Value;
                    newRec.Quality   = val_s2.Quality;
                    newRec.Timestamp = val_s2.Timestamp;

                    if (val_s1_prev != null)
                    {
                        if (System.Convert.ToDateTime(val_s1_prev.Timestamp) <= System.Convert.ToDateTime(val_s2.Timestamp))
                        {
                            var v1 = 0;
                            if (val_s1_prev.Quality >= 192)
                            {
                                v1 = System.Convert.ToInt32(val_s1_prev.Value);
                            }
                            var v2 = 0;
                            if (val_s2.Quality >= 192)
                            {
                                v2 = System.Convert.ToInt32(val_s2.Value);
                            }
                            newRec.Value = v1 + v2;
                        }
                    }

                    newLst.Add(newRec);
                    j++;
                }
                if (System.Convert.ToDateTime(val_s1.Timestamp) == System.Convert.ToDateTime(val_s2.Timestamp))
                {
                    newRec.Value     = val_s1.Value;
                    newRec.Quality   = val_s1.Quality;
                    newRec.Timestamp = val_s1.Timestamp;

                    if (newRec.Quality < 192)
                    {
                        newRec.Value     = val_s2.Value;
                        newRec.Quality   = val_s2.Quality;
                        newRec.Timestamp = val_s2.Timestamp;
                    }

                    if (!endOne)
                    {
                        var v1 = 0;
                        if (val_s1.Quality >= 192)
                        {
                            v1 = Convert.ToInt32(val_s1.Value);
                        }
                        var v2 = 0;
                        if (val_s2.Quality >= 192)
                        {
                            v2 = Convert.ToInt32(val_s2.Value);
                        }
                        newRec.Value = v1 + v2;
                    }
                    else
                    {
                        if (i >= s1.ValueCollection.Count)
                        {
                            var v1 = 0;
                            if (s1.ValueCollection[s1.ValueCollection.Count - 1].Quality >= 192)
                            {
                                v1 = System.Convert.ToInt32(s1.ValueCollection[s1.ValueCollection.Count - 1].Value);
                            }
                            var v2 = 0;
                            if (val_s2.Quality >= 192)
                            {
                                v2 = System.Convert.ToInt32(val_s2.Value);
                            }
                            newRec.Value = v1 + v2;
                        }
                        if (j >= s2.ValueCollection.Count)
                        {
                            var v1 = 0;
                            if (s2.ValueCollection[s2.ValueCollection.Count - 1].Quality >= 192)
                            {
                                v1 = System.Convert.ToInt32(s2.ValueCollection[s2.ValueCollection.Count - 1].Value);
                            }
                            var v2 = 0;
                            if (val_s1.Quality >= 192)
                            {
                                v2 = System.Convert.ToInt32(val_s1.Value);
                            }
                            newRec.Value = v1 + v2;
                        }
                    }

                    newLst.Add(newRec);
                    i++;
                    j++;
                }

                if (i >= s1.ValueCollection.Count && j >= s2.ValueCollection.Count)
                {
                    exit = true;
                }
            }

            s.ValueCollection = newLst;
            foreach (var r in s.ValueCollection)
            {
                if (r.Quality < 192 && System.Convert.ToInt32(r.Value) > 0)
                {
                    r.Quality = 192;
                }
            }


            return(s);
        }
Esempio n. 3
0
        public static SOpcRecord GetValue(Signal signal, object obj_dt)
        {
            DateTime dt = DateTime.Now;

            if (obj_dt is string)
            {
                dt = System.Convert.ToDateTime(obj_dt);
            }
            else if (obj_dt is DateTime)
            {
                dt = (DateTime)obj_dt;
            }
            else
            {
                dt = signal.DtBegin;
            }


            if (dt < signal.DtBegin || dt > signal.DtEnd)
            {
                return(null);
            }

            var rec  = new OpcRecord();
            var sRec = new SOpcRecord();

            rec.Quality   = signal.PrevValue.Quality;
            rec.Timestamp = dt.ToString();
            rec.Value     = signal.PrevValue.Value;

            if (signal.ValueCollection[0] == null)
            {
                rec.Timestamp = dt.ToString();
                sRec.OpcRec   = rec;
                sRec.Sec      = (signal.DtEnd - dt).TotalSeconds;
                return(sRec);
            }

            if (System.Convert.ToDateTime(signal.ValueCollection[0].Timestamp) > dt)
            {
                rec.Timestamp = dt.ToString();
                sRec.OpcRec   = rec;
                sRec.Sec      = (System.Convert.ToDateTime(signal.ValueCollection[0].Timestamp) - dt).TotalSeconds;
                return(sRec);
            }

            var i1 = 0;
            var i2 = signal.ValueCollection.Count - 1;

            if (System.Convert.ToDateTime(signal.ValueCollection[i2].Timestamp) <= dt)
            {
                rec.Quality   = signal.ValueCollection[i2].Quality;
                rec.Value     = signal.ValueCollection[i2].Value;
                rec.Timestamp = dt.ToString();
                sRec.OpcRec   = rec;
                sRec.Sec      = (signal.DtEnd - dt).TotalSeconds;
                return(sRec);
            }

            var x = 0;

            while (i2 - i1 >= 2)
            {
                x = i1 + (i2 - i1) / 2;
                if (System.Convert.ToDateTime(signal.ValueCollection[x].Timestamp) <= dt)
                {
                    i1 = x;
                }
                else
                {
                    i2 = x;
                }
            }
            x             = i1;
            rec.Quality   = signal.ValueCollection[x].Quality;
            rec.Value     = signal.ValueCollection[x].Value;
            rec.Timestamp = dt.ToString();
            sRec.OpcRec   = rec;
            sRec.Sec      = (System.Convert.ToDateTime(signal.ValueCollection[x + 1].Timestamp) - dt).TotalSeconds;
            return(sRec);
        }
Esempio n. 4
0
        public static OpcRecord GetLast(Signal signal, object obj_dt = null)
        {
            var dt = DateTime.Now;

            if (obj_dt is string)
            {
                dt = System.Convert.ToDateTime(obj_dt);
            }
            else
            if (obj_dt is DateTime)
            {
                dt = (DateTime)obj_dt;
            }
            else
            {
                dt = signal.DtEnd;
            }

            var rec = new OpcRecord();

            if (obj_dt == null)
            {
                rec.Timestamp = signal.DtEnd.ToString();
                dt            = signal.DtEnd;
            }
            else
            {
                rec.Timestamp = dt.ToString();
            }
            var exit = false;

            if (signal.ValueCollection == null)
            {
                return(rec);
            }
            var i = signal.ValueCollection.Count - 1;

            if (i == 0)
            {
                exit = true;
            }

            while (!exit)
            {
                var val = signal.ValueCollection[i];
                if (val.Quality >= 192 && dt <= System.Convert.ToDateTime(val.Timestamp))
                {
                    exit          = true;
                    rec.Quality   = val.Quality;
                    rec.Timestamp = val.Timestamp;
                    rec.Value     = val.Value;
                }
                i--;
                if (i < 0)
                {
                    exit = true;
                }
            }
            if (rec.Value == null || rec.Quality < 192)
            {
                if (signal.PropDict.ContainsKey("2"))
                {
                    rec.Value     = signal.PropDict["2"];
                    rec.Quality   = 192;
                    rec.Timestamp = dt.ToString();
                }
            }

            return(rec);
        }