/*
* 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.ComponentModel;
namespace Tizen.Network.IoTConnectivity
{
///
/// This class represents a lite resource.
/// It provides APIs to encapsulate resources.
/// This class is accessed by using a constructor to create a new instance of this object.
///
/// 3
public class LiteResource : Resource
{
///
/// The LiteResource constructor.
///
/// 3
///
/// Creates a lite resource, which can then be registered in server using .
/// When client requests some operations, it sends a response to client automatically.
/// length must be less than 128.
///
/// http://tizen.org/privilege/internet
/// public
/// The uri path of the lite resource.
/// The type of the resource.
/// Policy of the resource.
/// Optional attributes of the resource.
/// http://tizen.org/feature/iot.ocf
///
/// IoTConnectivityServerManager.Initialize() should be called to initialize.
///
///
///
///
/// list = new List() { "org.tizen.light" };
/// Attributes attributes = new Attributes() {
/// { "state", "ON" }
/// };
/// LiteResource res = new LiteResource("/light/1", new ResourceTypes(list), ResourcePolicy.Discoverable, attributes);
/// ]]>
public LiteResource(string uri, ResourceTypes types, ResourcePolicy policy, Attributes attribs = null)
: base(uri, types, new ResourceInterfaces(new string[] { ResourceInterfaces.DefaultInterface }), policy)
{
Attributes = attribs;
}
///
/// Gets or sets the attributes of the lite resource.
///
/// 3
/// The attributes of the lite resource.
/// list = new List() { "org.tizen.light" };
/// LiteResource res = new LiteResource("/light/1", new ResourceTypes(list), ResourcePolicy.Discoverable);
/// Attributes attributes = new Attributes() {
/// { "state", "ON" }
/// };
/// res.Attributes = newAttributes;
/// foreach (KeyValuePair pair in res.Attributes)
/// {
/// Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
/// }
/// ]]>
public Attributes Attributes { get; set; }
///
/// Decides whether to accept or reject a post request.
///
/// 3
///
/// Child classes of this class can override this method to accept or reject post request.
///
/// The new attributes of the lite resource.
/// true to accept post request, false to reject it.
///
/// public class MyLightResource : LiteResource
/// {
/// protected override bool OnPost(Attributes attributes)
/// {
/// object newAttributes;
/// attributes.TryGetValue("LIGHT_ATTRIBUTE", out newAttributes);
/// if((int)newAttributes == 1)
/// return true;
/// return false;
/// }
/// }
///
protected virtual bool OnPost(Attributes attribs)
{
return true;
}
///
/// This is called when the client performs get operation on this resource.
///
/// 3
/// A request from client.
/// A response having the representation and the result.
[EditorBrowsable(EditorBrowsableState.Never)]
protected sealed override Response OnGet(Request request)
{
Representation representation = new Representation()
{
UriPath = UriPath,
Interface = Interfaces,
Type = Types,
Attributes = Attributes
};
Response response = new Response()
{
Representation = representation,
Result = ResponseCode.Ok
};
return response;
}
///
/// This is called when the client performs put operation on this resource.
///
/// 3
/// A request from client.
/// A response.
[EditorBrowsable(EditorBrowsableState.Never)]
protected sealed override Response OnPut(Request request)
{
Response response = new Response();
response.Result = ResponseCode.Forbidden;
return response;
}
///
/// This is called when the client performs post operation on this resource.
///
/// 3
/// A request from client.
/// A response having the representation and the result.
[EditorBrowsable(EditorBrowsableState.Never)]
protected sealed override Response OnPost(Request request)
{
if (OnPost(request.Representation.Attributes))
{
Attributes = request.Representation.Attributes;
Representation representation = new Representation() {
UriPath = UriPath,
Interface = Interfaces,
Type = Types,
Attributes = Attributes
};
Response response = new Response() {
Representation = representation,
Result = ResponseCode.Ok
};
Notify(representation, QualityOfService.High);
return response;
}
return new Response()
{
Result = ResponseCode.Error
};
}
///
/// This is called when the client performs delete operation on this resource.
///
/// 3
/// A request from client.
/// A response.
[EditorBrowsable(EditorBrowsableState.Never)]
protected sealed override Response OnDelete(Request request)
{
Response response = new Response();
response.Result = ResponseCode.Forbidden;
return response;
}
///
/// Called on the observing event.
///
/// 3
/// A request from client.
/// Observer type.
/// Observe identifier.
/// Returns true.
[EditorBrowsable(EditorBrowsableState.Never)]
protected sealed override bool OnObserving(Request request, ObserveType observeType, int observeId)
{
return true;
}
}
}