using Hyperion; using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; namespace Common.BaseLibrary.IO { public class IO { public static byte[] SaveBin(object obj) { return SaveBin(obj, false); } public static object LoadBin(byte[] buff) { return LoadBin(buff, false); } public static byte[] SaveBin(object obj, bool compress) { byte[] buff = null; using (MemoryStream stream = new MemoryStream()) { if (compress) { using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Compress)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(gzipStream, obj); } } else { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, obj); } buff = stream.ToArray(); } return buff; } public static object LoadBin(byte[] buff, bool compress) { object obj = null; if (buff != null) { using (MemoryStream stream = new MemoryStream()) { stream.Write(buff, 0, buff.Length); stream.Seek(0, SeekOrigin.Begin); if (compress) { using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress)) { BinaryFormatter formatter = new BinaryFormatter(); try { obj = formatter.Deserialize(gzipStream); } catch //(SerializationException e) { //throw; } finally { gzipStream.Close(); } } } else { BinaryFormatter formatter = new BinaryFormatter(); try { obj = formatter.Deserialize(stream); } catch //(SerializationException e) { //throw; } finally { stream.Close(); } } } } return obj; } public static byte[] SerializeByHyperion<T>(T obj) { var hser = new Serializer(new SerializerOptions(false)); var hstream = new MemoryStream(); hser.Serialize(obj, hstream); byte[] buff = hstream.ToArray(); hstream.Close(); hstream.Dispose(); return buff; } public static T DeserializeByHyperion<T>(byte[] buff) { var hdeser = new Serializer(new SerializerOptions(false)); var hstream = new MemoryStream(buff); T obj = hdeser.Deserialize<T>(hstream); hstream.Close(); hstream.Dispose(); return obj; } } }
public void InsertWorkingDataDBRec(WorkDataModel model) { IExtensionRepository<WorkingData> workDataService = DIFactory.ObjectContainer.Resolve<IExtensionRepository<WorkingData>>(); if (model != null) { WorkingData data = new WorkingData() { TaskID = model.TaskID, NJStartTime = model.NJStartTime, NJStopTime = model.NJStopTime, NJWorkingDt = IO.SerializeByHyperion<List<RealTimeDataModel>>(JsonConvert.DeserializeObject<List<RealTimeDataModel>>(model.NJWorkingDt)),//壓縮數據流 OptFieldName = model.OptFieldName, OptLength = model.OptLength, GoodOptLength = model.GoodOptLength, OptArea = model.OptArea, OptPrice = model.OptPrice, OptGoodPercent = model.OptGoodPercent, OptAllowance = model.OptAllowance, OptAvgDepth = model.OptAvgDepth, IsNJOpt = model.IsNJOpt, OptAvgFuel = model.OptAvgFuel, }; workDataService.Insert(data); } }
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AIMS.Model { public class WorkingData { [Key] public int Id { get; set; } /// <summary> /// 任務id /// </summary> [Required] public int TaskID { get; set; } /// <summary> /// 開始時間 /// </summary> public DateTime? NJStartTime { get; set; } /// <summary> /// 結束時間 /// </summary> public DateTime? NJStopTime { get; set; } /// <summary> /// 農機數據序列化爲二進制 /// </summary> public byte[] NJWorkingDt { get; set; }//重點 /// <summary> /// 做業地塊的名稱 /// </summary> [StringLength(255)] public string OptFieldName { get; set; } /// <summary> /// 做業長度 /// </summary> public double? OptLength { get; set; } /// <summary> /// 做業達標長度 /// </summary> public double? GoodOptLength { get; set; } /// <summary> /// 做業面積 /// </summary> public double? OptArea { get; set; } /// <summary> /// 做業價格 /// </summary> public decimal? OptPrice { get; set; } /// <summary> /// 做業達標率 /// </summary> public double? OptGoodPercent { get; set; } /// <summary> /// 補貼價格 /// </summary> public decimal? OptAllowance { get; set; } /// <summary> /// 做業平均耕深 /// </summary> public double? OptAvgDepth { get; set; } /// <summary> /// 是否做業 /// </summary> public bool IsNJOpt { get; set; } /// <summary> /// 做業平均油耗 /// </summary> public double? OptAvgFuel { get; set; } } }