-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathLoxStatFile.cs
More file actions
203 lines (188 loc) · 6.84 KB
/
Copy pathLoxStatFile.cs
File metadata and controls
203 lines (188 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
namespace LoxStatEdit
{
public class LoxStatFile : LoxStatObject
{
/// <summary>The original Loxone file name. Contains UUID and datecode of the statfile.
/// </summary>
public string FileName { get; set; }
/// <summary>Offset 0, Length 2, number of values per data point
/// </summary>
public ushort ValueCount { get; set; }
/// <summary>Offset 2, Length 2, always 0x0000
/// </summary>
public ushort Unknown0x02 { get; set; }
/// <summary>Offset 4, Length 4, Flags?
/// </summary>
public uint Unknown0x04 { get; set; }
/// <summary>Offset 8, Length 4
/// </summary>
public int TextLength { get; set; }
/// <summary>Offset 12, Length LabelLength
/// </summary>
public byte[] TextBytes { get; set; }
/// <summary>Offset 12+TextLength, Text zero terminator
/// </summary>
public byte TextTerminator { get; set; }
/// <summary>Offset 13+TextLength, Length varying, Padding with 0 for 16-byte-alignment
/// </summary>
public byte[] Padding { get; set; }
/// <summary>Data Points
/// </summary>
public List<LoxStatDataPoint> DataPoints { get; set; }
/// <summary>Exception during load (if any)
/// </summary>
public Exception LoadException { get; private set; }
public Guid Guid
{
get
{
Guid value;
if (Path.GetFileNameWithoutExtension(FileName).Contains('_'))
{
Guid.TryParseExact
(
Path.GetFileNameWithoutExtension(FileName).Split('_')[0].
Replace("-", ""),
"N",
out value
);
}
else
{
Guid.TryParseExact
(
Path.GetFileNameWithoutExtension(FileName).
Replace("-", ""),
"N",
out value
);
}
return value;
}
}
public DateTime YearMonth
{
get
{
return GetYearMonth(FileName);
}
}
public string Text
{
get
{
return Encoding.UTF8.GetString(TextBytes);
}
set
{
Encoding.UTF8.GetBytes(value);
TextLength = TextBytes.Length;
Padding = new byte[GetPaddingLength(12 + TextLength)];
}
}
public static DateTime GetYearMonth(string fileName)
{
DateTime value;
DateTime.TryParseExact
(
Path.GetExtension(fileName).Substring(1),
"yyyyMM",
CultureInfo.InvariantCulture,
DateTimeStyles.NoCurrentDateDefault,
out value
);
return value;
}
/// <summary>Finds problems in that file
/// </summary>
/// <param name="collection"></param>
public override void AddProblems(ICollection<LoxStatProblem> collection)
{
try
{
if (Guid == Guid.Empty)
AddProblem(collection, "File name is not a Guid");
if (YearMonth == DateTime.MinValue)
AddProblem(collection, "File name extension is not a year/month");
if (ValueCount < 1)
AddProblem(collection, "Unexpected number of values per data point.");
//Intentionally not checking Unknown bytes
if (TextLength == 0)
AddProblem(collection, "Unexpected text length");
//Intentionally not checking Padding
foreach (var dataPoint in DataPoints)
dataPoint.AddProblems(collection);
}
catch (Exception ex)
{
AddProblem(collection, ex);
}
}
public override string ToString()
{
try
{
return Path.GetFileName(FileName);
}
catch (Exception ex)
{
return ex.ToString();
}
}
public int GetPaddingLength(int offset)
{
var pageWidth = 8 + ValueCount * 8 + (ValueCount > 1 ? 8 : 0);
return (pageWidth - (offset % pageWidth)) % pageWidth;
}
public static LoxStatFile Load(string fileName, bool headerOnly = false)
{
var loxStatFile = new LoxStatFile();
loxStatFile.FileName = fileName;
try
{
using (var reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
{
loxStatFile.ValueCount = reader.ReadUInt16();
loxStatFile.Unknown0x02 = reader.ReadUInt16();
loxStatFile.Unknown0x04 = reader.ReadUInt32();
loxStatFile.TextLength = reader.ReadInt32();
loxStatFile.TextBytes = reader.ReadBytes(loxStatFile.TextLength);
loxStatFile.TextTerminator = reader.ReadByte();
loxStatFile.Padding = reader.ReadBytes(
loxStatFile.GetPaddingLength(13 + loxStatFile.TextLength));
var dataPoints = new List<LoxStatDataPoint>();
loxStatFile.DataPoints = dataPoints;
if (!headerOnly)
LoxStatDataPoint.FromBinaryReader(loxStatFile, reader);
}
}
catch (Exception ex)
{
loxStatFile.LoadException = ex;
}
return loxStatFile;
}
public void Save()
{
using (var writer = new BinaryWriter(File.Open(FileName, FileMode.Create)))
{
writer.Write(ValueCount);
writer.Write(Unknown0x02);
writer.Write(Unknown0x04);
writer.Write(TextLength);
writer.Write(TextBytes);
writer.Write(TextTerminator);
writer.Write(Padding);
if (DataPoints != null)
foreach (var dataPoint in DataPoints)
dataPoint.Save(writer);
}
}
}
}