Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove SS and rfs from the code base #83

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions .github/workflows/build-and-push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
131 changes: 2 additions & 129 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,13 @@
# 📚 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()

# 🌐 Define CORS origins that are allowed to access the API
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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand 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

Expand All @@ -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}

Expand Down
6 changes: 0 additions & 6 deletions app/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Copy link
Contributor

@monicakochofar monicakochofar Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right this is referencing the old react code which is not in use! should we add more red here and delete the react /app folder too? @mikevespi

cc: @BallardRobinett

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I think it would make sense to just the whole /app folder if possible

setSensors(sensorsArray);
}, []);

Expand Down
4 changes: 2 additions & 2 deletions bin/vueLocalDeploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" ];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch on this!

if [ "$( docker inspect --format '{{json .State.Running}}' api )" = "true" ];
then
echo "docker is running, continuing execution..."
else
Expand All @@ -48,4 +48,4 @@ echo "installing node packages..."
npm install

echo "starting up vue app..."
npm run start
npm run start
17 changes: 0 additions & 17 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
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:
context: .
dockerfile: api/Dockerfile
ports:
- "8080:8080"
depends_on:
- real_fake_sensors
- sensational_sensors
environment:
- ENVIRONMENT=prod
app:
Expand Down
17 changes: 0 additions & 17 deletions prod.docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
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:
context: .
dockerfile: api/Dockerfile
ports:
- "8080:8080"
depends_on:
- real_fake_sensors
- sensational_sensors
environment:
- ENVIRONMENT=prod
app:
Expand Down
14 changes: 0 additions & 14 deletions real_fake_sensors/Dockerfile

This file was deleted.

4 changes: 0 additions & 4 deletions real_fake_sensors/README.md

This file was deleted.

Loading
Loading