OnsemiProgramDal.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using Microsoft.Extensions.Configuration;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using Cksoft.Data;
  7. using DllEapEntity;
  8. using Cksoft.Unity;
  9. using DllEapEntity.Dtos;
  10. using SharpCifs.Smb;
  11. namespace DllEapDal.Onsemi
  12. {
  13. public class OnsemiProgramDal
  14. {
  15. private readonly IConfiguration configuration;
  16. private IDatabase CurrDb = null;
  17. private readonly string userName;
  18. private string pwd;
  19. public OnsemiProgramDal(IDatabase db, IConfiguration configuration)
  20. {
  21. CurrDb = db;
  22. this.configuration = configuration;
  23. userName = configuration["DiscoUserName"];
  24. pwd = configuration["DiscoPassword"];
  25. // pwd = "Xq20140715";
  26. }
  27. public OnsemiProgramDal(IDatabase db)
  28. {
  29. CurrDb = db;
  30. }
  31. /// <summary>
  32. /// 获取程序
  33. /// </summary>
  34. /// <param name="macIp"></param>
  35. /// <param name="errorinfo"></param>
  36. /// <returns></returns>
  37. public IEnumerable<OnsemiProgramDto> GetProgramPaths(int macId, string proName, ref string errorinfo)
  38. {
  39. var machine = CurrDb.FindEntityFor<Machine>(macId);
  40. var macmodel = CurrDb.FindListForCondition<MacModel>($" and a.Id={machine.MModeID}", ref errorinfo)
  41. .FirstOrDefault();
  42. var rootPath = @"\\" + machine.IPAddress + "\\" + macmodel.ProRootPath;
  43. if (string.IsNullOrEmpty(macmodel.ProRootPath))
  44. {
  45. errorinfo = "程序根目录没找到,请先配置程序根目录";
  46. return null;
  47. }
  48. var uploadedPros = CurrDb.FindListForCondition<BusinessFileRelation>($" and c.UploadMacID={machine.ID}", ref errorinfo);
  49. var pros = new List<OnsemiProgramDto>();
  50. string rootpath = $@"\\{machine.IPAddress}\{macmodel.ProRootPath}";
  51. var folder = new SmbFile($"smb://{userName}:{pwd}@{machine.IPAddress}/{macmodel.ProRootPath}/");
  52. // SharpCifs.Config.SetProperty("jcifs.smb.client.disablePlainTextPasswords", "false");
  53. // SharpCifs.Config.SetProperty("jcifs.smb.client.disablePlainTextPasswords", "false");
  54. this.AppendDirectoryPros(folder, rootpath, pros, uploadedPros, ref errorinfo);
  55. pros = pros.Select(c => new OnsemiProgramDto
  56. {
  57. Path = c.Path.Replace(rootPath + "\\", ""),
  58. Name = c.Name,
  59. State = c.State
  60. }).ToList();
  61. if (!string.IsNullOrEmpty(proName))
  62. {
  63. pros = pros.Where(c => c.Name.Contains(proName)).ToList();
  64. }
  65. return pros;
  66. }
  67. /// <summary>
  68. /// 递归获取目录下的所有程序
  69. /// </summary>
  70. /// <param name="path">目录路径</param>
  71. /// <param name="fileNames">程序名列表</param>
  72. /// <param name="errorinfo">错误信息</param>
  73. private void AppendDirectoryPros(SmbFile smbFile, string rootPath,
  74. IList<OnsemiProgramDto> fileNames, IEnumerable<BusinessFileRelation> uploadedPros, ref string errorinfo)
  75. {
  76. var children = smbFile.ListFiles();
  77. var allFiles = new List<FileDto>();
  78. if (children != null && children.Count() > 0)
  79. {
  80. foreach (var item in children)
  81. {
  82. // item.
  83. if (item.IsDirectory()) // 如果当前元素是文件夹 则遍历文件夹里的程序文件
  84. {
  85. //this.AppendDirectoryPros(item, rootPath, fileNames, ref errorinfo);
  86. }
  87. else
  88. {
  89. var ms = new MemoryStream();
  90. var smbStream = item.GetInputStream();
  91. ((Stream)smbStream).CopyTo(ms);
  92. ms.Position = 0;
  93. allFiles.Add(new FileDto
  94. {
  95. Ext = Path.GetExtension(item.GetUncPath()),
  96. FileName = Path.GetFileNameWithoutExtension(item.GetName()),
  97. FileBytes = ms.ToArray(),
  98. UNCPath = item.GetUncPath()
  99. });
  100. smbStream.Close();
  101. smbStream.Dispose();
  102. ms.Close();
  103. ms.Dispose();
  104. }
  105. }
  106. }
  107. if (allFiles != null && allFiles.Count() > 0)
  108. {
  109. var dfdFiles = allFiles.Where(c => c.Ext.ToLower() == ".dfd");
  110. if (dfdFiles != null && dfdFiles.Count() > 0)
  111. {
  112. foreach (var item in dfdFiles)
  113. {
  114. bool hasCln = allFiles.Any(c => c.FileName == item.FileName &&
  115. c.Ext.ToLower() == ".cln");
  116. bool hasAlu = allFiles.Any(c => c.FileName == item.FileName &&
  117. c.Ext.ToLower() == ".alu");
  118. if (hasCln && hasAlu)
  119. {
  120. var proName = DiodesProgramFileHelper.GetProgramName01(item.FileBytes, ref errorinfo);
  121. var filePath = item.UNCPath;
  122. fileNames.Add(new OnsemiProgramDto
  123. {
  124. Name = proName,
  125. Path = item.UNCPath.Replace(item.Ext, ""),
  126. State = uploadedPros.Any(c => c.FileOrgName == proName) ? "已上传" : "未上传"
  127. });
  128. }
  129. }
  130. }
  131. }
  132. if (children != null && children.Count() > 0)
  133. {
  134. foreach (var item in children)
  135. {
  136. if (item.IsDirectory())
  137. {
  138. this.AppendDirectoryPros(item, rootPath, fileNames, uploadedPros, ref errorinfo);
  139. }
  140. }
  141. }
  142. }
  143. public int Upload(int macId, string proPath, string proName, string userCode, ref string errorinfo)
  144. {
  145. var machine = CurrDb.FindEntityFor<Machine>(macId);
  146. var macModel = CurrDb.FindListForCondition<MacModel>($" and a.Id={machine.MModeID}",
  147. ref errorinfo).FirstOrDefault();
  148. var ip = machine.IPAddress;
  149. var rootPath = @"\\" + machine.IPAddress + "\\" + $@"{macModel.ProRootPath}";
  150. var smbRootPath = $@"smb://{userName}:{pwd}@{machine.IPAddress}/{macModel.ProRootPath}/";
  151. var names = new List<SmbFile>
  152. {
  153. new SmbFile($"{smbRootPath}{proPath}.dfd"),
  154. new SmbFile($"{smbRootPath}{proPath}.cln"),
  155. new SmbFile($"{smbRootPath}{proPath}.alu")
  156. };
  157. var totalLengthBytes = new byte[0];
  158. var totalFileBytes = new byte[0];
  159. foreach (var item in names)
  160. {
  161. byte[] lBytes;
  162. var fileBytes = this.GetSmbFileBytes(item, out lBytes, ref errorinfo);
  163. if (fileBytes == null)
  164. {
  165. return -1;
  166. }
  167. totalLengthBytes = totalLengthBytes.Concat(lBytes).ToArray();
  168. totalFileBytes = totalFileBytes.Concat(fileBytes).ToArray();
  169. }
  170. var fileDatas = totalLengthBytes.Concat(totalFileBytes).ToArray();
  171. BusinessFileDal dal = new BusinessFileDal(CurrDb);
  172. BusinessFile busifile = dal.UpdownFile(fileDatas,
  173. $@"{configuration["ProgramDir"]}", proName,
  174. machine, proPath, userCode, ref errorinfo);
  175. if (busifile == null)
  176. return -1;
  177. return 1;
  178. }
  179. /// <summary>
  180. /// 获取单个文件的字节流及字节流的长度
  181. /// </summary>
  182. /// <param name="filePath"></param>
  183. /// <param name="lengthBytes"></param>
  184. /// <param name="errorinfo"></param>
  185. /// <returns></returns>
  186. private byte[] GetFileBytes(string filePath, out byte[] lengthBytes, ref string errorinfo)
  187. {
  188. try
  189. {
  190. var fs = File.OpenRead(filePath);
  191. int length = Convert.ToInt32(fs.Length);
  192. byte[] bytes = BitConverter.GetBytes(length);
  193. var fileBytes = new byte[length];
  194. fs.Read(fileBytes, 0, fileBytes.Length);
  195. fs.Close();
  196. lengthBytes = bytes;
  197. return fileBytes;
  198. }
  199. catch (Exception e)
  200. {
  201. errorinfo = e.Message;
  202. lengthBytes = null;
  203. return null;
  204. }
  205. }
  206. /// <summary>
  207. /// 获取SmbFile的字节流
  208. /// </summary>
  209. /// <param name="filePath"></param>
  210. /// <param name="lengthBytes"></param>
  211. /// <param name="errorinfo"></param>
  212. /// <returns></returns>
  213. public byte[] GetSmbFileBytes(SmbFile file, out byte[] lengthBytes, ref string errorinfo)
  214. {
  215. var fs = new MemoryStream();
  216. try
  217. {
  218. var readStream = file.GetInputStream();
  219. ((Stream)readStream).CopyTo(fs);
  220. int length = Convert.ToInt32(fs.Length);
  221. byte[] bytes = BitConverter.GetBytes(length);
  222. var fileBytes = new byte[length];
  223. fs.Position = 0;
  224. fs.Read(fileBytes, 0, fileBytes.Length);
  225. // var fileBytes = fs.ToArray();
  226. readStream.Close();
  227. readStream.Dispose();
  228. fs.Close();
  229. fs.Dispose();
  230. lengthBytes = bytes;
  231. return fileBytes;
  232. }
  233. catch (Exception e)
  234. {
  235. errorinfo = e.Message;
  236. fs.Close();
  237. fs.Dispose();
  238. lengthBytes = null;
  239. return null;
  240. }
  241. }
  242. }
  243. }