Skip to content

Commit

Permalink
KERUI protocol with unittest, in addition to PR #421 (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwalliczek authored and CurlyMoo committed Jan 24, 2019
1 parent d191c08 commit 8f43016
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeConfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ set(PROTOCOL_ELRO_SWITCH ON CACHE BOOL "support for the Elro switch protocol")
set(PROTOCOL_ELRO_CONTACT ON CACHE BOOL "support for the Elro contact protocol")
set(PROTOCOL_EURODOMEST_SWITCH ON CACHE BOOL "support for the Eurodomest switch protocol")
set(PROTOCOL_EV1527 ON CACHE BOOL "support for the EV1527 contact protocol")
set(PROTOCOL_KERUI ON CACHE BOOL "support for the KERUI contact protocol")
set(PROTOCOL_GENERIC_DIMMER ON CACHE BOOL "support for the generic dimmer protocol")
set(PROTOCOL_GENERIC_SCREEN ON CACHE BOOL "support for the generic screen protocol")
set(PROTOCOL_GENERIC_SWITCH ON CACHE BOOL "support for the generic switch protocol")
Expand Down
5 changes: 5 additions & 0 deletions libs/pilight/protocols/433.92/CMakeExclude.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ if(${PROTOCOL_EV1527} MATCHES "OFF")
list(REMOVE_ITEM ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/ev1527.h")
endif()

if(${PROTOCOL_KERUI} MATCHES "OFF")
list(REMOVE_ITEM ${PROJECT_NAME}_sources "${PROJECT_SOURCE_DIR}/kerui_d026.c")
list(REMOVE_ITEM ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/kerui_d026.h")
endif()

if(${PROTOCOL_BEAMISH_SWITCH} MATCHES "OFF")
list(REMOVE_ITEM ${PROJECT_NAME}_sources "${PROJECT_SOURCE_DIR}/beamish_switch.c")
list(REMOVE_ITEM ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/beamish_switch.h")
Expand Down
116 changes: 116 additions & 0 deletions libs/pilight/protocols/433.92/kerui_d026.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
Copyright (C) 2017 - 2019 by CurlyMo, Meloen, zeltom, mwka
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "../../core/pilight.h"
#include "../../core/common.h"
#include "../../core/dso.h"
#include "../../core/log.h"
#include "../protocol.h"
#include "../../core/binary.h"
#include "kerui_d026.h"


#define PULSE_MULTIPLIER 3
#define MIN_PULSE_LENGTH 260
#define MAX_PULSE_LENGTH 310
#define AVG_PULSE_LENGTH 280
#define RAW_LENGTH 50

static int validate(void) {
if(kerui_D026->rawlen == RAW_LENGTH) {
if(kerui_D026->raw[kerui_D026->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) &&
kerui_D026->raw[kerui_D026->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) {
return 0;
}
}

return -1;
}

static void createMessage(int unitcode, int state, int state2, int state3, int state4, char **message) {
int x = 0;
x = snprintf((*message), 255, "{\"unitcode\":%d,", unitcode);

if(state4 == 0) {
x += snprintf(&(*message)[x], 255-x, "\"state\":\"opened\"");
}
else if(state == 0) {
x += snprintf(&(*message)[x], 255-x, "\"state\":\"closed\"");
}
else if(state2 == 0) {
x += snprintf(&(*message)[x], 255-x, "\"state\":\"tamper\"");
}
else if(state3 == 0) {
x += snprintf(&(*message)[x], 255-x, "\"state\":\"not used\"");
}
else{
x += snprintf(&(*message)[x], 255-x, "\"battery\":\"low\"");
}
x += snprintf(&(*message)[x], 255-x, "}");
}

static void parseCode(char **message) {
int binary[RAW_LENGTH/2], x = 0, i = 0;

for(x=0;x<kerui_D026->rawlen-2;x+=2) {
if(kerui_D026->raw[x] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) {
binary[i++] = 1;
} else {
binary[i++] = 0;
}
}

int unitcode = binToDec(binary, 0, 19);
int state = binary[20];
int state2 = binary[21];
int state3 = binary[22];
int state4 = binary[23];
createMessage(unitcode, state, state2, state3, state4, message);
}

#if !defined(MODULE) && !defined(_WIN32)
__attribute__((weak))
#endif
void keruiD026Init(void) {

protocol_register(&kerui_D026);
protocol_set_id(kerui_D026, "kerui_D026");
protocol_device_add(kerui_D026, "kerui_D026", "KERUI D026 Door sensor");
kerui_D026->devtype = CONTACT;
kerui_D026->hwtype = RF433;
kerui_D026->minrawlen = RAW_LENGTH;
kerui_D026->maxrawlen = RAW_LENGTH;
kerui_D026->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV;
kerui_D026->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV;

options_add(&kerui_D026->options, "u", "unitcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, NULL);
options_add(&kerui_D026->options, "t", "opened", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL);
options_add(&kerui_D026->options, "f", "closed", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL);
options_add(&kerui_D026->options, "a", "tamper", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL);

kerui_D026->parseCode=&parseCode;
kerui_D026->validate=&validate;
}

#if defined(MODULE) && !defined(_WIN32)
void compatibility(struct module_t *module) {
module->name = "kerui_D026";
module->version = "2.0";
module->reqversion = "6.0";
module->reqcommit = "84";
}

void init(void) {
keruiD026Init();
}
#endif
17 changes: 17 additions & 0 deletions libs/pilight/protocols/433.92/kerui_d026.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright (C) 2017 - 2019 by CurlyMo, Meloen, zeltom, mwka
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#ifndef _PROTOCOL_KERUI_D026_H_
#define _PROTOCOL_KERUI_D026_H_

#include "../protocol.h"

struct protocol_t *kerui_D026;
void keruiD026Init(void);

#endif
41 changes: 41 additions & 0 deletions tests/protocols/kerui_d026.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright (C) 2019 by mwka
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

static const int kerui_d026_nrtests = 5;
static const struct raw_t kerui_d026_tests[] = {
{
"984 258 975 290 972 278 980 313 332 927 949 313 941 484 247 888 951 286 967 346 247 990 275 977 293 974 949 308 281 971 962 301 952 309 938 334 951 314 278 968 926 391 878 368 888 361 285 961 283 9500",
"{\"unitcode\":500591,\"state\":\"opened\"}",
NULL,
0
},
{
"949 297 939 316 948 317 946 304 288 982 930 337 919 330 320 950 913 378 1030 175 313 990 266 987 281 982 905 364 275 976 946 319 936 337 924 329 954 312 276 973 941 321 932 318 939 373 243 958 302 9505",
"{\"unitcode\":500591,\"state\":\"opened\"}",
NULL,
0
},
{
"991 322 294 928 370 908 364 861 392 884 909 374 901 349 334 974 303 886 946 368 928 646 598 359 300 936 920 331 303 980 904 344 915 435 839 390 863 397 232 1019 261 993 883 462 825 345 932 328 276 9495",
"{\"unitcode\":503393,\"state\":\"closed\"}",
NULL,
0
},
{
"970 291 350 908 344 912 350 930 327 924 969 297 937 317 402 885 368 921 914 302 952 364 897 378 297 896 962 382 362 868 934 328 899 333 912 362 912 364 340 953 298 981 880 364 891 364 893 700 133 9296",
"{\"unitcode\":503393,\"state\":\"closed\"}",
NULL,
0
},
{
"651 511 341 902 343 922 952 372 890 367 287 954 309 955 348 921 285 964 929 333 933 333 931 385 277 925 941 311 350 951 1009 272 937 321 957 345 906 343 311 1237 652 340 298 941 935 322 1005 264 323 9525",
"{\"unitcode\":503321,\"state\":\"tamper\"}",
NULL,
0
}
};
3 changes: 3 additions & 0 deletions tests/protocols_433.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "../libs/pilight/protocols/433.92/arctech_dimmer.h"
#include "../libs/pilight/protocols/433.92/arctech_dusk.h"
#include "../libs/pilight/protocols/433.92/livolo_switch.h"
#include "../libs/pilight/protocols/433.92/kerui_d026.h"
#include "../libs/pilight/protocols/433.92/tfa2017.h"

#include "alltests.h"
Expand All @@ -46,6 +47,7 @@ const struct test_t {
#include "protocols/arctech_dimmer.h"
#include "protocols/arctech_dusk.h"
#include "protocols/livolo_switch.h"
#include "protocols/kerui_d026.h"
#include "protocols/tfa2017.h"

static const struct test_t tests[] = {
Expand All @@ -55,6 +57,7 @@ static const struct test_t tests[] = {
{ &arctechDimmerInit, &arctech_dimmer, arctech_dimmer_tests, &arctech_dimmer_nrtests },
{ &arctechDuskInit, &arctech_dusk, arctech_dusk_tests, &arctech_dusk_nrtests },
{ &livoloSwitchInit, &livolo_switch, livolo_switch_tests, &livolo_switch_nrtests },
{ &keruiD026Init, &kerui_D026, kerui_d026_tests, &kerui_d026_nrtests },
{ &tfa2017Init, &tfa2017, tfa2017_tests, &tfa2017_nrtests },
};

Expand Down

0 comments on commit 8f43016

Please sign in to comment.