Skip to content

Commit 8c25c9c

Browse files
committed
First version with build in FTP
1 parent 38c471f commit 8c25c9c

7 files changed

Lines changed: 71 additions & 46 deletions

File tree

src/Wikiled.YiScanner.Tests/Monitoring/Source/SourceFactoryTests.cs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
using System.Net;
44
using Moq;
55
using NUnit.Framework;
6-
using Wikiled.YiScanner.Client;
76
using Wikiled.YiScanner.Client.Predicates;
8-
using Wikiled.YiScanner.Monitoring;
97
using Wikiled.YiScanner.Monitoring.Config;
108
using Wikiled.YiScanner.Monitoring.Source;
119

@@ -14,8 +12,6 @@ namespace Wikiled.YiScanner.Tests.Monitoring.Source
1412
[TestFixture]
1513
public class SourceFactoryTests
1614
{
17-
private FtpConfig ftpConfig;
18-
1915
private MonitoringConfig scanConfig;
2016

2117
private Mock<IPredicate> mockPredicate;
@@ -27,12 +23,14 @@ public class SourceFactoryTests
2723
[SetUp]
2824
public void SetUp()
2925
{
30-
ftpConfig = new FtpConfig();
31-
ftpConfig.FileMask = "Test";
3226
scanConfig = new MonitoringConfig();
33-
scanConfig.Out = "Out";
27+
scanConfig.YiFtp = new FtpConfig();
28+
scanConfig.YiFtp.FileMask = "Test";
29+
scanConfig.Output = new OutputConfig();
30+
scanConfig.Output.Out = "Out";
3431
mockPredicate = new Mock<IPredicate>();
35-
manager = new StaticHostManager(scanConfig);
32+
scanConfig.Known = new PredefinedCameraConfig();
33+
manager = new StaticHostManager(scanConfig.Known);
3634
instance = CreateFactory();
3735
}
3836

@@ -42,40 +40,25 @@ public void GetSources()
4240
var result = instance.GetSources(manager).ToArray();
4341
Assert.AreEqual(0, result.Length);
4442

45-
scanConfig.Known = "Test";
43+
scanConfig.Known.Cameras = "Test";
4644
result = instance.GetSources(manager).ToArray();
4745
Assert.AreEqual(0, result.Length);
4846

49-
scanConfig.Hosts = IPAddress.Any.ToString();
47+
scanConfig.Known.Hosts = IPAddress.Any.ToString();
5048
result = instance.GetSources(manager).ToArray();
5149
Assert.AreEqual(1, result.Length);
5250
}
5351

5452
[Test]
5553
public void Construct()
5654
{
57-
Assert.Throws<ArgumentNullException>(() => new SourceFactory(
58-
null,
59-
scanConfig,
60-
mockPredicate.Object));
61-
62-
Assert.Throws<ArgumentNullException>(() => new SourceFactory(
63-
ftpConfig,
64-
null,
65-
mockPredicate.Object));
66-
67-
Assert.Throws<ArgumentNullException>(() => new SourceFactory(
68-
ftpConfig,
69-
scanConfig,
70-
null));
55+
Assert.Throws<ArgumentNullException>(() => new SourceFactory(null, mockPredicate.Object));
56+
Assert.Throws<ArgumentNullException>(() => new SourceFactory(scanConfig, null));
7157
}
7258

7359
private SourceFactory CreateFactory()
7460
{
75-
return new SourceFactory(
76-
ftpConfig,
77-
scanConfig,
78-
mockPredicate.Object);
61+
return new SourceFactory(scanConfig, mockPredicate.Object);
7962
}
8063
}
8164
}

src/YiScanner/Downloader/FileDownloader.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ public async Task<DateTime> Download()
3838
{
3939
var path = Path.Combine(Environment.CurrentDirectory, config.Path);
4040
log.Info("Checking files: {0}", path);
41+
if (!Directory.Exists(path))
42+
{
43+
log.Error("Directory not found: {0}", path);
44+
return DateTime.Now;
45+
}
46+
4147
foreach (var file in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories))
4248
{
4349
if (predicate.CanDownload(lastScanned, file, File.GetLastWriteTime(file)))

src/YiScanner/Monitoring/MonitoringInstance.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Wikiled.YiScanner.Monitoring.Config;
1111
using Wikiled.YiScanner.Monitoring.Source;
1212
using Wikiled.YiScanner.Network;
13+
using Wikiled.YiScanner.Server;
1314

1415
namespace Wikiled.YiScanner.Monitoring
1516
{
@@ -29,6 +30,8 @@ public class MonitoringInstance
2930

3031
private IHostManager hostManager;
3132

33+
private ServerManager server;
34+
3235
public MonitoringInstance(IScheduler scheduler, MonitoringConfig configuration, ISourceFactory downloaderFactory, IDeleteArchiving archiving)
3336
{
3437
Guard.NotNull(() => configuration, configuration);
@@ -57,6 +60,13 @@ public bool Start()
5760
connections.Add(archivingObservable);
5861
}
5962

63+
if (configuration.Server != null)
64+
{
65+
log.Info("Setting FTP server");
66+
server = new ServerManager(configuration.Server);
67+
server.Start();
68+
}
69+
6070
var observableMonitor = Observable.Empty<bool>()
6171
.Delay(TimeSpan.FromSeconds(configuration.Scan), scheduler)
6272
.Concat(Observable.FromAsync(Download, scheduler))
@@ -76,6 +86,7 @@ public void Stop()
7686
connection.Dispose();
7787
}
7888

89+
server?.Dispose();
7990
connections.Clear();
8091
}
8192

src/YiScanner/Monitoring/Source/SourceFactory.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Wikiled.YiScanner.Client;
88
using Wikiled.YiScanner.Client.Predicates;
99
using Wikiled.YiScanner.Destinations;
10+
using Wikiled.YiScanner.Downloader;
1011
using Wikiled.YiScanner.Monitoring.Config;
1112

1213
namespace Wikiled.YiScanner.Monitoring.Source
@@ -19,21 +20,29 @@ public class SourceFactory : ISourceFactory
1920

2021
private readonly MonitoringConfig config;
2122

22-
private readonly ConcurrentDictionary<IPAddress, HostTracking> trackingInformation = new ConcurrentDictionary<IPAddress, HostTracking>();
23+
private readonly List<IDownloader> fixedDownloaders = new List<IDownloader>();
24+
25+
private readonly ConcurrentDictionary<IPAddress, IDownloader> trackingInformation = new ConcurrentDictionary<IPAddress, IDownloader>();
26+
27+
private readonly IDestination destination;
2328

2429
public SourceFactory(MonitoringConfig config, IPredicate filePredicate)
2530
{
2631
Guard.NotNull(() => config, config);
2732
Guard.NotNull(() => filePredicate, filePredicate);
2833
this.config = config;
2934
this.filePredicate = filePredicate;
35+
destination = ConstructDestination();
36+
if (config.Server != null)
37+
{
38+
fixedDownloaders.Add(new FileDownloader(config.Server, destination, filePredicate));
39+
}
3040
}
3141

3242
public IEnumerable<IDownloader> GetSources(IHostManager manager)
3343
{
34-
var destination = ConstructDestination();
35-
log.Info("Download from camera(s)");
36-
return manager.GetHosts().Select(item => ConstructDownloader(item, destination));
44+
log.Info("Downloading...");
45+
return fixedDownloaders.Union(manager.GetHosts().Select(item => ConstructDownloader(item, destination)));
3746
}
3847

3948
private IDestination ConstructDestination()
@@ -54,16 +63,14 @@ private IDestination ConstructDestination()
5463

5564
private IDownloader ConstructDownloader(Host host, IDestination destination)
5665
{
57-
if (!trackingInformation.TryGetValue(host.Address, out var tracking))
66+
if (!trackingInformation.TryGetValue(host.Address, out var downloader))
5867
{
59-
tracking = new HostTracking(config.YiFtp, host);
60-
trackingInformation[host.Address] = tracking;
68+
var tracking = new HostTracking(config.YiFtp, host);
69+
downloader = new FtpDownloader(tracking, destination, filePredicate);
70+
trackingInformation[host.Address] = downloader;
6171
}
6272

63-
return new FtpDownloader(
64-
tracking,
65-
destination,
66-
filePredicate);
73+
return downloader;
6774
}
6875
}
6976
}

src/YiScanner/Program.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ public static void Main(string[] args)
2929
}
3030

3131
MonitoringConfig config = JsonConvert.DeserializeObject<MonitoringConfig>(File.ReadAllText(Path.Combine(directory, "service.json")));
32+
if (config.Output == null)
33+
{
34+
log.Error("Output not configured");
35+
return;
36+
}
37+
38+
if (config.YiFtp == null)
39+
{
40+
log.Error("Yi Ftp not configured");
41+
return;
42+
}
3243

3344
log.Info("Starting {0} version utility...", Assembly.GetExecutingAssembly().GetName().Version);
3445
List<Command> commandsList = new List<Command>();

src/YiScanner/Server/ServerManager.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
using System.IO;
33
using FubarDev.FtpServer;
44
using FubarDev.FtpServer.AccountManagement;
5+
using FubarDev.FtpServer.AccountManagement.Anonymous;
56
using FubarDev.FtpServer.FileSystem.DotNet;
7+
using NLog;
68
using Wikiled.Common.Arguments;
9+
using Wikiled.Common.Extensions;
710
using Wikiled.YiScanner.Monitoring.Config;
811

912
namespace Wikiled.YiScanner.Server
1013
{
1114
public class ServerManager : IServerManager
1215
{
16+
private static readonly Logger log = LogManager.GetCurrentClassLogger();
17+
1318
private readonly ServerConfig config;
1419

1520
private FtpServer ftpServer;
@@ -22,12 +27,15 @@ public ServerManager(ServerConfig config)
2227

2328
public void Start()
2429
{
25-
var membershipProvider = new AnonymousMembershipProvider();
26-
var provider = new DotNetFileSystemProvider(Path.Combine(Environment.CurrentDirectory, config.Path), false);
27-
30+
log.Debug("Start");
31+
var outPath = Path.Combine(Environment.CurrentDirectory, config.Path);
32+
outPath.EnsureDirectoryExistence();
33+
var membershipProvider = new AnonymousMembershipProvider(new NoValidation());
34+
var provider = new DotNetFileSystemProvider(outPath, false);
35+
2836
// Initialize the FTP server
2937
ftpServer = new FtpServer(provider, membershipProvider, "127.0.0.1");
30-
38+
3139
// Start the FTP server
3240
ftpServer.Start();
3341
}

src/YiScanner/service.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
"FileMask": "*.mp4"
2020
},
2121

22-
"Server":
23-
{
24-
"Out": "CCTV"
22+
"Server": {
23+
"Path": "CCTV"
2524
}
2625
}

0 commit comments

Comments
 (0)