public Builder()
 {
     packet   = new UsbPacket();
     byteUtil = new ByteUtil();
 }
Exemplo n.º 2
0
        private void ParsePacket(UsbPacket pk)
        {
            Cmd cmd = (Cmd)pk.Cmd;

            //System.Diagnostics.Debug.WriteLine( "Cmd : {0}", cmd.ToString() );

            switch (cmd)
            {
            case Cmd.START:
            {
                isFirstStatus = true;
                int  protocolVersion = pk.GetInt();
                bool available       = pk.GetByte() == 0x01;
                if (available)
                {
                    GetDeviceInfoRequest();
                }
                else
                {
                    ConnectionRefused?.Invoke(this, null);
                }
            }
            break;

            case Cmd.GETDEVINFO:
            {
                DeviceName               = pk.GetString(16);
                FirmwareVersion          = pk.GetString(16);
                BluetoothProtocolVersion = pk.GetString(8);
                SubName            = pk.GetString(16);
                MacAddress         = BitConverter.ToString(pk.GetBytes(6)).Replace("-", ":");
                BluetoothModelName = pk.GetString(32);

                GetConfigRequest(ConfigType.AutoPowerOffTime, ConfigType.AutoPowerOn, ConfigType.Beep, ConfigType.DownSampling, ConfigType.PenCapOff, ConfigType.SaveOfflineData);
            }
            break;

            case Cmd.GETDISKINFO:
            {
                pk.Move(1);
                var total = pk.GetInt();
                var free  = pk.GetInt();
                StorageStatusReceived?.Invoke(this, new StorageStatusReceivedEventArgs(total, free));
            }
            break;

            case Cmd.GETCONFIG:
            {
                int typeCount = pk.GetByteToInt();

                for (int i = 0; i < typeCount; i++)
                {
                    int type = pk.GetByteToInt();

                    switch ((ConfigType)type)
                    {
                    case ConfigType.DateTime:
                        long timestamp = pk.GetLong();
                        DateTimeReceived?.Invoke(this, new DateTimeReceivedEventArgs(timestamp));
                        break;

                    case ConfigType.AutoPowerOffTime:
                        //AutoPowerOffTime = new TimeSpan( 0, pk.GetShort(), 0);
                        AutoPowerOffTime = TimeSpan.FromMinutes(pk.GetShort());
                        if (!isFirstStatus)
                        {
                            ConfigSetupResultReceived?.Invoke(this, new ConfigSetupResultReceivedEventArgs(ConfigType.AutoPowerOffTime, true));
                        }
                        break;

                    case ConfigType.AutoPowerOn:
                        IsAutoPowerOnEnabled = pk.GetByteToInt() == 0x01;
                        if (!isFirstStatus)
                        {
                            ConfigSetupResultReceived?.Invoke(this, new ConfigSetupResultReceivedEventArgs(ConfigType.AutoPowerOn, true));
                        }
                        break;

                    case ConfigType.PenCapOff:
                        IsPenCapOffEnabled = pk.GetByteToInt() == 0x01;
                        if (!isFirstStatus)
                        {
                            ConfigSetupResultReceived?.Invoke(this, new ConfigSetupResultReceivedEventArgs(ConfigType.PenCapOff, true));
                        }
                        break;

                    case ConfigType.Beep:
                        IsBeepEnabled = pk.GetByteToInt() == 0x01;
                        if (!isFirstStatus)
                        {
                            ConfigSetupResultReceived?.Invoke(this, new ConfigSetupResultReceivedEventArgs(ConfigType.Beep, true));
                        }
                        break;

                    case ConfigType.SaveOfflineData:
                        CanSaveOfflineData = pk.GetByteToInt() == 0x01;
                        if (!isFirstStatus)
                        {
                            ConfigSetupResultReceived?.Invoke(this, new ConfigSetupResultReceivedEventArgs(ConfigType.SaveOfflineData, true));
                        }
                        break;

                    case ConfigType.DownSampling:
                        IsDownsamplingEnabled = pk.GetByteToInt() == 0x01;
                        if (!isFirstStatus)
                        {
                            ConfigSetupResultReceived?.Invoke(this, new ConfigSetupResultReceivedEventArgs(ConfigType.DownSampling, true));
                        }
                        break;

                    case ConfigType.Battery:
                        var battery = pk.GetByteToInt();
                        BatteryStatusReceived?.Invoke(this, new BatteryStatusReceivedEventArgs(battery));
                        break;
                    }
                }

                if (isFirstStatus)
                {
                    IsActive = true;
                    Authenticated?.Invoke(this, null);
                    isFirstStatus = false;
                }
            }
            break;

            case Cmd.SETCONFIG:
            {
                int typeCount = pk.GetByteToInt();
                for (int i = 0; i < typeCount; i++)
                {
                    int  type   = pk.GetByteToInt();
                    bool result = pk.GetByteToInt() == 0x00;

                    if (result)
                    {
                        if ((ConfigType)type == ConfigType.DateTime)
                        {
                            ConfigSetupResultReceived?.Invoke(this, new ConfigSetupResultReceivedEventArgs(ConfigType.DateTime, true));
                        }
                        else
                        {
                            GetConfigRequest((ConfigType)type);
                        }
                    }
                    else
                    {
                        ConfigSetupResultReceived?.Invoke(this, new ConfigSetupResultReceivedEventArgs((ConfigType)type, false));
                    }
                }
            }
            break;

            case Cmd.FORMAT:
            {
                bool result = pk.GetByteToInt() == 0x00;
                FormatResultReceived?.Invoke(this, new ResultReceivedEventArgs(result ? ResultReceivedEventArgs.ResultType.Success : ResultReceivedEventArgs.ResultType.Failed));
            }
            break;

            case Cmd.GETOFFLINEDATALIST:
            case Cmd.GETLOGFILELIST:
            {
                int result     = pk.GetByteToInt();
                int fileCount  = pk.GetInt();
                var resultList = new List <string>();
                for (int i = 0; i < fileCount; i++)
                {
                    string fileName = pk.GetString(16);
                    resultList.Add(fileName);
                }
                FileListReceivedEventArgs.ResultType resultType;
                if (result == 0)
                {
                    resultType = FileListReceivedEventArgs.ResultType.Success;
                }
                else if (result == 1)
                {
                    resultType = FileListReceivedEventArgs.ResultType.Failed;
                }
                else if (result == 2)
                {
                    resultType = FileListReceivedEventArgs.ResultType.TooManyFileExists;
                }
                else
                {
                    resultType = FileListReceivedEventArgs.ResultType.UnknownError;
                }

                if (pk.Cmd == Cmd.GETOFFLINEDATALIST)
                {
                    OfflineFileListReceived?.Invoke(this, new FileListReceivedEventArgs(resultType, resultList.ToArray()));
                }
                else
                {
                    LogFileListReceived?.Invoke(this, new FileListReceivedEventArgs(resultType, resultList.ToArray()));
                }
            }
            break;

            case Cmd.GETFILE_H:
            {
                bool result     = pk.GetByteToInt() == 0x00;
                int  fileSize   = pk.GetInt();
                int  packetSize = pk.GetInt() - 8;

                if (fileRequestInfo)
                {
                    FileInfoReceived?.Invoke(this, new FileInfoReceivedEventArgs(result ? FileInfoReceivedEventArgs.ResultType.Success : FileInfoReceivedEventArgs.ResultType.FileNotExists, requestFileName, fileSize));
                }
                else
                {
                    if (!result)
                    {
                        FileDownloadResultReceived?.Invoke(this, new FileDownloadResultReceivedEventArgs(FileDownloadResultReceivedEventArgs.ResultType.FileNotExists));
                    }
                    else
                    {
                        fileSerializer     = new FileBuilder(targetFilePath, fileSize, packetSize);
                        downloadRetryCount = 0;
                        FileDownloadProgressChanged?.Invoke(this, new ProgressChangedEventArgs(0));
                        GetFilePacketRequest(0);
                    }
                }
            }
            break;

            case Cmd.GETFILE_D:
            {
                int    result     = pk.GetByteToInt();
                int    offset     = pk.GetInt();
                int    dataLength = pk.GetInt();
                byte[] data       = pk.GetBytes(dataLength);

                if (result == 0x00)
                {
                    fileSerializer.Put(data, offset);

                    int progress = (int)((double)offset / (double)(fileSerializer.FileSize - 1));
                    if (fileSerializer.GetNextOffset() < fileSerializer.FileSize)
                    {
                        FileDownloadProgressChanged?.Invoke(this, new ProgressChangedEventArgs(progress));
                        GetFilePacketRequest(fileSerializer.GetNextOffset());
                    }
                    else
                    {
                        bool fileCreated = fileSerializer.MakeFile();
                        if (fileCreated)
                        {
                            FileDownloadProgressChanged?.Invoke(this, new ProgressChangedEventArgs(100));
                        }
                        FileDownloadResultReceived?.Invoke(this, new FileDownloadResultReceivedEventArgs(fileCreated ? FileDownloadResultReceivedEventArgs.ResultType.Success : FileDownloadResultReceivedEventArgs.ResultType.Failed));
                    }
                }
                else
                {
                    if (downloadRetryCount++ > 3)
                    {
                        FileDownloadResultReceivedEventArgs.ResultType resultType;
                        if (result == 0x1)
                        {
                            resultType = FileDownloadResultReceivedEventArgs.ResultType.Failed;
                        }
                        else if (result == 0x10)
                        {
                            resultType = FileDownloadResultReceivedEventArgs.ResultType.OffsetInvalid;
                        }
                        else if (result == 0x11)
                        {
                            resultType = FileDownloadResultReceivedEventArgs.ResultType.CannotOpenFile;
                        }
                        else
                        {
                            resultType = FileDownloadResultReceivedEventArgs.ResultType.UnknownError;
                        }

                        FileDownloadResultReceived?.Invoke(this, new FileDownloadResultReceivedEventArgs(resultType));
                    }
                    else
                    {
                        GetFilePacketRequest(offset);
                    }
                }
            }
            break;

            case Cmd.DELETEFILE:
            {
                bool result = pk.GetByteToInt() == 0x00;
                DeleteFileResultReceived?.Invoke(this, new ResultReceivedEventArgs(result ? ResultReceivedEventArgs.ResultType.Success : ResultReceivedEventArgs.ResultType.Failed));
            }
            break;

            case Cmd.POWEROFF:
            {
                bool result = pk.GetByteToInt() == 0x00;
                PowerOffResultReceived?.Invoke(this, new ResultReceivedEventArgs(result ? ResultReceivedEventArgs.ResultType.Success : ResultReceivedEventArgs.ResultType.Failed));
            }
            break;

            case Cmd.UPDATE_START:
            {
                int result     = pk.GetByteToInt();
                int packetSize = pk.GetInt() - 8;
                if (result == 0x00 && fileSplitter != null)
                {
                    updateRetryCount = 0;

                    if (fileSplitter.Split(packetSize) && fileSplitter.PacketCount > 0)
                    {
                        UpdateProgressChanged?.Invoke(this, new ProgressChangedEventArgs(0));
                        UpdatePacketUploadRequest(0, fileSplitter.GetBytes(0));
                    }
                    else
                    {
                        isUpdating = false;
                        UpdateResultReceived?.Invoke(this, new UpdateResultReceivedEventArgs(UpdateResultReceivedEventArgs.ResultType.DeviceIsNotCorrect));
                    }
                }
                else
                {
                    isUpdating = false;
                    UpdateResultReceived?.Invoke(this, new UpdateResultReceivedEventArgs(result == 1 ? UpdateResultReceivedEventArgs.ResultType.DeviceIsNotCorrect : UpdateResultReceivedEventArgs.ResultType.FirmwareVersionIsNotCorrect));
                }
            }
            break;

            case Cmd.UPDATE_DO:
            {
                if (!isUpdating)
                {
                    return;
                }

                int result = pk.GetByteToInt();
                int offset = pk.GetInt();

                if (result == 0x00)
                {
                    updateRetryCount = 0;

                    if (offset + fileSplitter.PacketSize >= fileSplitter.FileSize - 1)
                    {
                        UpdateProgressChanged?.Invoke(this, new ProgressChangedEventArgs(100));
                        UpdateResultReceived?.Invoke(this, new UpdateResultReceivedEventArgs(UpdateResultReceivedEventArgs.ResultType.Complete));
                    }
                    else
                    {
                        int progress = (int)((double)offset / (double)(fileSplitter.FileSize - 1) * 100);
                        UpdateProgressChanged?.Invoke(this, new ProgressChangedEventArgs(progress));
                        UpdatePacketUploadRequest(offset + fileSplitter.PacketSize, fileSplitter.GetBytes(offset + fileSplitter.PacketSize));
                    }
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("UPDATE_DO : {0} {1}", offset, result);

                    if (updateRetryCount++ > 3)
                    {
                        UpdateResultReceived?.Invoke(this, new UpdateResultReceivedEventArgs(UpdateResultReceivedEventArgs.ResultType.UnknownError));
                    }
                    else
                    {
                        UpdatePacketUploadRequest(offset, fileSplitter.GetBytes(offset));
                    }
                }
            }
            break;
            }
        }
 internal UsbPacketEventArgs(UsbPacket packet)
 {
     Packet = packet;
 }