From 1985f05940b090f68433f6347dfa6b2f04e019ab Mon Sep 17 00:00:00 2001 From: openshwprojects Date: Thu, 17 Feb 2022 20:56:42 +0100 Subject: [PATCH 1/3] qiachip bk7231N (N version) template for button and relay pin --- src/httpserver/new_http.c | 3 +++ src/new_builtin_devices.c | 19 +++++++++++++++++++ src/new_pins.h | 1 + 3 files changed, 23 insertions(+) diff --git a/src/httpserver/new_http.c b/src/httpserver/new_http.c index 0e16a0677..12f33b15c 100644 --- a/src/httpserver/new_http.c +++ b/src/httpserver/new_http.c @@ -300,6 +300,9 @@ typedef struct template_s { template_t g_templates [] = { { Setup_Device_Empty, "Empty"}, + // BK7231N devices + { Setup_Device_BK7231N_CB2S_QiachipSmartSwitch, "[BK7231N][CB2S] QiaChip Smart Switch"}, + // BK7231T devices { Setup_Device_TuyaWL_SW01_16A, "WL SW01 16A"}, { Setup_Device_TuyaSmartLife4CH10A, "Smart Life 4CH 10A"}, { Setup_Device_IntelligentLife_NF101A, "Intelligent Life NF101A"}, diff --git a/src/new_builtin_devices.c b/src/new_builtin_devices.c index 3fbe3dc80..1647344fd 100644 --- a/src/new_builtin_devices.c +++ b/src/new_builtin_devices.c @@ -250,6 +250,25 @@ void Setup_Device_EmaxHome_EDU8774() { PIN_SaveToFlash(); } +// TODO - ELEKTRODA LINK +// QiachipSmartSwitch +void Setup_Device_BK7231N_CB2S_QiachipSmartSwitch() { + + + + PIN_ClearPins(); + // Button + PIN_SetPinRoleForPinIndex(7, IOR_Button); + PIN_SetPinChannelForPinIndex(7, 1); + // Relay + PIN_SetPinRoleForPinIndex(8, IOR_Relay); + PIN_SetPinChannelForPinIndex(8, 1); + // Led + + PIN_SaveToFlash(); +} + + // https://www.tokmanni.fi/alypistorasia-home-connect-ip20-6419860720456 // Marked as Smart-PFW02-G // Relay (with npn-transistor) at PWM4 P24 diff --git a/src/new_pins.h b/src/new_pins.h index 690526b2d..1a6e8760f 100644 --- a/src/new_pins.h +++ b/src/new_pins.h @@ -65,6 +65,7 @@ void Setup_Device_NedisWIFIPO120FWT_16A(); void Setup_Device_NedisWIFIP130FWT_10A(); void Setup_Device_EmaxHome_EDU8774(); void Setup_Device_TuyaSmartPFW02G(); +void Setup_Device_BK7231N_CB2S_QiachipSmartSwitch(); #endif From ed189a0f073ae11402529879a841889e9ce5e73b Mon Sep 17 00:00:00 2001 From: btsimonh Date: Fri, 18 Feb 2022 09:03:25 +0000 Subject: [PATCH 2/3] bugfix for POST /api/pins --- src/httpserver/rest_interface.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/httpserver/rest_interface.c b/src/httpserver/rest_interface.c index 539c39b74..44360493f 100644 --- a/src/httpserver/rest_interface.c +++ b/src/httpserver/rest_interface.c @@ -17,6 +17,7 @@ extern UINT32 flash_read(char *user_buf, UINT32 count, UINT32 address); +static int http_rest_error(http_request_t *request, int code, char *msg); static int http_rest_get(http_request_t *request); static int http_rest_post(http_request_t *request); @@ -580,7 +581,6 @@ static int http_rest_post_pins(http_request_t *request){ char *json_str = request->bodystart; int json_len = strlen(json_str); - http_setup(request, httpMimeTypeText); memset(p, 0, sizeof(jsmn_parser)); memset(t, 0, sizeof(jsmntok_t)*128); @@ -589,22 +589,18 @@ static int http_rest_post_pins(http_request_t *request){ if (r < 0) { ADDLOG_ERROR(LOG_FEATURE_API, "Failed to parse JSON: %d", r); sprintf(tmp,"Failed to parse JSON: %d\n", r); - poststr(request, tmp); - poststr(request, NULL); os_free(p); os_free(t); - return 0; + return http_rest_error(request, 400, tmp); } /* Assume the top-level element is an object */ if (r < 1 || t[0].type != JSMN_OBJECT) { ADDLOG_ERROR(LOG_FEATURE_API, "Object expected", r); sprintf(tmp,"Object expected\n"); - poststr(request, tmp); - poststr(request, NULL); os_free(p); os_free(t); - return 0; + return http_rest_error(request, 400, tmp); } /* Loop over all keys of the root object */ @@ -618,9 +614,9 @@ static int http_rest_post_pins(http_request_t *request){ int roleval, pr; jsmntok_t *g = &t[i + j + 2]; roleval = atoi(json_str + g->start); - pr = PIN_GetPinRoleForPinIndex(i); + pr = PIN_GetPinRoleForPinIndex(j); if(pr != roleval) { - PIN_SetPinRoleForPinIndex(i,roleval); + PIN_SetPinRoleForPinIndex(j,roleval); iChanged++; } } @@ -648,18 +644,23 @@ static int http_rest_post_pins(http_request_t *request){ } if (iChanged){ PIN_SaveToFlash(); + ADDLOG_DEBUG(LOG_FEATURE_API, "Changed %d - saved to flash", iChanged); } - poststr(request, NULL); os_free(p); os_free(t); + return http_rest_error(request, 200, "OK"); return 0; } static int http_rest_error(http_request_t *request, int code, char *msg){ request->responseCode = HTTP_RESPONSE_SERVER_ERROR; http_setup(request, httpMimeTypeJson); - hprintf128(request, "{\"error\":%d, \"msg\"=\"%s\"}", code, msg); + if (code != 200){ + hprintf128(request, "{\"error\":%d, \"msg\"=\"%s\"}", code, msg); + } else { + hprintf128(request, "{\"success\":%d, \"msg\"=\"%s\"}", code, msg); + } poststr(request,NULL); return 0; } From 25553af26a3ea67a42101d5febb9faba68f53e26 Mon Sep 17 00:00:00 2001 From: btsimonh Date: Fri, 18 Feb 2022 09:08:53 +0000 Subject: [PATCH 3/3] json bugfix --- src/httpserver/rest_interface.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/httpserver/rest_interface.c b/src/httpserver/rest_interface.c index 44360493f..a460ccbb0 100644 --- a/src/httpserver/rest_interface.c +++ b/src/httpserver/rest_interface.c @@ -657,9 +657,9 @@ static int http_rest_error(http_request_t *request, int code, char *msg){ request->responseCode = HTTP_RESPONSE_SERVER_ERROR; http_setup(request, httpMimeTypeJson); if (code != 200){ - hprintf128(request, "{\"error\":%d, \"msg\"=\"%s\"}", code, msg); + hprintf128(request, "{\"error\":%d, \"msg\":\"%s\"}", code, msg); } else { - hprintf128(request, "{\"success\":%d, \"msg\"=\"%s\"}", code, msg); + hprintf128(request, "{\"success\":%d, \"msg\":\"%s\"}", code, msg); } poststr(request,NULL); return 0;