Piscal Client improvements

This commit is contained in:
2016-02-14 23:12:31 -05:00
parent be89837a27
commit 5057d9b577
10 changed files with 143 additions and 22 deletions
+1
View File
@@ -50,6 +50,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Parsers\LeafInputCsvParserTests.cs" />
<Compile Include="Remote\PiscalSshClientTests.cs" />
<Compile Include="Utility\StringExtensionsTests.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Parsers\LeafInputData\LeafInput-valid.csv">
+2 -1
View File
@@ -1,5 +1,6 @@
using System.Configuration;
using LeafWeb.Core.Entities;
using LeafWeb.Core.Remote;
using LeafWeb.Core.Utility;
using NUnit.Framework;
@@ -11,7 +12,7 @@ namespace LeafWeb.Core.Tests.Remote
[Test]
public void Client()
{
var leafInputFile = new LeafInputFile {Filename = "blah", Id = 1, Contents = "test".GetBytes()};
var leafInputFile = new PiscalLeafInputFile{Filename = "blah", Id = 1, Contents = "test".GetBytes(), DirectoryName = "TestDirectory"};
var piscalConnectionString = ConfigurationManager.ConnectionStrings["PiscalServer"];
var client = new PiscalSshClient(piscalConnectionString.ConnectionString);
client.SubmitLeafInputFile(leafInputFile);
@@ -0,0 +1,42 @@
using System;
using LeafWeb.Core.Utility;
using NUnit.Framework;
namespace LeafWeb.Core.Tests.Utility
{
[TestFixture]
public class StringExtensionsTests
{
[Test]
public void FilterAlphaNumeric_AllGood()
{
var str = "thisIsFine123_";
var result = str.FilterAlphaNumeric();
Assert.That(result, Is.EqualTo(str));
}
[Test]
public void FilterAlphaNumeric_NotAllowed()
{
var str = "notAllowed%";
var result = str.FilterAlphaNumeric();
Assert.That(result, Is.EqualTo("notAllowed"));
}
[Test]
public void FilterAlphaNumeric_Space()
{
var str = "s p a c e s";
var result = str.FilterAlphaNumeric();
Assert.That(result, Is.EqualTo("spaces"));
}
[Test]
public void FilterAlphaNumeric_NewLine()
{
var str = "new" + Environment.NewLine + "line";
var result = str.FilterAlphaNumeric();
Assert.That(result, Is.EqualTo("newline"));
}
}
}
+5
View File
@@ -30,6 +30,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="AutoMapper, Version=4.2.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005, processorArchitecture=MSIL">
<HintPath>..\packages\AutoMapper.4.2.0\lib\net45\AutoMapper.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="CsvHelper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8c4959082be5c823, processorArchitecture=MSIL">
<HintPath>..\packages\CsvHelper.2.13.2.0\lib\net40-client\CsvHelper.dll</HintPath>
<Private>True</Private>
@@ -81,6 +85,7 @@
<Compile Include="Entities\LeafInputSite.cs" />
<Compile Include="Entities\LeafInputStatus.cs" />
<Compile Include="Parsers\FluxnetSiteCsvParser.cs" />
<Compile Include="Remote\PiscalLeafInputFile.cs" />
<Compile Include="Utility\BoolTypeConverter.cs" />
<Compile Include="Parsers\CntrlComparisonParser.cs" />
<Compile Include="Parsers\CsvParserBase.cs" />
+7 -1
View File
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using LeafWeb.Core.Utility;
namespace LeafWeb.Core.Entities
{
@@ -20,7 +21,7 @@ namespace LeafWeb.Core.Entities
[Required(ErrorMessage = "A unique identifier is required")]
public string Identifier { get; set; }
[Required(ErrorMessage = "")]
[Required(ErrorMessage = "Site Id required")]
public string SiteId { get; set; }
[Required(ErrorMessage = "PhotosynthesisType required")]
@@ -32,5 +33,10 @@ namespace LeafWeb.Core.Entities
[DataType(DataType.Date)]
public DateTime? Processed { get; set; }
/// <summary>
/// An cleaned up identifier to be used for file naming, etc
/// </summary>
public string CombinedIdentifier => $"{Name.FilterAlphaNumeric()}_{Identifier.FilterAlphaNumeric()}";
}
}
+2
View File
@@ -14,5 +14,7 @@
public string Filename { get; set; }
public byte[] Contents { get; set; }
public string CombinedIdentifier => $"{LeafInput.CombinedIdentifier}_{Id}";
}
}
+32 -20
View File
@@ -1,52 +1,58 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using LeafWeb.Core.Entities;
using LeafWeb.Core.Utility;
using Renci.SshNet;
namespace LeafWeb.Core
namespace LeafWeb.Core.Remote
{
public interface IPiscalClient
{
void SubmitLeafInputFile(LeafInputFile file);
void RetrieveLeafInputResult();
void SubmitLeafInputFile(PiscalLeafInputFile file);
void RetrieveLeafInputResult(PiscalLeafInputFile file);
}
public class PiscalSshClient : IPiscalClient
{
private readonly Dictionary<string, string> _conn;
private const string BaseDirectory = "LeafInput";
private readonly PasswordConnectionInfo _connectionInfo;
public PiscalSshClient(string connectionString)
{
_conn = connectionString.Split(';')
.Select(t => t.Split(new [] { '=' }, 2))
.ToDictionary(t => t[0].Trim(), t => t[1].Trim(), StringComparer.InvariantCultureIgnoreCase);
var conn = connectionString.SplitConnectionString();
var host = conn["host"];
var username = conn["username"];
var password = conn["password"];
_connectionInfo = new PasswordConnectionInfo(host, username, password);
}
private SshClient GetSshClient()
{
return new SshClient(_conn["host"], _conn["username"], _conn["password"]);
}
private ScpClient GetScpClient()
{
return new ScpClient(_conn["host"], _conn["username"], _conn["password"]);
return new SshClient(_connectionInfo);
}
public void SubmitLeafInputFile(LeafInputFile file)
private ScpClient GetScpClient()
{
return new ScpClient(_connectionInfo);
}
public void SubmitLeafInputFile(PiscalLeafInputFile file)
{
var inputPath = BaseDirectory + "/" + file.DirectoryName + "/" + file.Filename;
var outputPath = BaseDirectory + "/" + file.DirectoryName + "/" + file.Filename;
using (var scp = GetScpClient())
using (var stream = new MemoryStream(file.Contents))
{
scp.Connect();
scp.Upload(stream, file.Filename);
scp.Upload(stream, inputPath);
}
using (var ssh = GetSshClient())
{
ssh.Connect();
var lsCommend = ssh.CreateCommand("ls");
var lsCommend = ssh.CreateCommand("ls -r");
lsCommend.Execute();
//var extendedData = Encoding.ASCII.GetString(cmd.ExtendedOutputStream.ToArray());
var extendedData = new StreamReader(lsCommend.ExtendedOutputStream, Encoding.ASCII).ReadToEnd();
@@ -62,9 +68,15 @@ namespace LeafWeb.Core
// begin processing
}
public void RetrieveLeafInputResult()
public void RetrieveLeafInputResult(PiscalLeafInputFile file)
{
throw new System.NotImplementedException();
using (var scp = GetScpClient())
using (var stream = new MemoryStream(file.Contents))
{
scp.Connect();
scp.Upload(stream, BaseDirectory + "/" + file.DirectoryName + "/" + file.Filename);
}
}
}
}
+37
View File
@@ -0,0 +1,37 @@
using AutoMapper;
using LeafWeb.Core.Entities;
using LeafWeb.Core.Utility;
namespace LeafWeb.Core.Remote
{
public class PiscalLeafInputFile
{
private static readonly IMapper Mapper;
public int Id { get; set; }
public string Filename { get; set; }
public byte[] Contents { get; set; }
public string DirectoryName { get; set; }
static PiscalLeafInputFile()
{
var config =
new MapperConfiguration(cfg =>
{
cfg.CreateMap<LeafInputFile, PiscalLeafInputFile>()
.ForMember(dest => dest.DirectoryName, opt => opt.MapFrom(
src =>
$"{src.Id}_{src.LeafInput.Name.FilterAlphaNumeric()}_{src.LeafInput.Identifier.FilterAlphaNumeric()}"
));
});
Mapper = config.CreateMapper();
}
public PiscalLeafInputFile() { }
public PiscalLeafInputFile(LeafInputFile leafInputFile)
{
Mapper.Map(leafInputFile, this);
}
}
}
+14
View File
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
namespace LeafWeb.Core.Utility
{
@@ -38,5 +40,17 @@ namespace LeafWeb.Core.Utility
Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
public static Dictionary<string, string> SplitConnectionString(this string connectionString)
{
return connectionString.Split(';')
.Select(t => t.Split(new[] { '=' }, 2))
.ToDictionary(t => t[0].Trim(), t => t[1].Trim(), StringComparer.InvariantCultureIgnoreCase);
}
public static string FilterAlphaNumeric(this string input)
{
return Regex.Replace(input, @"[^\w_]", "");
}
}
}
+1
View File
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AutoMapper" version="4.2.0" targetFramework="net45" />
<package id="CsvHelper" version="2.13.2.0" targetFramework="net45" />
<package id="EntityFramework" version="6.1.3" targetFramework="net45" />
<package id="fasterflect" version="2.1.3" targetFramework="net45" />