Beispiel #1
0
        /// <summary>
        /// Gets current historian write operation state for specified handle.
        /// </summary>
        /// <param name="operationHandle">Handle to historian write operation state.</param>
        /// <returns>Current historian write operation state.</returns>
        public HistorianWriteOperationState GetHistorianWriteState(uint operationHandle)
        {
            if (m_historianWriteOperationStates.TryGetValue(operationHandle, out HistorianWriteOperationState operationState))
            {
                return(operationState);
            }

            // Returned a cancelled operation state if operation handle was not found
            operationState = new HistorianWriteOperationState();
            operationState.CancellationToken.Cancel();

            return(operationState);
        }
Beispiel #2
0
        /// <summary>
        /// Begins a new historian write operation.
        /// </summary>
        /// <param name="instanceName">Historian instance name.</param>
        /// <param name="values">Enumeration of <see cref="TrendValue"/> instances to write.</param>
        /// <param name="totalValues">Total values to write, if known in advance.</param>
        /// <param name="timestampType">Type of timestamps.</param>
        /// <returns>New operational state handle.</returns>
        public uint BeginHistorianWrite(string instanceName, IEnumerable <TrendValue> values, long totalValues = 0, TimestampType timestampType = TimestampType.UnixMilliseconds)
        {
            HistorianWriteOperationState operationState = new HistorianWriteOperationState
            {
                Total = totalValues
            };

            uint operationHandle = Random.UInt32;

            while (!m_historianWriteOperationStates.TryAdd(operationHandle, operationState))
            {
                operationHandle = Random.UInt32;
            }

            new Thread(() =>
            {
                operationState.StartTime = DateTime.UtcNow.Ticks;

                try
                {
                    SnapServer server = GetServer(instanceName)?.Host;

                    if (server == null)
                    {
                        throw new InvalidOperationException($"Server is null for instance [{instanceName}].");
                    }

                    using (SnapClient connection = SnapClient.Connect(server))
                        using (ClientDatabaseBase <HistorianKey, HistorianValue> database = connection.GetDatabase <HistorianKey, HistorianValue>(instanceName))
                        {
                            if (database == null)
                            {
                                throw new InvalidOperationException($"Database is null for instance [{instanceName}].");
                            }

                            HistorianKey key     = new HistorianKey();
                            HistorianValue value = new HistorianValue();

                            foreach (TrendValue trendValue in values)
                            {
                                key.PointID = (ulong)trendValue.ID;

                                switch (timestampType)
                                {
                                case TimestampType.Ticks:
                                    key.Timestamp = (ulong)trendValue.Timestamp;
                                    break;

                                case TimestampType.UnixSeconds:
                                    key.Timestamp = (ulong)trendValue.Timestamp * 10000000UL + 621355968000000000UL;
                                    break;

                                case TimestampType.UnixMilliseconds:
                                    key.Timestamp = (ulong)trendValue.Timestamp * 10000UL + 621355968000000000UL;
                                    break;
                                }

                                value.AsSingle = (float)trendValue.Value;

                                database.Write(key, value);
                                operationState.Progress++;

                                if (operationState.CancellationToken.IsCancelled)
                                {
                                    break;
                                }
                            }

                            operationState.Completed = !operationState.CancellationToken.IsCancelled;
                        }
                }
                catch (Exception ex)
                {
                    operationState.Failed       = true;
                    operationState.FailedReason = ex.Message;
                }

                // Schedule operation handle to be removed
                CancelHistorianWrite(operationHandle);

                operationState.StopTime = DateTime.UtcNow.Ticks;
            })
            {
                IsBackground = true
            }
            .Start();

            return(operationHandle);
        }