Dun.Jason 2 роки тому
батько
коміт
9395e478e0

+ 1 - 0
src/CallCenter.Api/Controllers/CallController.cs

@@ -5,6 +5,7 @@ using CallCenter.Share.Requests;
 using MapsterMapper;
 using MessagePack.Formatters;
 using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Options;
 
 namespace CallCenter.Api.Controllers
 {

+ 2 - 2
src/CallCenter.Api/Properties/launchSettings.json

@@ -8,8 +8,8 @@
       "environmentVariables": {
         "ASPNETCORE_ENVIRONMENT": "Development"
       },
-      "applicationUrl": "http://localhost:50001",
-      "dotnetRunMessages": true
+      "applicationUrl": "http://localhost:50001"
+      //"dotnetRunMessages": true
     }
   }
 }

+ 4 - 1
src/CallCenter.Api/StartupExtensions.cs

@@ -21,6 +21,7 @@ using Identity.Admin.HttpClient;
 using Microsoft.AspNetCore.Authentication.JwtBearer;
 using Microsoft.IdentityModel.Tokens;
 using Serilog;
+using CallCenter.Calls;
 
 namespace CallCenter.Api;
 
@@ -35,13 +36,15 @@ internal static class StartupExtensions
         services.AddHttpContextAccessor();
 
 #if DEBUG
-        builder.WebHost.UseUrls("http://192.168.100.36:50001", "http://localhost:50001");
+        //builder.WebHost.UseUrls("http://192.168.100.36:50001", "http://localhost:50001");
+        builder.WebHost.UseUrls("http://*:50001");
 #endif
 
         services.Configure<DeviceConfigs>(d => configuration.GetSection("DeviceConfigs").Bind(d));
 
         services.Configure<WorkTimeSettings>(d => configuration.GetSection("WorkTimeSettings").Bind(d));
 
+        services.Configure<RecordSettings>(d => configuration.GetSection("RecordSettings").Bind(d));
         // Add services to the container.
         services
             .BatchInjectServices()

+ 4 - 0
src/CallCenter.Api/appsettings.json

@@ -59,5 +59,9 @@
     "WorkDay": [ 1, 2, 3, 4, 5, 0, 6 ],
     "WorkCategory": "08da9b9f-a35d-4ade-8ea7-55e8abbcdefd",
     "RestCategory": "08daa5f5-ac7a-4ced-8295-1c78baa15f9e"
+  },
+  "RecordSettings": {
+    "Remote": "http://192.168.100.100/mcc/Recorder/",
+    "Local": "http://192.168.100.36:50001/Recorder/"
   }
 }

+ 1 - 0
src/CallCenter.Application/Handlers/FlowControl/CdrNotificationHandler.cs

@@ -41,6 +41,7 @@ namespace CallCenter.Application.Handlers
                     TrunkNumber = notification.TrunkNumber,
                     Recording = notification.Recording,
                     RecCodec = notification.RecCodec,
+                    IsDown = false
                 };
                 if (!string.IsNullOrEmpty(notification.VisitorId))
                     model.VisitorId = notification.VisitorId;

+ 104 - 0
src/CallCenter.CacheManager/CallRecordManager.cs

@@ -0,0 +1,104 @@
+
+using CallCenter.Calls;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Options;
+using SqlSugar;
+using System.Net;
+using XF.Domain.Cache;
+
+namespace CallCenter.CacheManager
+{
+    public class CallRecordManager : BackgroundService
+    {
+        private readonly IServiceScopeFactory _serviceScopeFactory;
+
+        public CallRecordManager(IServiceScopeFactory serviceScopeFactory)
+        {
+            _serviceScopeFactory = serviceScopeFactory;
+        }
+
+
+        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
+        {
+            var time = TimeSpan.FromMinutes(1);
+            await Task.Delay(time, stoppingToken);
+            while(!stoppingToken.IsCancellationRequested)
+            {
+                using var scope = _serviceScopeFactory.CreateScope();
+                var _callRecordRepository = scope.ServiceProvider.GetService<ICallRecordRepository>();
+                //var _recordSettings = scope.ServiceProvider.GetConfigValue<RecordSettings>();
+                var _recordSettings = _serviceScopeFactory.CreateScope().ServiceProvider.GetRequiredService<IOptionsSnapshot<RecordSettings>>();
+                //TODO 查询所有没有下载语音文件的记录
+                var canDownList = await _callRecordRepository!.QueryAsync(x => !x.IsDown);
+                if (canDownList != null)
+                {
+                    //TODO 下载语音文件
+                    foreach (var item in canDownList)
+                    {
+                        //分隔数据
+                        if (!string.IsNullOrEmpty(item.Recording))
+                        {
+                            string dir = item.Recording.Split("/")[0];
+                            string filename = item.Recording.Split("/")[1];
+                            //string url = "";
+                            string url = _recordSettings.Value.Remote + item.Recording;//远程地址
+                            string localfile = "Recorder/" + dir + "/"; //本地地址
+                            long startPosition = 0;//上次下载的文件起始位置
+                            FileStream writeStream;//写入本地文件流对象
+                            if (File.Exists(localfile+filename))
+                            {
+                                writeStream = File.OpenWrite(localfile + filename);
+                                startPosition = writeStream.Length;     //获取已经下载的长度
+                                writeStream.Seek(startPosition, SeekOrigin.Current);//本地文件写入位置定位
+                            }
+                            else
+                            {
+                                if (!File.Exists(localfile))
+                                {
+                                    System.IO.Directory.CreateDirectory(localfile);
+                                }
+                                writeStream = new FileStream(localfile + filename, FileMode.Create);//保存
+                                startPosition = 0;
+                            }
+
+                            try
+                            {
+                                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url);// 打开网络连接
+
+                                if (startPosition>0)
+                                {
+                                    myRequest.AddRange((int)startPosition); //设置Range值,与上面的writeStream.Seek用意相同,是为了定义远程文件读取位置
+                                }
+                                Stream readStream = myRequest.GetResponse().GetResponseStream();// 向服务器请求,获得服务器的回应数据流
+                                byte[] btArray = new byte[512];// 定义一个字节数据,用来向readStream读取内容和向writeStream写入内容
+                                int contentSize = readStream.Read(btArray, 0, btArray.Length);// 向远程文件读第一次
+
+                                while (contentSize > 0)// 如果读取长度大于零则继续读
+                                {
+                                    writeStream.Write(btArray, 0, contentSize);// 写入本地文件
+                                    contentSize = readStream.Read(btArray, 0, btArray.Length);// 继续向远程文件读取
+                                }
+
+                                //关闭流
+                                writeStream.Close();
+                                readStream.Close();
+
+                                //成功 修改状态
+                                item.IsDown = true;
+                                await _callRecordRepository.UpdateAsync(item);
+                            }
+                            catch
+                            {
+                                writeStream.Close();
+                            }
+                        }
+                    }
+                }
+
+
+                await Task.Delay(time,stoppingToken);
+            }
+        }
+    }
+}

+ 6 - 0
src/CallCenter/Calls/CallRecord.cs

@@ -119,5 +119,11 @@ namespace CallCenter.Calls
         /// </summary>
         [SugarColumn(IsNullable = true, ColumnDescription = "用户名")]
         public string? UserName { get; set; }
+
+        /// <summary>
+        /// 是否下载
+        /// </summary>
+        [SugarColumn(ColumnDataType ="是否下载")]
+        public bool IsDown { get; set; }
     }
 }

+ 15 - 0
src/CallCenter/Calls/RecordSettings.cs

@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace CallCenter.Calls
+{
+    public class RecordSettings
+    {
+        public string Remote { get; set; }
+
+        public string Local { get; set; }
+    }
+}