//************************************************************************** // PicoC code for Adatis door terminal integration into Loxone // // Copyright (C) 2019, Sascha-Kr // // This program 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 3 of the License, or // (at your option) any later version. // // This program 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 this program. If not, see //************************************************************************** #define BUFF_SIZE 20000 #define RD_BLOCK_SIZE 128 char pTcpCmd[2500]; // POST command string char pTcpCmd_cont[2500]; // POST command string continued char *tok; // pointer to authentication token char str[5]; // string containing POST command length char streamname[200]; STREAM* pTcpStream; char szBuffer[BUFF_SIZE]; // buffer to store read data char szTmpBuffer[RD_BLOCK_SIZE]; // temp buffer to read into int nCnt; // bytes read from stream int nBytesReceived; // set total bytes read to zero // delay program start after boot (avoid boot loop) sleeps(120); printf("Adatis Door Terminal program started"); // get text input values TI1 and TI2 char* ip = getinputtext(0); char* password = getinputtext(1); if (!(strlen(ip) && strlen(password))) { printf("Terminal IP and/or password parameter missing"); exit(); // exit the program } strcpy(streamname, "/dev/tcp/"); strcat(streamname, ip); strcat(streamname, "/80"); do // endless loop start { // read input values, AI1 = trigger, TI2 = function code float trigger = getinput(0); float func = getinput(1); if (trigger > 0 && func > 0) // trigger and function ok - start { pTcpStream = stream_create(streamname,0,0); // create tcp stream if (pTcpStream != NULL) // tcp stream available { strcpy(pTcpCmd_cont, "token.GetYWRtaW4="); strcat(pTcpCmd_cont, password); strcat(pTcpCmd_cont, ""); sprintf(str, "%d", strlen(pTcpCmd_cont)); strcpy(pTcpCmd, "POST /RPC2 HTTP/1.1\r\nHost: "); strcat(pTcpCmd, ip); strcat(pTcpCmd, "\r\nUser-Agent: LoxLIVE [en]\r\nAccept: */*\r\nContent-Length: "); strcat(pTcpCmd, str); strcat(pTcpCmd, "\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n"); strcat(pTcpCmd, pTcpCmd_cont); stream_write(pTcpStream,pTcpCmd,strlen(pTcpCmd)); // write to output buffer stream_flush(pTcpStream); // flush output buffer nBytesReceived = 0; // set total bytes read to zero do { // read stream nCnt = stream_read(pTcpStream,szTmpBuffer,RD_BLOCK_SIZE,2000); if (nCnt + nBytesReceived > BUFF_SIZE - 1) { nBytesReceived = 0; printf("response is too large"); break; // response is too large } else if (nCnt > 0) { // data has been received, append to buffer strncpy((char*)szBuffer + nBytesReceived, szTmpBuffer, nCnt); nBytesReceived += nCnt; // add bytes read to total buffer length } } while (nCnt > 0); szBuffer[nBytesReceived] = '\0'; // extract authentication token tok = getxmlvalue(szBuffer,0,"string"); stream_close(pTcpStream); } // tcp stream available pTcpStream = stream_create(streamname,0,0); // create tcp stream if (pTcpStream != NULL) // tcp stream available { switch (func) // evaluate function code { case 1 : printf("Trigger Relais 1"); strcpy(pTcpCmd_cont, "event.SetYWRtaW4="); strcat(pTcpCmd_cont, tok); strcat(pTcpCmd_cont, "114000"); strcpy(pTcpCmd, "POST /RPC2 HTTP/1.1\r\nHost: "); strcat(pTcpCmd, ip); strcat(pTcpCmd, "\r\nUser-Agent: LoxLIVE [en]\r\nAccept: */*\r\nContent-Length: "); sprintf(str, "%d", strlen(pTcpCmd_cont)); strcat(pTcpCmd, str); strcat(pTcpCmd, "\r\nContent-Type: application/x-www-form-urlencoded\r\nExpect: 100-continue\r\n\r\n"); strcat(pTcpCmd, pTcpCmd_cont); break; case 2 : printf("Trigger Relais 2"); strcpy(pTcpCmd_cont, "event.SetYWRtaW4="); strcat(pTcpCmd_cont, tok); strcat(pTcpCmd_cont, "114100"); strcpy(pTcpCmd, "POST /RPC2 HTTP/1.1\r\nHost: "); strcat(pTcpCmd, ip); strcat(pTcpCmd, "\r\nUser-Agent: LoxLIVE [en]\r\nAccept: */*\r\nContent-Length: "); sprintf(str, "%d", strlen(pTcpCmd_cont)); strcat(pTcpCmd, str); strcat(pTcpCmd, "\r\nContent-Type: application/x-www-form-urlencoded\r\nExpect: 100-continue\r\n\r\n"); strcat(pTcpCmd, pTcpCmd_cont); break; case 3 : printf("Screensaver on (10s)"); strcpy(pTcpCmd_cont, "dc.SetYWRtaW4="); strcat(pTcpCmd_cont, tok); strcat(pTcpCmd_cont, "1210010"); strcpy(pTcpCmd, "POST /RPC2 HTTP/1.1\r\nHost: "); strcat(pTcpCmd, ip); strcat(pTcpCmd, "\r\nUser-Agent: LoxLIVE [en]\r\nAccept: application/xml, text/xml, */*; q=0.01\r\nContent-Length: "); sprintf(str, "%d", strlen(pTcpCmd_cont)); strcat(pTcpCmd, str); strcat(pTcpCmd, "Content-Type: text/xml\r\n\r\n"); strcat(pTcpCmd, pTcpCmd_cont); break; case 4 : printf("Screensaver off"); strcpy(pTcpCmd_cont, "dc.SetYWRtaW4="); strcat(pTcpCmd_cont, tok); strcat(pTcpCmd_cont, "121000"); strcpy(pTcpCmd, "POST /RPC2 HTTP/1.1\r\nHost: "); strcat(pTcpCmd, ip); strcat(pTcpCmd, "\r\nUser-Agent: LoxLIVE [en]\r\nAccept: application/xml, text/xml, */*; q=0.01\r\nContent-Length: "); sprintf(str, "%d", strlen(pTcpCmd_cont)); strcat(pTcpCmd, str); strcat(pTcpCmd, "Content-Type: text/xml\r\n\r\n"); strcat(pTcpCmd, pTcpCmd_cont); break; default : printf("No valid function code"); } // evaluate function code stream_write(pTcpStream,pTcpCmd,strlen(pTcpCmd)); // write to output buffer stream_flush(pTcpStream); // flush output buffer nBytesReceived = 0; // set total buffer length to zero do { // read stream nCnt = stream_read(pTcpStream,szTmpBuffer,RD_BLOCK_SIZE,2000); if (nCnt + nBytesReceived > BUFF_SIZE - 1) { nBytesReceived = 0; printf("Page is too large"); break; //Page is too large } else if (nCnt > 0) { strncpy((char*)szBuffer + nBytesReceived, szTmpBuffer, nCnt); nBytesReceived += nCnt; } } while (nCnt > 0); szBuffer[nBytesReceived] = '\0'; free(tok); stream_close(pTcpStream); sleeps(1); // sleeptime before next command can be executed } // tcp stream available } sleep(500); // sleeptime for each loop } while (1); // endless loop end