Beispiel #1
0
        public void AddResourceType(string resourceTypeName, IDictionary <string, object> properties)
        {
            OCStackResult result = OCStack.OCBindResourceTypeToResource(_handle, resourceTypeName);

            OCStackException.ThrowIfError(result, "Failed to add resource type");
            _resourceProperties.Add(resourceTypeName, new IotivityValueDictionary(properties));
        }
Beispiel #2
0
        private static void CreateResources()
        {
            //bswitch = new BinarySwitchDevice("/switch/1", false);
            //bswitch2 = new BinarySwitchDevice("/switch/2", true);
            // bswitch3 = new BinarySwitchDevice("/switch/3", true);
            // bswitch4 = new BinarySwitchDevice("/switch/4", true);
            // light = new LightDevice("/light/1", true, 50, 180, 100);
            // light2 = new LightDevice("/foo/1", false, 75);
            // light3 = new LightDevice("/lifx/2", true);
            IntPtr _handle;
            string resourceTypeName      = "oic.r.switch.binary";
            string resourceInterfaceName = "oic.if.baseline";

            OCStackResult result = OCStack.OCCreateResource(out _handle, resourceTypeName, resourceInterfaceName,
                                                            "/switch/1", null, IntPtr.Zero, OCResourceProperty.OC_DISCOVERABLE | OCResourceProperty.OC_OBSERVABLE);

            result = OCStack.OCCreateResource(out _handle, resourceTypeName + "2", resourceInterfaceName,
                                              "/switch/2", null, IntPtr.Zero, OCResourceProperty.OC_DISCOVERABLE | OCResourceProperty.OC_OBSERVABLE);

            result = OCStack.OCCreateResource(out _handle, resourceTypeName + "3", resourceInterfaceName,
                                              "/switch/3", null, IntPtr.Zero, OCResourceProperty.OC_DISCOVERABLE | OCResourceProperty.OC_OBSERVABLE);

            //result = OCStack.OCCreateResource(out _handle, resourceTypeName + "4", resourceInterfaceName,
            //    "/switch/4", null, IntPtr.Zero, OCResourceProperty.OC_DISCOVERABLE | OCResourceProperty.OC_OBSERVABLE);
        }
Beispiel #3
0
 private static string GetMessage(OCStackResult result, string message = null)
 {
     if (string.IsNullOrEmpty(message))
     {
         return(result.ToString());
     }
     return(message + " (" + result.ToString() + ")");
 }
Beispiel #4
0
        internal static void ThrowIfError(OCStackResult result, string message = null)
        {
            var err = CreateException(result, message);

            if (err != null)
            {
                throw err;
            }
        }
Beispiel #5
0
        public DeviceResource(string uri, string resourceTypeName, IDictionary <string, object> properties, string resourceInterfaceName = IotivityDotNet.Interop.Defines.OC_RSRVD_INTERFACE_DEFAULT)
        {
            _resourceCallback = this.OCEntityHandler;
            OCStackResult result = OCStack.OCCreateResource(out _handle, resourceTypeName, resourceInterfaceName, uri, _resourceCallback, IntPtr.Zero, OCResourceProperty.OC_DISCOVERABLE | OCResourceProperty.OC_OBSERVABLE | OCResourceProperty.OC_SECURE);

            OCStackException.ThrowIfError(result, "Failed to create resource");
            _uri = uri;
            _resourceProperties = new Dictionary <string, IotivityValueDictionary>();
            if (properties != null)
            {
                _resourceProperties.Add(resourceTypeName, new IotivityValueDictionary(properties));
            }
        }
Beispiel #6
0
        internal static void ThrowIfError(OCStackResult result, string message = null)
        {
            if (result == OCStackResult.OC_STACK_OK)
            {
                return;
            }
            Exception exception = new OCStackException(result, message);

            if (result == OCStackResult.OC_STACK_INVALID_PARAM)
            {
                exception = new ArgumentException(message, exception);
            }
            else if (result == OCStackResult.OC_STACK_INVALID_METHOD)
            {
                exception = new InvalidOperationException(message, exception);
            }
            else if (result == OCStackResult.OC_STACK_UNAUTHORIZED_REQ)
            {
                exception = new UnauthorizedAccessException(message, exception);
            }
            throw exception;
        }
Beispiel #7
0
        protected void BindInterface(string resourceInterfaceName)
        {
            OCStackResult result = OCStack.OCBindResourceInterfaceToResource(_handle, resourceInterfaceName);

            OCStackException.ThrowIfError(result, "Failed to bind interface");
        }
Beispiel #8
0
 private OCStackException(OCStackResult result, string message) : base(GetMessage(result, message))
 {
     ErrorCode = result;
 }