Skip to content

Commit c31cdd9

Browse files
berry time and openweathermap access with self tests
* test * owm * fix * test * test
1 parent 2086cb8 commit c31cdd9

File tree

5 files changed

+137
-4
lines changed

5 files changed

+137
-4
lines changed

src/cmnds/cmd_berry.c

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
#if ENABLE_OBK_BERRY
77

8+
#include "../driver/drv_openWeatherMap.h"
89
#include "../driver/drv_ir.h"
10+
#include "../driver/drv_ntp.h"
911
#include "../driver/drv_local.h"
1012
#include "../driver/drv_public.h"
1113
#include "../driver/drv_uart.h"
@@ -309,6 +311,14 @@ int be_setTimeout(bvm *vm) {
309311
be_setTimeoutInternal(vm, 0);
310312
return 1;
311313
}
314+
#if ENABLE_DRIVER_OPENWEATHERMAP
315+
int be_getOpenWeatherReply(bvm *vm) {
316+
const char *data = Weather_GetReply();
317+
be_pushstring(vm, data);
318+
be_return(vm);
319+
return 1;
320+
}
321+
#endif
312322
int be_get(bvm *vm) {
313323
char tmpA[64];
314324
int top = be_top(vm);
@@ -334,6 +344,49 @@ int be_setInterval(bvm *vm) {
334344
be_setTimeoutInternal(vm, -1);
335345
return 1;
336346
}
347+
int be_gmtime(bvm *vm) {
348+
struct tm *ltm;
349+
const char *fields;
350+
int i;
351+
352+
int top = be_top(vm);
353+
if (top == 1 && be_isstring(vm, 1)) {
354+
fields = be_tostring(vm, 1);
355+
}
356+
else {
357+
fields = "YMDhmsw"; // default all fields
358+
}
359+
360+
ltm = gmtime(&g_ntpTime);
361+
362+
be_newobject(vm, "list"); // create a new list on top of the stack
363+
364+
for (i = 0; fields[i]; i++) {
365+
int value = 0;
366+
switch (fields[i]) {
367+
case 'Y': value = ltm->tm_year + 1900; break;
368+
case 'M': value = ltm->tm_mon + 1; break;
369+
case 'D': value = ltm->tm_mday; break;
370+
case 'h': value = ltm->tm_hour; break;
371+
case 'm': value = ltm->tm_min; break;
372+
case 's': value = ltm->tm_sec; break;
373+
case 'w': value = ltm->tm_wday; break;
374+
}
375+
376+
be_pushint(vm, value); // push value onto stack
377+
be_data_push(vm, -2); // append value to list at -2
378+
be_pop(vm, 1); // pop the temporary value
379+
}
380+
381+
be_pop(vm, 1);
382+
be_return(vm); // return the list on top of the stack
383+
}
384+
385+
386+
387+
388+
389+
337390

338391
static int BasicInit() {
339392
if (!g_vm) {
@@ -354,7 +407,12 @@ static int BasicInit() {
354407
be_regfunc(g_vm, "runCmd", be_runCmd);
355408
be_regfunc(g_vm, "poststr", be_poststr);
356409
be_regfunc(g_vm, "echo", be_echo);
357-
410+
411+
be_regfunc(g_vm, "gmtime", be_gmtime);
412+
413+
#if ENABLE_DRIVER_OPENWEATHERMAP
414+
be_regfunc(g_vm, "getOpenWeatherReply", be_getOpenWeatherReply);
415+
#endif
358416
be_regfunc(g_vm, "rtosDelayMs", be_rtosDelayMs);
359417
be_regfunc(g_vm, "delayUs", be_delayUs);
360418
be_regfunc(g_vm, "cancel", be_CancelThread);

src/driver/drv_openWeatherMap.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ static void json_get_string(cJSON *parent, const char *name, char *dst, int dstS
6161
strncpy(dst, src, dstSize - 1);
6262
dst[dstSize - 1] = '\0';
6363
}
64-
64+
const char *Weather_GetReply() {
65+
return g_reply;
66+
}
6567
void Weather_SetReply(const char *s) {
6668
const char *json_start = strstr(s, "\r\n\r\n");
6769
if (json_start) {

src/driver/drv_openWeatherMap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ typedef struct weatherChannels_s {
2222
} weatherChannels_t;
2323

2424
weatherData_t *Weather_GetData();
25+
const char *Weather_GetReply();
2526

2627
#endif // __DRV_OPENWEATHERMAP_H__

src/selftest/selftest_berry.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,8 +1450,78 @@ void Test_Berry_NTP() {
14501450

14511451
CMD_ExecuteCommand("berry setChannel(5,getVar(\"$day\"))", 0);
14521452
SELFTEST_ASSERT_CHANNEL(5, 5);
1453+
1454+
CMD_ExecuteCommand("berry setChannel(5,getVar(\"$minute\"))", 0);
1455+
SELFTEST_ASSERT_CHANNEL(5, 27);
1456+
1457+
CMD_ExecuteCommand("berry setChannel(5, gmtime(\"YMDhms\")[0])", 0);
1458+
SELFTEST_ASSERT_CHANNEL(5, 2022);
1459+
1460+
CMD_ExecuteCommand("berry setChannel(6, gmtime(\"YMDhms\")[1])", 0);
1461+
SELFTEST_ASSERT_CHANNEL(6, 6);
1462+
1463+
CMD_ExecuteCommand("berry setChannel(7, gmtime(\"YMDhms\")[2])", 0);
1464+
SELFTEST_ASSERT_CHANNEL(7, 10);
1465+
1466+
CMD_ExecuteCommand("berry setChannel(8, gmtime(\"YMDhms\")[3])", 0);
1467+
SELFTEST_ASSERT_CHANNEL(8, 9); // hour
1468+
1469+
CMD_ExecuteCommand("berry setChannel(9, gmtime(\"YMDhms\")[4])", 0);
1470+
SELFTEST_ASSERT_CHANNEL(9, 27); // minute
1471+
1472+
CMD_ExecuteCommand("berry setChannel(10, gmtime(\"YMDhms\")[5])", 0);
1473+
SELFTEST_ASSERT_CHANNEL(10, 34); // second
1474+
1475+
CMD_ExecuteCommand("berry setChannel(11, gmtime(\"YMDhmsw\")[6])", 0);
1476+
SELFTEST_ASSERT_CHANNEL(11, 5); // weekday
1477+
1478+
CMD_ExecuteCommand("berry setChannel(11, gmtime(\"sw\")[1])", 0);
1479+
SELFTEST_ASSERT_CHANNEL(11, 5); // weekday
1480+
1481+
CMD_ExecuteCommand("berry setChannel(11, gmtime(\"ws\")[0])", 0);
1482+
SELFTEST_ASSERT_CHANNEL(11, 5); // weekday
1483+
1484+
CMD_ExecuteCommand("berry setChannel(11, gmtime(\"sw\")[0]+gmtime(\"sw\")[1])", 0);
1485+
SELFTEST_ASSERT_CHANNEL(11, 5+34); // weekday+second
1486+
1487+
SELFTEST_ASSERT_CHANNEL(5, 2022);
14531488
}
14541489

1490+
void Test_Berry_JSON() {
1491+
1492+
// reset whole device
1493+
SIM_ClearOBK(0);
1494+
CMD_ExecuteCommand("lfs_format", 0);
1495+
1496+
CMD_ExecuteCommand("berry import json; setChannel(11, json.load('{\"key\": 123}')[\"key\"])", 0);
1497+
SELFTEST_ASSERT_CHANNEL(11, 123);
1498+
1499+
1500+
CMD_ExecuteCommand("berry import json; setChannel(11, 1000+json.load('{\"a\": 567}')[\"a\"])", 0);
1501+
SELFTEST_ASSERT_CHANNEL(11, 1000+567);
1502+
1503+
CMD_ExecuteCommand("berry import json; setChannel(11, 1000+json.load('{\"a\": {\"b\": 678}}')[\"a\"][\"b\"])", 0);
1504+
SELFTEST_ASSERT_CHANNEL(11, 1000 + 678);
1505+
}
1506+
extern const char *owm_sample_reply;
1507+
void Test_Berry_OpenWeatherMap() {
1508+
1509+
// HTTP header + json
1510+
Weather_SetReply(owm_sample_reply);
1511+
1512+
CMD_ExecuteCommand("berry import json; setChannel(11, json.load(getOpenWeatherReply())[\"main\"][\"temp\"])", 0);
1513+
SELFTEST_ASSERT_CHANNEL(11, 23);
1514+
CMD_ExecuteCommand("berry import json; setChannel(11, json.load(getOpenWeatherReply())[\"main\"][\"pressure\"])", 0);
1515+
SELFTEST_ASSERT_CHANNEL(11, 998);
1516+
CMD_ExecuteCommand("berry import json; setChannel(11, json.load(getOpenWeatherReply())[\"main\"][\"humidity\"])", 0);
1517+
SELFTEST_ASSERT_CHANNEL(11, 85);
1518+
CMD_ExecuteCommand("berry import json; setChannel(11, 10*json.load(getOpenWeatherReply())[\"wind\"][\"speed\"])", 0);
1519+
SELFTEST_ASSERT_CHANNEL(11, 36);
1520+
CMD_ExecuteCommand("berry import json; setChannel(11, 100*json.load(getOpenWeatherReply())[\"wind\"][\"speed\"])", 0);
1521+
SELFTEST_ASSERT_CHANNEL(11, 360);
1522+
CMD_ExecuteCommand("berry import json; setChannel(11, 1*json.load(getOpenWeatherReply())[\"wind\"][\"deg\"])", 0);
1523+
SELFTEST_ASSERT_CHANNEL(11, 180);
1524+
}
14551525

14561526
void Test_Berry() {
14571527

@@ -1488,6 +1558,8 @@ void Test_Berry() {
14881558
Test_Berry_TuyaMCU_Bytes();
14891559
Test_Berry_TuyaMCU_Bytes2();
14901560
Test_Berry_NTP();
1561+
Test_Berry_JSON();
1562+
Test_Berry_OpenWeatherMap();
14911563
}
14921564

14931565
#endif

src/selftest/selftest_openWeatherMap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "selftest_local.h"
44
#include "../driver/drv_openWeatherMap.h"
55

6-
const char *sample_reply =
6+
const char *owm_sample_reply =
77
"HTTP/1.1 200 OK\r\n"
88
"Date: Sat, 09 Nov 2025 12:00:00 GMT\r\n"
99
"Server: openweathermap.org\r\n"
@@ -40,7 +40,7 @@ const char *reply_no_weather =
4040
void Test_OpenWeatherMap() {
4141

4242
// HTTP header + json
43-
Weather_SetReply(sample_reply);
43+
Weather_SetReply(owm_sample_reply);
4444

4545
weatherData_t *w = Weather_GetData();
4646
SELFTEST_ASSERT_FLOATCOMPARE(w->humidity, 85);

0 commit comments

Comments
 (0)