Skip to content

Instantly share code, notes, and snippets.

@r15ch13
Last active December 14, 2023 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r15ch13/fe55093751926c7b9e7121e732bb630e to your computer and use it in GitHub Desktop.
Save r15ch13/fe55093751926c7b9e7121e732bb630e to your computer and use it in GitHub Desktop.
Get pending printer jobs from a CUPS Printer with Node-RED

Get pending printer jobs from a CUPS Printer with Node-RED

Inject the printer uri via the msg.topic from your calling node (e.g. Timestamp node).

Printer uri should look like this: ipp://mycupsserver:632/printers/Brother_HL2030

Add a function to your flow and add the module @sealsystems/ipp on the Setup tab.

Then add the following code to the On Message tab.

const printer = sealsystemsIpp.Printer(msg.topic);
const ippmsg = {
    'operation-attributes-tag': {
        'attributes-charset': 'utf-8',
        'attributes-natural-language': 'en',
        'printer-uri': msg.topic,
        'which-jobs': 'pending',
        'requested-attributes': [
            'job-id',
            'job-state',
            'job-name',
        ]
    }
}

printer.execute('Get-Jobs', ippmsg, function (err, res) {
    msg.topic = 'pending-jobs';
    msg.payload = 0;
    if(err) {
        msg.topic = 'error';
        msg.payload = err;
    } else {
        if (Array.isArray(res['job-attributes-tag'])) {
            msg.payload = res['job-attributes-tag'].length;
        } else if (res['job-attributes-tag']) {
            // single job is returned as object
            msg.payload = 1;
        }
    }
    node.send(msg);

});
return;

Payload contains the pending jobs count:

{ _msgid: "c00f96ee4ec4408c", payload: 1, topic: "pending-jobs" }

This can be modified to return any information about your printer and print jobs. E.g. which-jobs can be set to:

aborted
all
canceled
pending
pending-held
processing
processing-stopped

More examples for @sealsystems/ipp can be found here:

https://github.com/sealsystems/node-ipp/tree/master/examples

https://github.com/sealsystems/node-ipp/blob/master/examples/Get-Jobs.js

https://ftp.pwg.org/pub/pwg/candidates/cs-ippjobext21-20230210-5100.7.pdf page 67

@r15ch13
Copy link
Author

r15ch13 commented Dec 14, 2023

Inject:
image

Function setup:
image

Function code;
image

@r15ch13
Copy link
Author

r15ch13 commented Dec 14, 2023

Flow:

[
    {
        "id": "514e88cc56897b85",
        "type": "tab",
        "label": "Get print jobs from CUPS",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "a0d43df0389a1c52",
        "type": "inject",
        "z": "514e88cc56897b85",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "ipp://mycupsserver:632/printers/Brother_HL2030",
        "payload": "",
        "payloadType": "date",
        "x": 480,
        "y": 280,
        "wires": [
            [
                "a3d4a9fbff239890"
            ]
        ]
    },
    {
        "id": "ec968df22ca2d1c0",
        "type": "debug",
        "z": "514e88cc56897b85",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 960,
        "y": 280,
        "wires": []
    },
    {
        "id": "a3d4a9fbff239890",
        "type": "function",
        "z": "514e88cc56897b85",
        "name": "function 1",
        "func": "const printer = sealsystemsIpp.Printer(msg.topic);\nconst ippmsg = {\n    'operation-attributes-tag': {\n        'attributes-charset': 'utf-8',\n        'attributes-natural-language': 'en',\n        'printer-uri': msg.topic,\n        'which-jobs': 'pending-held',\n        'requested-attributes': [\n            'job-id',\n            'job-state',\n            'job-name',\n        ]\n    }\n}\n\nprinter.execute('Get-Jobs', ippmsg, function (err, res) {\n    msg.topic = 'pending-jobs';\n    msg.payload = 0;\n    console.log(res)\n    if(err) {\n        msg.topic = 'error';\n        msg.payload = err;\n    } else {\n        if (Array.isArray(res['job-attributes-tag'])) {\n            msg.payload = res['job-attributes-tag'].length;\n        } else if (res['job-attributes-tag']) {\n            // single job is returned as object\n            msg.payload = 1;\n        }\n    }\n    node.send(msg);\n    \n});\nreturn;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [
            {
                "var": "sealsystemsIpp",
                "module": "@sealsystems/ipp"
            }
        ],
        "x": 720,
        "y": 280,
        "wires": [
            [
                "ec968df22ca2d1c0"
            ]
        ]
    }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment