/* * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Runtime.InteropServices; namespace Tizen.Network.IoTConnectivity { /// /// This class provides API to manage representation. /// A representation is a payload of a request or a response. /// public class Representation : IDisposable { internal IntPtr _representationHandle = IntPtr.Zero; private bool _disposed = false; private ObservableCollection _children = new ObservableCollection(); /// /// The Representation constructor /// /// /// Thrown when the iotcon is not supported /// Thrown when there is not enough memory /// Thrown when there is an invalid parameter /// /// Representation repr = new Representation(); /// public Representation() { int ret = Interop.IoTConnectivity.Common.Representation.Create(out _representationHandle); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create representation"); throw IoTConnectivityErrorFactory.GetException(ret); } _children.CollectionChanged += ChildrenCollectionChanged; } // Constructor for cloning native representation object internal Representation(IntPtr handleToClone) { int ret = (int)IoTConnectivityError.InvalidParameter; if (handleToClone != IntPtr.Zero) { ret = Interop.IoTConnectivity.Common.Representation.Clone(handleToClone, out _representationHandle); } if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create representation"); throw IoTConnectivityErrorFactory.GetException(ret); } _children.CollectionChanged += ChildrenCollectionChanged; } /// /// Destructor of the Representation class. /// ~Representation() { Dispose(false); } /// /// The URI path of resource /// /// /// Setter can throw exceptions /// /// Thrown when the iotcon is not supported /// Thrown when there is an invalid parameter /// Thrown when the operation is invalid /// /// Representation repr = new Representation(); /// repr.UriPath = "/a/light"; /// Console.WriteLine("URI is {0}", repr.UriPath); //Getter /// public string UriPath { get { IntPtr path; int ret = Interop.IoTConnectivity.Common.Representation.GetUriPath(_representationHandle, out path); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get uri"); throw IoTConnectivityErrorFactory.GetException(ret); } return Marshal.PtrToStringAnsi(path); } set { int ret = (int)IoTConnectivityError.InvalidParameter; if (value != null) ret = Interop.IoTConnectivity.Common.Representation.SetUriPath(_representationHandle, value); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set uri"); throw IoTConnectivityErrorFactory.GetException(ret); } } } /// /// The type of resource /// /// /// Thrown when the iotcon is not supported /// Thrown when there is an invalid parameter /// Thrown when the operation is invalid /// /// Representation repr = new Representation(); /// ResourceTypes types = new ResourceTypes (new List(){ "org.tizen.light" }); /// repr.Type = types; /// var type = repr.Type; // Getter /// foreach (string item in type) /// { /// Console.WriteLine("Type is {0}", item); /// } /// public ResourceTypes Type { get { IntPtr typeHandle; int ret = Interop.IoTConnectivity.Common.Representation.GetResourceTypes(_representationHandle, out typeHandle); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type"); return null; } return (typeHandle == IntPtr.Zero) ? null : new ResourceTypes(typeHandle); } set { int ret = (int)IoTConnectivityError.InvalidParameter; if (value != null) ret = Interop.IoTConnectivity.Common.Representation.SetResourceTypes(_representationHandle, value._resourceTypeHandle); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set type"); throw IoTConnectivityErrorFactory.GetException(ret); } } } /// /// The interface of the resource /// /// /// Thrown when the iotcon is not supported /// Thrown when there is an invalid parameter /// Thrown when the operation is invalid /// /// Representation repr = new Representation(); /// ResourceInterfaces ifaces = new ResourceInterfaces (new List(){ ResourceInterfaces.DefaultInterface }); /// repr.Interface = ifaces; /// var iface = repr.Interface; // Getter /// foreach (string item in iface) /// { /// Console.WriteLine("Interface is {0}", iface); /// } /// public ResourceInterfaces Interface { get { IntPtr interfaceHandle; int ret = Interop.IoTConnectivity.Common.Representation.GetResourceInterfaces(_representationHandle, out interfaceHandle); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get interface"); return null; } return new ResourceInterfaces(interfaceHandle); } set { int ret = (int)IoTConnectivityError.InvalidParameter; if (value != null) ret = Interop.IoTConnectivity.Common.Representation.SetResourceInterfaces(_representationHandle, value.ResourceInterfacesHandle); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set interface"); throw IoTConnectivityErrorFactory.GetException(ret); } } } /// /// Current attributes of the resource /// /// /// Thrown when the iotcon is not supported /// Thrown when there is an invalid parameter /// Thrown when the operation is invalid /// /// Representation repr = new Representation(); /// Attributes attributes = new Attributes() { /// { "state", "ON" }, /// { "dim", 10 } /// }; /// repr.Attributes = attributes; /// var newAttributes = repr.Attributes; // Getter /// string strval = newAttributes["state"] as string; /// int intval = (int)newAttributes["dim"]; /// Console.WriteLine("attributes are {0} and {1}", strval, intval); /// public Attributes Attributes { get { IntPtr attributeHandle; int ret = Interop.IoTConnectivity.Common.Representation.GetAttributes(_representationHandle, out attributeHandle); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes"); throw IoTConnectivityErrorFactory.GetException(ret); } return new Attributes(attributeHandle); } set { int ret = (int)IoTConnectivityError.InvalidParameter; if (value != null) { ret = Interop.IoTConnectivity.Common.Representation.SetAttributes(_representationHandle, value._resourceAttributesHandle); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set attributes"); throw IoTConnectivityErrorFactory.GetException(ret); } } } } /// /// List of Child resource representation /// /// /// Representation repr = new Representation(); /// Representation child1 = new Representation(); /// ResourceTypes types1 = new ResourceTypes(new List() { "org.tizen.light" }); /// child1.Type = types1; /// ResourceInterfaces ifaces1 = new ResourceInterfaces(new List() { ResourceInterfaces.DefaultInterface }); /// child1.Interface = ifaces1; /// try /// { /// repr.Children.Add(child1); /// Console.WriteLine("Number of children : {0}", repr.Children.Count); /// Representation firstChild = repr.Children.ElementAt(0); /// } catch(Exception ex) /// { /// Console.WriteLine("Exception caught : " + ex.Message); /// } /// public ICollection Children { get { return _children; } } /// /// Releases any unmanaged resources used by this object. /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects. /// /// If true, disposes any disposable objects. If false, does not dispose disposable objects. protected virtual void Dispose(bool disposing) { if (_disposed) return; if (disposing) { // Free managed objects Type?.Dispose(); Interface?.Dispose(); Attributes?.Dispose(); foreach(var child in Children) { child.Dispose(); } } Interop.IoTConnectivity.Common.Representation.Destroy(_representationHandle); _disposed = true; } private void ChildrenCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) { foreach (Representation r in e.NewItems) { int ret = Interop.IoTConnectivity.Common.Representation.AddChild(_representationHandle, r._representationHandle); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add child"); throw IoTConnectivityErrorFactory.GetException(ret); } } } else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove) { foreach (Representation r in e.NewItems) { int ret = Interop.IoTConnectivity.Common.Representation.RemoveChild(_representationHandle, r._representationHandle); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove child"); throw IoTConnectivityErrorFactory.GetException(ret); } } } } } }