using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Cksoft.Data;
using DllEapEntity;
using Cksoft.Unity;
using DllEapEntity.Dtos;
using SharpCifs.Smb;
namespace DllEapDal.Onsemi
{
public class OnsemiProgramDal
{
private readonly IConfiguration configuration;
private IDatabase CurrDb = null;
private readonly string userName;
private string pwd;
public OnsemiProgramDal(IDatabase db, IConfiguration configuration)
{
CurrDb = db;
this.configuration = configuration;
userName = configuration["DiscoUserName"];
pwd = configuration["DiscoPassword"];
// pwd = "Xq20140715";
}
public OnsemiProgramDal(IDatabase db)
{
CurrDb = db;
}
///
/// 获取程序
///
///
///
///
public IEnumerable GetProgramPaths(int macId, string proName, ref string errorinfo)
{
var machine = CurrDb.FindEntityFor(macId);
var macmodel = CurrDb.FindListForCondition($" and a.Id={machine.MModeID}", ref errorinfo)
.FirstOrDefault();
var rootPath = @"\\" + machine.IPAddress + "\\" + macmodel.ProRootPath;
if (string.IsNullOrEmpty(macmodel.ProRootPath))
{
errorinfo = "程序根目录没找到,请先配置程序根目录";
return null;
}
var uploadedPros = CurrDb.FindListForCondition($" and c.UploadMacID={machine.ID}", ref errorinfo);
var pros = new List();
string rootpath = $@"\\{machine.IPAddress}\{macmodel.ProRootPath}";
var folder = new SmbFile($"smb://{userName}:{pwd}@{machine.IPAddress}/{macmodel.ProRootPath}/");
// SharpCifs.Config.SetProperty("jcifs.smb.client.disablePlainTextPasswords", "false");
// SharpCifs.Config.SetProperty("jcifs.smb.client.disablePlainTextPasswords", "false");
this.AppendDirectoryPros(folder, rootpath, pros, uploadedPros, ref errorinfo);
pros = pros.Select(c => new OnsemiProgramDto
{
Path = c.Path.Replace(rootPath + "\\", ""),
Name = c.Name,
State = c.State
}).ToList();
if (!string.IsNullOrEmpty(proName))
{
pros = pros.Where(c => c.Name.Contains(proName)).ToList();
}
return pros;
}
///
/// 递归获取目录下的所有程序
///
/// 目录路径
/// 程序名列表
/// 错误信息
private void AppendDirectoryPros(SmbFile smbFile, string rootPath,
IList fileNames, IEnumerable uploadedPros, ref string errorinfo)
{
var children = smbFile.ListFiles();
var allFiles = new List();
if (children != null && children.Count() > 0)
{
foreach (var item in children)
{
// item.
if (item.IsDirectory()) // 如果当前元素是文件夹 则遍历文件夹里的程序文件
{
//this.AppendDirectoryPros(item, rootPath, fileNames, ref errorinfo);
}
else
{
var ms = new MemoryStream();
var smbStream = item.GetInputStream();
((Stream)smbStream).CopyTo(ms);
ms.Position = 0;
allFiles.Add(new FileDto
{
Ext = Path.GetExtension(item.GetUncPath()),
FileName = Path.GetFileNameWithoutExtension(item.GetName()),
FileBytes = ms.ToArray(),
UNCPath = item.GetUncPath()
});
smbStream.Close();
smbStream.Dispose();
ms.Close();
ms.Dispose();
}
}
}
if (allFiles != null && allFiles.Count() > 0)
{
var dfdFiles = allFiles.Where(c => c.Ext.ToLower() == ".dfd");
if (dfdFiles != null && dfdFiles.Count() > 0)
{
foreach (var item in dfdFiles)
{
bool hasCln = allFiles.Any(c => c.FileName == item.FileName &&
c.Ext.ToLower() == ".cln");
bool hasAlu = allFiles.Any(c => c.FileName == item.FileName &&
c.Ext.ToLower() == ".alu");
if (hasCln && hasAlu)
{
var proName = DiodesProgramFileHelper.GetProgramName01(item.FileBytes, ref errorinfo);
var filePath = item.UNCPath;
fileNames.Add(new OnsemiProgramDto
{
Name = proName,
Path = item.UNCPath.Replace(item.Ext, ""),
State = uploadedPros.Any(c => c.FileOrgName == proName) ? "已上传" : "未上传"
});
}
}
}
}
if (children != null && children.Count() > 0)
{
foreach (var item in children)
{
if (item.IsDirectory())
{
this.AppendDirectoryPros(item, rootPath, fileNames, uploadedPros, ref errorinfo);
}
}
}
}
public int Upload(int macId, string proPath, string proName, string userCode, ref string errorinfo)
{
var machine = CurrDb.FindEntityFor(macId);
var macModel = CurrDb.FindListForCondition($" and a.Id={machine.MModeID}",
ref errorinfo).FirstOrDefault();
var ip = machine.IPAddress;
var rootPath = @"\\" + machine.IPAddress + "\\" + $@"{macModel.ProRootPath}";
var smbRootPath = $@"smb://{userName}:{pwd}@{machine.IPAddress}/{macModel.ProRootPath}/";
var names = new List
{
new SmbFile($"{smbRootPath}{proPath}.dfd"),
new SmbFile($"{smbRootPath}{proPath}.cln"),
new SmbFile($"{smbRootPath}{proPath}.alu")
};
var totalLengthBytes = new byte[0];
var totalFileBytes = new byte[0];
foreach (var item in names)
{
byte[] lBytes;
var fileBytes = this.GetSmbFileBytes(item, out lBytes, ref errorinfo);
if (fileBytes == null)
{
return -1;
}
totalLengthBytes = totalLengthBytes.Concat(lBytes).ToArray();
totalFileBytes = totalFileBytes.Concat(fileBytes).ToArray();
}
var fileDatas = totalLengthBytes.Concat(totalFileBytes).ToArray();
BusinessFileDal dal = new BusinessFileDal(CurrDb);
BusinessFile busifile = dal.UpdownFile(fileDatas,
$@"{configuration["ProgramDir"]}", proName,
machine, proPath, userCode, ref errorinfo);
if (busifile == null)
return -1;
return 1;
}
///
/// 获取单个文件的字节流及字节流的长度
///
///
///
///
///
private byte[] GetFileBytes(string filePath, out byte[] lengthBytes, ref string errorinfo)
{
try
{
var fs = File.OpenRead(filePath);
int length = Convert.ToInt32(fs.Length);
byte[] bytes = BitConverter.GetBytes(length);
var fileBytes = new byte[length];
fs.Read(fileBytes, 0, fileBytes.Length);
fs.Close();
lengthBytes = bytes;
return fileBytes;
}
catch (Exception e)
{
errorinfo = e.Message;
lengthBytes = null;
return null;
}
}
///
/// 获取SmbFile的字节流
///
///
///
///
///
public byte[] GetSmbFileBytes(SmbFile file, out byte[] lengthBytes, ref string errorinfo)
{
var fs = new MemoryStream();
try
{
var readStream = file.GetInputStream();
((Stream)readStream).CopyTo(fs);
int length = Convert.ToInt32(fs.Length);
byte[] bytes = BitConverter.GetBytes(length);
var fileBytes = new byte[length];
fs.Position = 0;
fs.Read(fileBytes, 0, fileBytes.Length);
// var fileBytes = fs.ToArray();
readStream.Close();
readStream.Dispose();
fs.Close();
fs.Dispose();
lengthBytes = bytes;
return fileBytes;
}
catch (Exception e)
{
errorinfo = e.Message;
fs.Close();
fs.Dispose();
lengthBytes = null;
return null;
}
}
}
}