From b8a3d0383cf6f58ef00dc750903eb6394d6ca5bf Mon Sep 17 00:00:00 2001 From: markfodor Date: Mon, 27 Nov 2023 21:52:30 +0100 Subject: [PATCH] get around the chrome driver issue and update InkPlate code to use the API --- config/model/appConfig.py | 3 +- global.json | 3 +- inkplate/inkplate.ino | 35 +++++++++++++++-- main.py | 2 +- postman/InkCheck.postman_collection.json | 12 ------ postman/Inkcheck.postman_environment.json | 21 ++++++++++ renderer/renderer.py | 47 ++++++++++++++--------- 7 files changed, 84 insertions(+), 39 deletions(-) create mode 100644 postman/Inkcheck.postman_environment.json diff --git a/config/model/appConfig.py b/config/model/appConfig.py index b3066d5..f165dd0 100644 --- a/config/model/appConfig.py +++ b/config/model/appConfig.py @@ -7,5 +7,4 @@ class AppConfig: timestampFormat: str imageWidth: int imageHeight: int - collectors: list[str] = field(default_factory=list, init=True) - destinationFolder: str \ No newline at end of file + collectors: list[str] = field(default_factory=list, init=True) \ No newline at end of file diff --git a/global.json b/global.json index 6fa5085..eea6137 100644 --- a/global.json +++ b/global.json @@ -3,6 +3,5 @@ "timestampFormat": "%Y.%m.%d %H:%M:%S", "imageWidth": 1200, "imageHeight": 825, - "collectors": ["GoogleKeepCollector", "TrelloCollector"], - "destinationFolder": "YOUR_DESTINATION_FOLDER" + "collectors": ["GoogleKeepCollector", "TrelloCollector"] } \ No newline at end of file diff --git a/inkplate/inkplate.ino b/inkplate/inkplate.ino index 5a7d3c4..d37a7e0 100644 --- a/inkplate/inkplate.ino +++ b/inkplate/inkplate.ino @@ -18,6 +18,7 @@ #include "Inkplate.h" #include +#include #include #include #include @@ -26,7 +27,8 @@ Inkplate display(INKPLATE_3BIT); const char ssid[] = "YOUR WIFI SSID"; // Your WiFi SSID const char *password = "YOUR WIFI PASSWORD"; // Your WiFi password -const char *imgurl = "http://url.to.your.server/inkcheck.png"; // Your dashboard image web address +const char *generateurl = "http://:8080/generate"; // Your InkCheck generate endpoint +const char *imgurl = "http://:8080/image"; // Your InkCheck image endpoint // Initialize Telegram BOT #define BOTtoken "YOUR TELEGRAM BOT TOKEN" // your Bot Token (Get from Botfather) @@ -50,10 +52,35 @@ void setup() display.begin(); // Join wifi, retrieve image, update display - display.joinAP(ssid, password); + int joinAPresult = display.joinAP(ssid, password); + if(joinAPresult == 1) { + Serial.println("Joined the AP."); + } else { + Serial.println("Error joining the AP. Error code: " + success); + } + + // Trigger image generation + Serial.println("Getting InkCheck image."); + HTTPClient http; + http.begin(generateurl); + if (http.GET() > 0) { + Serial.println(http.getString()); + } + + // Sleep for a minute until the image generation + Serial.println("Going to sleep for a minute."); + delay(60000); + + // Get the image char url[256]; strcpy(url, imgurl); - Serial.println(display.drawImage(url, display.PNG, 0, 0)); + int drawResult = display.drawImage(url, display.PNG, 0, 0); + if (drawResult == 1) { + Serial.println("Image updated."); + } else { + Serial.println("Error drawing image. Error code: " + drawResult); + } + display.display(); //uncomment or delete the following section if not using Telegram to send message when battery is low @@ -89,4 +116,4 @@ int calc_battery_percentage(double battv) battery_percentage = 100; return battery_percentage; -} \ No newline at end of file +} diff --git a/main.py b/main.py index 7e48dc2..7e9f7e1 100644 --- a/main.py +++ b/main.py @@ -61,7 +61,7 @@ def generate(): data_list.append(collector.get_data()) renderer = Renderer(app_config.imageWidth, app_config.imageHeight) - renderer.render(timestamp, data_list, app_config.destinationFolder) + renderer.render(timestamp, data_list) response = 'Inkcheck image is updated.' logger.info(response) diff --git a/postman/InkCheck.postman_collection.json b/postman/InkCheck.postman_collection.json index 07b12c0..590a069 100644 --- a/postman/InkCheck.postman_collection.json +++ b/postman/InkCheck.postman_collection.json @@ -96,17 +96,5 @@ ] } } - ], - "variable": [ - { - "key": "host", - "value": "http://localhost", - "type": "string" - }, - { - "key": "port", - "value": "8080", - "type": "string" - } ] } \ No newline at end of file diff --git a/postman/Inkcheck.postman_environment.json b/postman/Inkcheck.postman_environment.json new file mode 100644 index 0000000..a240c2c --- /dev/null +++ b/postman/Inkcheck.postman_environment.json @@ -0,0 +1,21 @@ +{ + "id": "a4feb904-af9f-4ef5-93f7-2843ed94e1c8", + "name": "Inkcheck", + "values": [ + { + "key": "host", + "value": "http://localhost", + "type": "default", + "enabled": true + }, + { + "key": "port", + "value": "8080", + "type": "default", + "enabled": true + } + ], + "_postman_variable_scope": "environment", + "_postman_exported_at": "2023-11-27T20:01:21.210Z", + "_postman_exported_using": "Postman/10.20.3" +} \ No newline at end of file diff --git a/renderer/renderer.py b/renderer/renderer.py index 3554064..772d050 100644 --- a/renderer/renderer.py +++ b/renderer/renderer.py @@ -4,13 +4,13 @@ """ from selenium import webdriver -from selenium.webdriver.chrome.options import Options +from selenium.webdriver.chrome.service import Service from time import sleep from jinja2 import Environment, FileSystemLoader from selenium.webdriver.common.by import By from logger.logger import logger import pathlib -import shutil +import sys import os class Renderer: @@ -23,6 +23,9 @@ def __init__(self, width, height): self.output_html = 'inkcheck.html' self.curr_path = str(pathlib.Path(__file__).parent.absolute()) self.project_path = os.path.abspath(os.curdir) + + logger.info(f"On platform: {sys.platform}") + self.is_windows = sys.platform.startswith('win') self.absolute_input_html_template_path = os.path.join(self.curr_path, self.input_html_template) self.absolute_output_folder = os.path.join(self.project_path, self.output_folder) @@ -58,11 +61,18 @@ def take_screenshot(self): if not os.path.exists(self.absolute_output_folder): os.mkdir(self.absolute_output_folder) - opts = Options() + opts = webdriver.ChromeOptions() opts.add_argument("--headless") opts.add_argument("--hide-scrollbars") opts.add_argument('--force-device-scale-factor=1') - driver = webdriver.Chrome(options=opts) + + if not self.is_windows: + binary_location = self.get_chrome() + logger.info(f"Chrome driver found: {binary_location}") + service = Service(executable_path=binary_location) + driver = webdriver.Chrome(service=service, options=opts) + else: + driver = webdriver.Chrome(options=opts) self.set_viewport_size(driver) driver.get('file://' + self.output_html_file_path) @@ -74,7 +84,7 @@ def take_screenshot(self): else: logger.error('ERROR during the screen capture.') - def render(self, timestamp, data_list, destination_folder): + def render(self, timestamp, data_list): template_loader = FileSystemLoader(self.curr_path) template = Environment(loader=template_loader, autoescape=True).get_template(self.input_html_template) rendered_template = template.render( @@ -88,16 +98,17 @@ def render(self, timestamp, data_list, destination_folder): logger.info(f"Template is rendered and saved to {self.output_html_file_path}") self.take_screenshot() - self.copy_to_destination_folder(destination_folder) - - def copy_to_destination_folder(self, destination_folder): - if not destination_folder or not os.path.isdir(destination_folder): - logger.error('Destination folder is not configured properly.') - return - - if not os.path.exists(destination_folder): - os.makedirs(destination_folder) - logger.info(f"Destination folder created: {destination_folder}") - - shutil.copy(self.output_image_path, destination_folder) - logger.info(f"Image copied to destination folder: {destination_folder}") + # find the chrome driver on Linux distros + def get_chrome(self): + if os.path.isfile('/usr/bin/chromedriver'): + return '/usr/bin/chromedriver' + elif os.path.isfile('/usr/bin/chromium-browser'): + return '/usr/bin/chromium-browser' + elif os.path.isfile('/usr/bin/chromium'): + return '/usr/bin/chromium' + elif os.path.isfile('/usr/bin/chrome'): + return '/usr/bin/chrome' + elif os.path.isfile('/usr/bin/google-chrome'): + return '/usr/bin/google-chrome' + else: + return None