diff options
author | deva <deva> | 2011-04-25 06:27:08 +0000 |
---|---|---|
committer | deva <deva> | 2011-04-25 06:27:08 +0000 |
commit | eb91a2967ed18aa24166f13a635091e2193aecc0 (patch) | |
tree | 5931eb736092d9b59c35059ee1f26eafcf791194 /server | |
parent | a01d53ff5aa238f66cff193af22da8e0b7538437 (diff) |
Initial version of praxisd communications class.
Diffstat (limited to 'server')
-rw-r--r-- | server/configure.in | 5 | ||||
-rw-r--r-- | server/getdata/Makefile.am | 9 | ||||
-rw-r--r-- | server/getdata/getdata.cc | 54 | ||||
-rw-r--r-- | server/src/Makefile.am | 2 | ||||
-rw-r--r-- | server/src/praxisd.cc | 317 | ||||
-rw-r--r-- | server/src/praxisd.h | 141 |
6 files changed, 508 insertions, 20 deletions
diff --git a/server/configure.in b/server/configure.in index 63a7111..dc7856c 100644 --- a/server/configure.in +++ b/server/configure.in @@ -219,6 +219,11 @@ LIBS="$tmp_LIBS" AC_SUBST(PTHREAD_CFLAGS) AC_SUBST(PTHREAD_LIBS) +dnl ====================== +dnl Check for cURL library +dnl ====================== +PKG_CHECK_MODULES(CURL, libcurl >= 7.9.5) + AC_OUTPUT( Makefile src/Makefile diff --git a/server/getdata/Makefile.am b/server/getdata/Makefile.am index 8cd960f..c0e6f09 100644 --- a/server/getdata/Makefile.am +++ b/server/getdata/Makefile.am @@ -1,4 +1,4 @@ -bin_PROGRAMS = getdata +bin_PROGRAMS = getdata convertdata getdata_LDADD = $(LD_EFENCE) $(PQXX_LIBS) $(CONFIG_LIBS) @@ -7,6 +7,13 @@ getdata_CXXFLAGS = $(PQXX_CXXFLAGS) $(CONFIG_CXXFLAGS) getdata_SOURCES = \ getdata.cc +convertdata_LDADD = $(LD_EFENCE) $(PQXX_LIBS) $(CONFIG_LIBS) + +convertdata_CXXFLAGS = $(PQXX_CXXFLAGS) $(CONFIG_CXXFLAGS) + +convertdata_SOURCES = \ + convertdata.cc + EXTRA_DIST = ################ diff --git a/server/getdata/getdata.cc b/server/getdata/getdata.cc index b051de0..717d1a9 100644 --- a/server/getdata/getdata.cc +++ b/server/getdata/getdata.cc @@ -28,22 +28,16 @@ #include <string> #include <pqxx/pqxx> #include <stdlib.h> +/* +Commits: + patientid | template | version | timestamp | uid | status + +Transactions: + macro | version | timestamp | user | uid | cid -enum { - PATIENTID = 0, - MACRO = 1, - VERSION = 2, - TIMESTAMP = 3, - USER = 4, - UID = 5, - TEMPLATE = 6 -}; - -enum { - TRANSACTION = 0, - NAME = 1, - VALUE = 2 -}; +Fields: + transaction | name | value +*/ #define SEP "\t" @@ -62,6 +56,32 @@ std::string escape(std::string str) int main() { + std::vector<std::string> fields; + fields.push_back("visus.egen_korr.mangler.odxt"); + fields.push_back("visus.egen_korr.kontrast.odxt"); + fields.push_back("visus.egen_korr.snellen.odxt"); + fields.push_back("visus.egen_korr.etdrs.odxt"); + fields.push_back("visus.egen_korr.korr.sf.odxt"); + fields.push_back("visus.egen_korr.korr.cyl.odxt"); + fields.push_back("visus.egen_korr.korr.grader.odxt"); + fields.push_back("visus.egen_korr.st_hul.odxt"); + fields.push_back("visus.egen_korr.st_hul.snellen.odxt"); + fields.push_back("visus.egen_korr.st_hul.etdrs.odxt"); + fields.push_back("visus.egen_korr.mangler.osin"); + fields.push_back("visus.egen_korr.kontrast.osin"); + fields.push_back("visus.egen_korr.snellen.osin"); + fields.push_back("visus.egen_korr.etdrs.osin"); + fields.push_back("visus.egen_korr.korr.sf.osin"); + fields.push_back("visus.egen_korr.korr.cyl.osin"); + fields.push_back("visus.egen_korr.korr.grader.osin"); + fields.push_back("visus.egen_korr.st_hul.osin"); + fields.push_back("visus.egen_korr.st_hul.snellen.osin"); + fields.push_back("visus.egen_korr.st_hul.etdrs.osin"); + fields.push_back("visus.egen_korr.ou"); + fields.push_back("visus.egen_korr.kontrast.ou"); + fields.push_back("visus.egen_korr.snellen.ou"); + fields.push_back("visus.egen_korr.etdrs.ou"); + std::string like = "ref"; std::string host = "localhost"; @@ -76,11 +96,7 @@ int main() pqxx::connection conn(cs); pqxx::work W(conn); - // transactions: - // patientid | macro | version | timestamp | user | uid | template - // fields: - // transaction | name | value std::map<std::string, int> fieldnames; int idx = 0; diff --git a/server/src/Makefile.am b/server/src/Makefile.am index 003148e..ccbc63b 100644 --- a/server/src/Makefile.am +++ b/server/src/Makefile.am @@ -44,6 +44,7 @@ pracrod_SOURCES = \ pracrodao.cc \ pracrodaopgsql.cc \ pracrodaotest.cc \ + praxisd.cc \ queryhandlerpentominos.cc \ queryhandlerpracro.cc \ queryparser.cc \ @@ -101,6 +102,7 @@ EXTRA_DIST = \ pracrodao.h \ pracrodaopgsql.h \ pracrodaotest.h \ + praxisd.h \ queryhandler.h \ queryhandlerpentominos.h \ queryhandlerpracro.h \ diff --git a/server/src/praxisd.cc b/server/src/praxisd.cc new file mode 100644 index 0000000..b4b0a88 --- /dev/null +++ b/server/src/praxisd.cc @@ -0,0 +1,317 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * praxisd.cc + * + * Tue Apr 19 09:00:29 CEST 2011 + * Copyright 2011 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Pracro is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "praxisd.h" + +static std::string strtime(bool with_sec = true) +{ + std::string ret; + + time_t epoch = time(NULL); + struct tm t; + localtime_r(&epoch, &t); + char buf[32]; + snprintf(buf, sizeof(buf), "%04d-%02d-%02d", + t.tm_year + 1900, t.tm_mon + 1, t.tm_mday); + ret = buf; + + if(with_sec) { + snprintf(buf, sizeof(buf), "%02d:%02d:%02d", + t.tm_hour, t.tm_min, t.tm_sec); + ret += " "; + ret += buf; + } + + return ret; +} + +static size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) +{ + std::string *str = (std::string*)userp; + str->append((char*)buffer, size * nmemb); + return size * nmemb; +} + +Praxisd::Praxisd(std::string h, int port) +{ + ch = curl_easy_init(); + host = h; + + curl_easy_setopt(ch, CURLOPT_PORT, port); + curl_easy_setopt(ch, CURLOPT_FAILONERROR, 1L); + curl_easy_setopt(ch, CURLOPT_TIMEOUT, 150L); + curl_easy_setopt(ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); + curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT, 15L); + curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L); + curl_easy_setopt(ch, CURLOPT_USERAGENT, "libpraxisd"); + curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, write_data); + curl_easy_setopt(ch, CURLOPT_FILETIME, 1L); + curl_easy_setopt(ch, CURLOPT_HEADER, 0L); +} + +Praxisd::~Praxisd() +{ + curl_easy_cleanup(ch); +} + +// Get Journal By CPR +std::string Praxisd::journal_get_by_cpr(std::string cpr) +{ + std::string journal; + + std::string uri = host + "/praxisd/1.0/journal/get_by_cpr?cpr=" + cpr; + curl_easy_setopt(ch, CURLOPT_URL, uri.c_str()); + + curl_easy_setopt(ch, CURLOPT_POST, 0L); + curl_easy_setopt(ch, CURLOPT_WRITEDATA, &journal); + + CURLcode errornum = curl_easy_perform(ch); + if(errornum != CURLE_OK) { + printf("Ouch %d\n", errornum); + } + + time_t time; + errornum = curl_easy_getinfo(ch, CURLINFO_FILETIME, &time); + + return journal; +} + +time_t Praxisd::journal_last_changed(std::string cpr) +{ + std::string journal; + + std::string uri = host + "/praxisd/1.0/journal/get_by_cpr?cpr=" + cpr; + curl_easy_setopt(ch, CURLOPT_URL, uri.c_str()); + + curl_easy_setopt(ch, CURLOPT_POST, 0L); + curl_easy_setopt(ch, CURLOPT_WRITEDATA, &journal); + + CURLcode errornum = curl_easy_perform(ch); + if(errornum != CURLE_OK) { + printf("Ouch %d\n", errornum); + } + + time_t time; + errornum = curl_easy_getinfo(ch, CURLINFO_FILETIME, &time); + + return time; +} + + +void Praxisd::journal_add(std::string cpr, std::string entry) +{ + std::string xml; + xml += "<praxisd version=\"1.0\">\n"; + xml += " <journal cpr=\""+cpr+"\">"+entry+"</journal>\n"; + xml += "</praxisd>\n"; + + curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, (long)xml.length()); + curl_easy_setopt(ch, CURLOPT_POSTFIELDS, xml.c_str()); + curl_easy_setopt(ch, CURLOPT_POST, 1L); + + struct curl_slist *slist = NULL; + // Unset 'Expect' header, set by CURLOPT_POSTFIELDS + slist = curl_slist_append(slist, "Expect:"); + slist = curl_slist_append(slist, "Content-Type: text/xml"); + slist = curl_slist_append(slist, "Connection: keep-alive"); + curl_easy_setopt(ch, CURLOPT_HTTPHEADER, slist); + + std::string uri = host + "/praxisd/1.0/journal/add"; + curl_easy_setopt(ch, CURLOPT_URL, uri.c_str()); + + std::string reply; + curl_easy_setopt(ch, CURLOPT_WRITEDATA, &reply); + + CURLcode errornum = curl_easy_perform(ch); + if(errornum != CURLE_OK) { + printf("Ouch: %d %s\n", errornum, reply.c_str()); + } +} + +void Praxisd::add_sogeord(std::string cpr, std::string sogeord, + std::string sogetxt) +{ + + std::string datestr = strtime(false); + + std::string xml; + xml += "<praxisd version=\"1.0\">\n"; + xml += " <addsogeord cpr=\""+cpr+"\">\n"; + xml += " <sogeord sogenr=\""+sogeord+"\" date=\""+datestr+"\">"+ + sogetxt+"</sogeord>\n"; + xml += " </addsogeord>\n"; + xml += "</praxisd>\n"; + + curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, (long)xml.length()); + curl_easy_setopt(ch, CURLOPT_POSTFIELDS, xml.c_str()); + curl_easy_setopt(ch, CURLOPT_POST, 1L); + + struct curl_slist *slist = NULL; + // Unset 'Expect' header, set by CURLOPT_POSTFIELDS + slist = curl_slist_append(slist, "Expect:"); + slist = curl_slist_append(slist, "Content-Type: text/xml"); + slist = curl_slist_append(slist, "Connection: keep-alive"); + curl_easy_setopt(ch, CURLOPT_HTTPHEADER, slist); + + std::string uri = host + "/praxisd/1.0/patient/add_sogeord"; + curl_easy_setopt(ch, CURLOPT_URL, uri.c_str()); + + std::string reply; + curl_easy_setopt(ch, CURLOPT_WRITEDATA, &reply); + + CURLcode errornum = curl_easy_perform(ch); + if(errornum != CURLE_OK) { + printf("Ouch: %d %s\n", errornum, reply.c_str()); + } +} + +#define DOTAG(x) if(name == #x) str = &p.x; +#include "saxparser.h" +class PatientParser : public SAXParser { +public: + PatientParser(Praxisd::patient_t &_p) : p(_p) { str = NULL; } + + void characterData(std::string &data) + { + if(str) *str += data; + } + + void startTag(std::string name, std::map<std::string, std::string> attr) + { + DOTAG(fornavne); + DOTAG(efternavn); + DOTAG(stilling); + DOTAG(gade); + DOTAG(by); + DOTAG(telefonnumre); + DOTAG(sikringsgr); + DOTAG(amtsnr); + DOTAG(sygekontor); + DOTAG(henvnr); + DOTAG(frilinie1); + DOTAG(frilinie2); + DOTAG(frilinie3); + DOTAG(frilinie4); + DOTAG(frilinie5); + DOTAG(ydernr); + DOTAG(created); + DOTAG(donottouch); + DOTAG(visus); + DOTAG(labkort); + DOTAG(medkort); + DOTAG(jlock); + DOTAG(unknown1); + DOTAG(henvdato); + DOTAG(aarhund); + DOTAG(fakturadato); + DOTAG(fakturabelob); + DOTAG(betaldato); + DOTAG(betalbelob); + DOTAG(jdato); + DOTAG(unknown250); + DOTAG(unknown251); + DOTAG(jtime); + if(name == "sogeords") {} // do nothing + if(name == "sogeord") { + Praxisd::sogeord_t s; + if(attr.find("sogenr") != attr.end()) s.sogenr = attr["sogenr"]; + if(attr.find("sogedatpo") != attr.end()) s.sogedato = attr["sogedato"]; + p.sogeord.push_back(s); + str = &p.sogeord[p.sogeord.size() - 1].sogetxt; + } + } + + void endTag(std::string name) + { + str = NULL; + } + +private: + std::string *str; + Praxisd::patient_t &p; +}; + +Praxisd::patient_t Praxisd::patient_get_by_cpr(std::string cpr) +{ + patient_t p; + + std::string xml; + + std::string uri = host + "/praxisd/1.0/patient/get_by_cpr?cpr=" + cpr; + curl_easy_setopt(ch, CURLOPT_URL, uri.c_str()); + + curl_easy_setopt(ch, CURLOPT_POST, 0L); + curl_easy_setopt(ch, CURLOPT_WRITEDATA, &xml); + + CURLcode errornum = curl_easy_perform(ch); + if(errornum != CURLE_OK) { + printf("Ouch %d\n", errornum); + } + + PatientParser parser(p); + parser.parse(xml.data(), xml.length()); + + return p; +} + +#ifdef TEST_PRAXISD +//deps: saxparser.cc debug.cc log.cc +//cflags: $(CURL_CFLAGS) $(EXPAT_CFLAGS) -I.. +//libs: $(CURL_LIBS) $(EXPAT_LIBS) +#include "test.h" + +#define NL "\r\n" +#define CPR "1505050505" + +TEST_BEGIN; + +Praxisd p("http://localhost", 10000); + +std::string j = p.journal_get_by_cpr(CPR); +TEST_NOTEQUAL(j, "", "Did we get a journal?"); + +std::string more = "less is more... much much more."; +p.journal_add(CPR, more); + +time_t changed = p.journal_last_changed(CPR); +TEST_EQUAL_INT(changed, time(NULL), "Changed just now?"); + +Praxisd::patient_t patient = p.patient_get_by_cpr(CPR); +TEST_EQUAL_STR(patient.fornavne, "Kristian", "Who are you?"); + +/* +std::string exp = j + more + NL; +std::string j2 = p.journal_get_by_cpr(CPR); +TEST_EQUAL_INT(exp.length(), j2.length(), "Compare lengths"); +TEST_EQUAL_STR(exp, j2, "Did we correctly append to the journal?"); +*/ + +p.add_sogeord(CPR, "CA0003", "Nolder"); + +TEST_END; + +#endif/*TEST_PRAXISD*/ diff --git a/server/src/praxisd.h b/server/src/praxisd.h new file mode 100644 index 0000000..c0f6666 --- /dev/null +++ b/server/src/praxisd.h @@ -0,0 +1,141 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * praxisd.h + * + * Tue Apr 19 09:00:29 CEST 2011 + * Copyright 2011 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Pracro is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#ifndef __PRACRO_PRAXISD_H__ +#define __PRACRO_PRAXISD_H__ + +#include <string> +#include <vector> + +#include <curl/curl.h> + +#include <time.h> + +class Praxisd { +public: + Praxisd(std::string host, int port); + ~Praxisd(); + + time_t journal_last_changed(std::string cpr); + + // Get Journal By CPR + std::string journal_get_by_cpr(std::string cpr); + + // Get Patient By CPR + typedef struct { + std::string sogenr; + std::string sogedato; + std::string sogetxt; + } sogeord_t; + + typedef struct { + std::string cpr; + std::string fornavne; + std::string efternavn; + std::string stilling; + std::string gade; + std::string by; + std::string telefonnumre; + std::string sikringsgr; + std::string amtsnr; + std::string sygekontor; + std::string henvnr; + std::string frilinie1; + std::string frilinie2; + std::string frilinie3; + std::string frilinie4; + std::string frilinie5; + std::vector<sogeord_t> sogeord; + std::string ydernr; + std::string created; + std::string donottouch; + std::string visus; + std::string labkort; + std::string medkort; + std::string jlock; + std::string unknown1; + std::string henvdato; + std::string aarhund; + std::string fakturadato; + std::string fakturabelob; + std::string betaldato; + std::string betalbelob; + std::string jdato; + std::string unknown250; + std::string unknown251; + std::string jtime; + } patient_t; + patient_t patient_get_by_cpr(std::string cpr); + +#if 0 + // Get Diverse From Sogenr + typedef struct {} diverse_t; + std::vector<diverse_t> diverse_get_all_by_sogenr(std::string sogenr); + + // Get Aftale All by Date and Calendar + typedef struct {} aftale_t; + std::vector<aftale_t> aftale_get_all_by_date_and_calendar(int cal, int year, + int month, int day); + + // Get Aftale All by CPR + std::vector<aftale_t> aftale_get_all_by_cpr(std::string cpr); + + // Authenticate + bool authenticate(std::string user, std::string pass); + + // Get Name by UserID + std::string user_get_name_by_id(std::string user); + + // Get All Docmenu by CPR + typedef struct {} docmenu_t; + std::vector<docmenu_t> docmenu_get_all_by_cpr(std::string cpr); + + // Get Docmenu by Name and CPR + std::string docmenu_get_by_cpr_and_name(std::string cpr, std::string name); +#endif + + // POST: + // Add To Journal + void journal_add(std::string cpr, std::string entry); + + // Update Patient + + // Add Sogeord to Patient + void add_sogeord(std::string cpr, std::string sogeord, std::string sogetxt); + + // Update Aftale + // Add Aftale + // Delete Aftale + // Add File to Docmenu + // Delete File from Docmenu + +private: + CURL *ch; + std::string host; +}; + +#endif/*__PRACRO_PRAXISD_H__*/ |