tsSample.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include <rtthread.h>
  2. #include <flashdb.h>
  3. #define FDB_LOG_TAG "[tsdb_sample]"
  4. static uint32_t boot_count = 0;
  5. static time_t boot_time[10] = {0, 1, 2, 3};
  6. static struct fdb_kvdb kvdb = { 0 };
  7. static struct fdb_default_kv_node default_kv_table[] = {
  8. {"username", "armink ", 8}, /* string KV */
  9. {"password", "123456", 0}, /* string KV */
  10. {"boot_count", &boot_count, sizeof(boot_count)}, /* int type KV */
  11. {"boot_time", &boot_time, sizeof(boot_time)}, /* int array type KV */
  12. };
  13. struct fdb_tsdb tsdb = { 0 };
  14. static int counts = 0;
  15. static struct rt_semaphore tsdb_lock;
  16. static void lock(fdb_db_t db)
  17. {
  18. // __disable_irq();
  19. rt_sem_take(&tsdb_lock, RT_WAITING_FOREVER);
  20. }
  21. static void unlock(fdb_db_t db)
  22. {
  23. // __enable_irq();
  24. rt_sem_release(&tsdb_lock);
  25. }
  26. static fdb_time_t get_time(void)
  27. {
  28. /* Using the counts instead of timestamp.
  29. * Please change this function to return RTC time.
  30. */
  31. return counts++;
  32. }
  33. #define THREAD_PRIORITY 3
  34. #define THREAD_STACK_SIZE 800
  35. #define THREAD_TIMESLICE 5
  36. static rt_thread_t tid1 = RT_NULL;
  37. struct env_status {
  38. int temp;
  39. int humi;
  40. int xxx;
  41. int xxx2;
  42. int xxx3;
  43. int xxx4;
  44. int xxx5;
  45. int xxx6;
  46. int xxx7;
  47. };
  48. static bool query_cb(fdb_tsl_t tsl, void *arg)
  49. {
  50. struct fdb_blob blob;
  51. struct env_status status;
  52. fdb_tsdb_t db = arg;
  53. fdb_blob_read((fdb_db_t) db, fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &status, sizeof(status))));
  54. FDB_INFO("[query_cb] queried a TSL: time: %ld, temp: %d, humi: %d\n", tsl->time, status.temp, status.humi);
  55. return false;
  56. }
  57. static bool query_by_time_cb(fdb_tsl_t tsl, void *arg)
  58. {
  59. struct fdb_blob blob;
  60. struct env_status status;
  61. fdb_tsdb_t db = arg;
  62. fdb_blob_read((fdb_db_t) db, fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &status, sizeof(status))));
  63. FDB_INFO("[query_by_time_cb] queried a TSL: time: %ld, temp: %d, humi: %d status:%d\n", tsl->time, status.temp, status.humi,tsl->status);
  64. return false;
  65. }
  66. static bool set_status_cb(fdb_tsl_t tsl, void *arg)
  67. {
  68. fdb_tsdb_t db = arg;
  69. // FDB_INFO("set the TSL (time %ld) status from %d to %d\n", tsl->time, tsl->status, FDB_TSL_DELETED);
  70. fdb_tsl_set_status(db, tsl, FDB_TSL_DELETED);
  71. return false;
  72. }
  73. void tsdb_sample(fdb_tsdb_t tsdb)
  74. {
  75. struct fdb_blob blob;
  76. fdb_tsl_clean(tsdb);
  77. FDB_INFO("==================== tsdb_sample ====================\n");
  78. { /* APPEND new TSL (time series log) */
  79. struct env_status status;
  80. for(int i=0;i<400;i++)
  81. {
  82. /* append new log to TSDB */
  83. status.temp = i;
  84. status.humi = 85;
  85. fdb_tsl_append(tsdb, fdb_blob_make(&blob, &status, sizeof(status)));
  86. // FDB_INFO("append the new status.temp (%d) and status.humi (%d) (%d)\n", status.temp, status.humi, i);
  87. rt_thread_mdelay(10);
  88. }
  89. }
  90. { /* QUERY the TSDB by time */
  91. /* prepare query time (from 1970-01-01 00:00:00 to 2020-05-05 00:00:00) */
  92. struct tm tm_from = { .tm_year = 1970 - 1900, .tm_mon = 0, .tm_mday = 1, .tm_hour = 0, .tm_min = 0, .tm_sec = 0 };
  93. struct tm tm_to = { .tm_year = 2020 - 1900, .tm_mon = 4, .tm_mday = 5, .tm_hour = 0, .tm_min = 0, .tm_sec = 0 };
  94. time_t from_time = mktime(&tm_from), to_time = mktime(&tm_to);
  95. size_t count;
  96. /* query all TSL in TSDB by time */
  97. // fdb_tsl_iter_by_time(tsdb, from_time, to_time, query_by_time_cb, tsdb);
  98. /* query all FDB_TSL_WRITE status TSL's count in TSDB by time */
  99. count = fdb_tsl_query_count(tsdb, from_time, to_time, FDB_TSL_WRITE);
  100. FDB_INFO("query count is: %u\n", count);
  101. }
  102. fdb_tsl_iter(tsdb, set_status_cb, tsdb);
  103. { /* QUERY the TSDB by time */
  104. /* prepare query time (from 1970-01-01 00:00:00 to 2020-05-05 00:00:00) */
  105. struct tm tm_from = { .tm_year = 1970 - 1900, .tm_mon = 0, .tm_mday = 1, .tm_hour = 0, .tm_min = 0, .tm_sec = 0 };
  106. struct tm tm_to = { .tm_year = 2020 - 1900, .tm_mon = 4, .tm_mday = 5, .tm_hour = 0, .tm_min = 0, .tm_sec = 0 };
  107. time_t from_time = mktime(&tm_from), to_time = mktime(&tm_to);
  108. size_t count;
  109. /* query all TSL in TSDB by time */
  110. // fdb_tsl_iter_by_time(tsdb, from_time, to_time, query_by_time_cb, tsdb);
  111. /* query all FDB_TSL_WRITE status TSL's count in TSDB by time */
  112. count = fdb_tsl_query_count(tsdb, from_time, to_time, FDB_TSL_WRITE);
  113. FDB_INFO("query count is: %u\n", count);
  114. }
  115. { /* APPEND new TSL (time series log) */
  116. struct env_status status;
  117. for(int i=0;i<400;i++)
  118. {
  119. /* append new log to TSDB */
  120. status.temp = i;
  121. status.humi = 85;
  122. fdb_tsl_append(tsdb, fdb_blob_make(&blob, &status, sizeof(status)));
  123. // FDB_INFO("append the new status.temp (%d) and status.humi (%d) (%d)\n", status.temp, status.humi, i);
  124. rt_thread_mdelay(10);
  125. }
  126. }
  127. { /* QUERY the TSDB by time */
  128. /* prepare query time (from 1970-01-01 00:00:00 to 2020-05-05 00:00:00) */
  129. struct tm tm_from = { .tm_year = 1970 - 1900, .tm_mon = 0, .tm_mday = 1, .tm_hour = 0, .tm_min = 0, .tm_sec = 0 };
  130. struct tm tm_to = { .tm_year = 2020 - 1900, .tm_mon = 4, .tm_mday = 5, .tm_hour = 0, .tm_min = 0, .tm_sec = 0 };
  131. time_t from_time = mktime(&tm_from), to_time = mktime(&tm_to);
  132. size_t count;
  133. /* query all TSL in TSDB by time */
  134. // fdb_tsl_iter_by_time(tsdb, from_time, to_time, query_by_time_cb, tsdb);
  135. /* query all FDB_TSL_WRITE status TSL's count in TSDB by time */
  136. count = fdb_tsl_query_count(tsdb, from_time, to_time, FDB_TSL_WRITE);
  137. FDB_INFO("query count is: %u\n", count);
  138. }
  139. //
  140. // { /* QUERY the TSDB by time */
  141. // /* prepare query time (from 1970-01-01 00:00:00 to 2020-05-05 00:00:00) */
  142. // struct tm tm_from = { .tm_year = 1970 - 1900, .tm_mon = 0, .tm_mday = 1, .tm_hour = 0, .tm_min = 0, .tm_sec = 0 };
  143. // struct tm tm_to = { .tm_year = 2020 - 1900, .tm_mon = 4, .tm_mday = 5, .tm_hour = 0, .tm_min = 0, .tm_sec = 0 };
  144. // time_t from_time = mktime(&tm_from), to_time = mktime(&tm_to);
  145. // size_t count;
  146. //
  147. // /* query all FDB_TSL_WRITE status TSL's count in TSDB by time */
  148. // // fdb_tsl_iter(tsdb, set_status_cb, tsdb);
  149. // count = fdb_tsl_query_count(tsdb, from_time, to_time, FDB_TSL_WRITE);
  150. // FDB_INFO("query count is: %u\n", count);/* query all TSL in TSDB by time */
  151. // fdb_tsl_iter_by_time(tsdb, from_time, to_time, query_by_time_cb, tsdb);
  152. // }
  153. FDB_INFO("===========================================================\n");
  154. }
  155. void kvdb_type_blob_sample(fdb_kvdb_t kvdb)
  156. {
  157. struct fdb_blob blob;
  158. FDB_INFO("==================== kvdb_type_blob_sample ====================\n");
  159. { /* CREATE new Key-Value */
  160. int temp_data = 36;
  161. /* It will create new KV node when "temp" KV not in database.
  162. * fdb_blob_make: It's a blob make function, and it will return the blob when make finish.
  163. */
  164. fdb_kv_set_blob(kvdb, "temp", fdb_blob_make(&blob, &temp_data, sizeof(temp_data)));
  165. FDB_INFO("create the 'temp' blob KV, value is: %d\n", temp_data);
  166. }
  167. { /* GET the KV value */
  168. int temp_data = 0;
  169. /* get the "temp" KV value */
  170. fdb_kv_get_blob(kvdb, "temp", fdb_blob_make(&blob, &temp_data, sizeof(temp_data)));
  171. /* the blob.saved.len is more than 0 when get the value successful */
  172. if (blob.saved.len > 0) {
  173. FDB_INFO("get the 'temp' value is: %d\n", temp_data);
  174. }
  175. }
  176. { /* CHANGE the KV value */
  177. int temp_data = 38;
  178. /* change the "temp" KV's value to 38 */
  179. fdb_kv_set_blob(kvdb, "temp", fdb_blob_make(&blob, &temp_data, sizeof(temp_data)));
  180. FDB_INFO("set 'temp' value to %d\n", temp_data);
  181. }
  182. { /* GET the KV value */
  183. int temp_data = 0;
  184. /* get the "temp" KV value */
  185. fdb_kv_get_blob(kvdb, "temp", fdb_blob_make(&blob, &temp_data, sizeof(temp_data)));
  186. /* the blob.saved.len is more than 0 when get the value successful */
  187. if (blob.saved.len > 0) {
  188. FDB_INFO("get the 'temp' value is: %d\n", temp_data);
  189. }
  190. } { /* GET the KV value */
  191. char temp_data[20] = { 0 };
  192. int x=0;
  193. x=fdb_kv_get_blob(kvdb, "username", fdb_blob_make(&blob, &temp_data, 10));
  194. if (x > 0) {
  195. for (int i=0;i<x;i++){
  196. FDB_PRINT("0x%x ", temp_data[i]);
  197. }
  198. for (int i=0;i<x;i++){
  199. FDB_PRINT("%c ", temp_data[i]);
  200. }
  201. FDB_PRINT("\n");
  202. }
  203. }
  204. { /* DELETE the KV by name */
  205. fdb_kv_del(kvdb, "temp");
  206. FDB_INFO("delete the 'temp' finish\n");
  207. }
  208. FDB_INFO("===========================================================\n");
  209. }
  210. /* 线程 1 的入口函数 */
  211. static void threadsample_entry(void *parameter)
  212. {
  213. fdb_err_t result;
  214. bool ROLLOVER = true;
  215. rt_sem_init(&tsdb_lock, "tsdb_lock", 1, RT_IPC_FLAG_PRIO);
  216. fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_SET_LOCK, lock);
  217. fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_SET_UNLOCK, unlock);
  218. result = fdb_tsdb_init(&tsdb, "test", "ts_pf", get_time, 128, NULL);
  219. if (result != FDB_NO_ERR) {
  220. return ;
  221. }
  222. fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_SET_ROLLOVER, &ROLLOVER);
  223. /* read last saved time for simulated timestamp */
  224. fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_GET_LAST_TIME, &counts);
  225. struct fdb_default_kv default_kv;
  226. default_kv.kvs = default_kv_table;
  227. default_kv.num = sizeof(default_kv_table) / sizeof(default_kv_table[0]);
  228. fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_LOCK, lock);
  229. fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_UNLOCK, unlock);
  230. result = fdb_kvdb_init(&kvdb, "env", "kv", &default_kv, NULL);
  231. if (result != FDB_NO_ERR) {
  232. return ;
  233. }
  234. kvdb_type_blob_sample(&kvdb);
  235. /* run TSDB sample */
  236. tsdb_sample(&tsdb);
  237. }
  238. void threadsample_init(void)
  239. {
  240. tid1 = rt_thread_create("threadsample",
  241. threadsample_entry, RT_NULL,
  242. THREAD_STACK_SIZE,
  243. THREAD_PRIORITY, THREAD_TIMESLICE);
  244. /* 如果获得线程控制块,启动这个线程 */
  245. if (tid1 != RT_NULL)
  246. rt_thread_startup(tid1);
  247. }