Skip to content
templates.py 3.04 KiB
Newer Older
Davide Ricci's avatar
Davide Ricci committed
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

vertighel's avatar
vertighel committed
import html
import time
import subprocess
from flask import Response
from flask import render_template, make_response, request
Davide Ricci's avatar
Davide Ricci committed
from flask_restx import Namespace, Resource, fields

from .data_access_object import ObservationBlockObject as DAO
Davide Ricci's avatar
Davide Ricci committed

vertighel's avatar
vertighel committed
from sequencer import Sequencer
Davide Ricci's avatar
Davide Ricci committed

############################
# REST API
############################

api = Namespace('sequencer', description='Sequencer')

seq = Sequencer()

# import logging
# api.logger.disabled = True
# log = logging.getLogger('werkzeug')
# log.disabled = True
# # log = logging.getLogger('SHINS')
# # log.disabled = True

# # New, 2022 Method:

# logging.getLogger('werkzeug').disabled = True

list_of_ob = api.model('list', {
    'id': fields.Integer(readonly=True, description='The OB unique identifier'),
    'name': fields.String(required=True, description='The OB name')
})

Davide Ricci's avatar
Davide Ricci committed
@api.route("/")
class BobList(Resource):
    """Show and modify a list of OBs to be sequenced"""
    @api.doc('list_ob')
    @api.marshal_list_with(list_of_ob)
Davide Ricci's avatar
Davide Ricci committed
    def get(self):
        """Show all OB in the sequence"""
Davide Ricci's avatar
Davide Ricci committed
        return dao.todos

    @api.doc('add_ob')
    @api.expect(list_of_ob)
    @api.marshal_with(list_of_ob, code=201)
    def post(self):
        """Add a new OB to the sequence"""
        data = api.payload

vertighel's avatar
vertighel committed

@api.route('/<int:id>')
@api.response(404, 'OB not found')
@api.param('id', 'The OB identifier')
class Bob(Resource):
    '''Let delete a specific OB'''

    @api.doc('delete_ob')
    @api.response(204, 'OB deleted')
    def delete(self, id):
        '''Delete an OB given its identifier'''
        dao.delete(id)
        return '', 204

vertighel's avatar
vertighel committed

@api.route("/run")
class BobRun(Resource):
    """Manage the sequencer"""
Davide Ricci's avatar
Davide Ricci committed

    def get(self):
        """Show the sequencer status"""
        return ""
    def post(self):
        """Start the sequencer"""
        name = api.payload
        filename = dao.show(name)["filename"]
vertighel's avatar
vertighel committed
        seq.load_file(filename)
    def delete(self):
        """Stop the sequencer"""
        return "", 204


Davide Ricci's avatar
Davide Ricci committed
############################
# WEB VIEW
############################
Davide Ricci's avatar
Davide Ricci committed

Davide Ricci's avatar
Davide Ricci committed
web = Namespace('sequencer', description='Web Sequencer interface')
vertighel's avatar
vertighel committed

Davide Ricci's avatar
Davide Ricci committed

@web.route("/")

    def get(self):
        data = dao.todos
        return make_response(render_template("sequencer.html", data=data))


@web.route('/stream')
class Stream(Resource):
    def get(self):
        def inner():
            proc = subprocess.Popen(
                ['tail -f -n30  ./data/log/OARPAF.2022-06-20.log'],
                #['tail -f /var/log/syslog'],
                shell=True,
                stdout=subprocess.PIPE
            )

vertighel's avatar
vertighel committed
            for line in iter(proc.stdout.readline, ''):
                line = line.rstrip().decode()
                line = html.escape(line)
                time.sleep(0.1)
                yield 'data: {}\n\n'.format(line)
                # yield line.rstrip().decode() + '<br/>\n'
        print("qui")
        return Response(inner(), mimetype="text/event-stream")