Пример #1
0
 public static void SendMessage(this ICommunicationProtocol communicationProtocol, string message)
 {
     using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(message)))
     {
         communicationProtocol.SendMessage(memoryStream);
     }
 }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TcpCommunicationListener" /> class.
        /// </summary>
        /// <param name="server">RingMaster server</param>
        /// <param name="port">Port where this listener will listen</param>
        /// <param name="uriPublished">The specific uri to listen on</param>
        /// <param name="executor">RingMaster request executor</param>
        /// <param name="instrumentation">Instrumentation consumer</param>
        /// <param name="protocol">The Marshalling protocol</param>
        /// <param name="maximumSupportedProtocolVersion">Maximum supported version</param>
        public TcpCommunicationListener(
            RingMasterServer server,
            int port,
            string uriPublished,
            IRingMasterRequestExecutor executor,
            IRingMasterServerInstrumentation instrumentation,
            ICommunicationProtocol protocol,
            uint maximumSupportedProtocolVersion)
        {
            this.server          = server;
            this.port            = port;
            this.uriPublished    = uriPublished;
            this.instrumentation = instrumentation;
            this.protocol        = protocol;

            var transportConfig = new SecureTransport.Configuration
            {
                UseSecureConnection          = false,
                IsClientCertificateRequired  = false,
                CommunicationProtocolVersion = maximumSupportedProtocolVersion,
            };

            this.transport = new SecureTransport(transportConfig);
            this.executor  = executor;
        }
Пример #3
0
        public async Task DownVolateZero(ICommunicationProtocol _communicationProtocol, Xmldata.IXmlconfig _xmlconfig, CancellationToken token)
        {
            await ControlsPowerStata(true, _communicationProtocol, token);

            await _communicationProtocol.ThicknessAdjustable(true);

            await _communicationProtocol.SetTestPra(TestKind.ControlsVolateDown, 5, token);

            while (true)
            {
                newst : var data = await _communicationProtocol.ReadStataThree(token);

                token.ThrowIfCancellationRequested();
                if (data.Checked)
                {
                    if (data.AVolate > 5)
                    {
                        await _communicationProtocol.SetTestPra(TestKind.ControlsVolateDown, 2, token);
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    goto newst;
                }
            }

            //byte num = (byte)((await _communicationProtocol.GetCgfVolateDouble()) * 1000 / Convert.ToDouble(_xmlconfig.GetAddNodeValue("Abs")) + 2);
            //await _communicationProtocol.SetTestPra(TestKind.ControlsVolateDown, num);
        }
Пример #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RingMasterClient"/> class.
 /// </summary>
 /// <param name="communicationProtocol">Interface to the communication protocol</param>
 /// <param name="transport">Interface to the transport layer</param>
 /// <param name="cancellationToken">Token that will be observed for cancellation signal</param>
 public RingMasterClient(
     ICommunicationProtocol communicationProtocol,
     ITransport transport,
     CancellationToken cancellationToken)
     : this(null, communicationProtocol, transport, cancellationToken)
 {
 }
Пример #5
0
        public async Task <bool> ControlsPowerStata(bool Open, ICommunicationProtocol _communicationProtocol, CancellationToken token)
        {
            if (Open)
            {
                if (await _communicationProtocol.GetPowerStata())
                {
                    return(true);
                }
                else
                {
                    await _communicationProtocol.SetTestPra(TestKind.Start, 2, token);

                    await Task.Delay(7000, token);

                    return(await _communicationProtocol.GetPowerStata());
                }
            }
            else
            {
                if (await _communicationProtocol.GetPowerStata())
                {
                    return(await _communicationProtocol.SetTestPra(TestKind.Stop, 2, token));
                }
                else
                {
                    return(true);
                }
            }
        }
Пример #6
0
        private async void StartAndWaitForInterProcessUpdateInternal()
        {
            try
            {
                while (!CancellationToken.IsCancellationRequested)
                {
                    protocol?.Dispose();
                    log.LogVerbose($"Start other instance update receiver {serverName}.");

                    protocol = await NamedPipeCommunicationProtocol.Connect(serverName, streamFactory, log,
                                                                            cancellationToken : CancellationToken).ConfigureAwait(false);

                    log.LogVerbose($"Other instance connected to the update receiver {serverName}.");
                    AsyncAutoResetEvent waitEvent = new AsyncAutoResetEvent(false);
                    protocol.CommunicationError += ProtocolOnError;
                    protocol.MessageReceived    += ProtocolOnMessageReceived;
                    protocol.Start();
                    await waitEvent.WaitAsync(CancellationToken).ConfigureAwait(false);

                    void ProtocolOnError(object sender, EventArgs e)
                    {
                        protocol.FlushReceivedMessages();
                        protocol.CommunicationError -= ProtocolOnError;
                        protocol.MessageReceived    -= ProtocolOnMessageReceived;
                        waitEvent.Set();
                    }
                }
            }
            catch (Exception)
            {
                //Do not log anything as any log will lead to another exception
            }
        }
Пример #7
0
        public async Task <bool> Start(string serverName, bool heartbeat)
        {
            if (communicationProtocol != null)
            {
                StopServer();
            }

            try
            {
                startCancellationTokenSource = new CancellationTokenSource();
                communicationProtocol        =
                    await NamedPipeCommunicationProtocol.Connect(serverName, streamFactory, log,
                                                                 cancellationToken : startCancellationTokenSource.Token).ConfigureAwait(false);
            }
            catch (TaskCanceledException e)
            {
                log.LogError($"Connection to server was canceled.{Environment.NewLine}{e}");
                communicationProtocol = null;
                return(false);
            }
            finally
            {
                startCancellationTokenSource.Dispose();
                startCancellationTokenSource = null;
            }

            if (communicationProtocol != null)
            {
                communicationProtocol.CommunicationError += CommunicationProtocolOnCommunicationError;
            }
            OnConnected(new ServerConnectedEventArgs(communicationProtocol, heartbeat));
            communicationProtocol.Start();
            return(true);
        }
Пример #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RingMasterServer"/> class.
        /// </summary>
        /// <param name="protocol">Protocol used for communication</param>
        /// <param name="instrumentation">Instrumentation consumer</param>
        /// <param name="cancellationToken">Token to observe for cancellation signal</param>
        public RingMasterServer(ICommunicationProtocol protocol, IRingMasterServerInstrumentation instrumentation, CancellationToken cancellationToken)
        {
            this.protocol          = protocol ?? throw new ArgumentNullException(nameof(protocol));
            this.instrumentation   = instrumentation;
            this.cancellationToken = cancellationToken;

            QueuedWorkItemPool.Default.Initialize(Environment.ProcessorCount * 2, cancellationToken);
        }
Пример #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RingMasterClient"/> class.
 /// </summary>
 /// <param name="instrumentation">Instrumentation consumer</param>
 /// <param name="communicationProtocol">Interface to the communication protocol</param>
 /// <param name="transport">Interface to the transport layer</param>
 /// <param name="cancellationToken">Token that will be observed for cancellation signal</param>
 public RingMasterClient(
     IRingMasterClientInstrumentation instrumentation,
     ICommunicationProtocol communicationProtocol,
     ITransport transport,
     CancellationToken cancellationToken)
     : this(new Configuration(), instrumentation, communicationProtocol, transport, cancellationToken)
 {
 }
Пример #10
0
        public async Task <bool> SetVolatedata(double voltage, ICommunicationProtocol _communicationProtocol, Xmldata.IXmlconfig _xmlconfig, CancellationToken token, int TimeOver = 5)
        {
            // await _communicationProtocol.SwitchThincness(true, _communicationProtocol);
            double needdouble = Convert.ToDouble(_xmlconfig.GetAddNodeValue("UpvolateNeeddouble"));
            await _communicationProtocol.ThicknessAdjustable(true);

            await Task.Delay(100); int i = 0;

            while (Math.Abs(voltage - (await _communicationProtocol.ReadStataThree(token)).AVolate) >= 8)
            {
                token.ThrowIfCancellationRequested();

                double currentvolate = (await _communicationProtocol.ReadStataThree(token)).AVolate;
                double ClickTime     = (voltage - currentvolate) / 8;
                if (ClickTime >= 1)
                {
                    await _communicationProtocol.SetTestPra(TestKind.ControlsVolateUP, (byte)ClickTime, token);
                }
                if (ClickTime <= -1)
                {
                    var tf = await _communicationProtocol.SetTestPra(TestKind.ControlsVolateDown, (byte)Math.Abs(ClickTime), token);
                }
            }
            //await _communicationProtocol.SwitchThincness(false, _communicationProtocol);
            await _communicationProtocol.ThicknessAdjustable(false);

            while (Math.Abs(voltage - (await _communicationProtocol.ReadStataThree(token)).AVolate) < 8)
            {
                token.ThrowIfCancellationRequested();
                if (Math.Abs(voltage - (await _communicationProtocol.ReadStataThree(token)).AVolate) / voltage > needdouble)
                {
                    double ClickTime = (voltage - (await _communicationProtocol.ReadStataThree(token)).AVolate) / 1;
                    if (ClickTime >= 1)
                    {
                        await _communicationProtocol.SetTestPra(TestKind.ControlsVolateUP, (byte)ClickTime, token);
                    }
                    if (ClickTime <= -1)
                    {
                        await _communicationProtocol.SetTestPra(TestKind.ControlsVolateDown, (byte)Math.Abs(ClickTime), token);
                    }
                }
                else
                {
                    break;
                }
            }
            //if (Math.Abs(voltage - (await _communicationProtocol.ReadStataThree(token)).AVolate) / voltage > needdouble)
            //{
            //    token.ThrowIfCancellationRequested();

            //    if (++i > TimeOver)
            //        return false;
            //    goto herep;
            //}
            return(true);
        }
Пример #11
0
 //private async Task<TestKind> GetUpOrdownAsync(double volate, ICommunicationProtocol _communicationProtocol)
 //{
 //    if (volate - (await _communicationProtocol.ReadStataThree()).AVolate > 0)
 //        return TestKind.ControlsVolateUP;
 //    else
 //        return TestKind.ControlsVolateDown;
 //}
 private async Task <TestKind> GetUpOrdownHighAsync(double volate, ICommunicationProtocol _communicationProtocol)
 {
     if (volate - (await _communicationProtocol.GetCgfVolateDouble()) > 0)
     {
         return(TestKind.ControlsVolateUP);
     }
     else
     {
         return(TestKind.ControlsVolateDown);
     }
 }
Пример #12
0
 public InterProcessUpdateReceiver(IEnvironmentInformation environmentInformation, StreamFactory streamFactory, ILog log, ICommunicationProtocol externalProtocol, IMessageParser messageParser)
 {
     serverName = environmentInformation.InterProcessServerNameBase +
                  environmentInformation.CurrentProcessId.ToString("D", CultureInfo.InvariantCulture);
     this.streamFactory                = streamFactory;
     this.log                          = log;
     this.externalProtocol             = externalProtocol;
     this.messageParser                = messageParser;
     messageParser.HandshakeCompleted += MessageParserOnHandshakeCompleted;
     StartAndWaitForInterProcessUpdate();
 }
Пример #13
0
        protected override async Task <int> Execute(ICommandManager commandManager)
        {
            ExecutionContext context = LifetimeScope.Resolve <ExecutionContext>();

            context.WriteInformation(string.Format(CultureInfo.InvariantCulture, MessageResources.ClientStarting, ServerName));
            using (ICommunicationProtocol protocol = await NamedPipeCommunicationProtocol.Connect(ServerName,
                                                                                                  LifetimeScope.Resolve <StreamFactory>(),
                                                                                                  LifetimeScope.Resolve <ILog>(),
                                                                                                  actAsClient: true)
                                                     .ConfigureAwait(false))
                using (ManualResetEvent serverStoppedEvent = new ManualResetEvent(false))
                {
                    context.WriteInformation(MessageResources.ClientStarted);
                    protocol.CommunicationError += OnError;
                    protocol.MessageReceived    += OnMessageReceived;
                    protocol.Start();

                    Task.Run(ReadConsoleAsync);
                    serverStoppedEvent.WaitOne(-1, true);

                    return(0);

                    void OnError(object sender, EventArgs e)
                    {
                        protocol.CommunicationError -= OnError;
                        context.WriteInformation(MessageResources.ClientServerDisconnectedMessage);
                        serverStoppedEvent.Set();
                    }

                    void OnMessageReceived(object sender, MessageReceivedEventArgs e)
                    {
                        context.WriteInformation(MessageResources.ClientMessageReceived);
                        context.WriteInformation(Encoding.UTF8.GetString(e.Message.ReadToEnd()));
                    }

                    void ReadConsoleAsync()
                    {
                        string serverMessage = Console.ReadLine();

                        if (serverMessage?.Equals("kill", StringComparison.OrdinalIgnoreCase) == true)
                        {
                            serverStoppedEvent.Set();
                        }
                        using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(serverMessage)))
                        {
                            context.WriteInformation(MessageResources.ClientSendingMessage);
                            protocol.SendMessage(stream);
                        }

                        Task.Run(ReadConsoleAsync);
                    }
                }
        }
Пример #14
0
 public async Task DownAndClosePower(ICommunicationProtocol _communicationProtocolk, Xmldata.IXmlconfig _xmlconfig, CancellationToken token)
 {
     if (await _communicationProtocolk.GetPowerStata() && (await _communicationProtocolk.ReadStataThree(token)).AVolate > 10)
     {
         await DownVolateZero(_communicationProtocolk, _xmlconfig, token);
         await ControlsPowerStata(false, _communicationProtocolk, token);
     }
     else
     {
         await ControlsPowerStata(false, _communicationProtocolk, token);
     }
     await ControlsPowerStata(false, _communicationProtocolk, token);
 }
Пример #15
0
 public void StopServer()
 {
     lock (syncRoot)
     {
         if (communicationProtocol != null)
         {
             communicationProtocol.CommunicationError -= CommunicationProtocolOnCommunicationError;
         }
         startCancellationTokenSource?.Cancel();
         communicationProtocol?.Dispose();
         communicationProtocol = null;
         startCancellationTokenSource?.Dispose();
         OnDisconnected();
     }
 }
Пример #16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Session"/> class.
 /// </summary>
 /// <param name="server">Ring Master server object</param>
 /// <param name="sessionId">Session ID</param>
 /// <param name="onInitSession">Callback when the session is initialized</param>
 /// <param name="connection">Secure Transport connection object</param>
 /// <param name="protocol">Protocol object for sending/receiving responses</param>
 /// <param name="instrumentation">Instrumentation object</param>
 public Session(
     RingMasterServer server,
     ulong sessionId,
     Func<RequestInit, IRingMasterRequestHandlerOverlapped> onInitSession,
     IConnection connection,
     ICommunicationProtocol protocol,
     IRingMasterServerInstrumentation instrumentation)
 {
     this.server = server;
     this.sessionId = sessionId;
     this.connection = connection;
     this.onInitSession = onInitSession;
     this.protocol = protocol;
     this.instrumentation = instrumentation;
 }
Пример #17
0
        public async Task <int> SwitchThincness(bool open, ICommunicationProtocol _communicationProtocol, CancellationToken token)
        {
            if (open)
            {
                if ((await GetCurrentThickness(_communicationProtocol, token)) == 1)
                {
                    return(1);
                }
                else
                {
                    await _communicationProtocol.ThicknessAdjustable(true);

                    if ((await GetCurrentThickness(_communicationProtocol, token)) == 1)
                    {
                        return(1);
                    }
                    await _communicationProtocol.ThicknessAdjustable(false);

                    if ((await GetCurrentThickness(_communicationProtocol, token)) == 1)
                    {
                        return(1);
                    }
                }
            }
            else
            {
                if ((await GetCurrentThickness(_communicationProtocol, token)) == 0)
                {
                    return(0);
                }
                else
                {
                    await _communicationProtocol.ThicknessAdjustable(false);

                    if ((await GetCurrentThickness(_communicationProtocol, token)) == 0)
                    {
                        return(0);
                    }
                    await _communicationProtocol.ThicknessAdjustable(true);

                    if ((await GetCurrentThickness(_communicationProtocol, token)) == 0)
                    {
                        return(0);
                    }
                }
            }
            return(-1);
        }
Пример #18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RingMasterRequestHandler"/> class.
 /// </summary>
 /// <param name="configuration">Configuration settings</param>
 /// <param name="instrumentation">Instrumentation consumer</param>
 /// <param name="communicationProtocol">Interface to the communication protocol</param>
 /// <param name="transport">Interface to the transport layer</param>
 /// <param name="cancellationToken">Token that will be observed for cancellation signal</param>
 public RingMasterRequestHandler(
     Configuration configuration,
     IInstrumentation instrumentation,
     ICommunicationProtocol communicationProtocol,
     ITransport transport,
     CancellationToken cancellationToken)
 {
     this.configuration             = configuration;
     this.instrumentation           = instrumentation;
     this.communicationProtocol     = communicationProtocol;
     this.transport                 = transport;
     this.transport.OnNewConnection = this.OnNewConnection;
     this.cancellationTokenSource   = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
     this.outgoingRequests          = new BlockingCollection <RequestWrapper>(this.configuration.RequestQueueLength);
     this.outgoingRequestsAvailable = new SemaphoreSlim(0, this.configuration.RequestQueueLength);
     this.incomingResponses         = new BlockingCollection <ResponseWrapper>(this.configuration.ResponseQueueLength);
     this.responsesAvailable        = new SemaphoreSlim(0, this.configuration.ResponseQueueLength);
     this.manageRequestsTask        = Task.Run(this.ManageRequestLifetime);
     this.manageResponsesTask       = Task.Run(this.ManageResponses);
 }
        public NamedPipeServerProtocolSplitMessagesTest(ITestOutputHelper output)
        {
            NamedPipeServerFeature serverFeature = new NamedPipeServerFeature();

            if (!serverFeature.FeatureEnabled)
            {
                throw new SkipTestException("Disabled named pipe communication");
            }
            streamFactory = PageStreamFactory.CreateDefault(16);
            string serverName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
                                    ? Guid.NewGuid().ToByteString()
                                    : $"/tmp/{Guid.NewGuid().ToByteString()}";
            Task <ICommunicationProtocol> creationTask = NamedPipeCommunicationProtocol.Connect(serverName, streamFactory, new LogTracer(output));

            simulator = NamedPipeCommunicationProtocolSimulator.Connect(serverName, streamFactory, new LogTracer(output));
            creationTask.Wait();
            protocol = creationTask.Result;
            protocol.MessageReceived    += OnMessageReceived;
            protocol.CommunicationError += OnCommunicationError;
            protocol.Start();
        }
Пример #20
0
        public async Task <int> GetCurrentThickness(ICommunicationProtocol _communicationProtocol, CancellationToken token)
        {
            double startvolate  = (await _communicationProtocol.ReadStataThree(token)).AVolate;
            double finishvolate = 0;
            bool   cf           = await _communicationProtocol.SetTestPra(TestKind.ControlsVolateUP, 3, token);

            if (cf)
            {
                finishvolate = (await _communicationProtocol.ReadStataThree(token)).AVolate;
                await _communicationProtocol.SetTestPra(TestKind.ControlsVolateDown, 3, token);

                if (Math.Abs(startvolate - finishvolate) > 5)
                {
                    return(1);
                }
                else
                {
                    return(0);
                }
            }
            return(-1);
        }
Пример #21
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RingMasterClient"/> class.
        /// </summary>
        /// <param name="configuration">RingMasterClient configuration</param>
        /// <param name="instrumentation">Instrumentation consumer</param>
        /// <param name="communicationProtocol">Interface to the communication protocol</param>
        /// <param name="transport">Interface to the transport layer</param>
        /// <param name="cancellationToken">Token that will be observed for cancellation signal</param>
        public RingMasterClient(
            Configuration configuration,
            IRingMasterClientInstrumentation instrumentation,
            ICommunicationProtocol communicationProtocol,
            ITransport transport,
            CancellationToken cancellationToken)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var handlerConfiguration = new RingMasterRequestHandler.Configuration();

            handlerConfiguration.DefaultTimeout     = configuration.DefaultTimeout;
            handlerConfiguration.HeartBeatInterval  = configuration.HeartBeatInterval;
            handlerConfiguration.RequestQueueLength = configuration.RequestQueueLength;
            handlerConfiguration.RequireLockForReadOnlyOperations = configuration.RequireLockForReadOnlyOperations;
            handlerConfiguration.MustTransparentlyForwardRequests = configuration.MustTransparentlyForwardRequests;

            var handlerInstrumentation = new RingMasterRequestHandlerInstrumentation(instrumentation);

            this.requestHandler = new RingMasterRequestHandler(handlerConfiguration, handlerInstrumentation, communicationProtocol, transport, cancellationToken);
        }
Пример #22
0
 public HidDeviceCommunicationProtocol(HidDeviceViewModel device, ICommunicationProtocol communicationProtocol)
 {
     CommunicationProtocol = communicationProtocol;
     _device = device;
     _device.ReceiveBytes += hidDeviceOnReceiveBytes;
 }
 public HidDeviceCommunicationProtocol(HidDeviceViewModel device, ICommunicationProtocol communicationProtocol)
 {
     CommunicationProtocol = communicationProtocol;
     _device = device;
     _device.ReceiveBytes += hidDeviceOnReceiveBytes;
 }
Пример #24
0
 /// <summary>
 /// Initializes a new instance of the ShareSessionEventArgs with the session
 /// to be shared and the communication protocol to be used.
 /// </summary>
 /// <param name="session"></param>
 /// <param name="protocol"></param>
 public ShareSessionEventArgs(SessionContext session, ICommunicationProtocol protocol)
 {
     Protocol = protocol;
     Session = session;
 }
Пример #25
0
 public ServerConnectedEventArgs(ICommunicationProtocol protocol, bool heartbeat)
 {
     Protocol  = protocol;
     Heartbeat = heartbeat;
 }
Пример #26
0
 public JsonMessageParser(ILog log, ICommunicationProtocol communicationProtocol)
 {
     this.log = log;
     this.communicationProtocol             = communicationProtocol;
     communicationProtocol.MessageReceived += OnMessageReceived;
 }
Пример #27
0
        public async Task <bool> SettindVolate(double voltage, ICommunicationProtocol _communicationProtocol, Xmldata.IXmlconfig _xmlconfig, CancellationToken token, int TimeOver = 0)
        {
            if (voltage < 0.5)
            {
                await DownVolateZero(_communicationProtocol, _xmlconfig, token);

                return(true);
            }
            await ControlsPowerStata(true, _communicationProtocol, token);

            await Task.Delay(100, token); int i = 0;
            double needdouble = Convert.ToDouble(_xmlconfig.GetAddNodeValue("UpvolateNeeddouble"));
            await _communicationProtocol.ThicknessAdjustable(true);

            double foredata = 0;

            while (true)
            {
                token.ThrowIfCancellationRequested();
                TestKind ts;
                var      data = await _communicationProtocol.ReadStataThree(token);

                if (data.Checked)
                {
                    ts = (voltage - data.AVolate) > 0 ? TestKind.ControlsVolateUP : TestKind.ControlsVolateDown;
                    if (Math.Abs(voltage - data.AVolate) >= 16)
                    {
                        await _communicationProtocol.SetTestPra(ts, 1, token);

                        await Task.Delay(100, token);
                    }
                    else
                    {
                        break;
                    }
                    if (i++ > 0 && data.AVolate < 1 && foredata < 1 && (await _communicationProtocol.ReadStataThree(token)).AVolate < 1)
                    {
                        throw new OperationCanceledException();
                    }
                    foredata = data.AVolate;
                }
            }
            await _communicationProtocol.ThicknessAdjustable(false); i = 0; foredata = 0;

            while (true)
            {
                TestKind ts;
                var      data = await _communicationProtocol.ReadStataThree(token);

                token.ThrowIfCancellationRequested();
                if (data.Checked)
                {
                    ts = (voltage - data.AVolate) > 0 ? TestKind.ControlsVolateUP : TestKind.ControlsVolateDown;
                    if (Math.Abs(voltage - data.AVolate) < 16)
                    {
                        if (Math.Abs(voltage - data.AVolate) / voltage > needdouble)
                        {
                            await _communicationProtocol.SetTestPra(ts, 1, token);
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                    if (i++ > 0 && data.AVolate < 1 && foredata < 1 && (await _communicationProtocol.ReadStataThree(token)).AVolate < 1)
                    {
                        throw new OperationCanceledException();
                    }
                    foredata = data.AVolate;
                }
            }

            //if (Math.Abs(voltage - (await _communicationProtocol.ReadStataThree(token)).AVolate) / voltage > needdouble)
            //{
            //    token.ThrowIfCancellationRequested();



            //    if (++i > TimeOver)
            //        return false;
            //    goto heres;
            //}
            return(true);
        }
Пример #28
0
 public SimpleServer(IRingMasterRequestHandler requestHandler, ICommunicationProtocol protocol, uint protocolVersion = RingMasterCommunicationProtocol.MaximumSupportedVersion)
 {
     this.requestHandler  = requestHandler;
     this.protocol        = protocol;
     this.protocolVersion = protocolVersion;
 }
Пример #29
0
 public JsonMessageSender(ICommunicationProtocol communicationProtocol)
 {
     this.communicationProtocol = communicationProtocol;
 }
Пример #30
0
        public async Task <bool> SettindHighVolate(double voltage, ICommunicationProtocol _communicationProtocol, Xmldata.IXmlconfig _xmlconfig, CancellationToken token, int TimeOver = 5)
        {
            if (voltage < 0.5)
            {
                await DownVolateZero(_communicationProtocol, _xmlconfig, token);

                return(true);
            }
            await ControlsPowerStata(true, _communicationProtocol, token);

            await Task.Delay(100); int i = 0; double foredata = 0;
            double needdouble = Convert.ToDouble(_xmlconfig.GetAddNodeValue("UpvolateNeedHighdouble"));
            double Abs = Convert.ToDouble(_xmlconfig.GetAddNodeValue("Abs"));
            await _communicationProtocol.ThicknessAdjustable(true);

            while (Math.Abs(voltage - (await _communicationProtocol.GetCgfVolateDouble())) >= 1)
            {
                token.ThrowIfCancellationRequested();
                var data = await _communicationProtocol.GetCgfVolateDouble();

                var needchangecgf = voltage - data;
                if (needchangecgf > 0)
                {
                    await _communicationProtocol.SetTestPra(TestKind.ControlsVolateUP, 1, token);

                    await Task.Delay(50, token);
                }
                else
                {
                    await _communicationProtocol.SetTestPra(TestKind.ControlsVolateDown, 1, token);

                    await Task.Delay(50, token);
                }
                if (i++ > 0 && data < 0.1 && foredata < 0.1 && (await _communicationProtocol.GetCgfVolateDouble()) < 0.1)
                {
                    throw new OperationCanceledException();
                }
                foredata = data;
            }
            await _communicationProtocol.ThicknessAdjustable(false);

            while (Math.Abs(voltage - (await _communicationProtocol.GetCgfVolateDouble())) < 1)
            {
                token.ThrowIfCancellationRequested();
                var data = await _communicationProtocol.GetCgfVolateDouble();

                var needchangecgf = voltage - data;
                if (Math.Abs(voltage - data) / voltage > needdouble)
                {
                    if (needchangecgf > 0)
                    {
                        await _communicationProtocol.SetTestPra(TestKind.ControlsVolateUP, 1, token);

                        await Task.Delay(50, token);
                    }
                    else
                    {
                        await _communicationProtocol.SetTestPra(TestKind.ControlsVolateDown, 1, token);

                        await Task.Delay(50, token);
                    }
                }
                else
                {
                    break;
                }
                if (i++ > 0 && data < 0.1 && foredata < 0.1 && (await _communicationProtocol.GetCgfVolateDouble()) < 0.1)
                {
                    throw new OperationCanceledException();
                }
                foredata = data;
            }
            return(true);
        }
Пример #31
0
        public async Task <bool> SettindHighVolateByLow(double voltage, ICommunicationProtocol _communicationProtocol, Xmldata.IXmlconfig _xmlconfig, CancellationToken token, int TimeOver = 5)
        {
            double lowvolate = voltage * 1000 / Convert.ToDouble(_xmlconfig.GetAddNodeValue("Abs"));

            return(await SettindVolate(lowvolate, _communicationProtocol, _xmlconfig, token));
        }
Пример #32
0
        public async Task <bool> SettingFre(double Fre, ICommunicationProtocol _communicationProtocol, CancellationToken token, int TimeOver = 5)
        {
            await ControlsPowerStata(true, _communicationProtocol, token);

            await Task.Delay(100); int i = 0; double foredata = 0;
            await _communicationProtocol.ThicknessAdjustable(false);

            while ((await _communicationProtocol.ReadStataThree(token)).AVolate < 10)
            {
                await _communicationProtocol.SetTestPra(TestKind.ControlsVolateUP, 1, token);

                await Task.Delay(300);

                StataThree tpd = await _communicationProtocol.ReadStataThree(token);

                if (i++ > 0 && tpd.AVolate < 1 && foredata < 1 && (await _communicationProtocol.ReadStataThree(token)).AVolate < 1)
                {
                    throw new OperationCanceledException();
                }
                foredata = tpd.AVolate;
            }
            await _communicationProtocol.ThicknessAdjustable(true); i = 0;

            Heref : while (Math.Abs(Fre - (await _communicationProtocol.ReadStataThree(token)).Fre) >= 1)
            {
                p1 : var tpd = await _communicationProtocol.ReadStataThree(token);

                token.ThrowIfCancellationRequested();

                if (!tpd.Checked)
                {
                    goto p1;
                }
                var range = Fre - tpd.Fre;
                var data  = (byte)(Math.Abs((int)(Math.Round(range, 1))));
                if (range > 0)
                {
                    await _communicationProtocol.SetTestPra(TestKind.ControlsFreUp, data, token);
                }
                else
                {
                    await _communicationProtocol.SetTestPra(TestKind.ControlsFreDown, data, token);
                }
                if (i++ > 0 && tpd.AVolate < 1 && foredata < 1 && (await _communicationProtocol.ReadStataThree(token)).AVolate < 1)
                {
                    throw new OperationCanceledException();
                }
                foredata = tpd.AVolate;
                await Task.Delay(100, token);
            }
            await _communicationProtocol.ThicknessAdjustable(false); i = 0;

            while (Math.Abs(Fre - (await _communicationProtocol.ReadStataThree(token)).Fre) < 1)
            {
                p2 : var tpd = await _communicationProtocol.ReadStataThree(token);

                token.ThrowIfCancellationRequested();

                if (!tpd.Checked)
                {
                    goto p2;
                }
                var range = Fre - tpd.Fre;
                var data  = (byte)(Math.Abs((int)Math.Round((range * 10))));
                if (range > 0)
                {
                    await _communicationProtocol.SetTestPra(TestKind.ControlsFreUp, data, token);
                }
                else if (range < 0)
                {
                    await _communicationProtocol.SetTestPra(TestKind.ControlsFreDown, data, token);
                }
                else
                {
                    return(true);
                }
                if (i++ > 0 && tpd.AVolate < 1 && foredata < 1 && (await _communicationProtocol.ReadStataThree(token)).AVolate < 1)
                {
                    throw new OperationCanceledException();
                }
                foredata = tpd.AVolate;
                await Task.Delay(100, token);
            }
            if (Math.Abs(Fre - (await _communicationProtocol.ReadStataThree(token)).Fre) == 0)
            {
                return(true);
            }
            else
            {
                token.ThrowIfCancellationRequested();

                if (++i > TimeOver)
                {
                    return(false);
                }
                goto Heref;
            }
        }