#!/usr/bin/env python
# -*- coding: utf-8 -*-

from flask import render_template, make_response, request, redirect
from flask_restx import Namespace, Resource
from util import variables as uvars

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

api = Namespace('home', description='Home page')

@api.route("/myip")
class MyIp(Resource):
    def get(self):
        return {'ip': request.remote_addr}, 200

############################
# WEB VIEW
############################

web = Namespace('home', description='Home page')

@web.route("/")
class HomeWeb(Resource):
    """Show the home page"""

    def get(self):
        """Get the home page"""

        return make_response(render_template("index.html"))



# ############################
# # Stream
# ############################

# from flask import Response
# import json
# import time
# from astropy.time import Time
# import requests

# now = Time.now()

# @web.route('/stream')
# class Stream(Resource):
#     def get(self):
#         def inner():
#             while True:
#                 try:
#                     resource = requests.get("http://192.168.61.69")
#                     if resource:
#                         yield 'data: {}\n\n'.format(json.dumps(resource.json()))
#                 except GeneratorExit:
#                     print("Stop sensor sensor data")

#         response = inner()
#         print(Time.now() - now )
#         print("Streaming sensor data...")

#         return Response(response, mimetype="text/event-stream")

