# High Energy Ventilator

Rapidly-producible ventilator developed with CERN and collaborating institutes for the COVID-19 pandemic.

## Contents

* [Installation](#installation)
* [Usage](#usage)
* [Testing](#testing)
* [License](#license)
* [Acknowledgments](#acknowledgments)

## Installation

The following has been tested using Python 3.7 on:
* Raspberry Pi 4 4GB model B running Raspbian GNU/Linux 10 (buster) OS
* Raspbian Desktop VM (Buster)
* ASUS TinkerBoard S running Armbian Buster XFCE Desktop OS

### Prerequisites

Python Version: 3.7

Pip Version: 18.1 or later

Ansible: Version 2.7 - 2.10

### Setup

There are 2 methods of installation, [using SSH](#ssh-installation) or installing [locally](#local-installation). Both methods can be used with a Raspberry Pi, a Raspbain VM, or a Tinkerboard S depending on your setup and personal preference.

Installation over SSH defaults to `/home/$USER/hev`. Whereas, the local installation method installs all the requirements in the location you clone this repo to.

Before installation please run:

```bash
sudo apt update
sudo apt upgrade
```

#### SSH Installation

On your local PC, install [ansible](https://docs.ansible.com/ansible/latest/installation_guide/index.html) at least version 2.7. The easiest way to do this via `pip`:

```bash
pip3 install ansible
```

> ***WARNING:** SSH must be enabled on the target device.*

For ansible to work, you need to create an ssh keypair with your target device. On your local PC generate a ssh keypair and copy it over to the device:

```bash
ssh-keygen
ssh-copy-id $USER@IP-ADDRESS
```

To obtain the IP Address of your device, on your device run:

```bash
hostname -I
```

Change default Python to Python3.7 on remote device:

```bash
ssh $USER@IP-ADDRESS
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2
```

As part of the initial setup ansible needs access to run root commands without inputting in a password. On the remote device, edit `/etc/sudoers` (using root permissions) and replace

```bash
%sudo  ALL=(ALL:ALL) ALL
```

with:

```bash
%sudo  ALL=(ALL:ALL) NOPASSWD: ALL
```

This will grant every user the ability to bypass the sudo password prompt.

On your local PC, run and follow the prompts:

```bash
./setup.sh
```

Ansible logs are saved in `ansible/playbooks/logs`.

#### Local Installation

Change default Python to Python3.7:

```bash
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2
```

Install ansible:

```bash
sudo apt install ansible
sudo reboot
```

Clone this repo and checkout the `release/ui` branch. The most suitable location for the software is `~/opt`. As `/opt` is owned by root (admin user), you will need to change the ownership to the user.

```bash
sudo chown -R $USER /opt
cd /opt
git clone https://ohwr.org/project/hev.git
cd hev
git checkout release/ui
```

As part of the initial setup ansible needs access to run root commands without inputting in a password. Edit `/etc/sudoers` (using root permissions) and replace

```bash
%sudo  ALL=(ALL:ALL) ALL
```

with:

```bash
%sudo  ALL=(ALL:ALL) NOPASSWD: ALL
```

This will grant every user the ability to bypass the sudo password prompt.

Finally, run `setup.sh` and enter `localhost` when asked for an IP address.

```bash
./setup.sh
```

Ansible logs are saved in `ansible/playbooks/logs`.

## Usage

### Running the HEV UI

Running the HEV UI requires three separate python process running in the same virtualenv from the `hev` directory. This varies depending on your installation method as follows:

* Remote Installation:

    ```bash
    cd /home/$USER/hev
    ```

* Local Installation:

    ```bash
    cd /path/to/hev
    ```

1) Run ArduinoEmulator

    Note that a selection of dump files are provided in the `raspberry-dataserver/share` dir.

    ```bash
    source .hev_env/bin/activate
    ./raspberry-dataserver/ArduinoEmulator.py -f raspberry-dataserver/share/B6-20201207.dump
    ```

2) Run hevserver in another shell

    ```bash
    source .hev_env/bin/activate
    cd raspberry-dataserver
    ./hevserver.py --use-dump-data
    ```

3) Run NativeUI in another shell

    ```bash
    source .hev_env/bin/activate
    ./NativeUI/NativeUI.py
    ```

### Command-Line Arguments

NativeUI.py accepts the following command line arguments:

| Command | Pattern(s) | Effect | Example |
|---------|------------|--------|---------|
| Windowed | -w, --windowed | Create the user interface in windowed mode | ```./NativeUI/NativeUI.py -w``` |
| Debug | -d, --debug | Set the logging output level. By default only log messages of ERROR or higher priority will be displayed. The debug flag changes this to messages of INFO or higher if provided once, or DEBUG or higher if provided twice. | ```./NativeUI/NativeUI.py -dd```|
| Resolution | -r , --resolution | Takes the following string as specifying the desired size of the UI in pixels. Resolutions should be two integers separated by a non-numerical character. | ```./NativeUI/NativeUI.py -r 1600x900```

## Testing

For full testing documentation please refer to the [testing README](NativeUI/tests/README.md).

## Unit Tests

```bash
source .hev_env/bin/activate
pytest NativeUI
```

### Coverage

To get pytest coverage run from the root of the repo:

```bash
pip install pytest-cov
pytest --cov=NativeUI NativeUI
```

## License

See [LICENSE](LICENCE.txt) file.
