001/* 002 * $Revision: 1406 $ 003 * $Author: tgutwin $ 004 * $Date: 2020-11-08 11:09:35 -0800 (Sun, 08 Nov 2020) $ 005 * $URL: svn://fred.webarts.bc.ca/open/trunk/projects/WebARTS/ca/bc/webarts/tools/WeatherDataLogger.java $ 006 */ 007/* 008 * 009 * Written by Tom Gutwin - WebARTS Design. 010 * Copyright (C) 2020 WebARTS Design, Burnaby Canada 011 * http://www.webarts.bc.ca 012 * 013 * This program is free software; you can redistribute it and/or modify 014 * it under the terms of the GNU General Public License as published by 015 * the Free Software Foundation; either version 2 of the License, or 016 * (at your option) any later version. 017 * 018 * This program is distributed in the hope that it will be useful, 019 * but WITHOUT ANY WARRANTY; without_ even the implied warranty of 020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 021 * GNU General Public License for more details. 022 * 023 * You should have received a copy of the GNU General Public License 024 * along with this program; if not, write to the Free Software 025 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 026 */ 027package ca.bc.webarts.tools; 028 029import ca.bc.webarts.tools.WeatherStationRestRequester; 030import ca.bc.webarts.widgets.Util; 031 032/** A basic SQLQuery client that retrieves WeatherStation data and stores it into a database. 033 * 034 * This class has a main method to execute directly as an app messaging to a specified serverIP on {@link #DEFAULT_PORT DEFAULT_port} {@link #DEFAULT_PORT 44444}.<pre> 035 * java ca.bc.webarts.tools.WeatherDataLogger [WEATHERSTATION_HOST] 036 * - WEATHERSTATION_HOST is optional</pre> 037 * 038 **/ 039public class WeatherDataLogger extends SqlQuery 040{ 041 042 protected static String DEFAULT_DB_SERVER_HOST_IP = "10.0.0.200"; 043 protected static String DEFAULT_DB_NAME = "temperatureLog"; 044 protected static String DEFAULT_DB_USER = "tgutwin"; 045 protected static String DEFAULT_DB_PASSWORD = "18BoogieWoogie"; 046 protected static String DEFAULT_WEATHERSTATION_HOST = "10.0.0.80"; 047 048 /** Simple multi-level debug flag (0-2).The higher the number, the higher the verbosity. **/ 049 protected int debug_ = 1; 050 protected String weatherStationHostIP_ = DEFAULT_WEATHERSTATION_HOST; 051 protected String weatherDBHostIP_ = DEFAULT_DB_SERVER_HOST_IP; 052 protected String weatherDBName_ = DEFAULT_DB_NAME; 053 protected String weatherDBUser_ = DEFAULT_DB_USER; 054 protected String weatherDBPassword_ = DEFAULT_DB_PASSWORD; 055 056 protected WeatherStationRestRequester wsRR_ = new WeatherStationRestRequester(); 057 058 /** Default Constructor for the WeatherDataLogger object and uses the defaults weatherStation host. */ 059 public WeatherDataLogger() 060 { 061 debugOut = true; 062 /** My DB is a MariaDB, so set The SQLQuery DB type. **/ 063 //setUseMysqlDB(); 064 setUseMariadbDB(); 065 setDbIP(weatherDBHostIP_); 066 setDbName(weatherDBName_); 067 setDbUser(weatherDBUser_); 068 setDbPassword(weatherDBPassword_); 069 }// -- Constructor 070 071 072 /** Default Constructor for the WeatherDataLogger object and uses the defaults weatherStation host. */ 073 public WeatherDataLogger(String weatherStationHostIP) 074 { 075 debugOut = true; 076 /** My DB is a MariaDB, so set The SQLQuery type. **/ 077 //setUseMysqlDB(); 078 setUseMariadbDB(); 079 weatherStationHostIP_ = weatherStationHostIP; 080 setDbIP(weatherStationHostIP_); 081 setDbName(weatherDBName_); 082 setDbUser(weatherDBUser_); 083 setDbPassword(weatherDBPassword_); 084 }// -- Constructor 085 086 087 public static void main(String [] args) 088 { 089 StringBuffer helpMsg = new StringBuffer(SYSTEM_LINE_SEPERATOR); 090 helpMsg.append("--- WebARTS Database Query Helper Class ---------------------\n"); 091 helpMsg.append("---------------------------------------------------------------\n"); 092 helpMsg.append(SYSTEM_LINE_SEPERATOR); 093 helpMsg.append("ca.bc.webarts.tools.WeatherDataLogger [--serverIP=someNewIP] [--dbName=someOtherDBName] [--username=yourDBUserName] [--password=yourGroceryList] [sql]\n"); 094 helpMsg.append(" ALL commandlineOptions are OPTIONAL\n"); 095 helpMsg.append(" sql HASToBe / MuStBe at the end and all remaining parameters re combined to form th SQL\n"); 096 helpMsg.append(" DEFUALT with no options is to start logging to default database.\n"); 097 098 WeatherDataLogger instance = new WeatherDataLogger(); 099 String sqlQ = ""; 100 if (args==null || args.length<1) 101 { 102 System.out.println(helpMsg.toString()); 103 System.out.print("WeatherDataLogger> DB Connection ("+instance.getDbUser()+":"+instance.getDbPassword()+ "): "); 104 System.out.println(instance.getDbConnectString()); 105 System.out.println(instance.canConnect()); 106 107 instance.initiateLogging(); 108 } 109 else 110 { 111 String p = ""; 112 int l = 0; 113 for (int i=0; i< args.length; i++) 114 { 115 p = "--serverIP="; l = p.length(); 116 if (args[i].startsWith(p) && args[i].length()>l) instance.setDbIP(args[i].substring(l)); 117 p = "--dbName="; l = p.length(); 118 if (args[i].startsWith(p) && args[i].length()>l) instance.setDbName(args[i].substring(l)); 119 p = "--username="; l = p.length(); 120 if (args[i].startsWith(p) && args[i].length()>l) instance.setDbUser(args[i].substring(l)); 121 p = "--password="; l = p.length(); 122 if (args[i].startsWith(p) && args[i].length()>l) instance.setDbPassword(args[i].substring(l)); 123 124 if (!args[i].startsWith("--") ) 125 { 126 //SQL 127 sqlQ += " " + args[i]; 128 } 129 } 130 sqlQ = sqlQ.trim(); 131 132 if(sqlQ.length()>0) 133 { 134 System.out.print("Executing SQL query: "); 135 System.out.println(sqlQ); 136 System.out.print("DB Connection ("+instance.getDbUser()+":"+instance.getDbPassword()+ "): "); 137 System.out.println(instance.getDbConnectString()); 138 StringBuffer sqlResultSet = instance.query(sqlQ); 139 System.out.println(sqlResultSet.toString()); 140 } 141 } 142 } 143 144 145 /** Starts logging all weather data to database. **/ 146 public void initiateLogging() 147 { 148 System.out.println(" Initiating Logging..."); 149 int sleepTime = 60000; 150 151 while(true) 152 { 153 // read data from weatherStation rest calls 154 double temp = wsRR_.getTemperature(); 155 String tempUnits = wsRR_.getTemperatureUnits(); 156 double pressure = wsRR_.getPressure(); 157 String pressureUnits = wsRR_.getPressureUnits(); 158 159 // store in database 160 StringBuffer sqlResultSet = instance.query(sqlQ); 161 162 Util.sleep(sleepTime); 163 } 164 } 165 166 167 /** Gets the SQL that will log (add a DB Table row) the temperature data. **/ 168 private String getLogTempSql(double temp, String tempUnits) 169 { 170 String retVal = ""; 171 //insert into temperatureLog.temperature (value, units) VALUE (temp, tempUnits); 172 173 return retVal; 174 } 175}