Ejemplo n.º 1
0
 public void Reset()
 {
     if (resetNeeded)
     {
         blockIterator.Reset();
     }
     resetNeeded = true;
     hasCurrent  = false;
     pos         = -1;
     while (blockIterator.MoveNext())
     {
         TimeSeriesBlock block = (TimeSeriesBlock)blockIterator.Current;
         int             n = block.used;
         int             l = 0, r = n;
         while (l < r)
         {
             int i = (l + r) >> 1;
             if (from > block[i].Time)
             {
                 l = i + 1;
             }
             else
             {
                 r = i;
             }
         }
         Debug.Assert(l == r && (l == n || block[l].Time >= from));
         if (l < n)
         {
             pos       = l;
             currBlock = block;
             return;
         }
     }
 }
Ejemplo n.º 2
0
        public static int GetDAData(TimeSeriesBlock tsb)
        {
            Console.WriteLine("Read OPC DA Data:");
            try
            {
                ErrorMessage = "";
                if (session == null)
                {
                    Connect(tsb);
                }
                if (session != null)
                {
                    opcDAValues = new List <string>();
                    for (int i = 0; i < tsb.opcNodes.Count; i++)
                    {
                        string[] split = tsb.opcNodes[i].Split(',');
                        string   value = opc.ReadSubmodelElementValue(split[1], (ushort)Convert.ToInt32(split[0]));
                        opcDAValues.Add(value);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorMessage = ex.Message;
                return(0);
            }

            /*
             * session?.Close();
             * session?.Dispose();
             * session = null;
             */

            return(1);
        }
Ejemplo n.º 3
0
        public static void GetHistory(TimeSeriesBlock tsb)
        {
            Console.WriteLine("Read OPC UA Historical Data:");
            try
            {
                ErrorMessage         = "";
                startTime            = tsb.opcLastTimeStamp;
                endTime              = DateTime.UtcNow + TimeSpan.FromMinutes(120);
                tsb.opcLastTimeStamp = endTime;
                if (session == null)
                {
                    Connect(tsb);
                }
                GetData(tsb);
            }
            catch (Exception ex)
            {
                ErrorMessage = ex.Message;
                session?.Close();
                session?.Dispose();
                session = null;
                opc     = null;
            }

            /*
             * session?.Close();
             * session?.Dispose();
             * session = null;
             */
        }
Ejemplo n.º 4
0
 public void Reset()
 {
     hasCurrent = false;
     pos        = -1;
     blockIterator.Reset();
     while (blockIterator.MoveNext())
     {
         TimeSeriesBlock block = (TimeSeriesBlock)blockIterator.Current;
         int             n = block.used;
         int             l = 0, r = n;
         while (l < r)
         {
             int i = (l + r) >> 1;
             if (till >= block[i].Ticks)
             {
                 l = i + 1;
             }
             else
             {
                 r = i;
             }
         }
         Debug.Assert(l == r && (l == n || block[l].Ticks > till));
         if (l > 0)
         {
             pos       = l - 1;
             currBlock = block;
             return;
         }
     }
 }
Ejemplo n.º 5
0
        private void insertInBlock(TimeSeriesBlock block, TimeSeriesTick tick)
#endif
        {
            long t = tick.Time;
            int  i, n = block.used;

            int l = 0, r = n;

            while (l < r)
            {
                i = (l + r) >> 1;
                if (t >= block[i].Time)
                {
                    l = i + 1;
                }
                else
                {
                    r = i;
                }
            }
            Debug.Assert(l == r && (l == n || block[l].Time >= t));
            if (r == 0)
            {
                if (block[n - 1].Time - t > maxBlockTimeInterval || n == block.Ticks.Length)
                {
                    addNewBlock(tick);
                    return;
                }
                if (block.timestamp != t)
                {
                    index.Remove(new Key(block.timestamp), block);
                    block.timestamp = t;
                    index.Put(new Key(block.timestamp), block);
                }
            }
            else if (r == n)
            {
                if (t - block[0].Time > maxBlockTimeInterval || n == block.Ticks.Length)
                {
                    addNewBlock(tick);
                    return;
                }
            }
            if (n == block.Ticks.Length)
            {
                addNewBlock(block[n - 1]);
                Array.Copy(block.Ticks, r, block.Ticks, r + 1, n - r - 1);
            }
            else
            {
                if (n != r)
                {
                    Array.Copy(block.Ticks, r, block.Ticks, r + 1, n - r);
                }
                block.used += 1;
            }
            block[r] = tick;
            block.Modify();
        }
Ejemplo n.º 6
0
 public static void Connect(TimeSeriesBlock tsb)
 {
     if (opc == null)
     {
         opc = new UASampleClient(tsb.sourceAddress, true, 10000, tsb.username, tsb.password);
     }
     opc.ConsoleSampleClient().Wait();
     session = opc.session;
 }
Ejemplo n.º 7
0
        private void addNewBlock(T t)
        {
            TimeSeriesBlock block = new TimeSeriesBlock(blockSize);

            block.timestamp = t.Ticks;
            block.used      = 1;
            block[0]        = t;
            index.Put(block.timestamp, block);
        }
Ejemplo n.º 8
0
        private void addNewBlock(TimeSeriesTick t)
        {
            TimeSeriesBlock block = (TimeSeriesBlock)blockConstructor.Invoke(ClassDescriptor.noArgs);

            if (block == null)
            {
                throw new StorageError(StorageError.ErrorCode.CONSTRUCTOR_FAILURE);
            }
            block.timestamp = t.Time;
            block.used      = 1;
            block[0]        = t;
            index.Put(block.timestamp, block);
        }
Ejemplo n.º 9
0
        private long remove(long from, long till)
        {
            long        nRemoved      = 0;
            IEnumerator blockIterator = index.GetEnumerator(from - maxBlockTimeInterval, till);

            while (blockIterator.MoveNext())
            {
                TimeSeriesBlock block = (TimeSeriesBlock)blockIterator.Current;
                int             n = block.used;
                int             l = 0, r = n;
                while (l < r)
                {
                    int i = (l + r) >> 1;
                    if (from > block[i].Time)
                    {
                        l = i + 1;
                    }
                    else
                    {
                        r = i;
                    }
                }
                Debug.Assert(l == r && (l == n || block[l].Time >= from));
                while (r < n && block[r].Time <= till)
                {
                    r        += 1;
                    nRemoved += 1;
                }
                if (l == 0 && r == n)
                {
                    index.Remove(block.timestamp, block);
                    block.Deallocate();
                    blockIterator.Reset();
                }
                else if (l != r)
                {
                    if (l == 0)
                    {
                        index.Remove(block.timestamp, block);
                        block.timestamp = block[r].Time;
                        index.Put(block.timestamp, block);
                        blockIterator.Reset();
                    }
                    Array.Copy(block.Ticks, r, block.Ticks, l, n - r);
                    block.used = l + n - r;
                    block.Modify();
                }
            }
            return(nRemoved);
        }
Ejemplo n.º 10
0
            public void Reset()
            {
#if !USE_GENERICS
                if (resetNeeded)
                {
                    blockEnumerator.Reset();
                }
                resetNeeded = true;
#endif
                hasCurrent = false;
                pos        = -1;
                while (blockEnumerator.MoveNext())
                {
                    TimeSeriesBlock block = (TimeSeriesBlock)blockEnumerator.Current;
                    int             n = block.used;
                    int             l = 0, r = n;
                    while (l < r)
                    {
                        int i = (l + r) >> 1;
                        if (till >= block[i].Time)
                        {
                            l = i + 1;
                        }
                        else
                        {
                            r = i;
                        }
                    }
                    Debug.Assert(l == r && (l == n || block[l].Time > till));
                    if (l > 0)
                    {
                        pos       = l - 1;
                        currBlock = block;
                        return;
                    }
                }
            }
Ejemplo n.º 11
0
        public static void GetData(TimeSeriesBlock tsb)
        {
            if (session != null)
            {
                ReadRawModifiedDetails details = new ReadRawModifiedDetails();
                details.StartTime        = startTime;
                details.EndTime          = endTime;
                details.NumValuesPerNode = 0;
                details.IsReadModified   = false;
                details.ReturnBounds     = true;

                var nodesToRead = new HistoryReadValueIdCollection();
                for (int i = 0; i < tsb.opcNodes.Count; i++)
                {
                    var      nodeToRead = new HistoryReadValueId();
                    string[] split      = tsb.opcNodes[i].Split(',');
                    nodeToRead.NodeId = new NodeId(split[1], (ushort)Convert.ToInt32(split[0]));
                    nodesToRead.Add(nodeToRead);
                }

                table = new List <List <object> >();

                HistoryReadResultCollection results         = null;
                DiagnosticInfoCollection    diagnosticInfos = null;

                bool loop = true;
                while (loop)
                {
                    session.HistoryRead(
                        null,
                        new ExtensionObject(details),
                        TimestampsToReturn.Both,
                        false,
                        nodesToRead,
                        out results,
                        out diagnosticInfos);

                    ClientBase.ValidateResponse(results, nodesToRead);
                    ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToRead);

                    foreach (var res in results)
                    {
                        if (StatusCode.IsBad(res.StatusCode))
                        {
                            throw new ServiceResultException(res.StatusCode);
                        }
                    }

                    var historyData1 = ExtensionObject.ToEncodeable(results[0].HistoryData) as HistoryData;
                    var historyData2 = ExtensionObject.ToEncodeable(results[1].HistoryData) as HistoryData;
                    for (int i = 0; i < historyData1.DataValues.Count; i++)
                    {
                        var row = new List <object>();
                        row.Add(historyData1.DataValues[i].SourceTimestamp);
                        row.Add(historyData1.DataValues[i].Value);
                        row.Add(historyData2.DataValues[i].Value);
                        table.Add(row);
                    }

                    for (int i = 0; i < results.Count; i++)
                    {
                        if (results[i].ContinuationPoint == null || results[i].ContinuationPoint.Length == 0)
                        {
                            loop = false;
                            break;
                        }
                        nodesToRead[i].ContinuationPoint = results[i].ContinuationPoint;
                    }
                }
            }
        }
Ejemplo n.º 12
0
        public static void timeSeriesInit()
        {
            DateTime timeStamp = DateTime.Now;

            timeSeriesBlockList = new List <TimeSeriesBlock>();
            timeSeriesSubscribe = new List <AdminShellV20.SubmodelElementCollection>();

            int aascount = AasxServer.Program.env.Length;

            for (int i = 0; i < aascount; i++)
            {
                var env = AasxServer.Program.env[i];
                if (env != null)
                {
                    var aas = env.AasEnv.AdministrationShells[0];
                    aas.TimeStampCreate = timeStamp;
                    aas.setTimeStamp(timeStamp);
                    if (aas.submodelRefs != null && aas.submodelRefs.Count > 0)
                    {
                        foreach (var smr in aas.submodelRefs)
                        {
                            var sm = env.AasEnv.FindSubmodel(smr);
                            if (sm != null && sm.idShort != null)
                            {
                                sm.TimeStampCreate = timeStamp;
                                sm.SetAllParents(timeStamp);
                                int countSme = sm.submodelElements.Count;
                                for (int iSme = 0; iSme < countSme; iSme++)
                                {
                                    var sme = sm.submodelElements[iSme].submodelElement;
                                    if (sme is AdminShell.SubmodelElementCollection && sme.idShort.Contains("TimeSeries"))
                                    {
                                        bool nextSme = false;
                                        if (sme.qualifiers.Count > 0)
                                        {
                                            int j = 0;
                                            while (j < sme.qualifiers.Count)
                                            {
                                                var q = sme.qualifiers[j] as AdminShell.Qualifier;
                                                if (q.type == "SUBSCRIBE")
                                                {
                                                    timeSeriesSubscribe.Add(sme as AdminShell.SubmodelElementCollection);
                                                    // nextSme = true;
                                                    break;
                                                }
                                                j++;
                                            }
                                        }
                                        if (nextSme)
                                        {
                                            continue;
                                        }

                                        var smec      = sme as AdminShell.SubmodelElementCollection;
                                        int countSmec = smec.value.Count;

                                        var tsb = new TimeSeriesBlock();
                                        tsb.submodel          = sm;
                                        tsb.block             = smec;
                                        tsb.samplesProperties = new List <AdminShell.Property>();
                                        tsb.samplesValues     = new List <string>();
                                        tsb.opcLastTimeStamp  = DateTime.UtcNow - TimeSpan.FromMinutes(1) + TimeSpan.FromMinutes(120);

                                        for (int iSmec = 0; iSmec < countSmec; iSmec++)
                                        {
                                            var sme2    = smec.value[iSmec].submodelElement;
                                            var idShort = sme2.idShort;
                                            if (idShort.Contains("opcNode"))
                                            {
                                                idShort = "opcNode";
                                            }
                                            switch (idShort)
                                            {
                                            case "sourceType":
                                                if (sme2 is AdminShell.Property)
                                                {
                                                    tsb.sourceType = (sme2 as AdminShell.Property).value;
                                                }
                                                break;

                                            case "sourceAddress":
                                                if (sme2 is AdminShell.Property)
                                                {
                                                    tsb.sourceAddress = (sme2 as AdminShell.Property).value;
                                                }
                                                break;

                                            case "username":
                                                if (sme2 is AdminShell.Property)
                                                {
                                                    tsb.username = (sme2 as AdminShell.Property).value;
                                                }
                                                break;

                                            case "password":
                                                if (sme2 is AdminShell.Property)
                                                {
                                                    tsb.password = (sme2 as AdminShell.Property).value;
                                                }
                                                break;

                                            case "data":
                                                if (sme2 is AdminShell.SubmodelElementCollection)
                                                {
                                                    tsb.data = sme2 as AdminShell.SubmodelElementCollection;
                                                }
                                                break;

                                            case "sampleStatus":
                                                if (sme2 is AdminShell.Property)
                                                {
                                                    tsb.sampleStatus = sme2 as AdminShell.Property;
                                                }
                                                break;

                                            case "sampleMode":
                                                if (sme2 is AdminShell.Property)
                                                {
                                                    tsb.sampleMode = sme2 as AdminShell.Property;
                                                }
                                                break;

                                            case "sampleRate":
                                                if (sme2 is AdminShell.Property)
                                                {
                                                    tsb.sampleRate = sme2 as AdminShell.Property;
                                                }
                                                break;

                                            case "maxSamples":
                                                if (sme2 is AdminShell.Property)
                                                {
                                                    tsb.maxSamples = sme2 as AdminShell.Property;
                                                }
                                                break;

                                            case "actualSamples":
                                                if (sme2 is AdminShell.Property)
                                                {
                                                    tsb.actualSamples       = sme2 as AdminShell.Property;
                                                    tsb.actualSamples.value = "0";
                                                }
                                                break;

                                            case "maxSamplesInCollection":
                                                if (sme2 is AdminShell.Property)
                                                {
                                                    tsb.maxSamplesInCollection = sme2 as AdminShell.Property;
                                                }
                                                break;

                                            case "actualSamplesInCollection":
                                                if (sme2 is AdminShell.Property)
                                                {
                                                    tsb.actualSamplesInCollection       = sme2 as AdminShell.Property;
                                                    tsb.actualSamplesInCollection.value = "0";
                                                }
                                                break;

                                            case "maxCollections":
                                                if (sme2 is AdminShell.Property)
                                                {
                                                    tsb.maxCollections = sme2 as AdminShell.Property;
                                                }
                                                break;

                                            case "actualCollections":
                                                if (sme2 is AdminShell.Property)
                                                {
                                                    tsb.actualCollections       = sme2 as AdminShell.Property;
                                                    tsb.actualCollections.value = "0";
                                                }
                                                break;

                                            case "lowDataIndex":
                                                if (sme2 is AdminShell.Property)
                                                {
                                                    tsb.lowDataIndex       = sme2 as AdminShell.Property;
                                                    tsb.lowDataIndex.value = "0";
                                                }
                                                break;

                                            case "highDataIndex":
                                                if (sme2 is AdminShell.Property)
                                                {
                                                    tsb.highDataIndex = sme2 as AdminShell.Property;
                                                }
                                                break;

                                            case "opcNode":
                                                if (sme2 is AdminShell.Property)
                                                {
                                                    string   node  = (sme2 as AdminShell.Property).value;
                                                    string[] split = node.Split(',');
                                                    if (tsb.opcNodes == null)
                                                    {
                                                        tsb.opcNodes = new List <string>();
                                                    }
                                                    tsb.opcNodes.Add(split[1] + "," + split[2]);
                                                    var p = AdminShell.Property.CreateNew(split[0]);
                                                    tsb.samplesProperties.Add(p);
                                                    p.TimeStampCreate = timeStamp;
                                                    p.setTimeStamp(timeStamp);
                                                    tsb.samplesValues.Add("");
                                                }
                                                break;
                                            }
                                            if (tsb.sourceType == "aas" && sme2 is AdminShell.ReferenceElement r)
                                            {
                                                var el = env.AasEnv.FindReferableByReference(r.value);
                                                if (el is AdminShell.Property p)
                                                {
                                                    tsb.samplesProperties.Add(p);
                                                    tsb.samplesValues.Add("");
                                                }
                                            }
                                        }
                                        if (tsb.sampleRate != null)
                                        {
                                            tsb.threadCounter = Convert.ToInt32(tsb.sampleRate.value);
                                        }
                                        timeSeriesBlockList.Add(tsb);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // test
            if (test)
            {
                dummy = 0;
                for (int i = 0; i < 500; i++)
                {
                    timeSeriesSampling(false);
                }
                timeSeriesSampling(true);
            }
            else
            {
                timeSeriesThread = new Thread(new ThreadStart(timeSeriesSamplingLoop));
                timeSeriesThread.Start();
            }
        }
Ejemplo n.º 13
0
 private void insertInBlock(TimeSeriesBlock block, T tick)
Ejemplo n.º 14
0
 private void addNewBlock(T t)
 {
     TimeSeriesBlock block = new TimeSeriesBlock(blockSize);