From e7fedeae6295a34ab4c0eb7d8ff90050e39306d6 Mon Sep 17 00:00:00 2001 From: Mike Vesprini Date: Thu, 21 Sep 2023 10:08:00 -0700 Subject: [PATCH] chore: remove SS and rfs from the code base --- .github/workflows/build-and-push.yaml | 8 - README.md | 4 +- api/main.py | 131 +--------------- app/pages/index.tsx | 6 - bin/vueLocalDeploy.sh | 4 +- docker-compose.yml | 17 --- prod.docker-compose.yml | 17 --- real_fake_sensors/Dockerfile | 14 -- real_fake_sensors/README.md | 4 - real_fake_sensors/main.py | 88 ----------- real_fake_sensors/requirements.txt | 4 - sensational_sensors/Dockerfile | 14 -- sensational_sensors/README.md | 3 - sensational_sensors/main.py | 210 -------------------------- sensational_sensors/requirements.txt | 4 - 15 files changed, 6 insertions(+), 522 deletions(-) delete mode 100644 real_fake_sensors/Dockerfile delete mode 100644 real_fake_sensors/README.md delete mode 100644 real_fake_sensors/main.py delete mode 100644 real_fake_sensors/requirements.txt delete mode 100644 sensational_sensors/Dockerfile delete mode 100644 sensational_sensors/README.md delete mode 100644 sensational_sensors/main.py delete mode 100644 sensational_sensors/requirements.txt diff --git a/.github/workflows/build-and-push.yaml b/.github/workflows/build-and-push.yaml index 80a05cc8..0ca128fc 100644 --- a/.github/workflows/build-and-push.yaml +++ b/.github/workflows/build-and-push.yaml @@ -21,14 +21,6 @@ jobs: name: api build-args: | "BB_TOKEN=${{ secrets.BB_TOKEN }}" - - image: ghcr.io/button-inc/iot-system-prototype/real_fake_sensors - context: . - dockerfile: real_fake_sensors/Dockerfile - name: realFakeSensors - - image: ghcr.io/button-inc/iot-system-prototype/sensational_sensors - context: . - dockerfile: sensational_sensors/Dockerfile - name: sensationalSensors name: Build ${{ matrix.name }} docker image steps: - uses: actions/checkout@v3 diff --git a/README.md b/README.md index 1a4ecb54..4ae1a1d8 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ To stop, `ctrl`/`cmd` + `c`, or `docker-compose stop` if using the daemon. When working on the frontend code it can be useful to run `docker-compose stop app` , then `cd ./app`, `yarn dev` for the live changes. ## For backend reload on change + Need to have three terminals open: + - `cd api` then `uvicorn main:app --host 0.0.0.0 --port 8080 --reload` -- `cd real_fake_sensors` then `uvicorn main:app --host 0.0.0.0 --port 8081 --reload` -- `cd sensational_sensors` then `uvicorn main:app --host 0.0.0.0 --port 8082 --reload` \ No newline at end of file diff --git a/api/main.py b/api/main.py index 8efb15c7..bd7860b7 100644 --- a/api/main.py +++ b/api/main.py @@ -36,14 +36,6 @@ # 📚 Authenticate with Google Sheets service account sa = gspread.service_account(filename="google_sheets_sa_key.json") -# 🌎 Set base URLs based on the environment -if env == "prod": - REAL_FAKE_SENSORS_BASE_URL = "http://real-fake-sensors:8081" - SENSATIONAL_SENSORS_BASE_URL = "http://sensational-sensors:8082" -else: - REAL_FAKE_SENSORS_BASE_URL = "http://localhost:8081" - SENSATIONAL_SENSORS_BASE_URL = "http://localhost:8082" - # 🚀 Initialize FastAPI application app = FastAPI() @@ -51,10 +43,6 @@ origins = [ "http://localhost:5173", "http://localhost:3000", - "http://localhost:8081", - "http://localhost:8082", - "http://0.0.0.0:8081", - "http://0.0.0.0:8082", "http://wav.button.build", "http://34.123.69.225/", "http://34.123.69.225:80", @@ -97,35 +85,6 @@ class BasicSensor(BaseModel): bin_volume: str -# 📝 Model to represent a RealFakeSensor -class RealFakeSensor(BaseModel): - sensorsID: str - sensorCompany: str - sensorDeviceID: int - firmwareVersion: str - clientId: int - simCardNumber: str - connectivityProvider: str - latest_sensors_data: dict | None - latitude: float - longitude: float - asset_tag: str - bin_volume: str - - -# 📝 Model to represent a SensationalSensor -class SensationalSensor(BaseModel): - id: str - sensor_type: SensorType - fill_level: int - sim: str - lat: float - long: float - man: str - asset_tag: str - bin_volume: str - - # 📝 Model to represent a BrighterBinsSensorReading class BrighterBinsSensorReading(BaseModel): epoch_ms: int @@ -190,72 +149,6 @@ class TekelekSensor(BaseModel): PercentFull: Optional[float] = None -# 🔧 Function to convert RealFakeSensor to BasicSensor -def rfs_to_bs(sensor: RealFakeSensor) -> BasicSensor: - return BasicSensor( - id=sensor["sensorsID"], - sensor_type=SensorType.LIQUID_BIN_LEVEL, - fill_level=sensor["latest_sensors_data"]["level"] - if sensor["latest_sensors_data"] - else None, - lat=sensor["latitude"], - long=sensor["longitude"], - manufacturer=sensor["sensorCompany"], - bin_name=sensor["bin_name"], - address_line1=sensor["address_line1"], - address_line2=sensor["address_line2"], - group=sensor["group"], - bin_type=sensor["bin_type"], - material_type=sensor["material_type"], - asset_tag=sensor["asset_tag"], - bin_volume=sensor["bin_volume"], - ) - - -# 🔧 Function to convert RealFakeSensor dictionary to BasicSensor list -def rfs_dict_to_bs_list( - sensors: dict[str, RealFakeSensor | None] -) -> list[BasicSensor | None]: - bs_list = [] - for index, sensor_id in enumerate(sensors): - if sensor_id in sensors: - bs_list.append(rfs_to_bs(sensors[sensor_id])) - - return bs_list - - -# 🔧 Function to convert SensationalSensor to BasicSensor -def sensational_sensor_to_basic_sensor(sensor: SensationalSensor) -> BasicSensor: - return BasicSensor( - id=sensor["id"], - sensor_type=SensorType.SOLID_BIN_LEVEL, - fill_level=sensor["fill_level"] if sensor["fill_level"] else None, - lat=sensor["lat"], - long=sensor["long"], - manufacturer=sensor["man"], - bin_name=sensor["bin_name"], - address_line1=sensor["address_line1"], - address_line2=sensor["address_line2"], - group=sensor["group"], - bin_type=sensor["bin_type"], - material_type=sensor["material_type"], - asset_tag=sensor["asset_tag"], - bin_volume=sensor["bin_volume"], - ) - - -# 🔧 Function to convert SensationalSensor dictionary to BasicSensor list -def ss_dict_to_bs_list( - sensors: dict[str, SensationalSensor | None] -) -> list[BasicSensor | None]: - bs_list = [] - for index, sensor_id in enumerate(sensors): - if sensor_id in sensors: - bs_list.append(sensational_sensor_to_basic_sensor(sensors[sensor_id])) - - return bs_list - - # 🔧 Function to convert TekelekSensor to BasicSensor def tkl_to_bs(sensor: TekelekSensor) -> BasicSensor: lat, long = None, None # Default values in case of errors @@ -442,27 +335,9 @@ def make_http_request(url, method="GET", headers=None, params=None, data=None): # Caches for sensor data -rfs_cache = [] -ss_cache = [] bb_cache = {} -last_run_timestamp = 0 tkl_cache = {} - - -# 🤖 Event handler: Populate rfs_cache and ss_cache with initial data on start up -@app.on_event("startup") -def set_rfs_and_ss_cache(): - global rfs_cache - global ss_cache - # Convert the sensors to the common "BasicSensor" type and cache their values - # rfs and ss are mock so they have a simple case, we can fetch once and never again, they won't change. - rfs_response = requests.get(REAL_FAKE_SENSORS_BASE_URL + "/sensors").json() - rfs_as_bs = rfs_dict_to_bs_list(rfs_response) - rfs_cache = rfs_as_bs - # print(f"rfs_cache: {rfs_cache}") - ss_response = requests.get(SENSATIONAL_SENSORS_BASE_URL + "/sensors").json() - ss_cache = ss_dict_to_bs_list(ss_response) - return +last_run_timestamp = 0 # 🤖 Event handler: Populate Tekelek data in tkl_cache with initial data on start up @@ -611,8 +486,6 @@ def update_bb_cache() -> None: # 🚀 API endpoint: Get the latest readings from all types of sensors @app.get("/latest_readings") def get_latest_readings(): - global rfs_cache - global ss_cache global bb_cache global tkl_cache @@ -626,7 +499,7 @@ def get_latest_readings(): for index, sensor in enumerate(tkl_cache): tkl_readings.append(tkl_cache[sensor]) - latest_readings = bb_readings + tkl_readings + rfs_cache + ss_cache + latest_readings = bb_readings + tkl_readings # latest_readings = filter_nulls(latest_readings) return {"sensors": latest_readings} diff --git a/app/pages/index.tsx b/app/pages/index.tsx index 9112e875..bc21855a 100644 --- a/app/pages/index.tsx +++ b/app/pages/index.tsx @@ -61,12 +61,6 @@ const Home: NextPage = () => { for (const sensorId in responseJson.brighterBins) { sensorsArray.push(responseJson.brighterBins[sensorId]); } - for (const sensorId in responseJson.realFakeSensors) { - sensorsArray.push(responseJson.realFakeSensors[sensorId]); - } - for (const sensorId in responseJson.sensationalSensors) { - sensorsArray.push(responseJson.sensationalSensors[sensorId]); - } setSensors(sensorsArray); }, []); diff --git a/bin/vueLocalDeploy.sh b/bin/vueLocalDeploy.sh index 5520896a..21688f8f 100755 --- a/bin/vueLocalDeploy.sh +++ b/bin/vueLocalDeploy.sh @@ -24,7 +24,7 @@ sleep .5 echo "looking up docker containers..." docker container ls -a sleep .5 -if [ "$( docker inspect --format '{{json .State.Running}}' real-fake-sensors )" = "true" ] && [ "$( docker inspect --format '{{json .State.Running}}' sensational-sensors )" = "true" ]; +if [ "$( docker inspect --format '{{json .State.Running}}' api )" = "true" ]; then echo "docker is running, continuing execution..." else @@ -48,4 +48,4 @@ echo "installing node packages..." npm install echo "starting up vue app..." -npm run start \ No newline at end of file +npm run start diff --git a/docker-compose.yml b/docker-compose.yml index ff28617e..85dcbeac 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,20 +1,6 @@ version: "3.9" services: - real_fake_sensors: - container_name: real-fake-sensors - build: - context: . - dockerfile: real_fake_sensors/Dockerfile - ports: - - "8081:8081" - sensational_sensors: - container_name: sensational-sensors - build: - context: . - dockerfile: sensational_sensors/Dockerfile - ports: - - "8082:8082" api: container_name: api build: @@ -22,9 +8,6 @@ services: dockerfile: api/Dockerfile ports: - "8080:8080" - depends_on: - - real_fake_sensors - - sensational_sensors environment: - ENVIRONMENT=prod app: diff --git a/prod.docker-compose.yml b/prod.docker-compose.yml index 9bb8e1aa..cd6b8dcf 100644 --- a/prod.docker-compose.yml +++ b/prod.docker-compose.yml @@ -1,20 +1,6 @@ version: "3.9" services: - real_fake_sensors: - container_name: real-fake-sensors - build: - context: . - dockerfile: real_fake_sensors/Dockerfile - ports: - - "8081:8081" - sensational_sensors: - container_name: sensational-sensors - build: - context: . - dockerfile: sensational_sensors/Dockerfile - ports: - - "8082:8082" api: container_name: api build: @@ -22,9 +8,6 @@ services: dockerfile: api/Dockerfile ports: - "8080:8080" - depends_on: - - real_fake_sensors - - sensational_sensors environment: - ENVIRONMENT=prod app: diff --git a/real_fake_sensors/Dockerfile b/real_fake_sensors/Dockerfile deleted file mode 100644 index 376ef2a0..00000000 --- a/real_fake_sensors/Dockerfile +++ /dev/null @@ -1,14 +0,0 @@ -FROM python:3.11 - -WORKDIR / - -COPY ./real_fake_sensors/requirements.txt /requirements.txt - -RUN pip install --no-cache-dir --upgrade -r /requirements.txt -COPY ./real_fake_sensors/main.py main.py - -EXPOSE 8081 -CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8081"] - -# If running behind a proxy like Nginx or Traefik add --proxy-headers -# CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80", "--proxy-headers"] diff --git a/real_fake_sensors/README.md b/real_fake_sensors/README.md deleted file mode 100644 index 5314c02f..00000000 --- a/real_fake_sensors/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# Real Fake Sensors! - -Are you tired of hitting an API and getting a response from a **real** sensor? -Come on down to Real Fake Sensors! When you hit our API you'll get responses from fake sensors! diff --git a/real_fake_sensors/main.py b/real_fake_sensors/main.py deleted file mode 100644 index 73e7cfc3..00000000 --- a/real_fake_sensors/main.py +++ /dev/null @@ -1,88 +0,0 @@ -from enum import Enum - -from fastapi import FastAPI, HTTPException -from fastapi.middleware.cors import CORSMiddleware -from pydantic import BaseModel - -app = FastAPI() - -# whitelist -origins = ["http://localhost:8080", "api:8080"] - -app.add_middleware( - CORSMiddleware, - allow_origins=origins, - allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"], -) - - -class Sensor(BaseModel): - sensorsID: str - sensorCompany: str - sensorDeviceID: int - firmwareVersion: str - clientId: int - simCardNumber: str - connectivityProvider: str - latest_sensors_data: dict | None - latitude: float - longitude: float - bin_name: str - address_line1: str - address_line2: str - group: str - bin_type: str - material_type: str - asset_tag: str - bin_volume: str - - -sensors = { - "2453473454": { - "sensorsID": "2453473454", - "sensorCompany": "Real Fake Sensors", - "sensorDeviceID": 523452, - "firmwareVersion": "0.1.4", - "clientId": 1, - "simCardNumber": "12344423", - "connectivityProvider": "RFS inc", - "latest_sensors_data": {"level": 61, "timestamp": 1691381304819}, - "latitude": 43.844880, - "longitude": -80.057859, - "bin_name": "Big Purple Bin", - "address_line1": "55 John St", - "address_line2": "Alton, ON L7K 0C4", - "group": "Alton East", - "bin_type": "EMW Cathedral Container 10yd", - "material_type": "Cardboard", - "asset_tag": "up", - "bin_volume": "small", - }, - "1846567335": { - "sensorsID": "1846567335", - "sensorCompany": "Real Fake Sensors", - "sensorDeviceID": 5673546, - "firmwareVersion": "0.2.1", - "clientId": 1, - "simCardNumber": "+5424524555", - "connectivityProvider": "RFS inc", - "latest_sensors_data": {"level": 84, "timestamp": 1691381304819}, - "latitude": 43.666729, - "longitude": -79.549713, - "bin_name": "Little Orange Bin", - "address_line1": "127 Princess Margaret", - "address_line2": "Etobicoke, ON M9B 2Z4", - "group": "Etobicoke", - "bin_type": "EMW Cathedral Container 10yd", - "material_type": "Cardboard", - "asset_tag": "down", - "bin_volume": "large", - }, -} - - -@app.get("/sensors") -def get_sensors() -> dict[str, Sensor]: - return sensors diff --git a/real_fake_sensors/requirements.txt b/real_fake_sensors/requirements.txt deleted file mode 100644 index d275081e..00000000 --- a/real_fake_sensors/requirements.txt +++ /dev/null @@ -1,4 +0,0 @@ -fastapi>=0.68.0,<0.69.0 -pydantic>=1.8.0,<2.0.0 -uvicorn>=0.15.0,<0.16.0 -requests diff --git a/sensational_sensors/Dockerfile b/sensational_sensors/Dockerfile deleted file mode 100644 index 903c0778..00000000 --- a/sensational_sensors/Dockerfile +++ /dev/null @@ -1,14 +0,0 @@ -FROM python:3.11 - -WORKDIR / - -COPY ./sensational_sensors/requirements.txt /requirements.txt - -RUN pip install --no-cache-dir --upgrade -r /requirements.txt -COPY ./sensational_sensors/main.py main.py - -EXPOSE 8082 -CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8082"] - -# If running behind a proxy like Nginx or Traefik add --proxy-headers -# CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80", "--proxy-headers"] diff --git a/sensational_sensors/README.md b/sensational_sensors/README.md deleted file mode 100644 index 478e67a0..00000000 --- a/sensational_sensors/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Sensational Sensors - -A placeholder API for real IoTsensor APIs. diff --git a/sensational_sensors/main.py b/sensational_sensors/main.py deleted file mode 100644 index 4e0549b9..00000000 --- a/sensational_sensors/main.py +++ /dev/null @@ -1,210 +0,0 @@ -from enum import Enum - -from fastapi import FastAPI, HTTPException -from fastapi.middleware.cors import CORSMiddleware -from pydantic import BaseModel - -app = FastAPI() - -# whitelist -origins = [ - "http://localhost:8080", - "http://0.0.0.0:8081", -] - -app.add_middleware( - CORSMiddleware, - allow_origins=origins, - allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"], -) - - -class SensorType(Enum): - LIQUID_BIN_LEVEL = "liquid bin level" - - -class Sensor(BaseModel): - id: str - sensor_type: SensorType - fill_level: int - sim: str - lat: float - long: float - man: str - bin_name: str - address_line1: str - address_line2: str - group: str - bin_type: str - material_type: str - asset_tag: str - bin_volume: str - - -sensors = { - "564746584": { - "id": "564746584", - "sensor_type": SensorType.LIQUID_BIN_LEVEL, - "fill_level": 80, - "sim": "3jdf93deq", - "lat": 43.828165, - "long": -80.1, - "man": "Sensational Sensors", - "bin_name": "Big Blue Bin", - "address_line1": "19556 Mississauga Rd", - "address_line2": "Alton, ON L7K 1M5", - "group": "Alton North", - "bin_type": "EMW Cathedral Container 10yd", - "material_type": "Cardboard", - "asset_tag": "strange", - "bin_volume": "medium", - }, - "134563456": { - "id": "134563456", - "sensor_type": SensorType.LIQUID_BIN_LEVEL, - "fill_level": 75, - "sim": "asdgdfhfj", - "lat": 43.601509, - "long": -79.884398, - "man": "Sensational Sensors", - "bin_name": "Little Red Bin", - "address_line1": "13850 Steeles Ave", - "address_line2": "Halton Hills, ON L7G 0J1", - "group": "Halton West", - "bin_type": "EMW Cathedral Container 10yd", - "material_type": "Cardboard", - "asset_tag": "strange", - "bin_volume": "small", - }, - "265434931": { - "id": "265434931", - "sensor_type": SensorType.LIQUID_BIN_LEVEL, - "fill_level": 65, - "sim": "fjaksyr3j", - "lat": 43.951197, - "long": -79.947344, - "man": "Sensational Sensors", - "bin_name": "Strange Bin", - "address_line1": "12345 Fake Name Rd", - "address_line2": "Bolton, ON L7E 0Y2", - "group": "Halton West", - "bin_type": "EMW Cathedral Container 10yd", - "material_type": "Plastic", - "asset_tag": "charm", - "bin_volume": "large", - }, - "265434945": { - "id": "265434945", - "sensor_type": SensorType.LIQUID_BIN_LEVEL, - "fill_level": 25, - "sim": "86msjg91a", - "lat": 43.551197, - "long": -79.547344, - "man": "Sensational Sensors", - "bin_name": "Charming Bin", - "address_line1": "44444 Cow Rd", - "address_line2": "Bolton, ON L7E 0Y2", - "group": "Bolton South", - "bin_type": "EMW Cathedral Container 10yd", - "material_type": "Plastic", - "asset_tag": "up", - "bin_volume": "medium", - }, - "265434933": { - "id": "265434933", - "sensor_type": SensorType.LIQUID_BIN_LEVEL, - "fill_level": 10, - "sim": "324dg4gfs", - "lat": 43.822397, - "long": -79.647344, - "man": "Sensational Sensors", - "bin_name": "Cheddar Bin", - "address_line1": "41234 Cheese Rd", - "address_line2": "Bolton, ON L7E 0Y2", - "group": "Bolton South", - "bin_type": "EMW Cathedral Container 10yd", - "material_type": "Plastic", - "asset_tag": "bottom", - "bin_volume": "small", - }, - "265435142": { - "id": "265435142", - "sensor_type": SensorType.LIQUID_BIN_LEVEL, - "fill_level": 40, - "sim": "9185jak34", - "lat": 43.451197, - "long": -80.147344, - "man": "Sensational Sensors", - "bin_name": "Small Yellow Bin", - "address_line1": "626 Caramel Dr", - "address_line2": "Bolton, ON L7E 0Y2", - "group": "Bolton South", - "bin_type": "EMW Cathedral Container 10yd", - "material_type": "Cardboard", - "asset_tag": "strange", - "bin_volume": "medium", - }, - "265434817": { - "id": "265434817", - "sensor_type": SensorType.LIQUID_BIN_LEVEL, - "fill_level": 40, - "sim": "81kai41j8", - "lat": 43.452197, - "long": -80.147244, - "man": "Sensational Sensors", - "bin_name": "Cherry Bin", - "address_line1": "99473 Fruit Blvd", - "address_line2": "Bolton, ON L7E 0Y2", - "group": "Bolton South", - "bin_type": "EMW Cathedral Container 20yd", - "material_type": "Compost", - "asset_tag": "top", - "bin_volume": "small", - }, - "265434991": { - "id": "265434991", - "sensor_type": SensorType.LIQUID_BIN_LEVEL, - "fill_level": 90, - "sim": "01d83nn17", - "lat": 43.991197, - "long": -79.887344, - "man": "Sensational Sensors", - "bin_name": "Corn Bin", - "address_line1": "513 Cow Rd", - "address_line2": "Bolton, ON L7E 0Y2", - "group": "Bolton South", - "bin_type": "EMW Cathedral Container 20yd", - "material_type": "Compost", - "asset_tag": "top", - "bin_volume": "large", - }, -} - - -@app.get("/sensors") -def index() -> dict[str, Sensor]: - return sensors - - -# Selection = dict[str, str | int | SensorType | None] - - -# @app.get("/sensors/") -# def query_sensor_by_parameters( -# # sensor_type: SensorType | None = None, -# min_fill_level: int | None = None, -# # sim: str | None = None, -# ) -> dict[str, Selection]: -# def check_sensor(sensor: Sensor) -> bool: -# return all( -# ( -# # sensor_type is None or sensor.sensor_type == sensor_type, -# min_fill_level is None or sensor.fill_level >= min_fill_level, -# # sim is None or sensor.sim == sim, -# ) -# ) - -# selection = [sensor for sensor in sensors.values() if check_sensor(sensor)] -# return {"selection": selection} diff --git a/sensational_sensors/requirements.txt b/sensational_sensors/requirements.txt deleted file mode 100644 index d275081e..00000000 --- a/sensational_sensors/requirements.txt +++ /dev/null @@ -1,4 +0,0 @@ -fastapi>=0.68.0,<0.69.0 -pydantic>=1.8.0,<2.0.0 -uvicorn>=0.15.0,<0.16.0 -requests