Skip to content

Commit

Permalink
Allow to select and print files using objects
Browse files Browse the repository at this point in the history
  • Loading branch information
klein0r committed Dec 10, 2020
1 parent 3fd7e30 commit f29d9bd
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 42 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ Adapter to connect OctoPrint to ioBroker
- Custom Printer Commands
- System Commands
- Jog X, Y and Z axis
- Select a file or print it

## Changelog

### 1.0.5

* (klein0r) Allow to select and print files using objects
* (klein0r) Fixed .toFixed exception when no job is running

### 1.0.4
Expand Down
12 changes: 2 additions & 10 deletions io-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"version": "1.0.5",
"news": {
"1.0.5": {
"en": "Fixed .toFixed exception when no job is running",
"de": "Fehler .toFixed behoben, wenn keine Datei ausgewählt wurde"
"en": "Allow to select and print files using objects",
"de": "Möglichkeit hinzugefügt, Dateien zu wählen und zu drucken"
},
"1.0.4": {
"en": "Fixed .toFixed exception when no job is running",
Expand Down Expand Up @@ -457,14 +457,6 @@
"write": false
},
"native": {}
},
{
"_id": "files",
"type": "channel",
"common": {
"name": "Available files to print"
},
"native": {}
}
]
}
157 changes: 125 additions & 32 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,37 @@ class OctoPrint extends utils.Adapter {
},
jogCommand
);

} else if (id.match(new RegExp(this.namespace + '.files.[0-9]+.(select|print)'))) {
const matches = id.match(/.+\.files\.([0-9]+)\.(select|print)$/);
const fileId = matches[1];
const action = matches[2];

this.log.debug('selecting/printing file ' + fileId + ' - action: ' + action);

this.getState(
'files.' + fileId + '.path',
(err, state) => {
const fullPath = state.val;

this.log.debug('selecting/printing file with path ' + fullPath);

this.buildRequest(
'files/' + fullPath,
(content, status) => {
if (status == 204) {
this.log.debug('file selection/print successful');
this.refreshState();
} else {
this.log.error(content);
}
},
{
command: 'select',
print: (action == 'print')
}
);
}
);
}
} else {
this.log.info('OctoPrint API not connected');
Expand Down Expand Up @@ -482,52 +512,115 @@ class OctoPrint extends utils.Adapter {
}
}

addFiles(fileArray, counter) {

fileArray.forEach(function(file) {

if (file.type == 'machinecode' && file.origin == 'local') {

this.setObjectNotExists('files.' + counter, {
type: 'channel',
common: {
name: 'File ' + (counter + 1) + ' (' + file.display + ')',
},
native: {}
});

this.setObjectNotExists('files.' + counter + '.name', {
type: 'state',
common: {
name: 'File name',
type: 'string',
role: 'value',
read: true,
write: false
},
native: {}
});
this.setState('files.' + counter + '.name', {val: file.display, ack: true});

this.setObjectNotExists('files.' + counter + '.path', {
type: 'state',
common: {
name: 'File path',
type: 'string',
role: 'value',
read: true,
write: false
},
native: {}
});
this.setState('files.' + counter + '.path', {val: file.origin + '/' + file.path, ack: true});

this.setObjectNotExists('files.' + counter + '.date', {
type: 'state',
common: {
name: 'File date',
type: 'number',
role: 'date',
read: true,
write: false
},
native: {}
});
if (file.date) {
this.setState('files.' + counter + '.date', {val: new Date(file.date * 1000).getTime(), ack: true});
}

this.setObjectNotExists('files.' + counter + '.select', {
type: 'state',
common: {
name: 'Select file',
type: 'boolean',
role: 'button',
read: false,
write: true
},
native: {}
});

this.setObjectNotExists('files.' + counter + '.print', {
type: 'state',
common: {
name: 'Print file',
type: 'boolean',
role: 'button',
read: false,
write: true
},
native: {}
});

counter++;

} else if (file.type == 'folder') {
counter = this.addFiles(file.children, counter);
}

}.bind(this));

return counter;
}

async refreshFiles() {

if (this.apiConnected) {
this.buildRequest(
'files?recursive=true',
(content, status) => {

let counter = 0;
this.delObject('files', {recursive: true}, function() {

content.files.forEach(function(file) {

this.setObjectNotExists('files.' + counter, {
this.setObjectNotExists('files', {
type: 'channel',
common: {
name: 'File ' + (counter + 1),
},
native: {}
});

this.setObjectNotExists('files.' + counter + '.name', {
type: 'state',
common: {
name: 'File name',
type: 'string',
role: 'value',
read: true,
write: false
name: 'File list'
},
native: {}
});
this.setState('files.' + counter + '.name', {val: file.display, ack: true});

this.setObjectNotExists('files.' + counter + '.date', {
type: 'state',
common: {
name: 'File date',
type: 'string',
role: 'value',
read: true,
write: false
},
native: {}
});
this.setState('files.' + counter + '.date', {val: new Date(file.date * 1000).toLocaleDateString('de-DE'), ack: true});
this.addFiles(content.files, 0);

counter++;
}.bind(this));

},
Expand Down

0 comments on commit f29d9bd

Please sign in to comment.