Skip to content

Commit

Permalink
Merge pull request #12 from CDRV/dev-1.1.0
Browse files Browse the repository at this point in the history
Dev 1.1.0 merge into Dev
  • Loading branch information
SBriere authored Dec 5, 2023
2 parents d80451f + 749e942 commit d7850d2
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 93 deletions.
2 changes: 1 addition & 1 deletion Globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

config_man = ConfigManager()

version_string = '1.0.6'
version_string = '1.1.0'
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Main features of the project includes:
* OpenTera (https://github.com/introlab/opentera) server

## Requirements
Tested on Python 3.7 and 3.8
Tested on Python 3.10

## Pi setup

Expand All @@ -29,6 +29,7 @@ Initialize credential manager (to remember PAT) `git config --global credential.
3. `source venv/bin/activate`
4. `pip install -r requirements.txt`
5. `deactivate`
* **If miniconda is installed, the script "create_conda_venv" can also be used**
4. Edit the config file `/home/pi/Desktop/PiHub/config/PiHub.json` with the appropriate values
5. Setup the cron tasks using `sudo crontab -e` and the crontab job listed in the `/home/pi/Desktop/PiHub/setup/crontab.txt` file
6. Setup and enable the main pihub service using<br>
Expand All @@ -50,6 +51,7 @@ If not developping directly on a Raspberry Pi, a virtual Python environment (ven
3. Create the virtual environment: `python -m venv venv`
4. Enable the virtual environment: <br>
On Mac/Linux: `source venv/bin/activate`<br>
On Windows: `venv\Scripts\activate.bat`
On Windows: `venv\Scripts\activate.bat`<br>
**If miniconda is installed, the script "create_conda_venv" can also be used**
5. Install requirements: `pip install -r requirements.txt`

6 changes: 3 additions & 3 deletions config/PiHub.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
"sensor_ID": "Sensor_0"
},
"SFTP": {
"hostname": "",
"hostname": "telesante.cdrv.ca",
"port": 40091,
"username": "dev",
"password": ""
"username": "sftp_dev",
"password": "LS9PwJx7$829so"
},
"OpenTera": {
"hostname": "localhost",
Expand Down
8 changes: 8 additions & 0 deletions create_conda_venv.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@echo off
echo Create or update conda venv
call conda install -m -c conda-forge -y --copy -p venv python=3.10
echo Activating venv
call conda activate .\venv
echo Installing requirements
call pip install -r requirements.txt
call conda deactivate
7 changes: 7 additions & 0 deletions create_conda_venv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
echo "Create or update conda venv"
conda install -m -c conda-forge -y --copy -p $PWD/venv python=3.10
echo "Activating venv"
conda activate $PWD/venv
echo "Installing requirements (make sure git submodules are installed)..."
$PWD/venv/bin/pip install -r $PWD/requirements.txt
conda deactivate
3 changes: 2 additions & 1 deletion libs/hardware/PiHubHardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def ensure_internet_is_available():
logging.warning('Reboot completed, but still no Internet... Rebooting...')
# Still no internet connection - reboot the pi!
PiHubHardware.reboot()
logging.info('Internet is back. All is fine.')
else:
logging.info('Internet is back. All is fine.')

lock.release()

Expand Down
30 changes: 26 additions & 4 deletions libs/servers/WatchServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def run(self):
logging.info('Apple Watch Server starting...')

# Check if all files are on sync on the server
self.sync_files(check_internet=False) # Don't explicitely check internet connection on startup
# self.sync_files(check_internet=False) # Don't explicitely check internet connection on startup

request_handler = AppleWatchRequestHandler
request_handler.base_server = self
Expand All @@ -47,6 +47,11 @@ def run(self):
self.is_running = True
logging.info('Apple Watch Server started on port ' + str(self.port))

# Check if all files are on sync on the server (after the main server has started)
self.file_syncher_timer = threading.Timer(1, self.sync_files, [False])
self.file_syncher_timer.start()

# Thread will wait here
self.server.serve_forever()
self.server.server_close()

Expand All @@ -61,14 +66,14 @@ def stop(self):

def sync_files(self, check_internet: bool = True):
logging.info("WatchServer: Synchronizing files with server...")

if self.synching_files:
logging.info("*** WatchServer: Already synching files. Will wait for next time.")
return

self.synching_files = True
# Build list of files to transfer
base_folder = self.data_path + '/ToProcess/'
base_folder = base_folder.replace('/', os.sep)
files = []
full_files = []
file_folders = []
Expand Down Expand Up @@ -128,6 +133,11 @@ def sync_files(self, check_internet: bool = True):
# Set files as processed
if success:
self.move_processed_files()
else:
# Something occurred... Try again in 5 minutes
self.file_syncher_timer = threading.Timer(300, self.sync_files)
self.file_syncher_timer.start()

# for file in full_files:
# WatchServer.file_was_processed(file)
else:
Expand Down Expand Up @@ -156,7 +166,7 @@ def move_files(self, source_files, target_folder):
# raise

try:
os.replace(full_filepath, target_file)
os.rename(full_filepath, target_file)
except (OSError, IOError) as exc:
logging.error('Error moving ' + full_filepath + ' to ' + target_file + ': ' + exc.strerror)
continue
Expand Down Expand Up @@ -235,6 +245,7 @@ def do_POST(self):
self.base_server.file_syncher_timer.cancel()
self.base_server.file_syncher_timer = None

# Prepare to receive data
destination_dir = (self.base_server.data_path + '/ToProcess/' + device_name + '/' + file_path + '/')\
.replace('//', '/').replace('/', os.sep)
destination_path = destination_dir + file_name
Expand Down Expand Up @@ -290,7 +301,7 @@ def do_POST(self):
else:
fh.write(data)
content_size_remaining -= buffer_size
content_received = (content_length - content_size_remaining)
# content_received = (content_length - content_size_remaining)
# pc = math.floor((content_received / content_length) * 100)
# if pc != last_pc:
# self.streamer.update_progress.emit(file_name, " (" + str(content_received) + "/ " +
Expand All @@ -315,6 +326,17 @@ def do_POST(self):
error = "Transfer error: " + str(file_infos.st_size) + " bytes received, " + str(content_length) + \
" expected."
logging.error(device_name + " - " + file_name + " - " + error)
self.send_response(400)
self.send_header('Content-type', 'file-transfer/error')
self.end_headers()
return

if content_length == 0 or (file_infos.st_size == 0 and content_length != 0):
error = "Transfer error: 0 byte received."
logging.error(device_name + " - " + file_name + " - " + error)
self.send_response(400)
self.send_header('Content-type', 'file-transfer/error')
self.end_headers()
return

# All is good!
Expand Down
Loading

0 comments on commit d7850d2

Please sign in to comment.