Beispiel #1
0
        public Task Start(string domain = "", string username = "", string password = "")
        {
            Debugger.Instance.DebugMessage("RoboCommand started execution.");
            hasError = false;

            // make sure source path is valid
            if (!Directory.Exists(CopyOptions.Source.Replace("\"", "")))
            {
                Debugger.Instance.DebugMessage("The Source directory does not exist.");
                hasError = true;
                OnCommandError?.Invoke(this, new ErrorEventArgs("The Source directory does not exist."));
                Debugger.Instance.DebugMessage("RoboCommand execution stopped due to error.");
                return(null);
            }

            #region Create Destination Directory

            try
            {
                var dInfo = Directory.CreateDirectory(CopyOptions.Destination.Replace("\"", ""));
                if (!dInfo.Exists)
                {
                    Debugger.Instance.DebugMessage("The destination directory does not exist.");
                    hasError = true;
                    OnCommandError?.Invoke(this, new ErrorEventArgs("The Destination directory is invalid."));
                    Debugger.Instance.DebugMessage("RoboCommand execution stopped due to error.");
                    return(null);
                }
            }
            catch (Exception ex)
            {
                Debugger.Instance.DebugMessage(ex.Message);
                hasError = true;
                OnCommandError?.Invoke(this, new ErrorEventArgs("The Destination directory is invalid."));
                Debugger.Instance.DebugMessage("RoboCommand execution stopped due to error.");
                return(null);
            }

            #endregion

            backupTask = Task.Run(() =>
            {
                process = new Process();

                if (!string.IsNullOrEmpty(domain))
                {
                    Debugger.Instance.DebugMessage(string.Format("RoboCommand running under domain - {0}", domain));
                    process.StartInfo.Domain = domain;
                }

                if (!string.IsNullOrEmpty(username))
                {
                    Debugger.Instance.DebugMessage(string.Format("RoboCommand running under username - {0}", username));
                    process.StartInfo.UserName = username;
                }

                if (!string.IsNullOrEmpty(password))
                {
                    Debugger.Instance.DebugMessage("RoboCommand password entered.");
                    var ssPwd = new System.Security.SecureString();

                    for (int x = 0; x < password.Length; x++)
                    {
                        ssPwd.AppendChar(password[x]);
                    }

                    process.StartInfo.Password = ssPwd;
                }

                Debugger.Instance.DebugMessage("Setting RoboCopy process up...");
                process.StartInfo.UseShellExecute        = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError  = true;
                process.StartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
                process.StartInfo.CreateNoWindow         = true;
                process.StartInfo.FileName  = "ROBOCOPY.exe";
                process.StartInfo.Arguments = GenerateParameters();
                process.OutputDataReceived += process_OutputDataReceived;
                process.ErrorDataReceived  += process_ErrorDataReceived;
                process.Exited += Process_Exited;
                Debugger.Instance.DebugMessage("RoboCopy process started.");
                process.Start();
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                process.WaitForExit();
                Debugger.Instance.DebugMessage("RoboCopy process exited.");
            });

            backupTask.ContinueWith((continuation) =>
            {
                if (!hasError)
                {
                    // backup is complete
                    if (OnCommandCompleted != null)
                    {
                        OnCommandCompleted(this, new RoboCommandCompletedEventArgs());
                    }
                }

                Stop();
            });

            return(backupTask);
        }
Beispiel #2
0
        public Task Start(string domain = "", string username = "", string password = "")
        {
            Debugger.Instance.DebugMessage("RoboCommand started execution.");
            hasError = false;

            isRunning = true;

            var tokenSource = new CancellationTokenSource();
            CancellationToken cancellationToken = tokenSource.Token;

            resultsBuilder = new Results.ResultsBuilder();
            results        = null;

            // make sure source path is valid
            if (!Directory.Exists(CopyOptions.Source))
            {
                Debugger.Instance.DebugMessage("The Source directory does not exist.");
                hasError = true;
                OnCommandError?.Invoke(this, new CommandErrorEventArgs("The Source directory does not exist."));
                Debugger.Instance.DebugMessage("RoboCommand execution stopped due to error.");
                tokenSource.Cancel(true);
            }

            #region Create Destination Directory

            try
            {
                var dInfo = Directory.CreateDirectory(CopyOptions.Destination);
                if (!dInfo.Exists)
                {
                    Debugger.Instance.DebugMessage("The destination directory does not exist.");
                    hasError = true;
                    OnCommandError?.Invoke(this, new CommandErrorEventArgs("The Destination directory is invalid."));
                    Debugger.Instance.DebugMessage("RoboCommand execution stopped due to error.");
                    tokenSource.Cancel(true);
                }
            }
            catch (Exception ex)
            {
                Debugger.Instance.DebugMessage(ex.Message);
                hasError = true;
                OnCommandError?.Invoke(this, new CommandErrorEventArgs("The Destination directory is invalid."));
                Debugger.Instance.DebugMessage("RoboCommand execution stopped due to error.");
                tokenSource.Cancel(true);
            }

            #endregion

            backupTask = Task.Factory.StartNew(() =>
            {
                cancellationToken.ThrowIfCancellationRequested();
                process = new Process();

                if (!string.IsNullOrEmpty(domain))
                {
                    Debugger.Instance.DebugMessage(string.Format("RoboCommand running under domain - {0}", domain));
                    process.StartInfo.Domain = domain;
                }

                if (!string.IsNullOrEmpty(username))
                {
                    Debugger.Instance.DebugMessage(string.Format("RoboCommand running under username - {0}", username));
                    process.StartInfo.UserName = username;
                }

                if (!string.IsNullOrEmpty(password))
                {
                    Debugger.Instance.DebugMessage("RoboCommand password entered.");
                    var ssPwd = new System.Security.SecureString();

                    for (int x = 0; x < password.Length; x++)
                    {
                        ssPwd.AppendChar(password[x]);
                    }

                    process.StartInfo.Password = ssPwd;
                }

                Debugger.Instance.DebugMessage("Setting RoboCopy process up...");
                process.StartInfo.UseShellExecute        = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError  = true;
                process.StartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
                process.StartInfo.CreateNoWindow         = true;
                process.StartInfo.FileName  = Configuration.RoboCopyExe;
                process.StartInfo.Arguments = GenerateParameters();
                process.OutputDataReceived += process_OutputDataReceived;
                process.ErrorDataReceived  += process_ErrorDataReceived;
                process.EnableRaisingEvents = true;
                process.Exited += Process_Exited;
                Debugger.Instance.DebugMessage("RoboCopy process started.");
                process.Start();
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                process.WaitForExit();
                results = resultsBuilder.BuildResults(process?.ExitCode ?? -1);
                Debugger.Instance.DebugMessage("RoboCopy process exited.");
            }, cancellationToken, TaskCreationOptions.LongRunning, PriorityScheduler.BelowNormal);

            Task continueWithTask = backupTask.ContinueWith((continuation) =>
            {
                if (!hasError)
                {
                    // backup is complete
                    if (OnCommandCompleted != null)
                    {
                        OnCommandCompleted(this, new RoboCommandCompletedEventArgs(results));
                        isRunning = false;
                    }
                }

                Stop();
            }, cancellationToken);

            return(continueWithTask);
        }
Beispiel #3
0
        private void InvokeEvent(LazyNotification lazyNotification)
        {
            var ntf = lazyNotification.Notifications;

            switch (lazyNotification.NotifyType)
            {
            case NotificationType.ChannelChanged: {
                var ntfc = (ChannelChanged[])ntf;
                ProcessChannelChanged(ntfc);
                OnChannelChanged?.Invoke(this, ntfc);
                var ev   = OnEachChannelChanged;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachChannelChanged(that);
                }
                break;
            }

            case NotificationType.ChannelCreated: {
                var ntfc = (ChannelCreated[])ntf;
                ProcessChannelCreated(ntfc);
                OnChannelCreated?.Invoke(this, ntfc);
                var ev   = OnEachChannelCreated;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachChannelCreated(that);
                    book?.UpdateChannelCreated(that);
                }
                break;
            }

            case NotificationType.ChannelDeleted: {
                var ntfc = (ChannelDeleted[])ntf;
                ProcessChannelDeleted(ntfc);
                OnChannelDeleted?.Invoke(this, ntfc);
                var ev   = OnEachChannelDeleted;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachChannelDeleted(that);
                    book?.UpdateChannelDeleted(that);
                }
                break;
            }

            case NotificationType.ChannelEdited: {
                var ntfc = (ChannelEdited[])ntf;
                ProcessChannelEdited(ntfc);
                OnChannelEdited?.Invoke(this, ntfc);
                var ev   = OnEachChannelEdited;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachChannelEdited(that);
                    book?.UpdateChannelEdited(that);
                }
                break;
            }

            case NotificationType.ChannelGroupList: {
                var ntfc = (ChannelGroupList[])ntf;
                ProcessChannelGroupList(ntfc);
                OnChannelGroupList?.Invoke(this, ntfc);
                var ev   = OnEachChannelGroupList;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachChannelGroupList(that);
                }
                break;
            }

            case NotificationType.ChannelList: {
                var ntfc = (ChannelList[])ntf;
                ProcessChannelList(ntfc);
                OnChannelList?.Invoke(this, ntfc);
                var ev   = OnEachChannelList;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachChannelList(that);
                    book?.UpdateChannelList(that);
                }
                break;
            }

            case NotificationType.ChannelListFinished: {
                var ntfc = (ChannelListFinished[])ntf;
                ProcessChannelListFinished(ntfc);
                OnChannelListFinished?.Invoke(this, ntfc);
                var ev   = OnEachChannelListFinished;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachChannelListFinished(that);
                }
                break;
            }

            case NotificationType.ChannelMoved: {
                var ntfc = (ChannelMoved[])ntf;
                ProcessChannelMoved(ntfc);
                OnChannelMoved?.Invoke(this, ntfc);
                var ev   = OnEachChannelMoved;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachChannelMoved(that);
                    book?.UpdateChannelMoved(that);
                }
                break;
            }

            case NotificationType.ChannelPasswordChanged: {
                var ntfc = (ChannelPasswordChanged[])ntf;
                ProcessChannelPasswordChanged(ntfc);
                OnChannelPasswordChanged?.Invoke(this, ntfc);
                var ev   = OnEachChannelPasswordChanged;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachChannelPasswordChanged(that);
                }
                break;
            }

            case NotificationType.ChannelSubscribed: {
                var ntfc = (ChannelSubscribed[])ntf;
                ProcessChannelSubscribed(ntfc);
                OnChannelSubscribed?.Invoke(this, ntfc);
                var ev   = OnEachChannelSubscribed;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachChannelSubscribed(that);
                }
                break;
            }

            case NotificationType.ChannelUnsubscribed: {
                var ntfc = (ChannelUnsubscribed[])ntf;
                ProcessChannelUnsubscribed(ntfc);
                OnChannelUnsubscribed?.Invoke(this, ntfc);
                var ev   = OnEachChannelUnsubscribed;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachChannelUnsubscribed(that);
                }
                break;
            }

            case NotificationType.ClientChannelGroupChanged: {
                var ntfc = (ClientChannelGroupChanged[])ntf;
                ProcessClientChannelGroupChanged(ntfc);
                OnClientChannelGroupChanged?.Invoke(this, ntfc);
                var ev   = OnEachClientChannelGroupChanged;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachClientChannelGroupChanged(that);
                    book?.UpdateClientChannelGroupChanged(that);
                }
                break;
            }

            case NotificationType.ClientChatComposing: {
                var ntfc = (ClientChatComposing[])ntf;
                ProcessClientChatComposing(ntfc);
                OnClientChatComposing?.Invoke(this, ntfc);
                var ev   = OnEachClientChatComposing;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachClientChatComposing(that);
                }
                break;
            }

            case NotificationType.ClientDbIdFromUid: {
                var ntfc = (ClientDbIdFromUid[])ntf;
                ProcessClientDbIdFromUid(ntfc);
                OnClientDbIdFromUid?.Invoke(this, ntfc);
                var ev   = OnEachClientDbIdFromUid;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachClientDbIdFromUid(that);
                }
                break;
            }

            case NotificationType.ClientEnterView: {
                var ntfc = (ClientEnterView[])ntf;
                ProcessClientEnterView(ntfc);
                OnClientEnterView?.Invoke(this, ntfc);
                var ev   = OnEachClientEnterView;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachClientEnterView(that);
                    book?.UpdateClientEnterView(that);
                }
                break;
            }

            case NotificationType.ClientIds: {
                var ntfc = (ClientIds[])ntf;
                ProcessClientIds(ntfc);
                OnClientIds?.Invoke(this, ntfc);
                var ev   = OnEachClientIds;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachClientIds(that);
                }
                break;
            }

            case NotificationType.ClientLeftView: {
                var ntfc = (ClientLeftView[])ntf;
                ProcessClientLeftView(ntfc);
                OnClientLeftView?.Invoke(this, ntfc);
                var ev   = OnEachClientLeftView;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachClientLeftView(that);
                    book?.UpdateClientLeftView(that);
                }
                break;
            }

            case NotificationType.ClientMoved: {
                var ntfc = (ClientMoved[])ntf;
                ProcessClientMoved(ntfc);
                OnClientMoved?.Invoke(this, ntfc);
                var ev   = OnEachClientMoved;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachClientMoved(that);
                    book?.UpdateClientMoved(that);
                }
                break;
            }

            case NotificationType.ClientNeededPermissions: {
                var ntfc = (ClientNeededPermissions[])ntf;
                ProcessClientNeededPermissions(ntfc);
                OnClientNeededPermissions?.Invoke(this, ntfc);
                var ev   = OnEachClientNeededPermissions;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachClientNeededPermissions(that);
                }
                break;
            }

            case NotificationType.ClientPoke: {
                var ntfc = (ClientPoke[])ntf;
                ProcessClientPoke(ntfc);
                OnClientPoke?.Invoke(this, ntfc);
                var ev   = OnEachClientPoke;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachClientPoke(that);
                }
                break;
            }

            case NotificationType.ClientServerGroup: {
                var ntfc = (ClientServerGroup[])ntf;
                ProcessClientServerGroup(ntfc);
                OnClientServerGroup?.Invoke(this, ntfc);
                var ev   = OnEachClientServerGroup;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachClientServerGroup(that);
                }
                break;
            }

            case NotificationType.ClientServerGroupAdded: {
                var ntfc = (ClientServerGroupAdded[])ntf;
                ProcessClientServerGroupAdded(ntfc);
                OnClientServerGroupAdded?.Invoke(this, ntfc);
                var ev   = OnEachClientServerGroupAdded;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachClientServerGroupAdded(that);
                    book?.UpdateClientServerGroupAdded(that);
                }
                break;
            }

            case NotificationType.CommandError: {
                var ntfc = (CommandError[])ntf;
                ProcessCommandError(ntfc);
                OnCommandError?.Invoke(this, ntfc);
                var ev   = OnEachCommandError;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachCommandError(that);
                }
                break;
            }

            case NotificationType.ConnectionInfo: {
                var ntfc = (ConnectionInfo[])ntf;
                ProcessConnectionInfo(ntfc);
                OnConnectionInfo?.Invoke(this, ntfc);
                var ev   = OnEachConnectionInfo;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachConnectionInfo(that);
                    book?.UpdateConnectionInfo(that);
                }
                break;
            }

            case NotificationType.ConnectionInfoRequest: {
                var ntfc = (ConnectionInfoRequest[])ntf;
                ProcessConnectionInfoRequest(ntfc);
                OnConnectionInfoRequest?.Invoke(this, ntfc);
                var ev   = OnEachConnectionInfoRequest;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachConnectionInfoRequest(that);
                }
                break;
            }

            case NotificationType.FileDownload: {
                var ntfc = (FileDownload[])ntf;
                ProcessFileDownload(ntfc);
                OnFileDownload?.Invoke(this, ntfc);
                var ev   = OnEachFileDownload;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachFileDownload(that);
                }
                break;
            }

            case NotificationType.FileInfoTs: {
                var ntfc = (FileInfoTs[])ntf;
                ProcessFileInfoTs(ntfc);
                OnFileInfoTs?.Invoke(this, ntfc);
                var ev   = OnEachFileInfoTs;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachFileInfoTs(that);
                }
                break;
            }

            case NotificationType.FileList: {
                var ntfc = (FileList[])ntf;
                ProcessFileList(ntfc);
                OnFileList?.Invoke(this, ntfc);
                var ev   = OnEachFileList;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachFileList(that);
                }
                break;
            }

            case NotificationType.FileListFinished: {
                var ntfc = (FileListFinished[])ntf;
                ProcessFileListFinished(ntfc);
                OnFileListFinished?.Invoke(this, ntfc);
                var ev   = OnEachFileListFinished;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachFileListFinished(that);
                }
                break;
            }

            case NotificationType.FileTransfer: {
                var ntfc = (FileTransfer[])ntf;
                ProcessFileTransfer(ntfc);
                OnFileTransfer?.Invoke(this, ntfc);
                var ev   = OnEachFileTransfer;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachFileTransfer(that);
                }
                break;
            }

            case NotificationType.FileTransferStatus: {
                var ntfc = (FileTransferStatus[])ntf;
                ProcessFileTransferStatus(ntfc);
                OnFileTransferStatus?.Invoke(this, ntfc);
                var ev   = OnEachFileTransferStatus;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachFileTransferStatus(that);
                }
                break;
            }

            case NotificationType.FileUpload: {
                var ntfc = (FileUpload[])ntf;
                ProcessFileUpload(ntfc);
                OnFileUpload?.Invoke(this, ntfc);
                var ev   = OnEachFileUpload;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachFileUpload(that);
                }
                break;
            }

            case NotificationType.InitIvExpand: {
                var ntfc = (InitIvExpand[])ntf;
                ProcessInitIvExpand(ntfc);
                OnInitIvExpand?.Invoke(this, ntfc);
                var ev   = OnEachInitIvExpand;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachInitIvExpand(that);
                }
                break;
            }

            case NotificationType.InitIvExpand2: {
                var ntfc = (InitIvExpand2[])ntf;
                ProcessInitIvExpand2(ntfc);
                OnInitIvExpand2?.Invoke(this, ntfc);
                var ev   = OnEachInitIvExpand2;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachInitIvExpand2(that);
                }
                break;
            }

            case NotificationType.InitServer: {
                var ntfc = (InitServer[])ntf;
                ProcessInitServer(ntfc);
                OnInitServer?.Invoke(this, ntfc);
                var ev   = OnEachInitServer;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachInitServer(that);
                    book?.UpdateInitServer(that);
                }
                break;
            }

            case NotificationType.PluginCommand: {
                var ntfc = (PluginCommand[])ntf;
                ProcessPluginCommand(ntfc);
                OnPluginCommand?.Invoke(this, ntfc);
                var ev   = OnEachPluginCommand;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachPluginCommand(that);
                }
                break;
            }

            case NotificationType.ServerEdited: {
                var ntfc = (ServerEdited[])ntf;
                ProcessServerEdited(ntfc);
                OnServerEdited?.Invoke(this, ntfc);
                var ev   = OnEachServerEdited;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachServerEdited(that);
                    book?.UpdateServerEdited(that);
                }
                break;
            }

            case NotificationType.ServerGroupList: {
                var ntfc = (ServerGroupList[])ntf;
                ProcessServerGroupList(ntfc);
                OnServerGroupList?.Invoke(this, ntfc);
                var ev   = OnEachServerGroupList;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachServerGroupList(that);
                    book?.UpdateServerGroupList(that);
                }
                break;
            }

            case NotificationType.TextMessage: {
                var ntfc = (TextMessage[])ntf;
                ProcessTextMessage(ntfc);
                OnTextMessage?.Invoke(this, ntfc);
                var ev   = OnEachTextMessage;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachTextMessage(that);
                }
                break;
            }

            case NotificationType.TokenUsed: {
                var ntfc = (TokenUsed[])ntf;
                ProcessTokenUsed(ntfc);
                OnTokenUsed?.Invoke(this, ntfc);
                var ev   = OnEachTokenUsed;
                var book = Book;
                foreach (var that in ntfc)
                {
                    ev?.Invoke(this, that);
                    ProcessEachTokenUsed(that);
                }
                break;
            }

            case NotificationType.Unknown:
            default:
                throw Util.UnhandledDefault(lazyNotification.NotifyType);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Watch for device responses and notifications
        /// </summary>
        /// <returns></returns>
        private async Task Watch()
        {
            await Task.Factory.StartNew(async() =>
            {
                //while device is connected
                while (_tcpClient != null)
                {
                    lock (_syncLock)
                    {
                        //there is data avaiblable in the pipe
                        if (_tcpClient.Client.Available > 0)
                        {
                            byte[] bytes = new byte[_tcpClient.Client.Available];

                            //read datas
                            _tcpClient.Client.Receive(bytes);

                            try
                            {
                                string datas = Encoding.UTF8.GetString(bytes);
                                if (!string.IsNullOrEmpty(datas))
                                {
                                    //get every messages in the pipe
                                    foreach (string entry in datas.Split(new string[] { Constantes.LineSeparator }, StringSplitOptions.RemoveEmptyEntries))
                                    {
                                        CommandResult commandResult = JsonConvert.DeserializeObject <CommandResult>(entry, _serializerSettings);

                                        if (commandResult != null && commandResult.Result != null)
                                        {
                                            //command result
                                            _currentCommandResults[commandResult.Id] = commandResult;
                                        }
                                        else if (commandResult != null && commandResult.Error != null)
                                        {
                                            //error result
                                            OnCommandError?.Invoke(this, new CommandErrorEventArgs(commandResult.Error));
                                        }
                                        else
                                        {
                                            NotificationResult notificationResult = JsonConvert.DeserializeObject <NotificationResult>(entry, _serializerSettings);

                                            if (notificationResult != null && notificationResult.Method != null)
                                            {
                                                if (notificationResult.Params != null)
                                                {
                                                    //save properties
                                                    foreach (KeyValuePair <PROPERTIES, object> property in notificationResult.Params)
                                                    {
                                                        this[property.Key] = property.Value;
                                                    }
                                                }

                                                //notification result
                                                OnNotificationReceived?.Invoke(this, new NotificationReceivedEventArgs(notificationResult));
                                            }
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine($"Error while reading through pipe : {ex.Message}");
                            }
                        }
                    }
                    await Task.Delay(100);
                }
            }, TaskCreationOptions.LongRunning);
        }