Пример #1
0
        private static void SendDataToHistorian(uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e, bool dataFromQueue = false)
        {
            HistorianAccessError error;
            Double  localValue;
            Boolean result;
            UInt32  tagKey = 0;

            try
            {
                if (!HistorianOK())
                {
                    // Put the message in the queue
                    _messageQueue.Enqueue(new TimestampedMqttMsgPublishEventArgs(e));
                    return;
                }

                // First look for an exact match, no regex
                try
                {
                    tagKey = _HistorianTags.subscribetags.Single(t => t.topic.Equals(e.Topic) && t.useregex == false).historianTag.TagKey;
                }
                catch
                {
                    // Couldn't find it or some other isse so set to 0 so the next codeset can try
                    tagKey = 0;
                }


                if (tagKey == 0)
                {
                    tagKey = GetHistorianTagKey(e.Topic);
                }

                // If we still have tag key of 0 then just queue it and proces later
                if (tagKey == 0)
                {
                    // Put the message in the queue
                    _messageQueue.Enqueue(new TimestampedMqttMsgPublishEventArgs(e));
                    return;
                }

                // Finally check to make sure the tag is ready.  If it is not then just queue the message
                if (!TagIsReady(_HistorianTags.subscribetags.FirstOrDefault(t => t.historianTag.TagKey.Equals(tagKey)).historianTag.TagName))
                {
                    // Put the message in the queue
                    _messageQueue.Enqueue(new TimestampedMqttMsgPublishEventArgs(e));
                    return;
                }

                string messageValue = System.Text.ASCIIEncoding.UTF8.GetString(e.Message);

                HistorianDataValue myVTQ = new HistorianDataValue();
                myVTQ.TagKey        = tagKey;
                myVTQ.DataValueType = _HistorianTags.subscribetags.Find(x => x.topic.Equals(e.Topic)).historianTag.TagDataType;
                myVTQ.OpcQuality    = 192;

                if (Double.TryParse(messageValue, out localValue))
                {
                    myVTQ.Value = localValue;
                }
                else
                {
                    throw new Exception("Error Parsing Message to Double " + messageValue);
                }

                myVTQ.StartDateTime = DateTime.Now;

                result = (_historian.AddStreamedValue(myVTQ, out error));

                if (!result)
                {
                    throw new Exception("Failed to write VTQ : " + error.ErrorDescription);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }