protected virtual async Task <SRStream> GetOrCreateStreamAsync(SRStream stream, CancellationToken cancellationToken = default(CancellationToken)) { if (stream is null) { throw new ArgumentNullException(nameof(stream)); } var existing = _dbContext.Streams.FirstOrDefault(s => s.StreamIdentifier == stream.StreamIdentifier); if (existing is null) { var newStream = await _dbContext.Streams.AddAsync(stream); await _dbContext.SaveChangesAsync(); existing = newStream.Entity; } return(existing); }
public static IEnumerable <TalkGroup> TalkGroupsFromCsv(string talkGroupCsvPath) { using (var csvStream = new StreamReader(talkGroupCsvPath)) { while (!csvStream.EndOfStream) { var line = csvStream.ReadLine(); var lineParts = line.Split(','); ushort tgId = 0; ushort priority = 0; string hexId = null; string mode = null; string alphaTag = null; string tgName = null; string tgType = null; string tgCategory = null; string streamIds = null; string[] streams = null; if (lineParts.Length > 0) { if (!ushort.TryParse(lineParts[0], out tgId)) { continue; } } if (lineParts.Length > 1) { hexId = lineParts[1]; } if (lineParts.Length > 2) { mode = lineParts[2]; } if (lineParts.Length > 3) { alphaTag = lineParts[3]; } if (lineParts.Length > 4) { tgName = lineParts[4]; } if (lineParts.Length > 5) { tgType = lineParts[5]; } if (lineParts.Length > 6) { tgCategory = lineParts[6]; } if (lineParts.Length > 7) { ushort.TryParse(lineParts[7], out priority); } if (lineParts.Length > 8) { streamIds = lineParts[8]; } if (streamIds != null) { streams = streamIds.Split('|'); } var tg = new TalkGroup() { Identifier = tgId, //Priority = priority, Mode = TalkGroupMode.Digital, AlphaTag = alphaTag, Name = tgName, TalkGroupStreams = new Collection <TalkGroupStream>() }; var tgStreams = new List <TalkGroupStream>(); foreach (var stream in streams) { var s = new Stream(); s.StreamIdentifier = stream; tg.TalkGroupStreams.Add(new TalkGroupStream() { TalkGroup = tg, Stream = s }); } yield return(tg); } yield return(null); } }
public async Task Test1() { var templateResource = (new FileInfo("Resources/stream.defaults.liq")).FullName; var talkgroupsResource = (new FileInfo("Resources/danecom-talkgroups.priorities.csv")).FullName; var liquidBridgeConfig = new LiquidBridgeConfig() { IcecastHost = "192.168.1.215", IcecastPort = 8000, StreamPassword = "******", LiquidsoapSocketsPath = Path.Join(Directory.GetCurrentDirectory(), "ls-socks"), LiquidsoapTemplatePath = templateResource, ConnectionString = "http://127.0.0.1:8001/api/" }; var expectedTalkGroupIdentifier = (ushort)13050; var expectedCallIdentifier = (ulong)1594255860; var expectedFrequency = 172075000; var expectedOriginalExtension = "wav"; var expectedConvertedExtension = "mp3"; if (Directory.Exists(liquidBridgeConfig.LiquidsoapSocketsPath)) { Directory.Delete(liquidBridgeConfig.LiquidsoapSocketsPath); } Directory.CreateDirectory(liquidBridgeConfig.LiquidsoapSocketsPath); var tempDir = Path.GetTempPath(); var callWavPath = Path.Join(tempDir, string.Format("{0}-{1}_{2}.{3}", expectedTalkGroupIdentifier, expectedCallIdentifier, expectedFrequency, expectedOriginalExtension)); if (File.Exists(callWavPath)) { File.Delete(callWavPath); } File.Create(callWavPath).Close(); var mockStream1 = new Stream() { Id = 1, StreamIdentifier = "stream-1" }; var mockStream2 = new Stream() { Id = 2, StreamIdentifier = "stream-2" }; var mockStream3 = new Stream() { Id = 3, StreamIdentifier = "stream-3" }; var mockTalkGroup = new TalkGroup() { Id = 1, AlphaTag = "TGPALPHA", Identifier = expectedTalkGroupIdentifier, Name = "Tag Test Alpha (1)", Description = "This channel is used for testing", TalkGroupStreams = new Collection <TalkGroupStream>() }; mockTalkGroup.TalkGroupStreams.Add(new TalkGroupStream() { TalkGroup = mockTalkGroup, TalkGroupId = mockTalkGroup.Id, Stream = mockStream1, StreamId = mockStream1.Id }); mockTalkGroup.TalkGroupStreams.Add(new TalkGroupStream() { TalkGroup = mockTalkGroup, TalkGroupId = mockTalkGroup.Id, Stream = mockStream2, StreamId = mockStream2.Id }); mockTalkGroup.TalkGroupStreams.Add(new TalkGroupStream() { TalkGroup = mockTalkGroup, TalkGroupId = mockTalkGroup.Id, Stream = mockStream3, StreamId = mockStream3.Id }); var mockRadioCall = new RadioCall() { Id = 1, TalkGroupId = mockTalkGroup.Id, TalkGroup = mockTalkGroup, CallIdentifier = expectedCallIdentifier.ToString(), CallSerialNumber = (long)expectedCallIdentifier, CallWavPath = callWavPath, FrequencyHz = expectedFrequency, Frequency = expectedFrequency }; var clientMock = new Mock <ISignalRadioClient>(); clientMock .Setup(c => c.GetTalkGroupByIdentifierAsync(expectedTalkGroupIdentifier, It.IsAny <CancellationToken>())) .ReturnsAsync(mockTalkGroup); clientMock .Setup(c => c.PostCallAsync(It.IsAny <RadioCall>(), It.IsAny <CancellationToken>())) .ReturnsAsync(mockRadioCall); var talkGroupStreams = new Collection <Stream>(mockTalkGroup.TalkGroupStreams.Select(t => t.Stream).ToList()); clientMock .Setup(c => c.GetStreamsByTalkGroupIdAsync(mockTalkGroup.Id, It.IsAny <CancellationToken>())) .ReturnsAsync(talkGroupStreams); var handlerMock = new Mock <CallHandler>() { CallBase = true }; handlerMock .Protected() .Setup <int>("ExecuteProcess", "/usr/bin/liquidsoap", ItExpr.IsAny <string>(), ItExpr.IsAny <bool>(), ItExpr.IsAny <int>()) .Returns(0); //return 0 for success var handler = new CallHandler(liquidBridgeConfig, clientMock.Object); await handler.HandleCallAsync(callWavPath); }