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 /// <para>Creates a lite resource, which can then be registered in server using <see cref="IoTConnectivityServerManager.RegisterResource(Resource)"/>.</para>
36 /// <para>When client requests some operations, it sends a response to client automatically.</para>
37 /// <para><paramref name="uri" /> length must be less than 128.</para>
39 /// <privilege>http://tizen.org/privilege/internet</privilege>
40 /// <privlevel>public</privlevel>
41 /// <param name="uri">The uri path of the lite resource.</param>
42 /// <param name="types">The type of the resource.</param>
43 /// <param name="policy">Policy of the resource.</param>
44 /// <param name="attribs">Optional attributes of the resource.</param>
45 /// <feature>http://tizen.org/feature/iot.ocf</feature>
47 /// IoTConnectivityServerManager.Initialize() should be called to initialize.
49 /// <seealso cref="ResourceTypes"/>
50 /// <seealso cref="ResourcePolicy"/>
51 /// <seealso cref="Attributes"/>
53 /// List<string> list = new List<string>() { "org.tizen.light" };
54 /// Attributes attributes = new Attributes() {
57 /// LiteResource res = new LiteResource("/light/1", new ResourceTypes(list), ResourcePolicy.Discoverable, attributes);
59 public LiteResource(string uri, ResourceTypes types, ResourcePolicy policy, Attributes attribs = null)
60 : base(uri, types, new ResourceInterfaces(new string[] { ResourceInterfaces.DefaultInterface }), policy)
66 /// Gets or sets the attributes of the lite resource.
68 /// <since_tizen> 3 </since_tizen>
69 /// <value>The attributes of the lite resource.</value>
71 /// List<string> list = new List<string>() { "org.tizen.light" };
72 /// LiteResource res = new LiteResource("/light/1", new ResourceTypes(list), ResourcePolicy.Discoverable);
73 /// Attributes attributes = new Attributes() {
76 /// res.Attributes = newAttributes;
77 /// foreach (KeyValuePair<string, object> pair in res.Attributes)
79 /// Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
82 public Attributes Attributes { get; set; }
85 /// Decides whether to accept or reject a post request.
87 /// <since_tizen> 3 </since_tizen>
89 /// Child classes of this class can override this method to accept or reject post request.
91 /// <param name="attribs">The new attributes of the lite resource.</param>
92 /// <returns>true to accept post request, false to reject it.</returns>
94 /// public class MyLightResource : LiteResource
96 /// protected override bool OnPost(Attributes attributes)
98 /// object newAttributes;
99 /// attributes.TryGetValue("LIGHT_ATTRIBUTE", out newAttributes);
100 /// if((int)newAttributes == 1)
106 protected virtual bool OnPost(Attributes attribs)
112 /// This is called when the client performs get operation on this resource.
114 /// <since_tizen> 3 </since_tizen>
115 /// <param name="request">A request from client.</param>
116 /// <returns>A response having the representation and the result.</returns>
117 [EditorBrowsable(EditorBrowsableState.Never)]
118 protected sealed override Response OnGet(Request request)
120 Representation representation = new Representation()
123 Interface = Interfaces,
125 Attributes = Attributes
128 Response response = new Response()
130 Representation = representation,
131 Result = ResponseCode.Ok
138 /// This is called when the client performs put operation on this resource.
140 /// <since_tizen> 3 </since_tizen>
141 /// <param name="request">A request from client.</param>
142 /// <returns>A response.</returns>
143 [EditorBrowsable(EditorBrowsableState.Never)]
144 protected sealed override Response OnPut(Request request)
146 Response response = new Response();
147 response.Result = ResponseCode.Forbidden;
152 /// This is called when the client performs post operation on this resource.
154 /// <since_tizen> 3 </since_tizen>
155 /// <param name="request">A request from client.</param>
156 /// <returns>A response having the representation and the result.</returns>
157 [EditorBrowsable(EditorBrowsableState.Never)]
158 protected sealed override Response OnPost(Request request)
160 if (OnPost(request.Representation.Attributes))
162 Attributes = request.Representation.Attributes;
163 Representation representation = new Representation() {
165 Interface = Interfaces,
167 Attributes = Attributes
170 Response response = new Response() {
171 Representation = representation,
172 Result = ResponseCode.Ok
175 Notify(representation, QualityOfService.High);
179 return new Response()
181 Result = ResponseCode.Error
186 /// This is called when the client performs delete operation on this resource.
188 /// <since_tizen> 3 </since_tizen>
189 /// <param name="request">A request from client.</param>
190 /// <returns>A response.</returns>
191 [EditorBrowsable(EditorBrowsableState.Never)]
192 protected sealed override Response OnDelete(Request request)
194 Response response = new Response();
195 response.Result = ResponseCode.Forbidden;
200 /// Called on the observing event.
202 /// <since_tizen> 3 </since_tizen>
203 /// <param name="request">A request from client.</param>
204 /// <param name="observeType">Observer type.</param>
205 /// <param name="observeId">Observe identifier.</param>
206 /// <returns>Returns true.</returns>
207 [EditorBrowsable(EditorBrowsableState.Never)]
208 protected sealed override bool OnObserving(Request request, ObserveType observeType, int observeId)