diff --git a/app.py b/app.py index e1bb688..c05cbb7 100755 --- a/app.py +++ b/app.py @@ -7,16 +7,18 @@ import time import requests import json -global last_indoor_data +# global last_indoor_data global indoor_server_ip global indoor_server_password global outdoor_api_url +global location # Load the config file "config.json" config = json.loads(open("config.json", "r").read()) indoor_server_ip = config["indoor_server_ip"] indoor_server_password = config["indoor_server_password"] outdoor_api_url = config["outdoor_api_url"] +location = config["location"] # Assume that the indoor unit is offline # The get_indoor_data() function will update this variable @@ -24,86 +26,86 @@ last_indoor_data = { "offline": True } -def get_indoor_data() -> list: - global indoor_server_ip - global indoor_server_password - # SMB server details - server_name = indoor_server_ip - share_name = "airvisual" - username = "airvisual" - password = indoor_server_password +# def get_indoor_data() -> list: +# global indoor_server_ip +# global indoor_server_password +# # SMB server details +# server_name = indoor_server_ip +# share_name = "airvisual" +# username = "airvisual" +# password = indoor_server_password - # File details, The file is a text file with name: - # _AirVisual_values.txt - # Get the prefix of the file name - prefix = time.strftime("%Y%m", time.localtime()) - file_path = prefix + "_AirVisual_values.txt" +# # File details, The file is a text file with name: +# # _AirVisual_values.txt +# # Get the prefix of the file name +# prefix = time.strftime("%Y%m", time.localtime()) +# file_path = prefix + "_AirVisual_values.txt" - # Connect to the SMB server - conn = SMBConnection(username, password, "", "") - conn.connect(server_name, 139) +# # Connect to the SMB server +# conn = SMBConnection(username, password, "", "") +# conn.connect(server_name, 139) - # Read the file contents - file_obj = open(file_path, "wb") - conn.retrieveFile(share_name, file_path, file_obj) - conn.close() +# # Read the file contents +# file_obj = open(file_path, "wb") +# conn.retrieveFile(share_name, file_path, file_obj) +# conn.close() - # Open the local cached file - file_obj = open(file_path, "r") +# # Open the local cached file +# file_obj = open(file_path, "r") - # The first line of the file contains the header - # The header contains the column names separated by a semicolon (;) - # The rest of the file contains the data separated by a semicolon (;) - # Extract the column names and the data from the file - file_obj.seek(0) - header = file_obj.readline().strip().split(";") - data = file_obj.readlines() - # Split all the data into a list of lists - data = [row.strip().split(";") for row in data] - file_obj.close() +# # The first line of the file contains the header +# # The header contains the column names separated by a semicolon (;) +# # The rest of the file contains the data separated by a semicolon (;) +# # Extract the column names and the data from the file +# file_obj.seek(0) +# header = file_obj.readline().strip().split(";") +# data = file_obj.readlines() +# # Split all the data into a list of lists +# data = [row.strip().split(";") for row in data] +# file_obj.close() - # Remap the header names - headers_map = { - "PM2_5(ug/m3)": "pm25", - "PM10(ug/m3)": "pm10", - "PM1(ug/m3)": "pm1", - "CO2(ppm)": "co2", - "AQI(US)": "aqi", - "Temperature(C)": "temperature", - "Humidity(%RH)": "humidity", - "Timestamp": "time" - } +# # Remap the header names +# headers_map = { +# "PM2_5(ug/m3)": "pm25", +# "PM10(ug/m3)": "pm10", +# "PM1(ug/m3)": "pm1", +# "CO2(ppm)": "co2", +# "AQI(US)": "aqi", +# "Temperature(C)": "temperature", +# "Humidity(%RH)": "humidity", +# "Timestamp": "time" +# } - # Remove rows with header names that are not in the header map - # First, get the indices of the header names that are in the header map - headers_indices = [] - for index, name in enumerate(header): - if name in headers_map: - headers_indices.append(index) +# # Remove rows with header names that are not in the header map +# # First, get the indices of the header names that are in the header map +# headers_indices = [] +# for index, name in enumerate(header): +# if name in headers_map: +# headers_indices.append(index) - # Construct the new header with the header names that are in the header map - header = [header[index] for index in headers_indices] +# # Construct the new header with the header names that are in the header map +# header = [header[index] for index in headers_indices] - # Construct the new data with only the columns indicated by the header indices - data = [[row[index] for index in headers_indices] for row in data] +# # Construct the new data with only the columns indicated by the header indices +# data = [[row[index] for index in headers_indices] for row in data] - # Remap the header names - headers = [headers_map[name] for name in header] +# # Remap the header names +# headers = [headers_map[name] for name in header] - # Convert unix timestamp to human readable time - for row in data: - row[headers.index("time")] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(row[headers.index("time")]))) +# # Convert unix timestamp to human readable time +# for row in data: +# row[headers.index("time")] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(row[headers.index("time")]))) - # Create a list of dictionaries representing the data - # Each dictionary represents a row of data - data_list = [] - for row in data: - data_dict = {} - for header in headers: - data_dict[header] = row[headers.index(header)] - data_list.append(data_dict) - return data_list +# # Create a list of dictionaries representing the data +# # Each dictionary represents a row of data +# data_list = [] +# for row in data: +# data_dict = {} +# for header in headers: +# data_dict[header] = row[headers.index(header)] +# data_list.append(data_dict) +# return data_list def get_outdoor_data_current() -> dict: # Fetch the data from the AirVisual API @@ -163,6 +165,7 @@ def get_outdoor_data_current() -> dict: data["last_updated"] = int(time.time()) open("outdoor_data_cache.txt", "w").write(json.dumps(data)) # Remove the last_updated key + # TODO store the data in a database return data except: # Oops, we got rate limited @@ -193,11 +196,17 @@ app = Flask(__name__) # Refresh the indoor data every 30 seconds def refresh_data(): while True: - print("Fetching indoor data!") - indoor_data = get_indoor_data() - global last_indoor_data - # last_indoor_data the last dictionary in the list - last_indoor_data = indoor_data[-1] + + # print("Fetching indoor data!") + # indoor_data = get_indoor_data() + # global last_indoor_data + # # last_indoor_data the last dictionary in the list + # last_indoor_data = indoor_data[-1] + + # Fetch the outdoor data + print("Fetching outdoor data!") + outdoor_data = get_outdoor_data_current() + sleep(30) # Start the thread to refresh the data @@ -206,10 +215,14 @@ Thread(target=refresh_data).start() # Return All Data in the current month @app.route("/get_data", methods=["GET"]) def get_data_route(): - global last_indoor_data - indoor_data = last_indoor_data + global location + # global last_indoor_data + # indoor_data = last_indoor_data + + # Indoor data fetch is disabled + indoor_data = {} + outdoor_data = get_outdoor_data_current() merged_data = merge_data(indoor_data, outdoor_data) - return jsonify(merged_data) - -app.run("0.0.0.0", 5000) \ No newline at end of file + merged_data["location"] = location + return jsonify(merged_data) \ No newline at end of file diff --git a/apply_config.sh b/apply_config.sh new file mode 100755 index 0000000..348bc19 --- /dev/null +++ b/apply_config.sh @@ -0,0 +1 @@ +curl -X PUT --data-binary @nginx.json --unix-socket /var/run/control.unit.sock http://localhost/config diff --git a/static/card.html b/static/card.html new file mode 100644 index 0000000..9ca394e --- /dev/null +++ b/static/card.html @@ -0,0 +1,277 @@ + + + + + + + Air Quality Dashboard + + + + + + + + + + +
+
+

Loading . . .

+
+
+ +

Healthy

+
+
+

N/A

+
+

Air Quality Index (US)

+
+
+
+

N/A

+
+

PM2.5 (μg/m3)

+
+
+
+
+
+ +

Temperature: N/A

+
+
+ +

Humidity: N/A

+
+
+
+ + + + + + + \ No newline at end of file diff --git a/static/index.html b/static/index.html index 25e760d..e8ff6e5 100644 --- a/static/index.html +++ b/static/index.html @@ -53,8 +53,8 @@

Air Quality Monitoring System

Chulalongkorn University Demonstation School

Where are you?

- Primary School - Secondary School + Primary School + Secondary School
diff --git a/static/index_global.html b/static/index_global_old.html similarity index 98% rename from static/index_global.html rename to static/index_global_old.html index bfb1990..57e0185 100644 --- a/static/index_global.html +++ b/static/index_global_old.html @@ -19,12 +19,9 @@ align-items: center; height: 100vh; margin: 0; - background-color: #828282; font-family: "Roboto", sans-serif; } .card_frame { - margin-top: 10px; - margin-left: 10px; display: grid; width: 750px; height: 230px; diff --git a/static/satite/index.html b/static/satite/index.html deleted file mode 120000 index 45f996a..0000000 --- a/static/satite/index.html +++ /dev/null @@ -1 +0,0 @@ -/app/iqair-apiserver/static/index_global.html \ No newline at end of file diff --git a/static/satitm/index.html b/static/satitm/index.html deleted file mode 120000 index 45f996a..0000000 --- a/static/satitm/index.html +++ /dev/null @@ -1 +0,0 @@ -/app/iqair-apiserver/static/index_global.html \ No newline at end of file