2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.ComponentModel;
20 namespace Tizen.Network.IoTConnectivity
23 /// This class represents a lite resource.
24 /// It provides APIs to encapsulate resources.
25 /// This class is accessed by using a constructor to create a new instance of this object.
27 /// <since_tizen> 3 </since_tizen>
28 public class LiteResource : Resource
31 /// The LiteResource constructor.
33 /// <since_tizen> 3 </since_tizen>
35 /// Creates a lite resource, which can then be registered in server using <see cref="IoTConnectivityServerManager.RegisterResource(Resource)"/>.\n
36 /// When client requests some operations, it sends a response to client automatically.\n
37 /// @a uri length must be less than 128.
40 /// http://tizen.org/privilege/internet
42 /// <privlevel>public</privlevel>
43 /// <param name="uri">The uri path of the lite resource.</param>
44 /// <param name="types">The type of the resource.</param>
45 /// <param name="policy">Policy of the resource.</param>
46 /// <param name="attribs">Optional attributes of the resource.</param>
47 /// <feature>http://tizen.org/feature/iot.ocf</feature>
49 /// IoTConnectivityServerManager.Initialize() should be called to initialize.
51 /// <seealso cref="ResourceTypes"/>
52 /// <seealso cref="ResourcePolicy"/>
53 /// <seealso cref="Attributes"/>
55 /// List<string> list = new List<string>() { "org.tizen.light" };
56 /// Attributes attributes = new Attributes() {
59 /// LiteResource res = new LiteResource("/light/1", new ResourceTypes(list), ResourcePolicy.Discoverable, attributes);
61 public LiteResource(string uri, ResourceTypes types, ResourcePolicy policy, Attributes attribs = null)
62 : base(uri, types, new ResourceInterfaces(new string[] { ResourceInterfaces.DefaultInterface }), policy)
68 /// Gets or sets the attributes of the lite resource.
70 /// <since_tizen> 3 </since_tizen>
71 /// <value>The attributes of the lite resource.</value>
73 /// List<string> list = new List<string>() { "org.tizen.light" };
74 /// LiteResource res = new LiteResource("/light/1", new ResourceTypes(list), ResourcePolicy.Discoverable);
75 /// Attributes attributes = new Attributes() {
78 /// res.Attributes = newAttributes;
79 /// foreach (KeyValuePair<string, object> pair in res.Attributes)
81 /// Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
84 public Attributes Attributes { get; set; }
87 /// Decides whether to accept or reject a post request.
89 /// <since_tizen> 3 </since_tizen>
91 /// Child classes of this class can override this method to accept or reject post request.
93 /// <param name="attribs">The new attributes of the lite resource.</param>
94 /// <returns>true to accept post request, false to reject it.</returns>
96 /// public class MyLightResource : LiteResource
98 /// protected override bool OnPost(Attributes attributes)
100 /// object newAttributes;
101 /// attributes.TryGetValue("LIGHT_ATTRIBUTE", out newAttributes);
102 /// if((int)newAttributes == 1)
108 protected virtual bool OnPost(Attributes attribs)
114 /// This is called when the client performs get operation on this resource.
116 /// <since_tizen> 3 </since_tizen>
117 /// <param name="request">A request from client.</param>
118 /// <returns>A response having the representation and the result.</returns>
119 [EditorBrowsable(EditorBrowsableState.Never)]
120 protected sealed override Response OnGet(Request request)
122 Representation representation = new Representation()
125 Interface = Interfaces,
127 Attributes = Attributes
130 Response response = new Response()
132 Representation = representation,
133 Result = ResponseCode.Ok
140 /// This is called when the client performs put operation on this resource.
142 /// <since_tizen> 3 </since_tizen>
143 /// <param name="request">A request from client.</param>
144 /// <returns>A response.</returns>
145 [EditorBrowsable(EditorBrowsableState.Never)]
146 protected sealed override Response OnPut(Request request)
148 Response response = new Response();
149 response.Result = ResponseCode.Forbidden;
154 /// This is called when the client performs post operation on this resource.
156 /// <since_tizen> 3 </since_tizen>
157 /// <param name="request">A request from client.</param>
158 /// <returns>A response having the representation and the result.</returns>
159 [EditorBrowsable(EditorBrowsableState.Never)]
160 protected sealed override Response OnPost(Request request)
162 if (OnPost(request.Representation.Attributes))
164 Attributes = request.Representation.Attributes;
165 Representation representation = new Representation() {
167 Interface = Interfaces,
169 Attributes = Attributes
172 Response response = new Response() {
173 Representation = representation,
174 Result = ResponseCode.Ok
177 Notify(representation, QualityOfService.High);
181 return new Response()
183 Result = ResponseCode.Error
188 /// This is called when the client performs delete operation on this resource.
190 /// <since_tizen> 3 </since_tizen>
191 /// <param name="request">A request from client.</param>
192 /// <returns>A response.</returns>
193 [EditorBrowsable(EditorBrowsableState.Never)]
194 protected sealed override Response OnDelete(Request request)
196 Response response = new Response();
197 response.Result = ResponseCode.Forbidden;
202 /// Called on the observing event.
204 /// <since_tizen> 3 </since_tizen>
205 /// <param name="request">A request from client.</param>
206 /// <param name="observeType">Observer type.</param>
207 /// <param name="observeId">Observe identifier.</param>
208 /// <returns>Returns true.</returns>
209 [EditorBrowsable(EditorBrowsableState.Never)]
210 protected sealed override bool OnObserving(Request request, ObserveType observeType, int observeId)