Пример #1
0
 internal static Standalone Create(StandaloneType type,string arguments,bool log = false)
 {
     Standalone executable = new Standalone();
     switch(type)
     {
     case StandaloneType.Calibration:
         executable.StartInfo = new ProcessStartInfo()
         {
             FileName = CalibrationExe,
             Arguments = arguments,
             WorkingDirectory = CalibrationWorkingDir,
             UseShellExecute = false,
             RedirectStandardOutput = true,
             RedirectStandardError = true,
             CreateNoWindow = true
         };
         break;
     case StandaloneType.Reconstruction:
         var workingdir = Path.GetDirectoryName(ReconstructionExe);
         executable.StartInfo = new ProcessStartInfo()
         {
             FileName = ReconstructionExe,
             WorkingDirectory = workingdir,
             //Arguments = workingdir + " " + arguments,
             Arguments = arguments + DefaultReconstructionParameters,
             UseShellExecute = false,
             RedirectStandardOutput = true,
             RedirectStandardError = true,
             //CreateNoWindow = true
         };
         break;
     case StandaloneType.Synchronization:
         executable.StartInfo = new ProcessStartInfo()
         {
             FileName = SynchronizationExe,
             Arguments = arguments,
             UseShellExecute = false,
             RedirectStandardOutput = true,
             RedirectStandardError = true,
             CreateNoWindow = true
         };
         break;
         //					case StandaloneType.Evaluation:
         //						break;
     case StandaloneType.None:
     default:
         break;
     }
     if(log)
     {
         executable.IsLogging = true;
         executable.logger = new StreamWriter(Path.Combine(arguments.Split(new string[]{" "},StringSplitOptions.RemoveEmptyEntries)[1],LogFile));
         executable.process.OutputDataReceived += (sender, e) =>
         {
             executable.logger.WriteLine(e.Data);
             Console.Standalone.Log(e.Data);
         };
         executable.process.ErrorDataReceived += (sender, e) =>
         {
             executable.logger.WriteLine(e.Data);
             Console.Standalone.Error(e.Data);
         };
     }
     return executable;
 }
        private void InitStateMachine()
        {
            this.State = KinectState.None;
            this.machine = new StateMachine<KinectState,KinectAction>(() => this.State,(state) => this.State = state);
            // No State Config
            this.machine.Configure(KinectState.None)
                .OnEntry(() =>
                         {
                    StartCoroutine("AcceptConnections");
                    StopCoroutine("AcceptMessages");
                })
                    .Permit(KinectAction.Init,KinectState.Idle);
            // Idle State Config
            this.machine.Configure(KinectState.Idle)
                .OnEntryFrom(KinectAction.Init,() => StartCoroutine("AcceptMessages"))
                    .Permit(KinectAction.Reinit,KinectState.None)
                    .Permit(KinectAction.Reconstruct,KinectState.Reconstructing)
                    .Permit(KinectAction.Calibrate,KinectState.Calibrating)
                    .Permit(KinectAction.Sync,KinectState.Syncing)
                    .Permit(KinectAction.Record,KinectState.Recording);
            // Calibrating State Config
            this.machine.Configure(KinectState.Calibrating)
                .OnEntryFrom(KinectAction.Calibrate,() =>
                             {
                    StartCoroutine("WaitCapture",KinectAction.Calibrate);
                    this.SendToAll(new Message(MessageType.Start,MessageParameter.Calibrate));
                    this.doneReceivedNeeded = this.config.ClientCount;
                })
                    .OnExit(() =>
                            {

                    })
                    .PermitDynamic(KinectAction.Stop,() =>
                                   {
                        this.SendToAll(new Message(MessageType.Stop,MessageParameter.Calibrate));
                        return KinectState.StopCalibrating;
                    })
                    .Permit(KinectAction.Reinit,KinectState.None)
                    .PermitDynamic(KinectAction.Done,() => --this.doneReceivedNeeded == 0 ? KinectState.WaitingCalibratingTransfer : KinectState.Calibrating);
            // Stop Calibrating State Config
            this.machine.Configure(KinectState.StopCalibrating)
                .OnEntryFrom(KinectAction.Stop,() =>
                             {
                    this.doneReceivedNeeded = this.config.ClientCount;
                    StopCoroutine("WaitCapture");
                    this.CurrentCaptureProgress = 0.0f;
                })
                    .PermitDynamic(KinectAction.Done,() => --this.doneReceivedNeeded == 0 ? KinectState.Idle : KinectState.StopCalibrating);
            // Waiting Calibration Transfer State Config
            this.machine.Configure(KinectState.WaitingCalibratingTransfer)
                .OnEntryFrom(KinectAction.Done,() =>
                             {
                    this.SendToAll(new Message(MessageType.Merge,MessageParameter.Calibrate));
                    this.doneReceivedNeeded = this.config.ClientCount;
                })
                    .Permit(KinectAction.Reinit,KinectState.None)
                    .Permit(KinectAction.Stop,KinectState.Idle)
                    .PermitDynamic(KinectAction.Merge,() => --this.doneReceivedNeeded == 0 ? KinectState.CalibratingTransfer : KinectState.WaitingCalibratingTransfer);
            // Transfering Calibration State Config
            this.machine.Configure(KinectState.CalibratingTransfer)
                .OnEntryFrom(KinectAction.Merge,() =>
                             {
                    if(this.fileReceivers.All((receiver) => receiver.TryNext()))
                    {
                        StartCoroutine("WaitTransfer",KinectAction.Calibrate);
                    }//TODO:  missing case
                    this.doneReceivedNeeded = this.config.ClientCount;
                })
                    .OnExit(() =>
                            {
                        if(this.fileReceivers.Any((receiver) => receiver.Pending))
                        {
                            foreach(var receiver in this.fileReceivers.Where((receiver) => receiver.Pending))
                            {
                                receiver.Abort();
                            }
                            this.SendToAll(new Message(MessageType.Stop,MessageParameter.Merge));
                            this.CurrentTransferProgress = 0.0f;
                        }
                    })
                    .Permit(KinectAction.Stop,KinectState.Idle)
                    .Permit(KinectAction.Reinit,KinectState.None)
                    .PermitDynamic(KinectAction.Done,() =>--this.doneReceivedNeeded == 0 ? KinectState.ProcessingCalibration : KinectState.CalibratingTransfer);
            // Processing Calibration State Config
            this.machine.Configure(KinectState.ProcessingCalibration)
                .OnEntry(() =>
                         {
                    this.calibrationExe = Standalone.Factory.Create(
                        StandaloneType.Calibration,this.FindMostRecent(this.config.CalibrationOutputDirectory),true);
                    StartCoroutine("WaitStandalone",this.calibrationExe);
                    Console.Important("<b><color=lightblue>Calibration Process Started !</color></b>");
                })
                    .OnExit(() =>
                            {
                        StopCoroutine("WaitStandalone");
                        this.calibrationExe.Stop();
                        Console.Important("<b><color=orange>Calibration Process Ended !</color></b>");
                    })
                    .Permit(KinectAction.Reinit,KinectState.None)
                    .Permit(KinectAction.Done,KinectState.Idle);
            // Syncing State Config
            this.machine.Configure(KinectState.Syncing)
                .OnEntryFrom(KinectAction.Sync,() =>
                             {
                    StartCoroutine("WaitCapture",KinectAction.Sync);
                    this.SendToAll(new Message(MessageType.Start,MessageParameter.Sync));
                    this.doneReceivedNeeded = this.config.ClientCount;
                })
                    .PermitDynamic(KinectAction.Stop,() =>
                                   {
                        this.SendToAll(new Message(MessageType.Stop,MessageParameter.Sync));
                        return KinectState.StopSyncing;
                    })
                    .Permit(KinectAction.Reinit,KinectState.None)
                    .PermitDynamic(KinectAction.Done,() =>--this.doneReceivedNeeded == 0 ? KinectState.WaitingSyncingTransfer : KinectState.Syncing);
            // Stop Syncing State Config
            this.machine.Configure(KinectState.StopSyncing)
                .OnEntryFrom(KinectAction.Stop,() =>
                             {
                    this.doneReceivedNeeded = this.config.ClientCount;
                    StopCoroutine("WaitCapture");
                    this.CurrentCaptureProgress = 0.0f;
                })
                    .PermitDynamic(KinectAction.Done,() => --this.doneReceivedNeeded == 0 ? KinectState.Idle : KinectState.StopSyncing);
            // Waiting Syncing Transfer State Config
            this.machine.Configure(KinectState.WaitingSyncingTransfer)
                .OnEntryFrom(KinectAction.Done,() =>
                             {
                    this.SyncRecordingConfidence = this.kinects.Aggregate(0.0f,(seed,kinect) => seed += kinect.Elapsed.FPS / AudioNominalFPS) / this.kinects.Count;
                    this.SendToAll(new Message(MessageType.Merge,MessageParameter.Sync));
                    this.doneReceivedNeeded = this.config.ClientCount;
                })
                    .Permit(KinectAction.Reinit,KinectState.None)
                    .PermitDynamic(KinectAction.Merge,() =>  --this.doneReceivedNeeded == 0 ? KinectState.SyncingTransfer : KinectState.WaitingSyncingTransfer);
            // Transfering Syncing State Config
            this.machine.Configure(KinectState.SyncingTransfer)
                .OnEntryFrom(KinectAction.Merge,() =>
                             {
                    if(this.fileReceivers.All((receiver) => receiver.TryNext()))
                    {
                        StartCoroutine("WaitTransfer",KinectAction.Sync);
                    }
                    this.doneReceivedNeeded = this.config.ClientCount;
                })
                    .OnExit(() =>
                            {
                        if(this.fileReceivers.Any((receiver) => receiver.Pending))
                        {
                            foreach(var receiver in this.fileReceivers.Where((receiver) => receiver.Pending))
                            {
                                receiver.Abort();
                            }
                            this.SendToAll(new Message(MessageType.Stop,MessageParameter.Merge));
                            this.CurrentTransferProgress = 0.0f;
                        }
                    })
                    .Permit(KinectAction.Stop,KinectState.Idle)
                    .Permit(KinectAction.Reinit,KinectState.None)
                    .PermitDynamic(KinectAction.Done,() => --this.doneReceivedNeeded == 0 ? KinectState.ProcessingSynchronization : KinectState.SyncingTransfer);
            // Syncing Calibration State Config
            this.machine.Configure(KinectState.ProcessingSynchronization)
                .OnEntry(() =>
                         {
                    this.synchronizationExe = Standalone.Factory.Create(
                        StandaloneType.Synchronization,this.FindMostRecent(this.config.AudioOutputDirectory),true);
                    StartCoroutine("WaitStandalone",this.synchronizationExe);
                    Console.Important("<b><color=lightblue>Synchronization Process Started !</color></b>");
                })
                    .OnExit(() =>
                            {
                        StopCoroutine("WaitStandalone");
                        this.synchronizationExe.Stop();
                        Console.Important("<b><color=orange>Synchronization Process Ended !</color></b>");
                    })
                    .Permit(KinectAction.Reinit,KinectState.None)
                    .Permit(KinectAction.Done,KinectState.Idle);
            // Recording State Config
            this.machine.Configure(KinectState.Recording)
                .OnEntryFrom(KinectAction.Record,() =>
                             {
                    StartCoroutine("WaitCapture",KinectAction.Record);
                    this.SendToAll(new Message(MessageType.Start,MessageParameter.Record));
                    this.doneReceivedNeeded = this.config.ClientCount;
                })
                    .Permit(KinectAction.Reinit,KinectState.None)
                    .Permit(KinectAction.Stop,KinectState.StopRecording);
            // Stop Recording Transfer State Config
            this.machine.Configure(KinectState.StopRecording)
                .OnEntryFrom(KinectAction.Stop,() =>
                             {
                    this.SendToAll(new Message(MessageType.Stop,MessageParameter.Record));
                    this.doneReceivedNeeded = this.config.ClientCount;
                    StopCoroutine("WaitCapture");
                    this.CurrentCaptureProgress = 0.0f;
                })
                    .Permit(KinectAction.Reinit,KinectState.None)
                    .PermitDynamic(KinectAction.Done,() =>  --this.doneReceivedNeeded == 0 ? KinectState.WaitingRecordingTransfer : KinectState.StopRecording);
            // Waiting Recording Transfer State Config
            this.machine.Configure(KinectState.WaitingRecordingTransfer)
                .OnEntryFrom(KinectAction.Done,() =>
                 {
                    this.RecordingConfidence = this.kinects.Aggregate(0.0f,(seed,kinect) => seed += kinect.Elapsed.FPS / DepthNominalFPS) / this.kinects.Count;
                    this.SendToAll(new Message(MessageType.Merge,MessageParameter.Record));
                    this.doneReceivedNeeded = this.config.ClientCount;
                })
                    .Permit(KinectAction.Reinit,KinectState.None)
                    .PermitDynamic(KinectAction.Merge,() => --this.doneReceivedNeeded == 0 ? KinectState.RecordingTransfer : KinectState.WaitingRecordingTransfer);
            // Transfering Recording State Config
            this.machine.Configure(KinectState.RecordingTransfer)
                .OnEntryFrom(KinectAction.Merge,() =>
                             {
                    if(this.fileReceivers.All((receiver) => receiver.TryNext()))
                    {
                        StartCoroutine("WaitTransfer",KinectAction.Record);
                    }
                    this.doneReceivedNeeded = this.config.ClientCount;
                })
                    .OnExit(() =>
                            {
                        if(this.fileReceivers.Any((receiver) => receiver.Pending))
                        {
                            foreach(var receiver in this.fileReceivers.Where((receiver) => receiver.Pending))
                            {
                                receiver.Abort();
                            }
                            this.SendToAll(new Message(MessageType.Stop,MessageParameter.Merge));
                            this.CurrentTransferProgress = 0.0f;
                        }
                    })
                    .Permit(KinectAction.Stop,KinectState.Idle)
                    .Permit(KinectAction.Reinit,KinectState.None)
                    .PermitDynamic(KinectAction.Done,() => --this.doneReceivedNeeded == 0 ? KinectState.Idle : KinectState.RecordingTransfer);
            // Recording Calibration State Config
            this.machine.Configure(KinectState.Reconstructing)
                .OnEntry(() =>
                 {
                    var latestRecordings = this.FindMostRecent(this.config.RecordingOutputDirectory);
                    var latestRecordingsTime = Path.GetFileNameWithoutExtension(latestRecordings);
                    var meshfolder =Path.Combine(this.config.MeshOutputDirectory,latestRecordingsTime);
                    if(!Directory.Exists(meshfolder))
                    {
                        Directory.CreateDirectory(meshfolder);
                    }
                    var meshfile = Path.Combine(meshfolder, "raw.mesh");
                    this.reconstructionExe = Standalone.Factory.Create(
                        StandaloneType.Reconstruction,"/recordings " + latestRecordings + " /mesh " + meshfile + " /out " + meshfile + " /calibration "
                        + this.FindMostRecent(this.config.CalibrationOutputDirectory) + " /sync " + this.FindMostRecent(this.config.AudioOutputDirectory),true);
                    StartCoroutine("WaitStandalone",this.reconstructionExe);
                    Console.Important("<b><color=lightblue>Recording Process Started !</color></b>");
                })
                    .OnExit(() =>
                            {
                        StopCoroutine("WaitStandalone");
                        this.reconstructionExe.Stop();
                        Console.Important("<b><color=orange>Recording Process Ended !</color></b>");
                    })
                    .Permit(KinectAction.Reinit,KinectState.None)
                    .Permit(KinectAction.Done,KinectState.Idle);
            // Unhandled State Config
            this.machine.OnUnhandledTrigger((state,action) =>
                                            {
                Console.StateMachine.Unhandled(state.ToString(), action.ToString());
            });
            this.machine.OnTransitioned((trans) =>
                                        {
                Console.StateMachine.Transition(!trans.IsReentry,trans.Source.ToString(), trans.Destination.ToString());
            });
        }