1 // Copyright (c) 2017, Tobias Mueller tm(at)tm3d.de
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the
14 // * All advertising materials mentioning features or use of this
15 // software must display the following acknowledgement: This product
16 // includes software developed by tm3d.de and its contributors.
17 // * Neither the name of tm3d.de nor the names of its contributors may
18 // be used to endorse or promote products derived from this software
19 // without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "mySensorDB.h"
52 #include <fcntl.h> /* For O_RDWR */
62 #define RB "\e(0\x6a\e(B" // 188 Right Bottom corner
63 #define RT "\e(0\x6b\e(B" // 187 Right Top corner
64 #define LT "\e(0\x6c\e(B" // 201 Left Top cornet
65 #define LB "\e(0\x6d\e(B" // 200 Left Bottom corner
66 #define MC "\e(0\x6e\e(B" // 206 Midle Cross
67 #define HL "\e(0\x71\e(B" // 205 Horizontal Line
68 #define LC "\e(0\x74\e(B" // 204 Left Cross
69 #define RC "\e(0\x75\e(B" // 185 Right Cross
70 #define BC "\e(0\x76\e(B" // 202 Bottom Cross
71 #define TC "\e(0\x77\e(B" // 203 Top Cross
72 #define VL "\e(0\x78\e(B" // 186 Vertical Line
73 #define SP " " // space string
76 #define RB " "// 188 Right Bottom corner
77 #define RT " "// 187 Right Top corner
78 #define LT " "// 201 Left Top cornet
79 #define LB " "// 200 Left Bottom corner
80 #define MC " " // 206 Midle Cross
81 #define HL " "// 205 Horizontal Line
82 #define LC " "// 204 Left Cross
83 #define RC " "// 185 Right Cross
84 #define BC " "// 202 Bottom Cross
85 #define TC " "// 203 Top Cross
86 #define VL " "// 186 Vertical Line
87 #define SP " " // space string
91 printf("owTools - Programm for reading und controlling 1-Wire Devices from www.tm3d.de\n\n");
92 printf("run: owTools -a [COMn|USBn|GPIOn] [options]\n\n");
93 printf(" COMn -> Adapter DS9097 and compatible (e.g. LinkUSB)\n");
94 printf(" n=1 -> Windows COM1 -> Linux /dev/ttyS1\n");
95 printf(" USBn -> Adapter DS2490/DS9490 \n");
96 printf(" n=1 -> first USB-Adapter \n");
97 printf(" GPIOn -> port of Raspberry PI (port Name not Pin number)\n\n");
99 printf(" -i interactive mode\n");
100 printf(" select a device and get information about it\n");
101 printf(" -c read and print all continuous\n");
102 printf(" -p time in sec between readings\n");
103 printf(" -d [config file] put all Data in mysql Database descripted in Config file\n");
104 printf(" Config file: \n");
105 printf(" [server]\n");
106 printf(" [port] -> 3306\n");
107 printf(" [user] \n");
108 printf(" [password]\n");
109 printf(" [database]\n");
110 printf(" [prefix] -> a prefix of all tables \n\n");
111 printf(" -r Search everytime for new Devices\n");
112 printf(" -f hexfile Flash new\n");
113 printf(" -n change id \n");
114 printf(" -g get from server\n");
115 printf(" -w [1|2] show Warnings (1) or all Infos (2)\n");
128 //length of shown chars of an UTF8 string ... sometimes not the same as byte count
129 std::size_t utf8_length(std::string const &s) {
131 std::string::const_iterator begin = s.begin(), end = s.end();
132 while (begin != end) {
133 unsigned char c = *begin;
135 if ((c & 0x80) == 0) n = 1;
136 else if ((c & 0xE0) == 0xC0) n = 2;
137 else if ((c & 0xF0) == 0xE0) n = 3;
138 else if ((c & 0xF8) == 0xF0) n = 4;
147 std::vector<arg_t> comlist;
149 std::string getArg(std::string flag) {
150 for(arg_t a:comlist) {
151 if (a.flag==flag) return a.arg;
158 int getArgi(std::string flag) {
159 return atoi(getArg(flag).c_str());
164 mySensorDB *sdb=NULL;
167 snum_t getArgsnum(std::string flag) {
173 int findCPU(std::string cpu) {
174 std::ifstream fileInput;
176 fileInput.open("/proc/cpuinfo");
177 if(fileInput.is_open()) {
178 for(unsigned int curLine = 0; getline(fileInput, line); curLine++) {
179 if (line.find(cpu) != std::string::npos) {
193 owInterface *owi= NULL;
196 if (getArg("w")=="2")
197 owi->log->setLogLevel(0);
198 else if (getArg("w")=="2") owi->log->setLogLevel(0);
199 else owi->log->setLogLevel(OWLOG_ERROR);
204 void continuous(std::vector<owDevice*> *devices,int intervall,int headline,int searchrom) {
210 if (owi->isDevicesChanged()||(first==1)) {
214 for (owDevice* dev :*devices) {
215 if (dev->configstate!=OWDCS_NONE) {
216 for (size_t i=0;i<4;i++) {
217 if (dev->config->getPropertyID((uint8_t)i)!=0)
218 printf("\033[1;34m%02X.%02X%02X\033[0m\t",dev->getNum().byte[7],dev->getNum().byte[1],dev->getNum().byte[0]);
223 for (owDevice* dev :*devices) {
224 if (dev->configstate!=OWDCS_NONE) {
225 for (size_t i=0;i<4;i++) {
226 if (dev->config->getPropertyID((uint8_t)i)!=0)
227 printf("\033[0;36m%s\033[0m\t",dev->config->getQuantity((uint8_t)i).substr(0,7).c_str());
232 for (owDevice* dev :*devices) {
233 if (dev->configstate!=OWDCS_NONE) {
234 for (size_t i=0;i<4;i++) {
235 if (dev->config->getPropertyID((uint8_t)i)!=0) {
236 size_t l=utf8_length(dev->config->getUnit((uint8_t)i));
238 printf("\033[3;34m[%s]%s\033[0m\t",dev->config->getUnit((uint8_t)i).c_str(),ls.substr(0,5-l).c_str());
248 //owi->log->setLogLevel(0);
249 for (owDevice* dev : *devices) {
250 sdb->createDeviceTable(dev);
260 struct tm tm = *localtime(&t);
266 printf("%d-%02d-%02d %02d:%02d:%02d\t", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
267 for (owDevice* dev :*devices) {
268 for(int k=0;k<owi->maxrepeat;k++){
270 if (owi->log->last()<OWLOG_ERROR) break;
273 if (owi->log->last()<OWLOG_ERROR) {
274 if (dev->configstate!=OWDCS_NONE) {
275 for (size_t i=0;i<4;i++) {
276 if (dev->config->getPropertyID((int8_t)i)!=0) {
277 if (dev->values[i]<=-10)
278 printf("%0.2f \t",dev->values[i]);
279 else if (dev->values[i]<0)
280 printf("%0.3f \t",dev->values[i]);
281 else if (dev->values[i]<10)
282 printf("%0.4f \t",dev->values[i]);
283 else if (dev->values[i]<100)
284 printf("%0.3f \t",dev->values[i]);
285 else if (dev->values[i]<1000)
286 printf("%0.2f \t",dev->values[i]);
288 printf("%0.1f \t",dev->values[i]);
295 sdb->insertValues(dev);
299 owi->log->setLogLevel(OWLOG_INFO);
300 owi->log->set(OWLOG_ERROR,"Too many errors, mybee conection is dead.");
307 while (((int)time(NULL)) < (st + intervall)) {
318 void device_menu(owDevice* d) {
319 for(int i=0;i<70;i++) printf(HL);printf("\n");
320 printf("Selected Device: ");
321 snum_t snum=d->getNum();
322 printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num);
324 printf("Chip Info: ");
325 std::vector<std::string> info=d->getFamilyInfo();
326 for (std::string s : info) printf(" %s",s.c_str());
328 printf("Values info: ");
329 int tm3d=d->readConfig();
330 for (size_t i=0;i<4;i++) {
331 printf("%s in %s",d->config->getQuantity((uint8_t)i).c_str(),d->config->getUnit((uint8_t)i).c_str());
332 if (i<4) printf("; ");
334 if (tm3d) printf(" (tm3d.de)"); else printf(" (default)");
337 for (size_t i=0;i<d->values.size();i++) {
338 printf("\033[1;33m%0.4f %s\033[0m ",d->values[i],d->config->getUnit((uint8_t)i).c_str());
341 for(int i=0;i<70;i++) printf(HL);printf("\n");
343 printf("1) Run continuous\n");
346 printf("Select an option: ");std::getline (std::cin,inp);
347 int dnr=atoi(inp.c_str());
350 std::vector<owDevice*> v;
352 continuous(&v,1,1,0);
354 // if (dnr<=i) break;
355 // printf("\033[4;33mSelect a number between 0 and %i\033[0m\n",i);
364 for (owDevice* dev :owi->devices) {
366 snum_t snum=dev->getNum();
368 printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num);
371 for (int j=0;j<4;j++) printf("%s ",dev->config->getQuantity(j).c_str());
374 printf("0) Exit Programm\n");
378 printf("Select device with number: ");std::getline (std::cin,inp);
379 dnr=atoi(inp.c_str());
380 if (dnr<=0) return 0;
382 printf("\033[4;33mSelect a number between 0 and %i\033[0m\n",i);
388 int questionYesNo(std::string text) {
390 printf("%s [Y/n] ",text.c_str());std::getline (std::cin,inp);
391 if (inp=="Y") return 1; else return 0;
395 void interactive(owInterface *owi) {
397 int i=selectDevice();
399 device_menu(owi->devices[i-1]);
403 int main(int argc, char *argv[]) {
406 //printf("Kommando: %s:\n", argv[0]);
408 if (**++argv != '-') {
409 //printf(" Arg. = %s\n", *argv);
410 if (comlist.empty()) {
413 comlist.push_back(a);
415 if ((*(comlist.end()-1)).arg!="1") {
418 comlist.push_back(a);
420 (*(comlist.end()-1)).arg=*argv;
424 for( i=1; (*argv)[i] != '\0'; i++) {
425 //printf(" Flag = (%c)\n", (*argv)[i]);
428 comlist.push_back(a);
433 //for(arg_t a:comlist) printf("%s->%s\n",a.flag.c_str(),a.arg.c_str());
434 if (getArg("h")=="1") {
438 std::string adapter=getArg("a");
439 if (adapter.empty()) {
443 std::transform(adapter.begin(), adapter.end(),adapter.begin(), ::toupper);
447 if(adapter.find("COM")!=std::string::npos) {
448 owi=new owCOMInterface();
449 int port=atoi(adapter.substr(adapter.find("COM")+3).c_str());
450 printf("Open /dev/ttyS%i\n",port);
451 owi->InitAdapter(port);
453 if(adapter.find("USB")!=std::string::npos) {
454 owi=new owUSBInterface();
455 int port=atoi(adapter.substr(adapter.find("USB")+3).c_str());
456 printf("Open the %i. USB Adapter\n",port);
458 if ((err=owi->InitAdapter(port))<0) {
459 if (err==-1) printf("No Adapter found!\n On Linux, try: sudo owTools....\n On Windows: Install libusb0 driver\n");
460 if (err==-2) printf("Maybe Adapter is used ... (try sudo)\nOn Linux: If the kernel modul is loaded type: sudo rmmod ds2490\n");
464 if(adapter.find("GPIO")!=std::string::npos) {
465 if (findCPU("BCM2708")) {
466 printf("\033[1;33mRaspberry Pi 1 is dedected. 1-wire on the GPIO port can have many errors in the transfer.\033[0m\n");
467 owi=new owPiGPioInterface();
468 int port=atoi(adapter.substr(adapter.find("GPIO")+4).c_str());
469 printf("Open GPIO %i\n",port);
470 } else if (findCPU("BCM2709")) {
471 owi=new owPiGPioInterface();
472 int port=atoi(adapter.substr(adapter.find("GPIO")+4).c_str());
473 printf("Open GPIO %i\n",port);
474 owi->InitAdapter(port);
476 printf("\033[1;31mGPIO works with Raspberry PI only \033[0m\n");
480 if (adapter.find("ARDUINO") != std::string::npos) {
481 owi = new owARDUINOInterface();
482 int port = atoi(adapter.substr(adapter.find("ARDUINO") + 7).c_str());
483 printf("Open /dev/ttyS%i\n", port);
484 owi->InitAdapter(port);
489 if (adapter.find("USB") != std::string::npos) {
491 owi = new owTMEXWIN();
492 int port = atoi(adapter.substr(adapter.find("USB") + 3).c_str());
493 printf("Open the %i. USB Adapter\n", port);
496 sprintf_s(cs, 20, "{%i,6}", port);
497 if ((err = owi->InitAdapter(cs))==0) {
498 printf("ERROR Init USB Adapter\n");
503 if (adapter.find("COM") != std::string::npos) {
505 owi = new owTMEXWIN();
506 int port = atoi(adapter.substr(adapter.find("COM") + 3).c_str());
507 printf("Open Adapter on COM%i\n", port);
510 sprintf_s(cs, 20, "{%i,5}", port);
511 if ((err = owi->InitAdapter(cs))==0) {
512 printf("ERROR Init Serial Adapter\n");
518 printf("No 1-Wiremaster found\n");
525 if (getArg("i")=="1") {
528 if (getArg("c")=="1") {
529 int reload=(getArg("r")=="1");
532 //owi->log->setLogLevel(3);
535 if ((s=getArg("p")) !="") {
536 pause=atoi(s.c_str());
539 if ((s=getArg("d")) !="") {
541 printf("Use Database\n");
542 sdb=new mySensorDB(s,owi->log);
543 if (sdb->log->last()>OWLOG_WARNING) {
547 if (sdb->log->last()>OWLOG_WARNING) {
550 sdb->createSensorTable();
551 if (sdb->log->last()>OWLOG_WARNING) {
559 continuous(&(owi->devices),pause,1,reload);
561 if ((s=getArg("f"))!="") {
562 printf("Flash %s in selected Device\n",s.c_str());
563 int sel=selectDevice();
565 int resetid=questionYesNo("Would you reset to default ID? (This is necessary if another device is used!)");
566 owi->log->setLogLevel(OWLOG_INFO);
567 owi->flashHEXFile(s,owi->device_nums[sel-1],resetid,1);
571 for (owDevice* dev :owi->devices) {
572 snum_t snum=dev->getNum();
573 printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num);
577 printf("%i Devices found\n",(int)owi->devices.size());
580 if ((s=getArg("v"))!="") {
581 printf("Reset VOC\n");
582 int sel=selectDevice();
584 owDevice* dev=owi->devices[sel-1];
585 std::vector<uint8_t> cl;
590 dev->Communicate(&cl,4,0);
593 if ((s=getArg("n"))!="") {
594 printf("Change id of selected Device\n");
595 int sel=selectDevice();
599 snum_t snum=owi->devices[sel]->getNum();
600 if ((getArg("g"))=="1") {
601 printf("get ID from Server\n");
604 sprintf(s,"wget -q http://www.tm3d.de/shop99/owid.php?fam=%02X -O id.txt",snum.byte[0]);
607 sprintf_s(s, "wget -q http://www.tm3d.de/shop99/owid.php?fam=%02X -O id.txt", snum.byte[0]);
610 if ((err=system(s))==0) {
613 std::stringstream str;
619 while(fs.peek() != EOF) {
620 char c= (char) fs.get();
624 if ((c==',')|(c=='}')) {
625 isnum.byte[i]=(uint8_t)strtol(s, NULL, 16);
626 //printf("%x\n",strtol(s, NULL, 16));
642 } else (printf("ERROR %i\n",err));
649 printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num);
653 owi->devices[sel]->changeID(snum);
657 for (owDevice* dev :owi->devices) {
658 snum_t snum=dev->getNum();
659 printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num);
663 printf("%i Devices found\n",(int)owi->devices.size());
667 for (owDevice* dev :owi->devices) {
668 printf("_______________________________________________________________________\n");
669 snum_t snum=dev->getNum();
670 printf("\033[1;34m%016llX\033[0m ",(unsigned long long)snum.num);
671 switch (dev->configstate) {
672 case OWDCS_16:printf("old Configcode from www.tm3d.de");break;
673 case OWDCS_24:printf("Configcode from www.tm3d.de");break;
674 case OWDCS_DEFAULT:printf("Default Configcode set by this Software");break;
675 case OWDCS_NONE:printf("No Configcode");break;
679 printf(" %s",dev->getFamilyInfo()[i].c_str());
681 if (dev->configstate!=OWDCS_NONE) {
682 std::vector<std::vector<std::string>> ia;
683 std::vector<size_t> maxcol;
685 for(int i=0;i<4;i++) {
686 std::vector<std::string> colm;
688 colm.push_back(dev->config->getSensorChip(i));
689 if (max<utf8_length(colm[0])) max=utf8_length(colm[0]);
690 colm.push_back(dev->config->getQuantity(i));
691 if (max<utf8_length(colm[1])) max=utf8_length(colm[1]);
692 colm.push_back(dev->config->getUnit(i));
693 if (max<utf8_length(colm[2])) max=utf8_length(colm[2]);
694 if (dev->config->getPropertyID(i)==0) {
699 sprintf(hs,"%0.3f",dev->values[i]);
702 sprintf_s(hs, "%0.3f", dev->values[i]);
706 if (max<utf8_length(colm[3])) max=utf8_length(colm[3]);
708 maxcol.push_back(max);
710 std::string space=" ";
711 for(int i=0;i<4;i++) {
712 for(int j=0;j<4;j++) {
713 printf("%s%s",ia[j][i].c_str(),space.substr(0,maxcol[j]-utf8_length(ia[j][i])+2).c_str());
719 printf("%i Devices found\n",(int)owi->devices.size());