diff options
Diffstat (limited to 'lib/wes_base.cpp')
-rw-r--r-- | lib/wes_base.cpp | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/lib/wes_base.cpp b/lib/wes_base.cpp new file mode 100644 index 0000000..f8550a4 --- /dev/null +++ b/lib/wes_base.cpp @@ -0,0 +1,160 @@ +/* **************************************************************************** + * wes_base.cpp -- méthodes de la classe `wes_base`. + * Copyright (C) 2018 Thomas Touhey <thomas@touhey.fr> + * + * This project is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/or redistribute the software under the terms of the + * CeCILL license as circulated by CEA, CNRS and INRIA at the + * following URL: "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the project's author, the holder of the + * economic rights, and the successive licensors have only limited liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also therefore + * means that it is reserved for developers and experienced professionals + * having in-depth computer knowledge. Users are therefore encouraged to load + * and test the software's suitability as regards their requirements in + * conditions enabling the security of their systems and/or data to be + * ensured and, more generally, to use and operate it in the same conditions + * as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + * ************************************************************************* */ +#include "internals.hpp" + +using namespace wesh; + +/* --- + * Constructeur, destructeur. + * --- */ + +wes_base::wes_base(void) +{ + this->id = 0; + this->client = nullptr; +} + +wes_base::wes_base(int given_id) +{ + this->id = given_id; + this->client = nullptr; +} + +wes_base::~wes_base(void) +{ + if (this->client) + clnt_destroy(static_cast<CLIENT*>(this->client)); +} + +/* --- + * Utilitaires pour les méthodes de cette classe et les objets qui + * en dépendent. + * --- */ + +CLIENT *wes_base::get_clnt(void) +{ + if (this->client) + return (this->client); + + this->client = clnt_create("localhost", WESHD_PROG, WESHD_VERS1, "tcp"); + if (!this->client) + throw connexion_exception(); + + return (this->client); +} + +int wes_base::get_id(void) +{ + return (this->id); +} + +/* --- + * Récupération et suppression de la ressource. + * --- */ + +void wes_base::gather_(wespregopts_t *args) +{ + wespret_with_id_t *resp; + wespret_t ret; + + if (this->id != 0) + throw already_created_exception(); + + resp = gather_1(args, clnt(this)); + if (!resp) + throw connexion_exception(); + + ret = resp->ret; + if (ret != WESPRET_OK) + throw_exception_using_ret(ret); + + this->id = resp->id; +} + +void wes_base::remove(void) +{ + wespid_t wid; + wespret_t ret, *retp; + + if (this->id == 0) + throw not_loaded_exception(); + + wid = static_cast<wespid_t>(this->id); + retp = unregister_1(&wid, clnt(this)); + if (!retp) + throw connexion_exception(); + + ret = *retp; + if (ret != WESPRET_OK) + throw_exception_using_ret(ret); + + this->id = 0; +} + +/* --- + * Récupération d'éléments. + * --- */ + +struct tm wes_base::get_date(void) +{ + wespid_t wid; + wespret_with_datetime_t *resp; + wespdatetime_t *dt; + struct tm tm; + + if (this->id == 0) + throw not_loaded_exception(); + + wid = this->id; + resp = get_date_time_1(&wid, clnt(this)); + if (!resp) + throw connexion_exception(); + + if (resp->ret != WESPRET_OK) + throw_exception_using_ret(resp->ret); + + dt = &resp->dt; + tm.tm_year = dt->year - 1900; + tm.tm_mon = dt->mon - 1; + tm.tm_mday = dt->dom; + tm.tm_hour = dt->hour; + tm.tm_min = dt->min; + tm.tm_sec = dt->sec; + + mktime(&tm); + return (tm); +} + +electricity_meter wes_base::get_electricity_meter(int em_id) +{ + electricity_meter em(this, em_id); + + return (em); +} |