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.
35 #include "mySensorDB.h"
40 #include <fcntl.h> /* For O_RDWR */
54 #define RB "\e(0\x6a\e(B" // 188 Right Bottom corner
55 #define RT "\e(0\x6b\e(B" // 187 Right Top corner
56 #define LT "\e(0\x6c\e(B" // 201 Left Top cornet
57 #define LB "\e(0\x6d\e(B" // 200 Left Bottom corner
58 #define MC "\e(0\x6e\e(B" // 206 Midle Cross
59 #define HL "\e(0\x71\e(B" // 205 Horizontal Line
60 #define LC "\e(0\x74\e(B" // 204 Left Cross
61 #define RC "\e(0\x75\e(B" // 185 Right Cross
62 #define BC "\e(0\x76\e(B" // 202 Bottom Cross
63 #define TC "\e(0\x77\e(B" // 203 Top Cross
64 #define VL "\e(0\x78\e(B" // 186 Vertical Line
65 #define SP " " // space string
69 printf("owTools - Programm for reading und controlling 1-Wire Devices from www.tm3d.de\n\n");
70 printf("run: owTools -a [COMn|USBn|GPIOn] [options]\n\n");
71 printf(" COMn -> Adapter DS9097 and compatible (e.g. LinkUSB)\n");
72 printf(" n=1 -> Windows COM1 -> Linux /dev/ttyS1\n");
73 printf(" USBn -> Adapter DS2490/DS9490 \n");
74 printf(" n=1 -> first USB-Adapter \n");
75 printf(" GPIOn -> port of Raspberry PI (port Name not Pin number)\n\n");
77 printf(" -i interactive mode\n");
78 printf(" select a device and get information about it\n");
79 printf(" -c read and print all continuous\n");
80 printf(" -p time in sec between readings\n");
81 printf(" -d [config file] put all Data in mysql Database descripted in Config file\n");
82 printf(" Config file: \n");
83 printf(" [server]\n");
84 printf(" [port] -> 3306\n");
86 printf(" [password]\n");
87 printf(" [database]\n");
88 printf(" [prefix] -> a prefix of all tables \n\n");
89 printf(" -r Search everytime for new Devices\n");
90 printf(" -f hexfile Flash new\n");
91 printf(" -n change id \n");
92 printf(" -g get from server\n");
93 printf(" -w [1|2] show Warnings (1) or all Infos (2)\n");
106 //length of shown chars of an UTF8 string ... sometimes not the same as byte count
107 std::size_t utf8_length(std::string const &s) {
109 std::string::const_iterator begin = s.begin(), end = s.end();
110 while (begin != end) {
111 unsigned char c = *begin;
113 if ((c & 0x80) == 0) n = 1;
114 else if ((c & 0xE0) == 0xC0) n = 2;
115 else if ((c & 0xF0) == 0xE0) n = 3;
116 else if ((c & 0xF8) == 0xF0) n = 4;
125 std::vector<arg_t> comlist;
127 std::string getArg(std::string flag) {
128 for(arg_t a:comlist) {
129 if (a.flag==flag) return a.arg;
136 int getArgi(std::string flag) {
137 return atoi(getArg(flag).c_str());
142 mySensorDB *sdb=NULL;
145 snum_t getArgsnum(std::string flag) {
151 int findCPU(std::string cpu) {
152 std::ifstream fileInput;
154 fileInput.open("/proc/cpuinfo");
155 if(fileInput.is_open()) {
156 for(unsigned int curLine = 0; getline(fileInput, line); curLine++) {
157 if (line.find(cpu) != std::string::npos) {
171 owInterface *owi= NULL;
174 if (getArg("w")=="2")
175 owi->log->setLogLevel(0);
176 else if (getArg("w")=="2") owi->log->setLogLevel(0);
177 else owi->log->setLogLevel(OWLOG_ERROR);
182 void continuous(std::vector<owDevice*> *devices,int intervall,int headline,int searchrom) {
188 if (owi->isDevicesChanged()||(first==1)) {
192 for (owDevice* dev :*devices) {
193 if (dev->configstate!=OWDCS_NONE) {
194 for (size_t i=0;i<4;i++) {
195 if (dev->config->getPropertyID(i)!=0)
196 printf("\033[1;34m%02X.%02X%02X\033[0m\t",dev->getNum().byte[7],dev->getNum().byte[1],dev->getNum().byte[0]);
201 for (owDevice* dev :*devices) {
202 if (dev->configstate!=OWDCS_NONE) {
203 for (size_t i=0;i<4;i++) {
204 if (dev->config->getPropertyID(i)!=0)
205 printf("\033[0;36m%s\033[0m\t",dev->config->getQuantity(i).substr(0,7).c_str());
210 for (owDevice* dev :*devices) {
211 if (dev->configstate!=OWDCS_NONE) {
212 for (size_t i=0;i<4;i++) {
213 if (dev->config->getPropertyID(i)!=0) {
214 size_t l=utf8_length(dev->config->getUnit(i));
216 printf("\033[3;34m[%s]%s\033[0m\t",dev->config->getUnit(i).c_str(),ls.substr(0,5-l).c_str());
226 //owi->log->setLogLevel(0);
227 for (owDevice* dev : *devices) {
228 sdb->createDeviceTable(dev);
236 struct tm tm = *localtime(&t);
237 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);
238 for (owDevice* dev :*devices) {
239 for(int k=0;k<owi->maxrepeat;k++){
241 if (owi->log->last()<OWLOG_ERROR) break;
244 if (owi->log->last()<OWLOG_ERROR) {
245 if (dev->configstate!=OWDCS_NONE) {
246 for (size_t i=0;i<4;i++) {
247 if (dev->config->getPropertyID(i)!=0) {
248 if (dev->values[i]<=-10)
249 printf("%0.2f \t",dev->values[i]);
250 else if (dev->values[i]<0)
251 printf("%0.3f \t",dev->values[i]);
252 else if (dev->values[i]<10)
253 printf("%0.4f \t",dev->values[i]);
254 else if (dev->values[i]<100)
255 printf("%0.3f \t",dev->values[i]);
256 else if (dev->values[i]<1000)
257 printf("%0.2f \t",dev->values[i]);
259 printf("%0.1f \t",dev->values[i]);
265 sdb->insertValues(dev);
268 owi->log->setLogLevel(OWLOG_INFO);
269 owi->log->set(OWLOG_ERROR,"Too many errors, mybee conection is dead.");
276 while (((int)time(NULL))<(st+intervall)) sleep(1);
280 void device_menu(owDevice* d) {
281 for(int i=0;i<70;i++) printf(HL);printf("\n");
282 printf("Selected Device: ");
283 snum_t snum=d->getNum();
284 printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num);
286 printf("Chip Info: ");
287 std::vector<std::string> info=d->getFamilyInfo();
288 for (std::string s : info) printf(" %s",s.c_str());
290 printf("Values info: ");
291 int tm3d=d->readConfig();
292 for (size_t i=0;i<4;i++) {
293 printf("%s in %s",d->config->getQuantity(i).c_str(),d->config->getUnit(i).c_str());
294 if (i<4) printf("; ");
296 if (tm3d) printf(" (tm3d.de)"); else printf(" (default)");
299 for (size_t i=0;i<d->values.size();i++) {
300 printf("\033[1;33m%0.4f %s\033[0m ",d->values[i],d->config->getUnit(i).c_str());
303 for(int i=0;i<70;i++) printf(HL);printf("\n");
305 printf("1) Run continuous\n");
308 printf("Select an option: ");std::getline (std::cin,inp);
309 int dnr=atoi(inp.c_str());
312 std::vector<owDevice*> v;
314 continuous(&v,1,1,0);
316 // if (dnr<=i) break;
317 // printf("\033[4;33mSelect a number between 0 and %i\033[0m\n",i);
326 for (owDevice* dev :owi->devices) {
328 snum_t snum=dev->getNum();
330 printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num);
333 for (int j=0;j<4;j++) printf("%s ",dev->config->getQuantity(j).c_str());
336 printf("0) Exit Programm\n");
340 printf("Select device with number: ");std::getline (std::cin,inp);
341 dnr=atoi(inp.c_str());
342 if (dnr<=0) return 0;
344 printf("\033[4;33mSelect a number between 0 and %i\033[0m\n",i);
350 int questionYesNo(std::string text) {
352 printf("%s [Y/n] ",text.c_str());std::getline (std::cin,inp);
353 if (inp=="Y") return 1; else return 0;
357 void interactive(owInterface *owi) {
359 int i=selectDevice();
361 device_menu(owi->devices[i-1]);
365 int main(int argc, char *argv[]) {
368 //printf("Kommando: %s:\n", argv[0]);
370 if (**++argv != '-') {
371 //printf(" Arg. = %s\n", *argv);
372 if (comlist.empty()) {
375 comlist.push_back(a);
377 if ((*(comlist.end()-1)).arg!="1") {
380 comlist.push_back(a);
382 (*(comlist.end()-1)).arg=*argv;
386 for( i=1; (*argv)[i] != '\0'; i++) {
387 //printf(" Flag = (%c)\n", (*argv)[i]);
390 comlist.push_back(a);
395 //for(arg_t a:comlist) printf("%s->%s\n",a.flag.c_str(),a.arg.c_str());
396 if (getArg("h")=="1") {
400 std::string adapter=getArg("a");
401 if (adapter.empty()) {
405 std::transform(adapter.begin(), adapter.end(),adapter.begin(), ::toupper);
409 if(adapter.find("COM")!=std::string::npos) {
410 owi=new owCOMInterface();
411 int port=atoi(adapter.substr(adapter.find("COM")+3).c_str());
412 printf("Open /dev/ttyS%i\n",port);
413 owi->InitAdapter(port);
415 if(adapter.find("USB")!=std::string::npos) {
416 owi=new owUSBInterface();
417 int port=atoi(adapter.substr(adapter.find("USB")+3).c_str());
418 printf("Open the %i. USB Adapter\n",port);
420 if ((err=owi->InitAdapter(port))<0) {
421 if (err==-1) printf("No Adapter found!\n On Linux, try: sudo owTools....\n On Windows: Install libusb0 driver\n");
422 if (err==-2) printf("Maybe Adapter is used ... (try sudo)\nOn Linux: If the kernel modul is loaded type: sudo rmmod ds2490\n");
426 if(adapter.find("GPIO")!=std::string::npos) {
427 if (findCPU("BCM2708")) {
428 printf("\033[1;33mRaspberry Pi 1 is dedected. 1-wire on the GPIO port can have many errors in the transfer.\033[0m\n");
429 owi=new owPiGPioInterface();
430 int port=atoi(adapter.substr(adapter.find("GPIO")+4).c_str());
431 printf("Open GPIO %i\n",port);
432 } else if (findCPU("BCM2709")) {
433 owi=new owPiGPioInterface();
434 int port=atoi(adapter.substr(adapter.find("GPIO")+4).c_str());
435 printf("Open GPIO %i\n",port);
436 owi->InitAdapter(port);
438 printf("\033[1;31mGPIO works with Raspberry PI only \033[0m\n");
448 if (getArg("i")=="1") {
451 if (getArg("c")=="1") {
452 int reload=(getArg("r")=="1");
455 //owi->log->setLogLevel(3);
458 if ((s=getArg("p")) !="") {
459 pause=atoi(s.c_str());
461 if ((s=getArg("d")) !="") {
463 printf("Use Database\n");
464 sdb=new mySensorDB(s,owi->log);
465 if (sdb->log->last()>OWLOG_WARNING) {
469 if (sdb->log->last()>OWLOG_WARNING) {
472 sdb->createSensorTable();
473 if (sdb->log->last()>OWLOG_WARNING) {
480 continuous(&(owi->devices),pause,1,reload);
482 if ((s=getArg("f"))!="") {
483 printf("Flash %s in selected Device\n",s.c_str());
484 int sel=selectDevice();
486 int resetid=questionYesNo("Would you reset to default ID? (This is necessary if another device is used!)");
487 owi->log->setLogLevel(OWLOG_INFO);
488 owi->flashHEXFile(s,owi->device_nums[sel-1],resetid,1);
492 for (owDevice* dev :owi->devices) {
493 snum_t snum=dev->getNum();
494 printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num);
498 printf("%i Devices found\n",(int)owi->devices.size());
501 if ((s=getArg("v"))!="") {
502 printf("Reset VOC\n");
503 int sel=selectDevice();
505 owDevice* dev=owi->devices[sel-1];
506 std::vector<uint8_t> cl;
511 dev->Communicate(&cl,4,0);
514 if ((s=getArg("n"))!="") {
515 printf("Change id of selected Device\n");
516 int sel=selectDevice();
520 snum_t snum=owi->devices[sel]->getNum();
521 if ((getArg("g"))=="1") {
522 printf("get ID from Server\n");
524 sprintf(s,"wget -q http://www.tm3d.de/shop99/owid.php?fam=%02X -O id.txt",snum.byte[0]);
526 if ((err=system(s))==0) {
529 std::stringstream str;
535 while(fs.peek() != EOF) {
536 char c= (char) fs.get();
540 if ((c==',')|(c=='}')) {
541 isnum.byte[i]=strtol(s, NULL, 16);
542 //printf("%x\n",strtol(s, NULL, 16));
558 } else (printf("ERROR %i\n",err));
565 printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num);
569 owi->devices[sel]->changeID(snum);
573 for (owDevice* dev :owi->devices) {
574 snum_t snum=dev->getNum();
575 printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num);
579 printf("%i Devices found\n",(int)owi->devices.size());
583 for (owDevice* dev :owi->devices) {
584 printf("_______________________________________________________________________\n");
585 snum_t snum=dev->getNum();
586 printf("\033[1;34m%016llX\033[0m ",(unsigned long long)snum.num);
587 switch (dev->configstate) {
588 case OWDCS_16:printf("old Configcode from www.tm3d.de");break;
589 case OWDCS_24:printf("Configcode from www.tm3d.de");break;
590 case OWDCS_DEFAULT:printf("Default Configcode set by this Software");break;
591 case OWDCS_NONE:printf("No Configcode");break;
595 printf(" %s",dev->getFamilyInfo()[i].c_str());
597 if (dev->configstate!=OWDCS_NONE) {
598 std::vector<std::vector<std::string>> ia;
599 std::vector<size_t> maxcol;
601 for(int i=0;i<4;i++) {
602 std::vector<std::string> colm;
604 colm.push_back(dev->config->getSensorChip(i));
605 if (max<utf8_length(colm[0])) max=utf8_length(colm[0]);
606 colm.push_back(dev->config->getQuantity(i));
607 if (max<utf8_length(colm[1])) max=utf8_length(colm[1]);
608 colm.push_back(dev->config->getUnit(i));
609 if (max<utf8_length(colm[2])) max=utf8_length(colm[2]);
610 if (dev->config->getPropertyID(i)==0) {
614 sprintf(hs,"%0.3f",dev->values[i]);
617 if (max<utf8_length(colm[3])) max=utf8_length(colm[3]);
619 maxcol.push_back(max);
621 std::string space=" ";
622 for(int i=0;i<4;i++) {
623 for(int j=0;j<4;j++) {
624 printf("%s%s",ia[j][i].c_str(),space.substr(0,maxcol[j]-utf8_length(ia[j][i])+2).c_str());
630 printf("%i Devices found\n",(int)owi->devices.size());