Skip to content
Open
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
53 changes: 53 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: CI Pipeline

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Cache dependencies
uses: actions/cache@v2
with:
path: ~/.local/share/virtualenvs
key: ${{ runner.os }}-pipenv-${{ hashFiles('**/Pipfile.lock') }}
restore-keys: |
${{ runner.os }}-pipenv-

- name: Install Dependencies
run: |
pip install pipenv
pipenv install --deploy --ignore-pipfile

- name: Run unit tests
run: |
python3 -m unittest discover -s ./test

- name: Static code analysis and format
run: |
pipenv run lint
pipenv run format
pipenv run format-check

- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

- name: Build and push Docker image
run: |
docker build -t your-dockerhub-username/your-repo-name:latest .
docker push your-dockerhub-username/your-repo-name:latest
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,4 @@ deploy_rsa
deploy_rsa.pub

# Kubernetes config
kubeconfig.yaml
kubeconfig.yaml
30 changes: 29 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
## To implement
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files

- repo: https://github.com/psf/black
rev: 21.9b0
hooks:
- id: black
args: [--safe]

- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
language: system
types: [python]
args: ['--ignore=tests'] # adjust arguments as needed

- id: unittest
name: Run unittest
entry: python -m unittest discover -s ./test
language: system
types: [python]
12 changes: 11 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
## To implement
FROM python:3.8-alpine AS base

WORKDIR /oxygen-cs-grp01-eq6
COPY Pipfile Pipfile.lock /oxygen-cs-grp01-eq6/

RUN pip install pipenv
RUN pipenv install --deploy
RUN pip cache purge
RUN rm -rf /root/.cache/*

CMD ["pipenv", "run", "start"]
8 changes: 6 additions & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ requests = "*"
signalrcore = "*"

[dev-packages]
pylint = "*"
black = "*"

[requires]
python_version = "3.8"

[scripts]
start = "python src/main.py"
test = "# To implement"
lint = "# To implement"
test = "python3 -m unittest discover -s ./test"
lint = "pylint src/"
format= "black src/"
format-check = "black --check src/"
315 changes: 231 additions & 84 deletions Pipfile.lock

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 15 additions & 8 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class Main:
def __init__(self):
"""Setup environment variables and default values."""
self._hub_connection = None
self.HOST = None # Setup your host here
self.TOKEN = None # Setup your token here
self.HOST = "https://hvac-simulator-a23-y2kpq.ondigitalocean.app" # Setup your host here
self.TOKEN = "S1rN2enD5p" # Setup your token here

self.TICKETS = 1 # Setup your tickets here
self.T_MAX = None # Setup your max temperature here
self.T_MIN = None # Setup your min temperature here
self.TICKETS = 2 # Setup your tickets here
self.T_MAX = 40 # Setup your max temperature here
self.T_MIN = 10 # Setup your min temperature here
self.DATABASE = None # Setup your database here

def __del__(self):
Expand Down Expand Up @@ -52,10 +52,16 @@ def set_sensorhub(self):
)

self._hub_connection.on("ReceiveSensorData", self.on_sensor_data_received)
self._hub_connection.on_open(lambda: print("||| Connection opened.", flush=True))
self._hub_connection.on_close(lambda: print("||| Connection closed.", flush=True))
self._hub_connection.on_open(
lambda: print("||| Connection opened.", flush=True)
)
self._hub_connection.on_close(
lambda: print("||| Connection closed.", flush=True)
)
self._hub_connection.on_error(
lambda data: print(f"||| An exception was thrown closed: {data.error}", flush=True)
lambda data: print(
f"||| An exception was thrown closed: {data.error}", flush=True
)
)

def on_sensor_data_received(self, data):
Expand All @@ -65,6 +71,7 @@ def on_sensor_data_received(self, data):
date = data[0]["date"]
temperature = float(data[0]["data"])
self.take_action(temperature)
## Insert in DB, temperature + timestamp here
except Exception as err:
print(err, flush=True)

Expand Down
Empty file removed test/__init__.py
Empty file.
1 change: 0 additions & 1 deletion test/test.py

This file was deleted.

36 changes: 36 additions & 0 deletions test/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import unittest
from unittest.mock import patch, MagicMock
from src.main import Main

class TestMain(unittest.TestCase):

def setUp(self):
self.main = Main()

@patch('src.main.HubConnectionBuilder')
def test_set_sensorhub(self, MockHubConnectionBuilder):
self.main.set_sensorhub()
MockHubConnectionBuilder.assert_called_once()

@patch('src.main.requests.get')
def test_send_action_to_hvac(self, mock_get):
mock_response = MagicMock()
mock_response.text = '{"status": "ok"}'
mock_get.return_value = mock_response

action = "TurnOnAc"
self.main.send_action_to_hvac(action)

expected_url = f"{self.main.HOST}/api/hvac/{self.main.TOKEN}/{action}/{self.main.TICKETS}"
mock_get.assert_called_once_with(expected_url)

def test_take_action(self):
with patch.object(self.main, 'send_action_to_hvac') as mock_send_action:
self.main.take_action(self.main.T_MAX + 1)
mock_send_action.assert_called_with("TurnOnAc")

self.main.take_action(self.main.T_MIN - 1)
mock_send_action.assert_called_with("TurnOnHeater")

if __name__ == "__main__":
unittest.main()