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 public class LiteResource : Resource
28 /// The LiteResource constructor
31 /// Creates a lite resource which can then be registered in server using <see cref="IoTConnectivityServerManager.RegisterResource()"/>.\n
32 /// When client requests some operations, it send a response to client, automatically.\n
33 /// @a uri length must be less than 128.
36 /// http://tizen.org/privilege/internet
38 /// <param name="uri">The uri path of the lite resource</param>
39 /// <param name="types">The type of the resource</param>
40 /// <param name="policy">Policy of the resource</param>
41 /// <param name="attribs">Optional attributes of the resource</param>
43 /// IoTConnectivityServerManager.Initialize() should be called to initialize
45 /// <seealso cref="ResourceTypes"/>
46 /// <seealso cref="ResourcePolicy"/>
47 /// <seealso cref="Attributes"/>
49 /// List<string> list = new List<string>() { "org.tizen.light" };
50 /// Attributes attributes = new Attributes() {
53 /// LiteResource res = new LiteResource("/light/1", new ResourceTypes(list), ResourcePolicy.Discoverable, attributes);
55 public LiteResource(string uri, ResourceTypes types, ResourcePolicy policy, Attributes attribs = null)
56 : base(uri, types, new ResourceInterfaces(new string[] { ResourceInterfaces.DefaultInterface }), policy)
62 /// Gets or sets the attributes of the lite resource
65 /// List<string> list = new List<string>() { "org.tizen.light" };
66 /// LiteResource res = new LiteResource("/light/1", new ResourceTypes(list), ResourcePolicy.Discoverable);
67 /// Attributes attributes = new Attributes() {
70 /// res.Attributes = newAttributes;
71 /// foreach (KeyValuePair<string, object> pair in res.Attributes)
73 /// Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
76 public Attributes Attributes { get; set; }
79 /// Decides whether to accept or reject a post request.
82 /// Child classes of this class can override this method to accept or reject post request.
84 /// <param name="attribs">The new attributes of the lite resource</param>
85 /// <returns>true to accept post request, false to reject it</returns>
87 /// public class MyLightResource : LiteResource
89 /// protected override bool OnPost(Attributes attributes)
91 /// object newAttributes;
92 /// attributes.TryGetValue("LIGHT_ATTRIBUTE", out newAttributes);
93 /// if((int)newAttributes == 1)
99 protected virtual bool OnPost(Attributes attribs)
104 // The code block untill @endcond should not appear in doxygen spec.
106 protected sealed override Response OnGet(Request request)
108 Representation representation = new Representation()
111 Interface = Interfaces,
113 Attributes = Attributes
116 Response response = new Response()
118 Representation = representation,
119 Result = ResponseCode.Ok
125 protected sealed override Response OnPut(Request request)
127 Response response = new Response();
128 response.Result = ResponseCode.Forbidden;
132 protected sealed override Response OnPost(Request request)
134 if (OnPost(request.Representation.Attributes))
136 Attributes = request.Representation.Attributes;
137 Representation representation = new Representation() {
139 Interface = Interfaces,
141 Attributes = Attributes
144 Response response = new Response() {
145 Representation = representation,
146 Result = ResponseCode.Ok
149 Notify(representation, QualityOfService.High);
153 return new Response()
155 Result = ResponseCode.Error
159 protected sealed override Response OnDelete(Request request)
161 Response response = new Response();
162 response.Result = ResponseCode.Forbidden;
166 protected sealed override bool OnObserving(Request request, ObserveType observeType, int observeId)