Beispiel #1
0
        static SerialPort sp = null; //串口对象

        protected override async Task DidReceiveCall(string method, JObject parameters,
                                                     Func <JToken, JsonRpcException, Task> completion)
        {
            switch (method)
            {
            case "connect":
                await completion(await Connect(parameters), null);

                break;

            case "disconnect":
                await completion(await DisConnect(parameters), null);

                break;

            case "send":
                await completion(await SendMessage(parameters), null);

                break;;

            case "read":
                await completion(await ReadMessage(parameters), null);

                break;

            default:
                throw JsonRpcException.MethodNotFound(method);
            }
        }
        /// <summary>
        /// Handle a client request
        /// </summary>
        /// <param name="method">The name of the method called by the client</param>
        /// <param name="parameters">The parameters passed by the client</param>
        /// <param name="completion">The completion handler to be called with the result</param>
        protected override async Task DidReceiveCall(string method, JObject parameters,
                                                     Func <JToken, JsonRpcException, Task> completion)
        {
            switch (method)
            {
            case "discover":
                if (m_peripheralData != null)
                {
                    SendRemoteRequest("didDiscoverPeripheral", m_peripheralData);
                }
                else
                {
                    Discover(parameters);
                }
                await completion(null, null);

                break;

            case "connect":
                await Connect(parameters);
                await completion(null, null);

                break;

            case "write":
                await completion(await Write(parameters), null);

                break;

            case "read":
                await completion(await Read(parameters), null);

                break;

            case "startNotifications":
                await StartNotifications(parameters);
                await completion(null, null);

                break;

            case "stopNotifications":
                await StopNotifications(parameters);
                await completion(null, null);

                break;

            case "pingMe":
                await completion("willPing", null);

                SendRemoteRequest("ping", null, (result, error) =>
                {
                    Debug.Print($"Got result from ping: {result}");
                    return(Task.CompletedTask);
                });
                break;

            default:
                throw JsonRpcException.MethodNotFound(method);
            }
        }
Beispiel #3
0
        protected override async Task DidReceiveCall(string method, JObject parameters,
                                                     Func <JToken, JsonRpcException, Task> completion)
        {
            switch (method)
            {
            case "discover":
                Discover(parameters);
                await completion(null, null);

                break;

            case "connect":
                if (_watcher != null && _watcher.Status == DeviceWatcherStatus.Started)
                {
                    _watcher.Stop();
                }
                await Connect(parameters);
                await completion(null, null);

                break;

            case "send":
                await completion(await SendMessage(parameters), null);

                break;

            default:
                throw JsonRpcException.MethodNotFound(method);
            }
        }
Beispiel #4
0
        // Override this to handle received RPC requests & notifications.
        // Call this method with `await super.DidReceiveCall(...)` to implement default calls like `getVersion`.
        // Call the completion handler when done with a request:
        // - pass your call's "return value" (or null) as `result` on success
        // - pass an instance of `JsonRpcException` for `error` on failure
        // You may also throw a `JsonRpcException` (or any other `Exception`) to signal failure.
        // Exceptions are caught even when thrown in an `async` method after `await`:
        // http://www.interact-sw.co.uk/iangblog/2010/11/01/csharp5-async-exceptions
        protected virtual async Task DidReceiveCall(string method, JObject parameters, CompletionHandler completion)
        {
            switch (method)
            {
            case "pingMe":
                await completion("willPing", null);

                SendRemoteRequest("ping", null, (result, error) =>
                {
                    Debug.Print($"Got result from ping: {result}");
                    return(Task.CompletedTask);
                });
                break;

            case "getVersion":
                await completion(GetVersion(), null);

                break;

            default:
                // unrecognized method
                throw JsonRpcException.MethodNotFound(method);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Handle a client request
        /// </summary>
        /// <param name="method">The name of the method called by the client</param>
        /// <param name="parameters">The parameters passed by the client</param>
        /// <param name="completion">The completion handler to be called with the result</param>
        protected override async Task DidReceiveCall(string method, JObject parameters,
                                                     Func <JToken, JsonRpcException, Task> completion)
        {
            switch (method)
            {
            case "discover":
                if (m_peripheralData != null)
                {
                    SendRemoteRequest("didDiscoverPeripheral", m_peripheralData);
                    firstconnect = false;
                }
                else
                {
                    Discover(parameters);
                    firstconnect = true;
                }
                await completion(null, null);

                break;

            case "connect":
                if (_services != null)
                {
                    foreach (var service in _services)
                    {
                        try
                        {
                            service.Dispose();
                        }
                        catch
                        {
                            // ignore: probably the peripheral is gone
                        }
                    }
                    _services = null;
                    _peripheral.Dispose();
                }
                _cachedCharacteristics.Clear();
                //if (firstconnect)

                JObject ret = null;

                try
                {
                    await Connect(parameters);

                    ret = new JObject
                    {
                        new JProperty("status", new JValue("OK"))
                    };
                }
                catch (Exception e)
                {
                    ret = new JObject
                    {
                        new JProperty("status", new JValue("Error")),
                        new JProperty("message", new JValue(e.Message))
                    };
                }
                SendRemoteRequest("connect", ret);
                await completion(null, null);

                break;

            case "write":
                await completion(await Write(parameters), null);

                break;

            case "read":
                await completion(await Read(parameters), null);

                break;

            case "startNotifications":
                //if (firstconnect)
                await StartNotifications(parameters);
                await completion(null, null);

                break;

            case "stopNotifications":
                await StopNotifications(parameters);
                await completion(null, null);

                break;

            case "pingMe":
                await completion("willPing", null);

                SendRemoteRequest("ping", null, (result, error) =>
                {
                    Debug.Print($"Got result from ping: {result}");
                    return(Task.CompletedTask);
                });
                break;

            default:
                throw JsonRpcException.MethodNotFound(method);
            }
        }