123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- 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;
- }
- /// <summary>
- /// 获取程序
- /// </summary>
- /// <param name="macIp"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public IEnumerable<OnsemiProgramDto> GetProgramPaths(int macId, string proName, ref string errorinfo)
- {
- var machine = CurrDb.FindEntityFor<Machine>(macId);
- var macmodel = CurrDb.FindListForCondition<MacModel>($" 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<BusinessFileRelation>($" and c.UploadMacID={machine.ID}", ref errorinfo);
- var pros = new List<OnsemiProgramDto>();
- 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;
- }
- /// <summary>
- /// 递归获取目录下的所有程序
- /// </summary>
- /// <param name="path">目录路径</param>
- /// <param name="fileNames">程序名列表</param>
- /// <param name="errorinfo">错误信息</param>
- private void AppendDirectoryPros(SmbFile smbFile, string rootPath,
- IList<OnsemiProgramDto> fileNames, IEnumerable<BusinessFileRelation> uploadedPros, ref string errorinfo)
- {
- var children = smbFile.ListFiles();
- var allFiles = new List<FileDto>();
- 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<Machine>(macId);
- var macModel = CurrDb.FindListForCondition<MacModel>($" 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<SmbFile>
- {
- 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;
- }
- /// <summary>
- /// 获取单个文件的字节流及字节流的长度
- /// </summary>
- /// <param name="filePath"></param>
- /// <param name="lengthBytes"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 获取SmbFile的字节流
- /// </summary>
- /// <param name="filePath"></param>
- /// <param name="lengthBytes"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
- }
|