]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
refactor get all options to one intern var
authorBastian Dehn <hhaalo@arcor.de>
Tue, 23 Jan 2024 18:52:14 +0000 (19:52 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Tue, 23 Jan 2024 18:52:14 +0000 (19:52 +0100)
src/kds_s2000w_handler.c

index cbcfc4b437d20921852bf194437c1475df5cd62e..a6debca22c72c2b47eaf98638257a7a7f26a981b 100644 (file)
@@ -8,6 +8,20 @@
 #include "kds_s2000w_client.h"
 
 int64_t _sessionid = 0;
+json_object* _all_options = NULL;
+
+void _get_all_options()
+{
+       response* resp = kds_s2000w_client_response_init();
+       int result = kds_s2000w_client_get_all_options(resp);
+
+       if (result != 0 || resp->code == 404)
+               return;
+
+       _all_options = json_tokener_parse(resp->data);
+       kds_s2000w_client_response_free(resp);
+       resp = NULL;
+}
 
 device_state kds_s2000w_handler_open()
 {
@@ -53,6 +67,8 @@ void kds_s2000w_handler_close()
 
        kds_s2000w_client_close_session(_sessionid);
        _sessionid = 0;
+       json_object_put(_all_options);
+       _all_options = NULL;
 }
 
 void kds_s2000w_handler_get_option(int option, void* value)
@@ -60,20 +76,17 @@ void kds_s2000w_handler_get_option(int option, void* value)
        if (option != 0)
                return;
 
-       response* resp = kds_s2000w_client_response_init();
-       int result = kds_s2000w_client_get_all_options(resp);
+       if (option == 0)
+               _get_all_options();
 
-       if (result != 0 || resp->code == 404)
+       if (_all_options == NULL)
                return;
 
-       json_object* resObj = json_tokener_parse(resp->data);
-       json_object* config = json_object_object_get(resObj, "Configuration");
+       json_object* config = json_object_object_get(_all_options, "Configuration");
        int* num_options = (int*) value;
        *num_options = 0;
        *num_options = json_object_object_length(config);
        *num_options++;
-
-       kds_s2000w_client_response_free(resp);
 }
 
 void kds_s2000w_handler_set_option(int option, void* value)