private void FromCurrentFormat(byte[] bytes, int offset = 0) { Signature = bytes.GetString(0, 2); Version = bytes.ToByte(2); Length = bytes.Length; offset += 3; //Get all inputs Inputs.AddRange(GetArray(bytes, InputPoint.GetCount(FileVersion), InputPoint.GetSize(FileVersion), ref offset) .Select(i => new InputPoint(i, 0, FileVersion))); //Get all outputs Outputs.AddRange(GetArray(bytes, OutputPoint.GetCount(FileVersion), OutputPoint.GetSize(FileVersion), ref offset) .Select(i => new OutputPoint(i, 0, FileVersion))); //Get all variables Variables.AddRange(GetArray(bytes, VariablePoint.GetCount(FileVersion), VariablePoint.GetSize(FileVersion), ref offset) .Select(i => new VariablePoint(i, 0, FileVersion))); //Get all programs Programs.AddRange(GetArray(bytes, ProgramPoint.GetCount(FileVersion), ProgramPoint.GetSize(FileVersion), ref offset) .Select(i => new ProgramPoint(i, 0, FileVersion))); //Get all controllers Controllers.AddRange(GetArray(bytes, ControllerPoint.GetCount(FileVersion), ControllerPoint.GetSize(FileVersion), ref offset) .Select(i => new ControllerPoint(i, 0, FileVersion))); //Get all screens Screens.AddRange(GetArray(bytes, ScreenPoint.GetCount(FileVersion), ScreenPoint.GetSize(FileVersion), ref offset) .Select(i => new ScreenPoint(i, 0, FileVersion))); //Get all graphics //TODO: Constants to object static Size(FileVersion) Count(FileVersion) Graphics.AddRange(GetArray(bytes, GraphicPoint.GetCount(FileVersion), GraphicPoint.GetSize(FileVersion), ref offset) .Select(i => new GraphicPoint(i, 0, FileVersion))); Users.AddRange(GetArray(bytes, UserPoint.GetCount(FileVersion), UserPoint.GetSize(FileVersion), ref offset) .Select(i => new UserPoint(i, 0, FileVersion))); CustomUnits.Digital.AddRange(GetArray(bytes, CustomDigitalUnitsPoint.GetCount(FileVersion), CustomDigitalUnitsPoint.GetSize(FileVersion), ref offset) .Select(i => new CustomDigitalUnitsPoint(i, 0, FileVersion))); Tables.AddRange(GetArray(bytes, TablePoint.GetCount(FileVersion), TablePoint.GetSize(FileVersion), ref offset) .Select(i => new TablePoint(i, 0, FileVersion))); Settings = new Settings( GetObject(bytes, Settings.GetSize(FileVersion), ref offset), 0, FileVersion); Schedules.AddRange(GetArray(bytes, SchedulePoint.GetCount(FileVersion), SchedulePoint.GetSize(FileVersion), ref offset) .Select(i => new SchedulePoint(i, 0, FileVersion))); Holidays.AddRange(GetArray(bytes, HolidayPoint.GetCount(FileVersion), HolidayPoint.GetSize(FileVersion), ref offset) .Select(i => new HolidayPoint(i, 0, FileVersion))); Monitors.AddRange(GetArray(bytes, MonitorPoint.GetCount(FileVersion), MonitorPoint.GetSize(FileVersion), ref offset) .Select(i => new MonitorPoint(i, 0, FileVersion))); ScheduleCodes.AddRange(GetArray(bytes, ScheduleCode.GetCount(FileVersion), ScheduleCode.GetSize(FileVersion), ref offset) .Select(i => new ScheduleCode(i, 0, FileVersion))); HolidayCodes.AddRange(GetArray(bytes, HolidayCode.GetCount(FileVersion), HolidayCode.GetSize(FileVersion), ref offset) .Select(i => new HolidayCode(i, 0, FileVersion))); int pcode_offset = offset; var ProgramCodeBytes = bytes.ToBytes(offset, ProgramCode.GetSize(FileVersion)); ProgramCodes.AddRange(GetArray(bytes, ProgramCode.GetCount(FileVersion), ProgramCode.GetSize(FileVersion), ref offset) //.Select(i => new ProgramCode(i, this, 0, FileVersion))); .Select(i => new ProgramCode())); ProgramCodes[0] = new ProgramCode(ProgramCodeBytes, this, 0, FileVersion); for (int i = 1; i < ProgramCode.GetCount(FileVersion); i++) { pcode_offset += ProgramCode.GetSize(FileVersion); ProgramCodeBytes = bytes.ToBytes(pcode_offset, ProgramCode.GetSize(FileVersion)); ProgramCodes[i] = new ProgramCode(ProgramCodeBytes, this, 0, FileVersion); Debug.WriteLine($"Leído ProgramCode[{i}]"); } CustomUnits.Analog.AddRange(GetArray(bytes, CustomAnalogUnitsPoint.GetCount(FileVersion), CustomAnalogUnitsPoint.GetSize(FileVersion), ref offset) .Select(i => new CustomAnalogUnitsPoint(i, 0, FileVersion))); CheckOffset(offset, Length); UpdateCustomUnits(); }
public byte[] ToCurrentFormat() { //Update if Unit changed UpdateCustomUnits(); var bytes = new List <byte>(); bytes.AddRange(Signature.ToBytes(2)); bytes.Add((byte)Version); //'for' instead 'foreach' for upgrade support for (var i = 0; i < InputPoint.GetCount(FileVersion); ++i) { var obj = Inputs.ElementAtOrDefault(i) ?? new InputPoint(); obj.FileVersion = FileVersion; bytes.AddRange(obj.ToBytes()); } for (var i = 0; i < OutputPoint.GetCount(FileVersion); ++i) { var obj = Outputs.ElementAtOrDefault(i) ?? new OutputPoint(); bytes.AddRange(obj.ToBytes()); } for (var i = 0; i < VariablePoint.GetCount(FileVersion); ++i) { var obj = Variables.ElementAtOrDefault(i) ?? new VariablePoint(); bytes.AddRange(obj.ToBytes()); } for (var i = 0; i < ProgramPoint.GetCount(FileVersion); ++i) { var obj = Programs.ElementAtOrDefault(i) ?? new ProgramPoint(); bytes.AddRange(obj.ToBytes()); } for (var i = 0; i < ControllerPoint.GetCount(FileVersion); ++i) { var obj = Controllers.ElementAtOrDefault(i) ?? new ControllerPoint(); bytes.AddRange(obj.ToBytes()); } for (var i = 0; i < ScreenPoint.GetCount(FileVersion); ++i) { var obj = Screens.ElementAtOrDefault(i) ?? new ScreenPoint(); bytes.AddRange(obj.ToBytes()); } for (var i = 0; i < GraphicPoint.GetCount(FileVersion); ++i) { var obj = Graphics.ElementAtOrDefault(i) ?? new GraphicPoint(); bytes.AddRange(obj.ToBytes()); } for (var i = 0; i < UserPoint.GetCount(FileVersion); ++i) { var obj = Users.ElementAtOrDefault(i) ?? new UserPoint(); bytes.AddRange(obj.ToBytes()); } for (var i = 0; i < CustomDigitalUnitsPoint.GetCount(FileVersion); ++i) { var obj = CustomUnits.Digital.ElementAtOrDefault(i) ?? new CustomDigitalUnitsPoint(); bytes.AddRange(obj.ToBytes()); } for (var i = 0; i < TablePoint.GetCount(FileVersion); ++i) { var obj = Tables.ElementAtOrDefault(i) ?? new TablePoint(); bytes.AddRange(obj.ToBytes()); } { var settings = Settings ?? new Settings(FileVersion); bytes.AddRange(settings.ToBytes()); } for (var i = 0; i < SchedulePoint.GetCount(FileVersion); ++i) { var obj = Schedules.ElementAtOrDefault(i) ?? new SchedulePoint(); bytes.AddRange(obj.ToBytes()); } for (var i = 0; i < HolidayPoint.GetCount(FileVersion); ++i) { var obj = Holidays.ElementAtOrDefault(i) ?? new HolidayPoint(); bytes.AddRange(obj.ToBytes()); } for (var i = 0; i < MonitorPoint.GetCount(FileVersion); ++i) { var obj = Monitors.ElementAtOrDefault(i) ?? new MonitorPoint(); bytes.AddRange(obj.ToBytes()); } for (var i = 0; i < ScheduleCode.GetCount(FileVersion); ++i) { var obj = ScheduleCodes.ElementAtOrDefault(i) ?? new ScheduleCode(); bytes.AddRange(obj.ToBytes()); } for (var i = 0; i < HolidayCode.GetCount(FileVersion); ++i) { var obj = HolidayCodes.ElementAtOrDefault(i) ?? new HolidayCode(); bytes.AddRange(obj.ToBytes()); } for (var i = 0; i < ProgramCode.GetCount(FileVersion); ++i) { var obj = ProgramCodes.ElementAtOrDefault(i) ?? new ProgramCode(); bytes.AddRange(obj.ToBytes()); } for (var i = 0; i < CustomAnalogUnitsPoint.GetCount(FileVersion); ++i) { var obj = CustomUnits.Analog.ElementAtOrDefault(i) ?? new CustomAnalogUnitsPoint(); bytes.AddRange(obj.ToBytes()); } if (!IsUpgraded) { CheckSize(bytes.Count, Length); } return(bytes.ToArray()); }