From 94250dc2b19a4afc2ba66eead281659b3404fd0d Mon Sep 17 00:00:00 2001
From: Jan Grewe <jan@faked.org>
Date: Tue, 8 Oct 2024 04:24:40 +0200
Subject: [PATCH] implement file deletion and print start

---
 TODO.txt         |  1 -
 main.py          | 25 +++++++++++++++----------
 web/index.html   | 19 ++++++++++++++++++-
 web/js/chitui.js | 40 +++++++++++++++++++++++++++++++++++++---
 4 files changed, 70 insertions(+), 15 deletions(-)

diff --git a/TODO.txt b/TODO.txt
index 7dd7e55..04c1280 100644
--- a/TODO.txt
+++ b/TODO.txt
@@ -1,5 +1,4 @@
 * upload progress should use to-printer upload
-* file deletion
 * manual adding of printers
 * camera frame/stream
 * print job control
diff --git a/main.py b/main.py
index 09d34ca..5122076 100644
--- a/main.py
+++ b/main.py
@@ -103,7 +103,8 @@ def upload_file(printer_ip, filepath):
         with open(filepath, 'rb') as f:
             f.seek(offset)
             file_part = f.read(part_size)
-            logger.debug("Uploading part {}/{} (offset: {})", i, num_parts, offset)
+            logger.debug("Uploading part {}/{} (offset: {})",
+                         i, num_parts, offset)
             if not upload_file_part(url, post_data, filename, file_part, offset):
                 logger.error("Uploading file to printer failed.")
                 break
@@ -116,7 +117,6 @@ def upload_file(printer_ip, filepath):
 def upload_file_part(url, post_data, file_name, file_part, offset):
     post_data['Offset'] = offset
     post_files = {'File': (file_name, file_part)}
-    #post_files = {'File': file_part}
     response = requests.post(url, data=post_data, files=post_files)
     status = json.loads(response.text)
     if status['success']:
@@ -125,14 +125,6 @@ def upload_file_part(url, post_data, file_name, file_part, offset):
     return False
 
 
-def read_in_chunks(file, chunk_size=126976):
-    while True:
-        data = file.read(chunk_size)
-        if not data:
-            break
-        yield data
-
-
 @socketio.on('connect')
 def sio_handle_connect(auth):
     logger.info('Client connected')
@@ -163,6 +155,19 @@ def sio_handle_printer_files(data):
     get_printer_files(data['id'], data['url'])
 
 
+@socketio.on('action_delete')
+def sio_handle_action_delete(data):
+    logger.debug('client.action_delete >> '+json.dumps(data))
+    send_printer_cmd(data['id'], 259, {"FileList": [data['data']]})
+
+
+@socketio.on('action_print')
+def sio_handle_action_print(data):
+    logger.debug('client.action_print >> '+json.dumps(data))
+    send_printer_cmd(data['id'], 128, {
+                     "Filename": data['data'], "StartLayer": 0})
+
+
 def get_printer_status(id):
     send_printer_cmd(id, 0)
 
diff --git a/web/index.html b/web/index.html
index 4512270..f8ac915 100644
--- a/web/index.html
+++ b/web/index.html
@@ -133,7 +133,24 @@
           </div>
           <div class="toast-body bg-body-tertiary">Success!</div>
         </div>
-          
+
+        <div class="modal fade" id="modalConfirm" tabindex="-1" aria-labelledby="modalConfirmTitle" aria-hidden="true">
+          <div class="modal-dialog modal-dialog-centered">
+            <div class="modal-content">
+              <div class="modal-header">
+                <h1 class="modal-title fs-5" id="modalConfirmTitle"</h1>
+                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
+              </div>
+              <div class="modal-body">
+                Please confirm that you want to <span id="modalConfirmAction"></span> <span class="badge bg-body-secondary font-monospace text-wrap" id="modalConfirmValue"></span>
+              </div>
+              <div class="modal-footer">
+                <button type="button" class="btn btn-danger" data-bs-dismiss="modal">Cancel</button>
+                <button type="button" class="btn btn-success" id="btnConfirm" data-action="" data-value="">Confirm</button>
+              </div>
+            </div>
+          </div>
+        </div>        
 
       </div>
 
diff --git a/web/js/chitui.js b/web/js/chitui.js
index 07e9090..ddf5a9a 100644
--- a/web/js/chitui.js
+++ b/web/js/chitui.js
@@ -1,7 +1,7 @@
 const socket = io();
 var websockets = []
 var printers = {}
-
+var currentPrinter = null
 
 socket.on("connect", () => {
   console.log('socket.io connected: ' + socket.id);
@@ -29,6 +29,10 @@ socket.on("printer_response", (data) => {
     case SDCP_CMD_RETRIEVE_FILE_LIST:
       handle_printer_files(data.Data.MainboardID, data.Data.Data.FileList)
       break
+    case SDCP_CMD_BATCH_DELETE_FILES:
+      modalConfirm.hide()
+    case SDCP_CMD_START_PRINTING:
+      modalConfirm.hide()
     default:
       console.log(data)
       break
@@ -102,6 +106,7 @@ function handle_printer_files(id, data) {
   })
   printers[id]['files'] = files
   createTable('Files', files)
+  addFileOptions()
 }
 
 function addPrinters(printers) {
@@ -129,6 +134,7 @@ function addPrinters(printers) {
 
 function showPrinter(id) {
   //console.log(JSON.stringify(printers[id]))
+  currentPrinter = id
   var p = printers[id]
   var printerIcon = (p.brand + '_' + p.model).split(" ").join("").toLowerCase()
   $('#printerName').text(p.name)
@@ -183,7 +189,7 @@ function fillTable(table, data) {
     if (typeof val === 'object') {
       val = JSON.stringify(val)
     }
-    var row = $('<tr><td>' + key + '</td><td>' + val + '</td></tr>')
+    var row = $('<tr><td class="fieldKey">' + key + '</td><td class="fieldValue">' + val + '</td></tr>')
     t.append(row)
   })
 }
@@ -192,6 +198,24 @@ function getPrinterFiles(id, url) {
   socket.emit("printer_files", { id: id, url: url })
 }
 
+function addFileOptions() {
+  $('#tableFiles .fieldValue').each(function () {
+    var file = $(this).text()
+    var options = $('<i class="bi bi-printer-fill fileOption ps-3" data-action="print" data-file="' + file + '"></i><i class="bi bi-trash-fill fileOption ps-1" data-action="delete" data-file="' + file + '"></i>')
+    $(this).append(options)
+    $(this).parent().attr('data-file', file)
+  })
+  $('.fileOption').on('click', function (e) {
+    var action = $(this).data('action')
+    var file = $(this).data('file')
+    $('#modalConfirmTitle').text('Confirm ' + action)
+    $('#modalConfirmAction').text(action)
+    $('#modalConfirmValue').text(file)
+    $('#btnConfirm').data('action', action).data('value', file)
+    modalConfirm.show()
+  })
+}
+
 function updatePrinterStatus(data) {
   var info = $('#printer_' + data.MainboardID).find('.printerInfo')
   switch (data.Status.CurrentStatus[0]) {
@@ -201,7 +225,7 @@ function updatePrinterStatus(data) {
       break
     case SDCP_MACHINE_STATUS_PRINTING:
       info.text("Printing")
-      updatePrinterStatusIcon(data.MainboardID, "primary", true)
+      updatePrinterStatusIcon(data.MainboardID, "success", true)
       break
     case SDCP_MACHINE_STATUS_FILE_TRANSFERRING:
       info.text("File Transfer")
@@ -317,6 +341,16 @@ $('#toastUpload .btn-close').on('click', function (e) {
   $("#toastUpload").hide()
 });
 
+var modalConfirm;
+$(document).ready(function () {
+  modalConfirm = new bootstrap.Modal($('#modalConfirm'), {})
+});
+
+$('#btnConfirm').on('click', function () {
+  socket.emit('action_' + $(this).data('action'), { id: currentPrinter, data: $(this).data('value') })
+  $('#tableFiles tr[data-file="'+$(this).data('value')+'"]').remove()
+});
+
 /* global bootstrap: false */
 (() => {
   'use strict'
-- 
GitLab