#!/usr/bin/env python
#version    :       2023.12.31
#language   :       ch
from maix import display
from maix import image
from maix import camera
import bs4
import ssl
import json
ssl._create_default_https_context = ssl._create_unverified_context
import http.client
camera.camera.config(size=(320,240))

ScreenOrientation = False
image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")

def get_content(city):
    weather_list = []
    try:        
        conn = http.client.HTTPSConnection("weather.visualcrossing.com")
        conn.request("GET", "/VisualCrossingWebServices/rest/services/timeline/"+city+"?unitGroup=us&include=days&key=XGMGVZSNH3RFJWNQYL4Z5THYS&contentType=json")
        res = conn.getresponse()
        data = res.read().decode("utf-8")
        # print(json.loads(data)["days"],'1111111111111111111')
        for i in json.loads(data)["days"]:
            # print(i)
            weather_list.extend([{"D":i["datetime"],"W":i["conditions"],
            "T":str(round((i["tempmax"]-32)/1.8))+"/"+str(round((i["tempmin"]-32)/1.8))+"℃"}])
    # weather_list = data.decode("utf-8")["days"]
    except:
        print('no result')
    # with open("/root/wResult.txt","w") as f:
    #     f.write(data.decode("utf-8"))
    # print(weather_list)
    return weather_list

# https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/weatherdata/forecast?locations=Herndon,VA,20170&aggregateHours=24&unitGroup=us&shortColumnNames=false&contentType=csv&key=YOURAPIKEY

canvas = image.new(size=(320, 240),color = (15,21,46),mode = "RGB")
canvas.draw_string(4 , 100 , "正在检查网络连接状态...", scale = 1, color = (255,255,255), thickness = 2)
display.show(canvas)
weather_all1 = get_content('shenzhen')
weather_all2 = get_content('HongKong')
weather_all3 = get_content('Macau')
weather_all4 = get_content('Guangzhou')
while True:
    canvas.clear()
    try:
        canvas = image.new(size=(320, 240),color = (15,21,46),mode = "RGB")
        #canvas.draw_string(2,20, ("Day:"+str(weather_all1[0]["D"])), scale = 1, color = (255,255,255) , thickness = 1)
        canvas.draw_string(2,20, ("气象变化:"+str(weather_all1[0]["W"])+"  ,温度:"+str(weather_all1[0]["T"]))[:-1]+"℃", scale = 1, color = (255,255,255) , thickness = 1)
        canvas.draw_string(0,0, "深圳", scale = 1, color = (255,255,255) , thickness = 1)
        canvas.draw_string(0,60, "香港", scale = 1, color = (255,255,255) , thickness = 1)
        #canvas.draw_string(2,80, ("Day:"+str(weather_all2[0]["D"])), scale = 1, color = (255,255,255) , thickness = 1)
        canvas.draw_string(2,80, ("气象变化:"+str(weather_all2[0]["W"])+"  ,温度:"+str(weather_all2[0]["T"]))[:-1]+"℃", scale = 1, color = (255,255,255) , thickness = 1)
        canvas.draw_string(0,120, "澳门", scale = 1, color = (255,255,255) , thickness = 1)
        #canvas.draw_string(2,140, ("Day:"+str(weather_all3[0]["D"])), scale = 1, color = (255,255,255) , thickness = 1)
        canvas.draw_string(2,140, ("气象变化:"+str(weather_all3[0]["W"])+"  ,温度:"+str(weather_all3[0]["T"]))[:-1]+"℃", scale = 1, color = (255,255,255) , thickness = 1)
        canvas.draw_string(0,180, "广州", scale = 1, color = (255,255,255) , thickness = 1)
        #canvas.draw_string(2,200, ("Day:"+str(weather_all4[0]["D"])), scale = 1, color = (255,255,255) , thickness = 1)
        canvas.draw_string(2,200, ("气象变化:"+str(weather_all4[0]["W"])+"  ,温度:"+str(weather_all4[0]["T"]))[:-1]+"℃",scale = 1, color = (255,255,255) , thickness = 1)
    except:
        canvas.draw_string(20,80, "获取天气信息失败!", scale = 1, color = (255,255,255) , thickness = 1)
    if ScreenOrientation:
        canvasVER = canvas.crop(0,0,240,320)
        canvasVER = canvasVER.rotate(-90, adjust=1)
        display.show(canvasVER)
    else:
        canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
        display.show(canvas)