[IoTConnectivity] Added Base implementation
[platform/core/csapi/iotcon.git] / Tizen.Network.IoTConnectivity / Tizen.Network.IoTConnectivity / LiteResource.cs
1 /// Copyright 2016 by Samsung Electronics, Inc.,
2 ///
3 /// This software is the confidential and proprietary information
4 /// of Samsung Electronics, Inc. ("Confidential Information"). You
5 /// shall not disclose such Confidential Information and shall use
6 /// it only in accordance with the terms of the license agreement
7 /// you entered into with Samsung.
8
9 using System.Collections.Generic;
10
11 namespace Tizen.Network.IoTConnectivity
12 {
13     public class LiteResource : Resource
14     {
15         /// <summary>
16         /// Constructor
17         /// </summary>
18         /// <param name="uri">The uri path of the lite resource</param>
19         /// <param name="types">Resource type</param>
20         /// <param name="policy">Policy of the resource</param>
21         /// <param name="state">Optional State of the resource</param>
22         public LiteResource(string uri, ResourceTypes types, ResourcePolicy policy, State state = null)
23             : base(uri, types, new ResourceInterfaces(new string[] { ResourceInterfaces.DefaultInterface }), policy)
24         {
25             State = state;
26         }
27
28         /// <summary>
29         /// The state of the lite resource
30         /// </summary>
31         public State State { get; set; }
32
33         /// <summary>
34         /// The method to accept post request
35         /// </summary>
36         /// <param name="state">The new state of the lite resource</param>
37         /// <returns>true to accept post request, false to reject it</returns>
38         public virtual bool OnPost(State state)
39         {
40             return true;
41         }
42
43         /// <summary>
44         /// Called on the get event.
45         /// </summary>
46         /// <param name="request">Request.</param>
47         public sealed override Response OnGet(Request request)
48         {
49             Representation representation = new Representation()
50             {
51                 UriPath = UriPath,
52                 Interface = Interfaces,
53                 Type = Types,
54                 State = State
55             };
56
57             Response response = new Response()
58             {
59                 Representation = representation,
60                 Result = ResponseCode.Ok
61             };
62
63             return response;
64         }
65
66         /// <summary>
67         /// Called on the put event.
68         /// </summary>
69         /// <param name="request">Request.</param>
70         public sealed override Response OnPut(Request request)
71         {
72             Response response = new Response();
73             response.Result = ResponseCode.Forbidden;
74             return response;
75         }
76
77         /// <summary>
78         /// Called on the post event.
79         /// </summary>
80         /// <param name="request">Request.</param>
81         public sealed override Response OnPost(Request request)
82         {
83             if (OnPost(request.Representation.State))
84             {
85                 State = request.Representation.State;
86                 Representation representation = new Representation() {
87                     UriPath = UriPath,
88                     Interface = Interfaces,
89                     Type = Types,
90                     State = State
91                 };
92
93                 Response response = new Response() {
94                     Representation = representation,
95                     Result = ResponseCode.Ok
96                 };
97
98                 Notify(representation, QualityOfService.High);
99                 return response;
100             }
101
102             return new Response()
103             {
104                 Result = ResponseCode.Error
105             };
106         }
107
108         /// <summary>
109         /// Called on the delete event.
110         /// </summary>
111         /// <param name="request">Request.</param>
112         public sealed override Response OnDelete(Request request)
113         {
114             Response response = new Response();
115             response.Result = ResponseCode.Forbidden;
116             return response;
117         }
118
119         /// <summary>
120         /// Called on the observing event.
121         /// </summary>
122         /// <param name="request">Request.</param>
123         /// <param name="observerType">Observer type</param>
124         /// <param name="observeId">Observe identifier.</param>
125         public sealed override bool OnObserving(Request request, ObserveType observeType, int observeId)
126         {
127             return true;
128         }
129     }
130 }