Beispiel #1
0
 public static extern OCStackResult OCDoResource(out IntPtr handle,
                                                 OCMethod method,
                                                 [MarshalAs(UnmanagedType.LPStr)] string requestUri,
                                                 OCDevAddr destination,
                                                 IntPtr payload, //OCPayload payload,
                                                 OCConnectivityType connectivityType,
                                                 OCQualityOfService qos,
                                                 OCCallbackData cbData,
                                                 OCHeaderOption options,
                                                 byte numOptions);
        private async Task <ClientResponse <Payload> > SendAsync(string resourceTypeName, Dictionary <string, object> data, OCMethod method)
        {
            var tcs          = new TaskCompletionSource <ClientResponse <Payload> >();
            var callbackData = new OCCallbackData();
            OCClientResponseHandler handler = (context, handle, clientResponse) =>
            {
                GCHandle.FromIntPtr(context).Free();
                if (clientResponse.result > OCStackResult.OC_STACK_RESOURCE_CHANGED)
                {
                    tcs.SetException(new Exception("Resource returned error: " + clientResponse.result.ToString()));
                }
                else
                {
                    tcs.SetResult(new ClientResponse <Payload>(clientResponse));
                }
                return(OCStackApplicationResult.OC_STACK_DELETE_TRANSACTION);
            };
            var gcHandle = GCHandle.Alloc(handler);

            callbackData.cb      = handler;
            callbackData.context = GCHandle.ToIntPtr(gcHandle);

            IntPtr payloadHandle = IntPtr.Zero;

            if (resourceTypeName != null)
            {
                RepPayload payload = new RepPayload(data);

                payload.SetUri(_resourceUri);
                payload.Types.Add(resourceTypeName);
                payloadHandle = payload.AsOCRepPayload();
            }

            var result = OCStack.OCDoResource(out _handle, method, _resourceUri, _address.OCDevAddr, payloadHandle, OCConnectivityType.CT_DEFAULT, OCQualityOfService.OC_LOW_QOS, callbackData, null, 0);

            // if (payloadHandle != IntPtr.Zero)
            //     OCPayloadInterop.OCPayloadDestroy(payloadHandle);
            OCStackException.ThrowIfError(result, "Failed to send to resource");
            var response = await tcs.Task.ConfigureAwait(false);

            return(response);
        }