Parcourir la source

Modified initial settings

Kanggu Park il y a 4 ans
Parent
commit
ec7d75f5fe
1 fichiers modifiés avec 48 ajouts et 16 suppressions
  1. 48 16
      RealTimeDataAccumulator.py

+ 48 - 16
RealTimeDataAccumulator.py

@@ -187,7 +187,7 @@ def get_weather(start_day, end_day):
 	
 def Check_Restoring_Unknown_past_data(targetDB_IP, targetDB_UserID, targetDB_UserPW, targetDB_Name, n_x, n_y):
 	# MSSQL Access
-	conn = pymssql.connect(host = targetDB_IP, user = targetDB_UserID, password = targetDB_UserPW, database = targetDB_Name)
+	conn = pymssql.connect(host = targetDB_IP, user = targetDB_UserID, password = targetDB_UserPW, database = targetDB_Name, autocommit=True)
 	# Create Cursor from Connection
 	cursor = conn.cursor()
 	
@@ -309,7 +309,7 @@ def Check_Restoring_Unknown_past_data(targetDB_IP, targetDB_UserID, targetDB_Use
 	
 	### Restoring unknown data from actual past weather data
 	# MSSQL Access
-	conn = pymssql.connect(host = targetDB_IP, user = targetDB_UserID, password = targetDB_UserPW, database = targetDB_Name)
+	conn = pymssql.connect(host = targetDB_IP, user = targetDB_UserID, password = targetDB_UserPW, database = targetDB_Name, autocommit=True)
 	# Create Cursor from Connection
 	cursor = conn.cursor()	
 	for i in range(len(FinalDay_UnkownDate_Tem)):
@@ -317,8 +317,7 @@ def Check_Restoring_Unknown_past_data(targetDB_IP, targetDB_UserID, targetDB_Use
 		tem_date = InitialDay_UnknownData_Tem[i]
 		for j in range(0,len(Tem),3):
 			try:
-				cursor.execute("INSERT INTO " + targetDBName + ".dbo.BemsMonitoringPointWeatherForecasted (SiteId, CreatedDateTime, Category, BaseDateTime, ForecastedDateTime, ForecastedValue, nx, ny) VALUES(1," + "'" + datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "','"+ "Temperature" + "','"+ str(tem_date) + "','" + str(tem_date) + "'," + str(Tem[j]) + "," + n_x + "," + n_y + ")")
-				conn.commit()				
+				cursor.execute("INSERT INTO " + targetDBName + ".dbo.BemsMonitoringPointWeatherForecasted (SiteId, CreatedDateTime, Category, BaseDateTime, ForecastedDateTime, ForecastedValue, nx, ny) VALUES(1," + "'" + datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "','"+ "Temperature" + "','"+ str(tem_date) + "','" + str(tem_date) + "'," + str(Tem[j]) + "," + n_x + "," + n_y + ")")			
 			except:
 				print('There is an issue in the progress of restoring unknown weather forecast data to actual past weather data. (Temperature)')						
 			tem_date += timedelta(hours=3)
@@ -328,8 +327,7 @@ def Check_Restoring_Unknown_past_data(targetDB_IP, targetDB_UserID, targetDB_Use
 		hum_date = InitialDay_UnknownData_Hum[i]
 		for j in range(0,len(Hum),3):
 			try:
-				cursor.execute("INSERT INTO " + targetDBName + ".dbo.BemsMonitoringPointWeatherForecasted (SiteId, CreatedDateTime, Category, BaseDateTime, ForecastedDateTime, ForecastedValue, nx, ny) VALUES(1," + "'" + datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "','"+ "Humidity" + "','"+ str(hum_date) + "','" + str(hum_date) + "'," + str(Hum[j]) + "," + n_x + "," + n_y + ")")
-				conn.commit()				
+				cursor.execute("INSERT INTO " + targetDBName + ".dbo.BemsMonitoringPointWeatherForecasted (SiteId, CreatedDateTime, Category, BaseDateTime, ForecastedDateTime, ForecastedValue, nx, ny) VALUES(1," + "'" + datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "','"+ "Humidity" + "','"+ str(hum_date) + "','" + str(hum_date) + "'," + str(Hum[j]) + "," + n_x + "," + n_y + ")")	
 			except:
 				print('There is an issue in the progress of restoring unknown weather forecast data to actual past weather data. (Humidity)')						
 			hum_date += timedelta(hours=3)
@@ -352,7 +350,7 @@ if __name__ == "__main__" :
 	myINI = configparser.ConfigParser()
 	myINI.read("Config.ini", "utf-8" )
 	# MSSQL Access
-	conn = pymssql.connect(host=myINI.get('LocalDB_Info','ip_address'), user=myINI.get('LocalDB_Info','user_id'), password=myINI.get('LocalDB_Info','user_password'), database=myINI.get('LocalDB_Info','db_name'))
+	conn = pymssql.connect(host=myINI.get('LocalDB_Info','ip_address'), user=myINI.get('LocalDB_Info','user_id'), password=myINI.get('LocalDB_Info','user_password'), database=myINI.get('LocalDB_Info','db_name'), autocommit=True)
 	# Create Cursor from Connection
 	cursor = conn.cursor()			
 
@@ -372,10 +370,48 @@ if __name__ == "__main__" :
 	#### Check Unknown past data when starting program ####	
 	Check_Restoring_Unknown_past_data(targetDBIP, targetDBUserID, targetDBUserPW, targetDBName, nx, ny)
 	
+	##########################################################
+	## 오늘 예보데이터 확인 후 없으면 어제 데이터 임시 삽입
+	# MSSQL Access
+	conn = pymssql.connect(host = targetDBIP, user = targetDBUserID, password = targetDBUserPW, database = targetDBName, autocommit=True)
+	# Create Cursor from Connection
+	cursor = conn.cursor()	
+	now = datetime.now()
+	cursor.execute("SELECT * FROM " + targetDBName + ".dbo.BemsMonitoringPointWeatherForecasted where SiteId = 1 and ForecastedDateTime >= '" + now.strftime('%Y-%m-%d 00:00:00') + "' ")	
+	# 데이타 하나씩 Fetch하여 출력
+	row = cursor.fetchone()
+	rawData=[]
+	while row:
+		row = cursor.fetchone()
+		rawData.append(row)
+	
+	if len(rawData)==0:
+		cursor.execute("SELECT * FROM " + targetDBName + ".dbo.BemsMonitoringPointWeatherForecasted where SiteId = 1 and ForecastedDateTime >= '" + (now - timedelta(days=1)).strftime('%Y-%m-%d 00:00:00') + "' order by CreatedDateTime desc")
+		# 데이타 하나씩 Fetch하여 출력
+		row_ = cursor.fetchone()
+		rawData_=[row_]
+		while row_:
+			row_ = cursor.fetchone()
+			rawData_.append(row_)
+		rawData_ = rawData_[:len(rawData_)-1]
+		for i in range(len(rawData_)):
+			cursor.execute("INSERT INTO " + targetDBName + ".dbo.BemsMonitoringPointWeatherForecasted (SiteId, CreatedDateTime, Category, BaseDateTime, ForecastedDateTime, ForecastedValue, nx, ny) VALUES(1," + "'" + datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "','"+ str(rawData_[i][2]) + "','" + rawData_[i][3].strftime('%Y-%m-%d 01:00:00') + "','" + (rawData_[i][4] + timedelta(days=1)).strftime('%Y-%m-%d %H:%M:%S') + "','" + str(rawData_[i][5]) + "'," + nx + "," + ny + ")")
+	conn.close()
+	##########################################################
+	
 	# Accumulate weather forecast data
 	while True:		
 		now = datetime.now()
 		if now.hour == 1 and now.minute <= 15:				## 하루에 한번 오전 1시 이후에 과거 데이터 체크 (오전 1시는 임의로 정한 시각)
+			##########################################################
+			## 예보데이터 확인 후 임시데이터라면 데이터 삭제 후 삽입
+			# MSSQL Access
+			conn = pymssql.connect(host = targetDBIP_, user = targetDBUserID_, password = targetDBUserPW_, database = targetDBName_, autocommit=True)
+			# Create Cursor from Connection
+			cursor = conn.cursor()	
+			cursor.execute("DELETE " + targetDBName + ".dbo.BemsMonitoringPointWeatherForecasted where SiteId = 1 and BaseDateTime ='" +(now - timedelta(days=2)).strftime('%Y-%m-%d 01:00:00') + "' and ForecastedDateTime >= '" + (now - timedelta(days=1)).strftime('%Y-%m-%d 00:00:00') + "' order by CreatedDateTime desc")		
+			conn.close()
+			##########################################################
 			Check_Restoring_Unknown_past_data(targetDBIP, targetDBUserID, targetDBUserPW, targetDBName, nx, ny)		## 과거 실 데이터가 어제까지만 제공되기 때문에 매일 어제 데이터가 있는지 체크
 		if now.hour == 20 and now.minute >= 45 and now.minute <= 59:
 			AccumulationActive = True
@@ -389,13 +425,13 @@ if __name__ == "__main__" :
 				time.sleep(60*15)
 		
 		if AccumulationActive:
-							
+				
 			TempWF, HumWF = get_weather_forecast(nx, ny) 
 			
 			# MSSQL Access
-			conn = pymssql.connect(host = targetDBIP, user = targetDBUserID, password = targetDBUserPW, database = targetDBName)
+			conn = pymssql.connect(host = targetDBIP, user = targetDBUserID, password = targetDBUserPW, database = targetDBName, autocommit=True)
 			# Create Cursor from Connection
-			cursor = conn.cursor()	
+			cursor = conn.cursor()		
 			
 			for i in range(1, len(TempWF)):
 				baseDate = TempWF[i][0]
@@ -406,7 +442,6 @@ if __name__ == "__main__" :
 				FcstDateTime = datetime(int(FcstDate[0:4]), int(FcstDate[4:6]), int(FcstDate[6:]), int(FcstTime[0:2]), int(FcstTime[2:]))
 				try:
 					cursor.execute("INSERT INTO " + targetDBName + ".dbo.BemsMonitoringPointWeatherForecasted (SiteId, CreatedDateTime, Category, BaseDateTime, ForecastedDateTime, ForecastedValue, nx, ny) VALUES(1," + "'" + datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "','"+ "Temperature" + "','"+ str(baseDateTime) + "','" + str(FcstDateTime) + "'," + TempWF[i][4] + "," + nx + "," + ny + ")")
-					conn.commit()
 				except:
 					print('Weather forecasted temperature data already exists! (ForecastDateTime : '+str(FcstDateTime)+')')
 			for i in range(1, len(HumWF)):
@@ -418,16 +453,13 @@ if __name__ == "__main__" :
 				FcstDateTime = datetime(int(FcstDate[0:4]), int(FcstDate[4:6]), int(FcstDate[6:]), int(FcstTime[0:2]), int(FcstTime[2:]))
 				try:
 					cursor.execute("INSERT INTO " + targetDBName + ".dbo.BemsMonitoringPointWeatherForecasted (SiteId, CreatedDateTime, Category, BaseDateTime, ForecastedDateTime, ForecastedValue, nx, ny) VALUES(1," + "'" + datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "','"+ "Humidity" + "','"+ str(baseDateTime) + "','" + str(FcstDateTime) + "'," + HumWF[i][4] + "," + nx + "," + ny + ")")
-					conn.commit()
 				except:					
-					print('Weather forecasted humidity data already exists! (ForecastDateTime : '+str(FcstDateTime)+')')
-				
+					print('Weather forecasted humidity data already exists! (ForecastDateTime : '+str(FcstDateTime)+')')		
 			conn.close()
-
+			
 			print("Sleeping for 15 minutes ...")
 			time.sleep(60*15)
 			
 			
 			
 			
-