private ChannelMetadataRecord ToChannelMetadataRecord(ChannelMetadataRecord record, IndexMetadataRecord indexMetadata) { var uri = GetChannelUri(record.ChannelName); var channel = new ChannelMetadataRecord() { ChannelUri = uri, ContentType = uri.ContentType, ChannelId = record.ChannelId, ChannelName = record.ChannelName, Uom = record.Uom, MeasureClass = record.MeasureClass, DataType = record.DataType, Description = record.Description, Uuid = record.Uuid, Status = record.Status, Source = record.Source, Indexes = new[] { indexMetadata }, CustomData = new Dictionary <string, DataValue>() }; Channels.Add(channel); return(channel); }
private object ToChannelIndexValue(ChannelMetadataRecord channel, IndexMetadataRecord index, DateTimeOffset indexDateTimeOffset) { if (index.IndexKind == ChannelIndexKind.Time) { return(indexDateTimeOffset.ToUnixTimeMicroseconds()); } object indexValue; if (ChannelStreamingInfo.ContainsKey(channel.Id)) { indexValue = ChannelStreamingInfo[channel.Id]; if (indexValue is double) { indexValue = (double)indexValue + Math.Pow(10, index.Scale) * 0.1; } } else { indexValue = 0d; } ChannelStreamingInfo[channel.Id] = indexValue; return(indexValue); }
public IChannelMetadataRecord ToChannelMetadataRecord(IChannelMetadataRecord channelMetadata, IIndexMetadataRecord indexMetadata) { var uri = GetChannelUri(channelMetadata.ChannelName); var channel = new ChannelMetadataRecord { ChannelUri = uri, ContentType = uri.ContentType, ChannelId = channelMetadata.ChannelId, ChannelName = channelMetadata.ChannelName, Uom = channelMetadata.Uom, MeasureClass = channelMetadata.MeasureClass, DataType = channelMetadata.DataType, Description = channelMetadata.Description, Uuid = channelMetadata.Uuid, Status = (ChannelStatuses)channelMetadata.Status, Source = channelMetadata.Source, Indexes = new[] { indexMetadata } .OfType <IndexMetadataRecord>() .ToList(), CustomData = new Dictionary <string, DataValue>() }; return(channel); }
/* * * protected virtual void OnStart(object sender, ProtocolEventArgs<Start> e) * { * TaskRunner = new TaskRunner(e.Message.MinMessageInterval) * { * OnExecute = StreamChannelData, * OnError = LogStreamingError * }; * * if (Client.Handler<IChannelStreamingProducer>().IsSimpleStreamer) * { * var channelMetadata = Simulator.GetChannelMetadata(e.Header) * .Cast<ChannelMetadataRecord>() * .ToList(); * * Channels.Clear(); * Channels.AddRange(channelMetadata); * * Client.Handler<IChannelStreamingProducer>() * .ChannelMetadata(e.Header, channelMetadata); * * foreach (var channel in channelMetadata.Select(ToChannelStreamingInfo)) * ChannelStreamingInfo.Add(channel); * * TaskRunner.Start(); * } * } * * protected virtual void OnChannelDescribe(object sender, ProtocolEventArgs<ChannelDescribe, IList<ChannelMetadataRecord>> e) * { * Simulator.GetChannelMetadata(e.Header) * .Cast<ChannelMetadataRecord>() * .ForEach(e.Context.Add); * } * * protected virtual void OnChannelStreamingStart(object sender, ProtocolEventArgs<ChannelStreamingStart> e) * { * e.Message.Channels.ForEach(ChannelStreamingInfo.Add); * TaskRunner.Start(); * } * * protected virtual void OnChannelStreamingStop(object sender, ProtocolEventArgs<ChannelStreamingStop> e) * { * TaskRunner.Stop(); * } * * private void StreamChannelData() * { * if (!Client.IsOpen) return; * * var dataItems = ChannelStreamingInfo * .Select(ToChannelDataItem) * .ToList(); * * Client.Handler<IChannelStreamingProducer>() * .ChannelData(null, dataItems); * } * * private static ChannelStreamingInfo ToChannelStreamingInfo(ChannelMetadataRecord record) * { * return new ChannelStreamingInfo * { * ChannelId = record.ChannelId, * ReceiveChangeNotification = false, * StartIndex = new StreamingStartIndex * { * // "null" indicates a request for the latest value * Item = null * } * }; * } * * private DataItem ToChannelDataItem(ChannelStreamingInfo streamingInfo) * { * var channel = Channels.FirstOrDefault(x => x.ChannelId == streamingInfo.ChannelId); * if (channel == null) return null; * * var indexDateTimeOffset = DateTimeOffset.UtcNow; * * return new DataItem * { * ChannelId = channel.ChannelId, * Indexes = channel.Indexes * .Select(x => ToChannelIndexValue(streamingInfo, x, indexDateTimeOffset)) * .ToList(), * ValueAttributes = new DataAttribute[0], * Value = new DataValue * { * Item = ToChannelDataValue(channel, indexDateTimeOffset) * } * }; * } * * private long ToChannelIndexValue(ChannelStreamingInfo streamingInfo, IndexMetadataRecord index, DateTimeOffset indexDateTimeOffset) * { * if (index.IndexKind == ChannelIndexKinds.Time) * return indexDateTimeOffset.ToUnixTimeMicroseconds(); * * var value = 0d; * * if (streamingInfo.StartIndex.Item is double) * { * value = (double)streamingInfo.StartIndex.Item + Math.Pow(10, index.Scale) * 0.1; + } + + streamingInfo.StartIndex.Item = value; + + return (long)value; + } + */ #endregion private object ToChannelDataValue(ChannelMetadataRecord channel, DateTimeOffset indexDateTimeOffset) { object dataValue; var indexType = channel.Indexes.Select(i => i.IndexKind).FirstOrDefault(); LogDataType logDataType; if (!Enum.TryParse(channel.DataType, out logDataType)) { return(null); } switch (logDataType) { case LogDataType.@byte: { dataValue = "Y"; break; } case LogDataType.datetime: { var dto = indexType == ChannelIndexKind.Time ? indexDateTimeOffset : indexDateTimeOffset.AddSeconds(_random.Next(1, 5)); dataValue = dto.ToString("o"); break; } case LogDataType.@double: case LogDataType.@float: { dataValue = _random.NextDouble().ToString(CultureInfo.InvariantCulture); break; } case LogDataType.@int: case LogDataType.@long: case LogDataType.@short: { dataValue = _random.Next(11); break; } case LogDataType.@string: { dataValue = "abc"; break; } default: { dataValue = "null"; break; } } return(dataValue); }
/// <summary> /// 关键函数,生成随机数据的值 /// </summary> /// <param name="channel"></param> /// <param name="indexDateTimeOffset"></param> /// <returns></returns> private object ToChannelDataValue(ChannelMetadataRecord channel, DateTimeOffset indexDateTimeOffset) { object dataValue = null; var indexType = channel.Indexes.Select(i => i.IndexType).FirstOrDefault(); LogDataType logDataType; var logDataTypeExists = Enum.TryParse <LogDataType>(channel.DataType, out logDataType); switch (logDataType) { case LogDataType.@byte: { dataValue = "Y"; break; } case LogDataType.datetime: { var dto = indexType == ChannelIndexTypes.Time ? indexDateTimeOffset : indexDateTimeOffset.AddSeconds(_random.Next(1, 5)); dataValue = dto.ToString("o"); break; } case LogDataType.@double: case LogDataType.@float: { double tmpvalue = _random.NextDouble() * 100; dataValue = tmpvalue.ToString(CultureInfo.InvariantCulture); break; } case LogDataType.@int: case LogDataType.@long: case LogDataType.@short: { dataValue = _random.Next(11); break; } case LogDataType.@string: { dataValue = "wang xiaotian"; break; } default: { dataValue = "null"; } break; } return(dataValue); }
private void VerifyChannelProperties(ChannelMetadataRecord channel, string description, string source, string dataType, string uom, long startIndex, long endIndex) { Assert.AreEqual(description, channel.Description); Assert.AreEqual(source, channel.Source); Assert.AreEqual(dataType, channel.DataType); Assert.AreEqual(uom, channel.Uom); Assert.AreEqual(startIndex, channel.StartIndex); Assert.AreEqual(endIndex, channel.EndIndex); }
private LogCurveInfo ToLogCurveInfo(ChannelMetadataRecord channel) { return(new LogCurveInfo() { Mnemonic = new ShortNameStruct(channel.ChannelName), Unit = channel.Uom, CurveDescription = channel.Description, TypeLogData = LogDataType.@double, }); }
private ChannelStreamingInfo ToChannelStreamingInfo(ChannelMetadataRecord channel, object value = null) { return(new ChannelStreamingInfo { ChannelId = channel.ChannelId, StartIndex = new StreamingStartIndex { Item = value } }); }
private ChannelStreamingInfo ToChannelStreamingInfo(ChannelMetadataRecord channel) { return(new ChannelStreamingInfo() { ChannelId = channel.ChannelId, StartIndex = new StreamingStartIndex() { Item = GetStreamingStartValue() } }); }
private ChannelStreamingInfo ToChannelStreamingInfo(ChannelMetadataRecord record) { return(new ChannelStreamingInfo() { ChannelId = record.ChannelId, ReceiveChangeNotification = false, StartIndex = new StreamingStartIndex() { // "null" indicates a request for the latest value Item = null } }); }
/// <summary> /// Creates the channel streaming information for the specified channel metadata. /// </summary> /// <param name="channel">The channel metadata.</param> /// <returns>A new <see cref="ChannelStreamingInfo"/> instance.</returns> protected virtual ChannelStreamingInfo ToChannelStreamingInfo(ChannelMetadataRecord channel) { return(new ChannelStreamingInfo { ChannelId = channel.ChannelId, ReceiveChangeNotification = false, StartIndex = new StreamingStartIndex { // "null" indicates a request for the latest value Item = null } }); }
private LogCurveInfo ToLogCurveInfo(ChannelMetadataRecord channel) { return(new LogCurveInfo { Mnemonic = new ShortNameStruct(channel.ChannelName), Uid = channel.ChannelName, Unit = channel.Uom, CurveDescription = string.IsNullOrWhiteSpace(channel.Description) ? channel.ChannelName : channel.Description, TypeLogData = LogDataType.@double }); }
private void VerifyChannelIndex(ChannelMetadataRecord channelMetadataRecord, ChannelIndexTypes channelIndexType, IndexDirections indexDirection) { // Verify that the channel exists Assert.IsNotNull(channelMetadataRecord); // Verify that the channel has an index var index = channelMetadataRecord.Indexes.FirstOrDefault(); Assert.IsNotNull(index); // Verify that the index type and diretion are as expected. Assert.AreEqual(channelIndexType, index.IndexType); Assert.AreEqual(indexDirection, index.Direction); }
private IndexMetadataRecord ToIndexMetadataRecord(ChannelMetadataRecord record, int scale = 3) { return(new IndexMetadataRecord() { Uri = GetChannelUri(record.ChannelName), Mnemonic = record.ChannelName, Description = record.Description, Uom = record.Uom, Scale = scale, IndexType = Model.LogIndexType == LogIndexType.datetime || Model.LogIndexType == LogIndexType.elapsedtime ? ChannelIndexTypes.Time : ChannelIndexTypes.Depth, Direction = IndexDirections.Increasing, CustomData = new Dictionary <string, DataValue>(0), }); }
private DataItem ToChannelDataItem(ChannelMetadataRecord channel) { var indexDateTimeOffset = DateTimeOffset.UtcNow; return(new DataItem { ChannelId = channel.ChannelId, Indexes = channel.Indexes .Select(x => ToChannelIndexValue(channel, x, indexDateTimeOffset)) .Select(x => new IndexValue { Item = x }) .ToList(), ValueAttributes = new DataAttribute[0], Value = new DataValue { Item = ToChannelDataValue(channel, indexDateTimeOffset) } }); }
private long ToScale(ChannelMetadataRecord channel, double indexValue) { var scale = channel.Indexes.Select(x => x.Scale).FirstOrDefault(); return(Convert.ToInt64(indexValue * Math.Pow(10, scale))); }
private ChannelRangeInfo ToChannelRangeInfo(ChannelMetadataRecord channel, long startIndex, long endIndex) { return(ToChannelRangeInfo(new [] { channel }, startIndex, endIndex)); }