Ejemplo n.º 1
0
def print_status(id):
    """
        Print Status
        Get printer status
        ---
        tags:
          - printer
        parameters:
          - in: path
            description: ID of the printer
            required: true
            type: integer
            name: id
        responses:
          200:
            description: returns printer information
            schema:
              $ref: "#/definitions/Web_Printer"

    """
    internal = request.args.get("internal", "false")
    if internal.lower() == "true":
        printer = Printer.get_by_id(id)
        if printer == None:
            abort(404)
        return json.jsonify(printer.to_dict())
    printer = Printer.get_by_webid(id)
    if printer == None:
        abort(404)
    return json.jsonify(printer.to_web())
Ejemplo n.º 2
0
def print_action(id):
    """
        Print Action
        Add print action
        ---
        tags:
          - printer
        parameters:
          - in: path
            required: true
            name: id
            description: ID of printer
            type: integer
          - in: body
            name: Command
            description: Command to be excecuted
            required: true
            schema:
              required:
                - id
                - type
              properties:
                id:
                  type: integer
                  description: id of the command
                name:
                  type: string
                  description: name of command to send [start, pause, cancel, clear]
        responses:
          201:
            description: Returns "(action) successfully sent to the printer."

        """
    printer = Printer.get_by_webid(id)
    if printer == None:
        abort(404)
    id = printer.id
    ip = printer.ip
    port = printer.port
    key = printer.key
    jobs = printer.jobs
    url = ip + ":" + str(port)
    log = hub.log
    data = request.get_json()
    command_id = data.get("id")
    action = data.get("name")

    if action == "start":
        t = Command(id, log, "start", hub.Webapi, command_id=command_id)
    elif action == "pause":
        t = Command(id, log, "pause", hub.Webapi, command_id=command_id)
    elif action == "cancel":
        t = Command(id, log, "cancel", hub.Webapi, command_id=command_id)
    elif action in ["clear", "next"]:
        t = Command(id, log, "clear", hub.Webapi, command_id=command_id)
    t.start()
    return json.jsonify(
        {"message": action + " successfully sent to the printer."}), 201
Ejemplo n.º 3
0
def jobs_post(id):
    """
        Add a Job
        Add job to printer
        ---
        tags:
          - printer
        parameters:
          - in: path
            description: ID of the printer
            required: true
            type: integer
            name: id
          - in: body
            description: Job to add to the printer
            required: true
            name: Job
            schema:
              $ref: "#/definitions/Web_Job"
        responses:
          201:
            description: returns "(job) has been uploaded successfully"
          400:
            description: no file was provided
          404:
            description: printer isn't found
        """
    printer = Printer.get_by_webid(id)
    if printer == None:
        abort(404)
    id = printer.id
    f = request.files.get('file', None)
    if f:
        webid = request.form.get('id')
        job = Job.get_by_webid(webid)
        if not job:
            job = Job(int(webid))
            ext = f.filename.rsplit(".", 1)[1]
            name = str(job.id) + "." + ext
            fpath = os.path.join(app.config['UPLOAD_FOLDER'], name)
            f.save(fpath)
            file = File(f.filename, fpath)
            job.set_file(file)
        elif job.file == None:
            ext = f.filename.rsplit(".", 1)[1]
            name = str(job.id) + "." + ext
            fpath = os.path.join(app.config['UPLOAD_FOLDER'], name)
            f.save(fpath)
            file = File(f.filename, fpath)
            job.set_file(file)
        printer = Printer.get_by_id(id)
        printer.add_job(job)
        t = threading.Thread(target=hub.Webapi.patch_job,
                             args=(job.to_web(), ))
        t.start()


#        if printer.current_job().id == job.id:
#            t = Command(printer.id, hub.log, "start",
#                        hub.Webapi)
#            t.start()
    else:
        abort(400)
    return json.jsonify(
        {"message":
         "Job " + str(webid) + " has been uploaded successfully"}), 201
Ejemplo n.º 4
0
def jobs_list(id):
    """
        Jobs List
        Get list of queued jobs
        ---
        tags:
          - printer
        parameters:
          - in: path
            description: ID of the printer
            required: true
            type: integer
            name: id
        definitions:
          - schema:
              id: Web_Job
              required:
                - id
              properties:
                id:
                  type: integer
                  description: ID of the printer
                data:
                  schema:
                    id: Web_Job_Data
                    properties:
                      estimated_print_time:
                        type: integer
                        description: size of the job in bytes
                      status:
                        type: string
                        description: status of the job
                      file:
                        schema:
                          id: Web_Job_File
                          properties:
                            name:
                              type: string
                              description: name of the file
                            origin:
                              type: string
                              description: origin of the file
                            size:
                              type: integer
                              description: size of the file in bytes
                            date:
                              type: string
                              description: date the file was created
        responses:
          200:
            description: returns json of queued jobs
            schema:
              properties:
                jobs:
                  type: array
                  items:
                    $ref: "#/definitions/Web_Job"

    """
    printer = Printer.get_by_webid(id)
    if printer:
        jobs = {"jobs": [job.to_web() for job in printer.jobs]}
        return json.jsonify(jobs)
    abort(404)