ASCU_ALL/DataClient/ByteConverter.cs

71 lines
2.1 KiB
C#
Raw Normal View History

2021-06-08 20:57:54 +05:00
using DataClient.Struct;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace DataClient
{
public static class ByteConverter
{
public static TechCycle[] BytesToTechcycles(byte[] arr)
{
var res = new List<TechCycle>();
try
{
var str = new List<byte>();
for (var i = 0; i < arr.Length; i++)
if (arr[i] == 0x0a)
{
res.Add(BytesToTechcycle(str.ToArray()));
str.Clear();
}
else
str.Add(arr[i]);
} catch { throw; }
return res.ToArray();
}
public static TechCycle BytesToTechcycle(byte[] arr)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var enc = Encoding.GetEncoding(866);
var exc = new ArgumentException("Wrong structure in byte array.");
var flag = 0;
var str = new List<byte>();
var res = new TechCycle();
try
{
for (var i = 0; i < arr.Length; i++)
if (arr[i] == 0x09)
switch (flag)
{
case 0:
var idxStr = enc.GetString(str.ToArray());
if (idxStr == "00") res.Operation = TechCycle.Oper.end_cycle_manual;
else if (int.TryParse(idxStr, out int idx)) res.index = (sbyte?)idx;
str.Clear();
flag++;
break;
case 1:
var timestr = enc.GetString(str.ToArray());
var rgx = new Regex(@"^([0-1]\d{1}|[2][0-3]):([0-5]\d{1}):([0-5]\d{1})$");
if (!rgx.IsMatch(timestr)) throw exc;
str.Clear();
flag++;
break;
default: throw exc;
}
else str.Add(arr[i]);
if (flag != 2) throw exc;
var unixTimeStr = enc.GetString(str.ToArray());
if (!int.TryParse(unixTimeStr, out int unixTime)) throw exc;
res.start = (new DateTime(1970, 1, 1, 0, 0, 0, 0)).AddSeconds(unixTime).ToLocalTime();
return res;
}
catch { throw; }
}
}
}