天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
7.1 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Common.TextUtil;
namespace SSC
{
public class Z005 : NSC.IService
{
/// <summary>
/// 获取最新文件
/// </summary>
/// <param name="userAuth"></param>
/// <param name="input"></param>
/// <returns></returns>
public NSC.NetServiceStruct ServiceFunction(Common.Data.UserAuthority userAuth, NSC.NetServiceStruct input)
{
string version = "";
StringBuilder query = new StringBuilder();
query.Append("SELECT VERSIONNO FROM T_UPDATE WHERE DEVICENAME='{DEVICENAME}' AND UPDATETIME<={UPDATETIME} ORDER BY CREATETIME DESC LIMIT 1");
query.Replace("{DEVICENAME}", input.FirmwareModel);
query.Replace("{UPDATETIME}", Common.Data.SqlLite.SqlLiteHelper.LOCAL_TIME_CMD);
Common.Data.SqlLite.SqlLiteHelper sqlite = new Common.Data.SqlLite.SqlLiteHelper();
using (var conn = sqlite.OpenConnection(Common.Config.ConfigSetting.GetConnectionString()))
using (var table = sqlite.QueryReturnDataTable(conn, query.ToString()))
{
if (table.Rows.Count > 0)
{
version = table.Rows[0]["VERSIONNO"].ToString();
}
}
if (version == "")
{
throw new Exception("找不到适用的更新文件!");
}
//打开文件
string filename = Common.Config.ConfigSetting.GetSetting("0001") + input.FirmwareModel + @"\" + version + @"\update.bin";
byte[] bytes = null;
using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
{
bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
}
input.ExceptionType = Common.Data.ExceptionType.NoneException;
input.Returns.Add("Version", new NSC.ParameterStruct(version));
input.Returns.Add("Files", new NSC.ParameterStruct(bytes));
return input;
}
/// <summary>
/// 解码更新文件
/// </summary>
/// <param name="binFile">要解码的更新文件流</param>
/// <param name="version">版本号</param>
/// <param name="tmpPath">临时文件夹路径</param>
/// <returns>解码后的临时文件信息</returns>
public List<FileInformation> DecodeUpdateBinFile(byte[] binFile, string version, string tmpPath)
{
byte[] buff = Common.IO.Compression.DeflateCompression.Decompress(binFile);
int length = BitConverter.ToInt32(buff, 0);
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(Encoding.GetEncoding("gb2312").GetString(buff, 4, length));
var nodes = doc.SelectNodes("FileUpdateData/File");
if (System.IO.Directory.Exists(tmpPath))
{
System.IO.Directory.Delete(tmpPath, true);
}
System.IO.Directory.CreateDirectory(tmpPath);
List<FileInformation> fileList = new List<FileInformation>();
int position = length + 3;
foreach (System.Xml.XmlNode n in nodes)
{
FileInformation file = new FileInformation();
file.FileName = n.Attributes["Name"].Value;
file.UpdatePath = n.Attributes["Path"].Value;
file.FileLength = int.Parse(n.Attributes["Length"].Value);
if (file.UpdatePath.IsEmptyOrNull())
{
file.FilePath = tmpPath + @"\" + file.FileName;
}
else
{
System.IO.Directory.CreateDirectory(tmpPath + @"\" + file.UpdatePath);
file.FilePath = tmpPath + @"\" + file.UpdatePath + @"\" + file.FileName;
}
using (System.IO.FileStream fs = new System.IO.FileStream(file.FilePath,
System.IO.FileMode.CreateNew, System.IO.FileAccess.Write, System.IO.FileShare.None))
{
fs.Write(buff, position, file.FileLength);
}
position += file.FileLength;
fileList.Add(file);
}
return fileList;
}
/// <summary>
/// 编码更新文件
/// </summary>
/// <param name="fileList">文件列表</param>
/// <param name="module">更新模块定义</param>
/// <param name="version">版本号</param>
/// <returns>编码后的字节流</returns>
public static byte[] EncodeUpdateBinFile(List<FileInformation> fileList, string module, string version)
{
using (MemoryStream tmpStream = new MemoryStream())
{
//写版本及文件信息
System.Xml.XmlTextWriter xtw = new System.Xml.XmlTextWriter(tmpStream, EncodingHelper.GB2312);
xtw.WriteStartDocument();
xtw.WriteStartElement("FileUpdateData");
xtw.WriteAttributeString("ModuleCode", module);
xtw.WriteAttributeString("VersionNumber", version);
foreach (var f in fileList)
{
xtw.WriteStartElement("File");
xtw.WriteAttributeString("Name", f.FileName);
xtw.WriteAttributeString("Path", f.UpdatePath);
xtw.WriteAttributeString("Length", f.FileLength.ToString());
xtw.WriteEndElement();
}
xtw.WriteEndElement();
xtw.WriteEndDocument();
xtw.Flush();
int length = (int)tmpStream.Length;
tmpStream.Position = 0;
tmpStream.Write(BitConverter.GetBytes(length), 0, 4);
tmpStream.Position = tmpStream.Length - 1;
//写入文件流
foreach (var f in fileList)
{
using (System.IO.FileStream fs = new FileStream(f.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
byte[] buff = new byte[fs.Length];
fs.Read(buff, 0, (int)fs.Length);
tmpStream.Write(buff, 0, buff.Length);
}
}
return Common.IO.Compression.DeflateCompression.Compress(tmpStream.ToArray());
}
}
}
/// <summary>
/// 文件信息
/// </summary>
public struct FileInformation
{
/// <summary>
/// 文件名
/// </summary>
public string FileName;
/// <summary>
/// 文件存放路径
/// </summary>
public string FilePath;
/// <summary>
/// 文件更新目标相对路径
/// </summary>
public string UpdatePath;
/// <summary>
/// 文件长度
/// </summary>
public int FileLength;
}
}