#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import subprocess
import http.server as s
from urllib.parse import urlparse
from urllib.parse import parse_qs
RaspberrypiIP = '192.168.0.10'
class MyHandler(s.BaseHTTPRequestHandler):
    def do_GET(self):
        if str(self.path[1:]) != "favicon.ico" :
            self.make_data()
        else :
            self.send_response(404)
    def make_data(self):
        cmd = str(self.path[1:])
        if cmd != "" :
            subprocess.call(["cgir", "send", cmd, "-g", "13"])
            body = '<html lang="ja"><head><meta charset="UTF-8"><title></title><link rel="icon" href="data:,">'\
            '</head><body onload="history.back();"><h1>送信完了</h1></body><html>'
        else :
            body = '<html lang="ja"><head><meta charset="UTF-8"><title>リモコン</title><link rel="icon" href="data:,">'\
            '<meta name="viewport" content="width=device-width,initial-scale=1">'\
            '</head><body><h1>リモコン</h1>'\
            '<a href="http://'+RaspberrypiIP+':7000/light:on" ><button>loght:on </button></a> '\
            '<a href="http://'+RaspberrypiIP+':7000/light:off"><button>loght:off</button></a> '\
            '<a href="http://'+RaspberrypiIP+':7000/light:up" ><button>loght:up </button></a> '\
            '<a href="http://'+RaspberrypiIP+':7000/light:dn" ><button>loght:dn </button></a> '\
            '<a href="http://'+RaspberrypiIP+':7000/light:sml"><button>loght:sml</button></a> '\
            '</body></html>'
        self.send_response(200)
        self.send_header('Content-type', 'text/html; charset=utf-8')
        self.send_header('Content-length', len(body))
        self.end_headers()
        self.wfile.write(body.encode())

host = '0.0.0.0'
port = 7000
httpd = s.HTTPServer((host, port), MyHandler)
print('サーバを起動しました。ポート:%s' % port)
httpd.serve_forever()