123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using System;
- using System.Text;
- namespace Vinno.FIS.Sonopost.Features.Oled
- {
- public class MessageReceiver
- {
- public static OKResult OK = new OKResult();
- public static WrongHeaderResult WrongHeader = new WrongHeaderResult();
- public static ErrorResult Error = new ErrorResult();
- public static CommonErrorResult CommonError = new CommonErrorResult();
- public static IReceiveResult Parse(byte[] data, byte command)
- {
- if (ParseHeader(data, command))
- {
- if (command == (byte)MessageType.SearchPage)
- {
- return ParseSearchCommand(data);
- }
- return ParseNormalCommand(data);
- }
- return WrongHeader;
- }
- private static IReceiveResult ParseNormalCommand(byte[] data)
- {
- int dataLength = data[3];
- var returnValue = new byte[dataLength];
- Buffer.BlockCopy(data, 4, returnValue, 0, dataLength);
- var result = Encoding.UTF8.GetString(returnValue);
- if (result == "OK")
- {
- return OK;
- }
- return Error;
- }
- private static IReceiveResult ParseSearchCommand(byte[] data)
- {
- int dataLength = data[3];
- var pageCount = data[4];
- var returnValue = new byte[dataLength - 1];
- Buffer.BlockCopy(data, 5, returnValue, 0, dataLength - 1);
- var result = Encoding.UTF8.GetString(returnValue);
- if (result == "OK")
- {
- return new SearchPageResult(pageCount);
- }
- return Error;
- }
- private static bool ParseHeader(byte[] data, byte command)
- {
- return data[0] == 0XAA && data[1] == command && data[2] == 0xff;
- }
- }
- }
|