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 namespace Tizen.Network.IoTConnectivity
21 /// This class represents a lite resource.
22 /// It provides APIs to encapsulate resources.
23 /// This class is accessed by using a constructor to create a new instance of this object.
25 /// <since_tizen> 3 </since_tizen>
26 public class LiteResource : Resource
29 /// The LiteResource constructor.
31 /// <since_tizen> 3 </since_tizen>
33 /// Creates a lite resource, which can then be registered in server using <see cref="IoTConnectivityServerManager.RegisterResource()"/>.\n
34 /// When client requests some operations, it sends a response to client automatically.\n
35 /// @a uri length must be less than 128.
38 /// http://tizen.org/privilege/internet
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)
111 // The code block untill @endcond should not appear in doxygen spec.
113 protected sealed override Response OnGet(Request request)
115 Representation representation = new Representation()
118 Interface = Interfaces,
120 Attributes = Attributes
123 Response response = new Response()
125 Representation = representation,
126 Result = ResponseCode.Ok
132 protected sealed override Response OnPut(Request request)
134 Response response = new Response();
135 response.Result = ResponseCode.Forbidden;
139 protected sealed override Response OnPost(Request request)
141 if (OnPost(request.Representation.Attributes))
143 Attributes = request.Representation.Attributes;
144 Representation representation = new Representation() {
146 Interface = Interfaces,
148 Attributes = Attributes
151 Response response = new Response() {
152 Representation = representation,
153 Result = ResponseCode.Ok
156 Notify(representation, QualityOfService.High);
160 return new Response()
162 Result = ResponseCode.Error
166 protected sealed override Response OnDelete(Request request)
168 Response response = new Response();
169 response.Result = ResponseCode.Forbidden;
173 protected sealed override bool OnObserving(Request request, ObserveType observeType, int observeId)