SmartCradle v1
Baby smart cradle
database.c
Go to the documentation of this file.
1
10#include "../inc/database.h"
11
12PyObject *pName, *pModule, *pDict;
13PyObject *pFuncSend_notification_flag, *pFuncGet_live_flag, *pFuncGet_swing_flag, *pFuncSend_temp_hum, *pFuncSend_swing_flag,*pResult;
14
15
16int send_temp_hum( float temp, float hum)
17{
18 //Call the function with arguments -> i = one integer , "ii" = two integers , "d" -> double mkvalue-style format string
19 pResult = PyObject_CallFunction(pFuncSend_temp_hum, "ff", temp, hum);
20 if(pResult == NULL)
21 {
22 PyErr_Print();
23 return 0;
24 }
25
26 //Cleanup
27 Py_XDECREF(pResult);
28
29 return 1;
30}
31
33{
34 //Call the function with arguments -> i = one integer , "ii" = two integers , "d" -> double mkvalue-style format string
35 pResult = PyObject_CallFunction(pFuncGet_swing_flag, NULL);
36 if(pResult == NULL)
37 {
38 PyErr_Print();
39 return 0;
40 }
41
42 //Truncates the result to int since the Python function results
43 //Translates from Python to C double
44 int aux = (int)PyLong_AsLong(pResult);
45
46 if (aux < 0)
47 {
48 //Cleanup
49 Py_XDECREF(pResult);
50 printf("ERROR GETTING FLAG");
51 return -ERROR;
52 }
53
54 //Cleanup
55 Py_XDECREF(pResult);
56
57 return aux;
58}
59
61{
62 //Call the function with arguments -> i = one integer , "ii" = two integers , "d" -> double mkvalue-style format string
63 pResult = PyObject_CallFunction(pFuncGet_live_flag, NULL);
64 if(pResult == NULL)
65 {
66 PyErr_Print();
67 return 0;
68 }
69 PyErr_Print();
70
71 //Truncates the result to int since the Python function results
72 //Translates from Python to C double
73 int aux = (int)PyLong_AsLong(pResult);
74
75 if (aux < 0)
76 {
77 //Cleanup
78 Py_XDECREF(pResult);
79 printf("ERROR GETTING FLAG");
80 return -ERROR;
81 }
82
83 //Cleanup
84 Py_XDECREF(pResult);
85
86 return aux;
87}
88
89int send_notification_flag( int notification_flag)
90{
91 //Call the function with arguments -> i = one integer , "ii" = two integers , "d" -> double mkvalue-style format string
92 pResult = PyObject_CallFunction(pFuncSend_notification_flag, "i", notification_flag);
93 if(pResult == NULL)
94 {
95 PyErr_Print();
96 return 0;
97 }
98
99 //Cleanup
100 Py_XDECREF(pResult);
101
102 return 1;
103}
104
105int send_swing_flag(int swing_flag)
106{
107 //Call the function with arguments -> i = one integer , "ii" = two integers , "d" -> double mkvalue-style format string
108 pResult = PyObject_CallFunction(pFuncSend_swing_flag, "i", swing_flag);
109 if(pResult == NULL)
110 {
111 PyErr_Print();
112 return 0;
113 }
114
115 //Cleanup
116 Py_XDECREF(pResult);
117
118 return 1;
119}
120
122{
123 pName = PyUnicode_FromString((char*)"database");
124
125 //Import the Python Script
126 pModule = PyImport_Import(pName);
127
128 if(pModule == NULL)
129 {
130 printf("No module with that name");
131 return -ERROR;
132 }
133
134 //Get the dictionary referent to the imported module
135 pDict = PyModule_GetDict(pModule);
136
137 Py_DECREF(pName);
138 Py_DECREF(pModule);
139
140 pFuncSend_notification_flag = PyDict_GetItemString(pDict, (char*)"send_notification_flag");
141
142 //Check if its callable
143 if (!PyCallable_Check(pFuncSend_notification_flag))
144 {
145 PyErr_Print();
146 return 0;
147 }
148
149 pFuncSend_temp_hum = PyDict_GetItemString(pDict, (char*) "send_temp_hum");
150
151 //Check if its callable
152 if (!PyCallable_Check(pFuncSend_temp_hum))
153 {
154 PyErr_Print();
155 return 0;
156 }
157
158 pFuncGet_live_flag = PyDict_GetItemString(pDict, (char*) "get_live_flag");
159
160 //Check if its callable
161 if (!PyCallable_Check(pFuncGet_live_flag))
162 {
163 PyErr_Print();
164 return 0;
165 }
166
167 pFuncGet_swing_flag = PyDict_GetItemString(pDict, (char*) "get_swing_flag");
168
169 //Check if its callable
170 if (!PyCallable_Check(pFuncGet_swing_flag))
171 {
172 PyErr_Print();
173 return 0;
174 }
175
176 pFuncSend_swing_flag = PyDict_GetItemString(pDict, (char*) "send_swing_flag");
177
178 //Check if its callable
179 if (!PyCallable_Check(pFuncSend_swing_flag))
180 {
181 PyErr_Print();
182 return 0;
183 }
184
185 return 1;
186}
187
int send_swing_flag(int swing_flag)
Update swing flag in database.
Definition: database.c:105
int send_notification_flag(int notification_flag)
Update notification flag in database.
Definition: database.c:89
int send_temp_hum(float temp, float hum)
Update temperature and humidity in database.
Definition: database.c:16
int initDatabase()
Initializes all the functions.
Definition: database.c:121
int get_swing_flag()
Get the swing flag from database.
Definition: database.c:32
int get_live_flag()
Get the live flag from database.
Definition: database.c:60