Updated Makefiles and CMakeLists.txt to point to resource, not oic-resource
[platform/upstream/iotivity.git] / examples / ocicuc / light_resource.hpp
1 #ifndef __LIGHT_RESOURCE_HPP
2  #define __LIGHT_RESOURCE_HPP
3
4 #include <map>
5 #include <atomic>
6 #include <thread>
7 #include <string>
8 #include <ostream>
9 #include <sstream>
10 #include <iostream>
11 #include <functional>
12
13 #include "OCApi.h"
14 #include "OCResource.h"
15 #include "OCPlatform.h"
16
17 /* An example of a server-side resource: */
18 namespace Intel { namespace OCDemo {
19
20 using namespace OC;
21 using namespace std;
22
23 /// This class represents a single resource named 'lightResource'. This resource has
24 /// two simple properties named 'state' and 'power'
25 class LightResource
26 {
27  public:
28     bool m_state;       // off or on?
29     int m_power;        // power level
30     OCRepresentation m_rep;
31
32     private:
33     atomic<bool> m_observation; // are we under observation?
34
35     private:
36     static atomic<bool> shutdown_flag;
37     static thread observe_thread;
38
39     private:
40     OCResourceHandle m_resourceHandle;
41
42     public:
43     LightResource()
44      : m_state(false),
45        m_power(0),
46        m_observation(false)
47     {}
48
49     ~LightResource()
50     {
51         shutdown_flag = true;
52
53         if(observe_thread.joinable())
54          observe_thread.join();
55     }
56
57     private:
58     inline std::string make_URI(const unsigned int resource_number)
59     {
60         std::string uri = std::string("/a/light") + "_" + std::to_string(resource_number);
61         m_rep.setUri(uri);
62         return uri;
63     }
64
65     public:
66     // This function internally calls registerResource API.
67     void createResource(const unsigned int resource_number);
68     void unregisterResource();
69     OCResourceHandle getHandle() const { return m_resourceHandle; }
70
71     void setRepresentation(const OCRepresentation& rep);
72     OCRepresentation getRepresentation(void);
73
74     void addType(const std::string& type) const;
75     void addInterface(const std::string& interface) const;
76
77     private:
78     OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request,
79                                         std::shared_ptr<OCResourceResponse> response);
80
81     private:
82     void observe_function();
83
84     // Request handlers:
85     private:
86     void dispatch_request(const std::string& request_type, std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response);
87      void handle_get_request(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response);
88      void handle_put_request(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response);
89      void handle_post_request(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response);
90      void handle_delete_request(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response);
91      void handle_observe_event(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response);
92 };
93
94 }} // namespace Intel::OCDemo
95
96 #endif