Beispiel #1
0
        private async Task InitServiceHub()
        {
            _serviceHub = new SerialServiceHub(WebServerUrl, this);
            var connection = await _serviceHub.Start();

            connection.On("queueEmpty", (int id) =>
            {
                if (PortId == id)
                {
                    CommandQueueEmpty?.Invoke(this, new SerialEventArgs());
                }
            });
            connection.On("queueChanged", (int id, int queuelength) =>
            {
                if (PortId == id)
                {
                    CommandQueueChanged?.Invoke(this, new SerialEventArgs(queuelength, null));
                }
            });
            connection.On("sendingCommand", (int id, int seqId) =>
            {
                if (PortId == id)
                {
                    CommandSending?.Invoke(this, new SerialEventArgs(new SerialCommand()
                    {
                        SeqId = seqId
                    }));
                }
            });
        }
Beispiel #2
0
        public async Task DisconnectAsync()
        {
            if (PortId >= 0)
            {
                using (var scope = CreateScope())
                {
                    var response = await scope.Instance.PostAsJsonAsync($@"{_api}/{PortId}/disconnect", "x");
                    if (response.IsSuccessStatusCode)
                    {
                        _serviceHub?.Stop();
                        _serviceHub = null;
                        IsConnected = false;
                        PortId      = -1;
                        return;
                    }
                }
            }

            throw new Exception("DisConnect to SerialPort failed");
        }
Beispiel #3
0
        public async Task DisconnectAsync()
        {
            if (PortId != 0)
            {
                using (HttpClient client = CreateHttpClient())
                {
                    HttpResponseMessage response = await client.PostAsJsonAsync($@"{_api}/{PortId}/disconnect", "x");

                    if (response.IsSuccessStatusCode)
                    {
                        _serviceHub?.Stop();
                        _serviceHub = null;
                        IsConnected = false;
                        PortId      = 0;
                        return;
                    }
                }
            }
            throw new Exception("DisConnect to SerialPort failed");
        }