private bool MusicFileOnLocal()
        {
            string musicPath =
                Path.Combine(GlobalVariables.LOCAL_DIR + GlobalVariables.FOLDER_ROOT + GlobalVariables.FOLDER_PLAYLIST,
                             MotionID.ToString() + ".mp3");

            return(File.Exists(musicPath));
        }
Example #2
0
        //private StreamWriter writer = new StreamWriter(@"WriteMotion.txt");
        // Transfer process:
        // 1. Create pair
        // 2. Send packets for motion file
        // 3. Close motion transfer
        // 4. Send packets for music file
        // 5. Close pair: success, save motion pair
        // 6. Cancel: cancel the operation, data won't be stored in mRobo,
        //  this command could be called any time after "Create pair" command
        public object Process()
        {
            const int trunkDataSize = 4096;
            string    musicPath     =
                Path.Combine(GlobalVariables.LOCAL_DIR + GlobalVariables.FOLDER_ROOT + GlobalVariables.FOLDER_PLAYLIST,
                             MotionID.ToString() + ".mp3");

            byte[] musicData = GlobalFunction.FileToByteArray(musicPath);
            if (musicData == null)
            {
                OnProcessError(ErrorCode.FileNotExist, "Music file not exist");
                if (_writeProcess)
                {
                    _logger.Error("Music file not exist");
                    DebugHelper.WriteLine("Music file not exist");
                }
                return(null);
            }
            string motionPath =
                Path.Combine(GlobalVariables.LOCAL_DIR + GlobalVariables.FOLDER_ROOT + GlobalVariables.FOLDER_PLAYLIST,
                             MotionID.ToString() + ".mrb");

            byte[] motionData = GlobalFunction.FileToByteArray(motionPath);
            if (motionData == null)
            {
                OnProcessError(ErrorCode.FileNotExist, "Motion file not exist");
                if (_writeProcess)
                {
                    _logger.Error("Motion file not exist");
                    DebugHelper.WriteLine("Motion file not exist");
                }
                return(null);
            }



            int numberOfMotionTrunk = motionData.Length / trunkDataSize;
            int numberOfMusicTrunk  = musicData.Length / trunkDataSize;

            int totalPackets      = numberOfMotionTrunk + numberOfMusicTrunk + 2;
            int count             = 0;
            int currentPercentage = -1;
            int tempPercentage    = 0;


            List <byte[]> listMotionTrunk = SplitFile(motionPath, trunkDataSize);

            List <byte[]> listMusicTrunk = SplitFile(musicPath, trunkDataSize);

            // Create pair
            if (CreatePair() != 1)
            {
                OnProcessError(ErrorCode.CreatePairError, "Create pair false");
                if (_writeProcess)
                {
                    _logger.Error("Create pair false");
                    DebugHelper.WriteLine("Create pair false");
                }
                return(0);
            }

            if (_writeProcess)
            {
                _logger.Info("Create pair done");
                DebugHelper.WriteLine("Create pair done");
            }
            // Transfer motion
            foreach (var trunk in listMotionTrunk)
            {
                var writeResult = WriteData(trunk, RobotPacket.PacketID.WriteMotionData);
                if (writeResult == 0)
                {
                    OnProcessError(ErrorCode.WriteDataError, "Write motion error");
                    if (_writeProcess)
                    {
                        _logger.Error("Write motion error");
                        DebugHelper.WriteLine("Write motion error");
                    }
                    return(0);
                }
                if (writeResult == 2)
                {
                    return(0);
                }
                count++;

                tempPercentage = count * 100 / totalPackets;
                if (tempPercentage > currentPercentage)
                {
                    currentPercentage = tempPercentage;
                    OnProgessReport(currentPercentage);
                    if (_writeProcess)
                    {
                        DebugHelper.WriteLine("Percentages: " + tempPercentage);
                        _logger.Info("Percentages: " + tempPercentage);
                    }
                }
            }

            // Close motion file
            if (CloseMotionFile() != 1)
            {
                OnProcessError(ErrorCode.CloseMotionFileError, "Close motion file error");
                if (_writeProcess)
                {
                    DebugHelper.WriteLine("Close motion file error");
                    _logger.Error("Close motion file error");
                }
                return(0);
            }
            if (_writeProcess)
            {
                DebugHelper.WriteLine("Transfer motion file done");
                _logger.Info("Transfer motion file done");
            }

            // Transfer music
            foreach (var trunk in listMusicTrunk)
            {
                var writeResult = WriteData(trunk, RobotPacket.PacketID.WriteMusicData);
                if (writeResult == 0)
                {
                    OnProcessError(ErrorCode.WriteDataError, "Write music error");
                    if (_writeProcess)
                    {
                        DebugHelper.WriteLine("Write music error");
                        _logger.Error("Write music error");
                    }
                    return(0);
                }
                if (writeResult == 2)
                {
                    return(0);
                }
                count++;
                tempPercentage = count * 100 / totalPackets;
                if (tempPercentage > currentPercentage)
                {
                    currentPercentage = tempPercentage;
                    OnProgessReport(currentPercentage);
                    if (_writeProcess)
                    {
                        DebugHelper.WriteLine("Percentages: " + tempPercentage);
                        _logger.Info("Percentages: " + tempPercentage);
                    }
                }
            }

            //Close pair
            if (ClosePair() != 1)
            {
                OnProcessError(ErrorCode.ClosePairError, "Close pair error");
                if (_writeProcess)
                {
                    DebugHelper.WriteLine("Close pair error");
                    _logger.Error("Close pair error");
                }
                return(0);
            }
            //writer.Close();
            if (_writeProcess)
            {
                DebugHelper.WriteLine("Close pair done");
                _logger.Info("Close pair done");
            }
            OnProgessReport(100);
            OnProcessSuccessfully(null);
            return(1);
        }