90712e2c5b837df8f07b9cb01298a6cfdb7b0a00
[apps/native/co2-meter.git] / src / capability / capability_thermostatcoolingsetpoint.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "st_things.h"
18 #include "log.h"
19
20 static const char* PROP_TEMPERATURE = "temperature";
21 static const char* PROP_RANGE = "range";
22 static const char* PROP_UNITS = "units";
23
24 static double g_temperature = 0;
25 static double g_range[2] = { 0, 5000. };
26 static size_t g_length = 2;
27 static char g_unit[4] = "mV";
28
29 extern void resource_init_co2_sensor(void);
30 extern int resource_get_co2_sensor_parameter(void);
31 extern void resource_set_sensor_parameter(int zero_volts);
32
33 bool handle_get_request_on_resource_capability_thermostatcoolingsetpoint(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep)
34 {
35         DBG("Received a GET request on %s\n", req_msg->resource_uri);
36
37         if (req_msg->has_property_key(req_msg, PROP_TEMPERATURE)) {
38                 DBG("requested temperature");
39                 if (0 == g_temperature)
40                         g_temperature = (double)resource_get_co2_sensor_parameter();
41                 resp_rep->set_double_value(resp_rep, PROP_TEMPERATURE, g_temperature);
42         }
43         if (req_msg->has_property_key(req_msg, PROP_RANGE)) {
44                 //DBG("requested range");
45                 resp_rep->set_double_array_value(resp_rep, PROP_RANGE, (double*)&g_range, g_length);
46         }
47         if (req_msg->has_property_key(req_msg, PROP_UNITS)) {
48                 //DBG("requested units");
49                 resp_rep->set_str_value(resp_rep, PROP_UNITS, g_unit);
50         }
51         return true;
52 }
53
54 bool handle_set_request_on_resource_capability_thermostatcoolingsetpoint(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep)
55 {
56         DBG("Received a SET request on %s\n", req_msg->resource_uri);
57
58         double dvalue = 0;
59         if (req_msg->rep->get_double_value(req_msg->rep, PROP_TEMPERATURE, &dvalue)) {
60                 g_temperature = dvalue;
61                 resource_set_sensor_parameter((int32_t)g_temperature);
62                 resource_init_co2_sensor();
63                 resp_rep->set_double_value(resp_rep, PROP_TEMPERATURE, dvalue);
64                 DBG("set requested: %.f\n", g_temperature);
65         }
66
67         char *str_value = NULL;
68         if (req_msg->rep->get_str_value(req_msg->rep, PROP_UNITS, &str_value)) {
69                 resp_rep->set_str_value(resp_rep, PROP_UNITS, g_unit);
70                 free(str_value);
71         }
72
73         double davalue[2] = { 0, };
74         size_t dlength = 0;
75         if (req_msg->rep->get_double_array_value(req_msg->rep, PROP_RANGE, (double**)&davalue, &dlength )) {
76                 resp_rep->set_double_array_value(resp_rep, PROP_RANGE, g_range, g_length);
77         }
78         st_things_notify_observers(req_msg->resource_uri);
79
80         return true;
81 }